CodeForces500c

来源:互联网 发布:unity3d rpg 编辑:程序博客网 时间:2024/06/05 03:20

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 w1, w2, …, wn
(1 ≤ wi ≤ 100) — the weight of each book.

The third line contains m space separated integers b1, 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.

Sample Input

3 5
1 2 3
1 3 2 3 1

Sample Output

12

Note

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

样例解释

Explanation

对于这个样例,我们可以从后面往前面看
最后一个是1,1前面是3,那么将3放在1上面,3前面是2,那么将2放在3上面,
现在的序列是2,3,1,我们继续往前看,2前面是3,那么将之前的3拿走并放在2上面,
3前面是1,则将之前的1拿走并放在3上面,则现在的序列为1,3,2即为调整后的书的序列,
然后根据拿书的顺序模拟拿书操作进行加和即可

Code

#include <stdio.h>#include <string.h>#define maxn 1005int book[maxn]; //当前书的序列int day[maxn]; //day[i]为第i天要读的书int weigh[maxn]; //书的weightint vis[maxn]; //初始book数组时用到的标记数组int main(){    int n,m;    int now = 0;    long long sum_weight = 0;    memset(vis,0,sizeof(vis));    scanf("%d%d",&n,&m);    for(int i = 0; i < n; i++)        scanf("%d", &weigh[i]);    for(int i = 0; i < m; i++){        scanf("%d", &day[i]);        day[i] --;//使书的编号从0开始    }    for(int i = 0; i < m; i++){        if(!vis[day[i]]){            book[now++] = day[i];            vis[day[i]]++; //对书的序列初始化以使sum_weight最小        }    }    for(int i = 0; i < m; i++){        for(int j = 0; j < n; j++){            if(book[j] != day[i]){                sum_weight += weigh[book[j]];            }            else{                for(int k = j - 1; k >= 0; k--){                    book[k+1] = book[k]; //其上面的书下移                }                book[0] = day[i];                break;            }        }    }    printf("%lld\n", sum_weight);    return 0;}
原创粉丝点击