【Goodbye2014】Codeforces 500C New Year Book Reading【贪心+模拟】

C. New Year Book Reading
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He has n books numbered by integers from 1 to n. The weight of the i-th (1 ≤ in) book is wi.

As Jaehyun's house is not large enough to have a bookshelf, he keeps the n books by stacking them vertically. When he wants to read a certain book x, he follows the steps described below.

  1. He lifts all the books above book x.
  2. He pushes book x out of the stack.
  3. He puts down the lifted books without changing their order.
  4. After reading book x, he puts book x on the top of the stack.

He decided to read books for m days. In the j-th (1 ≤ jm) day, he will read the book that is numbered with integer bj (1 ≤ bjn). To read the book, he has to use the process described in the paragraph above. It is possible that he decides to re-read the same book several times.

After making this plan, he realized that the total weight of books he should lift during m days would be too heavy. So, he decided to change the order of the stacked books before the New Year comes, and minimize the total weight. You may assume that books can be stacked in any possible order. Note that book that he is going to read on certain step isn't considered as lifted on that step. Can you help him?

Input

The first line contains two space-separated integers n (2 ≤ n ≤ 500) and m (1 ≤ m ≤ 1000) — the number of books, and the number of days for which Jaehyun would read books.

The second line contains n space-separated integers w1, w2, ..., wn (1 ≤ wi ≤ 100) — the weight of each book.

The third line contains m space separated integers b1, b2, ..., bm (1 ≤ bjn) — the order of books that he would read. Note that he can read the same book more than once.

Output

Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books.

Examples
Input
3 5
1 2 3
1 3 2 3 1
Output
12
Note

Here's a picture depicting the example. Each vertical column presents the stacked books.


题目大意:

一共有N本书(每本书的重量已知)和M个读书的顺序。

我们读一本书的时候要进行如下操作:

①找到这本书的位子。

②拿起这本书上边所有的书,并且花费对应重量的体力。

③将这本书拿出来阅读。

④将这本书放在这一叠书的最上边。

现在让我们随意分配一个初始状态,使得按照顺序读完这M本书花费的体力最小。


思路:


1、假设我们此时有这样一种情况:
M=10,n=3:

1 2 3 2 1 3 2 1 2 3

那么很显然,我们最初的状态只会影响前三次拿书的结果。

因为拿完前三次的书之后 ,再之后书本的摆放状态就已经确定了。

所以这里我们只要贪心前3次拿书的最小体力花费即可。

那么很显然,我们只要按照1、2、3的摆放顺序(无论谁的权值大都一定是这么放最优。)放置了初始的状态之后,模拟操作过程即可。


2、那么假设再有这种情况:

M=10,n=4:

1 3 2 1 3 2 1 2 4

那么很显然,只处理前四本阅读的书的摆放顺序是没有将所有书安排完的,需要考虑到最后一本书4的位子,那么其实我们只要找到拿每种书的第一次出现的位子,然后按照相对顺序安排位子即可(m=10,n=4的时候,安排书本放置顺序为:1 3 2 4)。

那么安排好了最初的状态之后,模拟拿取过程统计花费和就是结果。


Ac代码:

#include<stdio.h>
#include<string.h>
using namespace std;
int que[50000];
int vis[500000];
int a[50000];
int b[10000];
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        for(int i=1;i<=m;i++)scanf("%d",&b[i]);
        int cnt=0;
        for(int i=1;i<=m;i++)
        {
            if(vis[b[i]]==0)
            {
                que[++cnt]=b[i];
                vis[b[i]]=1;
            }
        }
        for(int i=1;i<=n;i++)
        {
            if(vis[i]==0)
            {
                que[++cnt]=i;
            }
        }
        int output=0;
        for(int i=1;i<=m;i++)
        {
            int sum=0;
            for(int j=1;j<=n;j++)
            {
                sum+=a[que[j]];
                if(que[j]==b[i])
                {
                    sum-=a[que[j]];
                    for(int z=j;z>=2;z--)
                    {
                        que[z]=que[z-1];
                    }
                    que[1]=b[i];
                    break;
                }
            }
            output+=sum;
        }
        printf("%d\n",output);
    }
}







版权声明:本文为mengxiang000000原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。