New Year Book Reading

source

Description

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 ≤ i ≤ n) 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.
He lifts all the books above book x.
He pushes book x out of the stack.
He puts down the lifted books without changing their order.
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 ≤ j ≤ m) day, he will read the book that is numbered with integer bj (1 ≤ bj ≤ n). 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 *w*1,\ *w*2, ..., *w**n* (1 ≤ *w**i* ≤ 100) — the weight of each book.
The third line contains m space separated integers * b * 1,  * b * 2, ..., *b**m* (1 ≤ *b**j* ≤ *n*) — 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.

Example
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.


题意:Jaehyun决定读n本书,每本书的id都不一样,每本书都有它的重量。每次读特定id的书时,Jaehyun 先搬起位于它前面的书,然后取出该书,剩下的书的位置不变,再放下搬起的书,读完后把该书放到书堆的顶端。求怎么安排初始的书堆顺序,使得Jaehyun 要搬起的书的重量最小,输出最小重量。

题解:假设前7本书的阅读顺序仅仅出现书A,B,C,而第8本书首次出现书D;则对于第8本书:书D而言,无论前7本选择怎样的阅读顺序,例如顺序为ABCABAA或CABBCBB等等,到阅读第8本书D的时候,都要搬起书A,B,C。由此猜想,书堆的摆放顺序应该是:书的阅读顺序中首次出现的书应该越接近书堆顶。

栈模拟版本

#include
#include
#include
#include
using namespace std;
int main()
{
        int n,m,temp,sum=0;
        scanf("%d%d",&n,&m);
        int *wight=new int[n+1];
        int *step=new int [m+1];
        for(int i=1;i<=n;i++)
        {
            scanf("%d",wight+i);
        }
        list mystack;
        set s;
        for(int i=1;i<=m;i++)
        {
            scanf("%d",&temp);
            step[i]=temp;
            if(s.find(temp)==s.end())
            {
                mystack.push_front(temp);
                s.insert(temp);
            }
        }
        list sta;
        while(!mystack.empty())
        {
            sta.push_front(mystack.front());
            mystack.pop_front();
        }
        for(int i=1;i<=m;i++)
        {
            for(list::iterator it=sta.begin();;it++)
            {
                if(*it==step[i]){
                    sta.erase(it);
                    sta.push_front(step[i]);
                    break;
                }
                else{
                sum+=wight[*it];
                }

            }
        }
        printf("%d\n",sum);
}

不用构造栈的版本

#include
#include
using namespace std;
int wight[510];
int book[1010];
bool vis[510];
int main()
{
        int n,m,sum=0;
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++)
        {
            scanf("%d",wight+i);
        }
        for(int i=1;i<=m;i++)
        {
            scanf("%d",book+i);
        }
        for(int i=2;i<=m;i++)
        {
            memset(vis,false,sizeof(vis));
            for(int j=i-1;j>0;j--)
            {
                if(book[j]==book[i]) break;
                if(!vis[book[j]])
                {
                    sum+=wight[book[j]];
                    vis[book[j]]=true;
                }
            }
        }
        printf("%d\n",sum);
}

你可能感兴趣的:(New Year Book Reading)