您的位置:首页 > 编程语言 > C语言/C++

CodeForces500c - New Year Book Reading

2017-12-09 17:42 381 查看

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 1005
int book[maxn]; //当前书的序列
int day[maxn]; //day[i]为第i天要读的书
int weigh[maxn]; //书的weight
int 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  贪心 codeforces c语言