C. New Year Book Reading codeforces

来源:互联网 发布:java线程结束 编辑:程序博客网 时间:2024/05/16 13:46
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 hasn books numbered by integers from 1 ton. The weight of thei-th (1 ≤ i ≤ n) book iswi.

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 bookx, 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 thej-th (1 ≤ j ≤ m) day, he will read the book that is numbered with integerbj (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) andm (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 integersw1, w2, ..., wn (1 ≤ wi ≤ 100) — the weight of each book.

The third line contains m space separated integersb1, b2, ..., bm (1 ≤ bj ≤ 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.

Examples
Input
3 51 2 31 3 2 3 1
Output
12
Note

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


   
题目大意:
放书问题:
这位小朋友每天要看书 看m天,每天看一本,他一共有n本书,放书规则看上图,我就不解释了哈,
其实刚开始我也有点迷糊,知道学长一语点醒我,才发现有个规律我没找到,不就是放书吗,
他给出以每天看啥书,问刚开始应该怎么放置书籍可以让他搬书总重量最小,
如果把它看做很多子过程的话,那么每个子过程就是每天为了看一本书ban了多少重量;子过程贪心即可
第一天看的那本书一定是最上边的位置,这样使得ban第一天书时重量最少(第一个子过程完成)
第二天ban书时,如果不和第一天的书相同,那么第二天的书原来的位置应该在第二个位置,这样可以第二天ban的书的重量最少,以此类推,如果某天看的书以前看过,那么想要完成这个子过程,就需要算一下上一次看这本书到这一次时,中途看过几种书(注意,中途可能也有 一本书看了好多次,所以这一种书只加一次),这些书的重量和就是完成这个子过程所ban的重量,完成m个子过程。解决问题!
#include<iostream>#include<string.h>using namespace std;int main(){int m,n;int sum,sum1;int a[505];int b[1005],vis[505];while(cin>>n>>m){int i,j;sum=0;for(i=0;i<n;i++)cin>>a[i];for(i=0;i<m;i++)cin>>b[i];for(i=0;i<m;i++){memset(vis,0,sizeof(vis));for(j=i-1;j>=0;j--){if(b[i]==b[j])break;if(!vis[b[j]-1]){sum+=a[b[j]-1];vis[b[j]-1]=1;}} } cout<<sum<<endl;}return 0;}

            
 
原创粉丝点击