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

  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 ≤ 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 test(s)
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.   

贪心。

假设要按照 i->j 顺序看 , 假设 i 在 j 上边,花费就是 wi , 假设j 在 i 上边 ,花费就是wj + wi 了 。

那么直接按照m个日子的顺序来模拟一次 。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = ; int n , m , w[N] , d[N];
bool vis[N];
int main()
{
while( cin >> n >> m ) {
for( int i = ; i <= n ; ++i ) cin >> w[i] ;
for( int i = ; i <= m ; ++i ) cin >> d[i] ;
LL ans = ;
for( int i = ; i <= m ; ++i ) {
memset( vis , false , sizeof vis );
for( int j = i - ; j > ; --j ){
if( d[i] == d[j] ) break ;
if( !vis[ d[j] ] ) { ans += w[ d[j] ] ; vis[ d[j] ] = true ; }
}
}
cout << ans << endl ;
}
}

Codeforces 500C New Year Book Reading的更多相关文章

  1. Educational Codeforces Round 31 A. Book Reading【暴力】

    A. Book Reading time limit per test 2 seconds memory limit per test 256 megabytes input standard inp ...

  2. codeforces 820A. Mister B and Book Reading 解题报告

    题目链接:http://codeforces.com/problemset/problem/820/A 坑爹题目,坑爹题目,坑爹题目....汗 = =!  后台还110个 test 有个地方需要注意下 ...

  3. Codeforces Round #546 (Div. 2) A. Nastya Is Reading a Book

    链接:https://codeforces.com/contest/1136/problem/A 题意: 给n个区间,每个区间范围不超过100,n不超过100. 给一个位置k,1-(k-1)是遍历过的 ...

  4. 【Codeforces Round #421 (Div. 2) A】Mister B and Book Reading

    [题目链接]:http://codeforces.com/contest/820/problem/A [题意] 每天看书能看v页; 且这个v每天能增加a; 但是v有上限v1; 然后每天还必须往回看t页 ...

  5. 【Educational Codeforces Round 31 A】Book Reading

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 水模拟 [代码] #include <bits/stdc++.h> using namespace std; const ...

  6. Codeforces 1213C Book Reading

    cf题面 中文题意 多组数据,每组给一个n给一个m,要求正整数\(1\)~\(n\)中,所有能被m整除的数的个位之和. 解题思路 首先,能被m整除的数的数量是\(\lfloor\frac{n}{m}\ ...

  7. Codeforces Round #582 (Div. 3) C. Book Reading

    传送门 题意: 给你n,k.表示在[1,n]这个区间内,在这个区间内找出来所有x满足x%k==0,然后让所有x的个位加到一起(即x%10),输出. 例如:输入10 2 那么满足要求的数是2 4 6 8 ...

  8. Codeforces Round #653 (Div. 3) E1. Reading Books (easy version) (贪心,模拟)

    题意:有\(n\)本书,A和B都至少要从喜欢的书里面读\(k\)本书,如果一本书两人都喜欢的话,那么他们就可以一起读来节省时间,问最少多长时间两人都能够读完\(k\)本书. 题解:我们可以分\(3\) ...

  9. Codeforces Round #Pi (Div. 2) B. Berland National Library set

    B. Berland National LibraryTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...

随机推荐

  1. js的预解析和作用域

    预解析指的就是,在js文件或者script里面的代码在正式开始执行之前,进行的一些解析工作.这个工作很简单,就是在全局中寻找var关键字声明的变量和通过function关键字声明的函数. 1.寻找 v ...

  2. RabbitMQ基于Stomp实现与MQTT客户端通信

    请参照RabbitMQ应用和SpringBoot集成RabbitMQ并实现消息确认机制 详情参照官方文档https://www.rabbitmq.com/stomp.html.https://gith ...

  3. 2019-1-5-Windows-的-Pen-协议

    title author date CreateTime categories Windows 的 Pen 协议 lindexi 2019-01-05 11:14:49 +0800 2019-01-0 ...

  4. 2018-2-13-C#-搜索算法

    title author date CreateTime categories C# 搜索算法 lindexi 2018-2-13 17:23:3 +0800 2018-2-13 17:23:3 +0 ...

  5. 关于linux的日志

    日志的三种类型# 内核及系统日志: 这种日志数据由系统服务rsyslog统一管理,根据其主配置文件/etc/rsyslog.conf中的设置决定将内核消息及各种系统程序消息记录到什么位置.系统中有相当 ...

  6. python如何获取变量的变量名

    假设现在存在一个值为1变量名为a的变量,如何通过一个函数获取该变量的变量名a? 上面这个需求来源于某群友的一个要求,希望能有一个这样的函数来方便打印. 这个需求很扯淡啊,为什么不用格式化输出?它回复到 ...

  7. Sass函数:Sass Maps的函数-map-get($map,$key)

    map-get($map,$key) 函数的作用是根据 $key 参数,返回 $key 在 $map 中对应的 value 值.如果 $key 不存在 $map中,将返回 null 值.此函数包括两个 ...

  8. Sass函数-join()函数

    join() 函数是将两个列表连接合并成一个列表. >> join(10px 20px, 30px 40px) (10px 20px 30px 40px) >> join((b ...

  9. Go 数组(1)

    1.一旦声明,数组里存储的数据类型和数组长度就都不能改变了.如果需要存储更多的元素, 就需要先创建一个更长的数组,再把原来数组里的值复制到新数组里. 例如: ]int 2.使用数组字面量声明数组 // ...

  10. 我们为什么选择Ceph来建立块存储

    我们为什么选择Ceph来建立块存储?国内知名黑客组织东方联盟是这样回答的,卷管理器的大小和增长受到管理程序的驱动器补充的限制,与其他Droplet共享.一旦Droplet被摧毁,储存就会被释放.术语“ ...