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
 

题意:有n本书,编号为1~n,每一本书的重量为w[i],这堆书竖直堆在一起

现在有m天,第i天会从中拿出编号为day[i]的书看,代价是day[i]上面的书的总重量,书一天看完,看完后放在最上面。

问:最开始的时候你按照什么顺序放书,会让你这m天要搬的书的总重量最小。

贪心可知,拿每一本书第一次出现的序列作为初始序列,没有出现的序列放在最下面,不用管了。

用一个vector存放这个序列,看完后放在最上面删除,插入到最上面。

 #include<cstdio>
#include<cstring>
#include<vector>
#include<iostream> using namespace std; #define ll long long
#define pb push_back bool vis[];
int w[];
int day[];
vector<int>v; int main()
{
int n,m;
scanf("%d %d",&n,&m);
for(int i=;i<=n;i++){
scanf("%d",&w[i]);
}
memset(vis,false,sizeof vis);
for(int i=;i<=m;i++){
scanf("%d",&day[i]);
if(!vis[day[i]]){
v.pb(day[i]);
vis[day[i]]=true;
}
}
int ans=;
for(int i=;i<=m;i++){
int j=;
for(;v[j]!=day[i];j++){
ans+=(ll)(w[v[j]]);
}
v.erase(v.begin()+j);
v.insert(v.begin(),day[i]);
} printf("%d\n",ans); return ;
}

CF 500 C. New Year Book Reading 贪心 简单题的更多相关文章

  1. cf 605A Sorting Railway Cars 贪心 简单题

    其实就是求总长度 - 一个最长“连续”自序列的长度 最长“连续”自序列即一个最长的lis,并且这个lis的值刚好是连续的,比如4,5,6... 遍历一遍,贪心就是了 遍历到第i个时,此时值为a[i], ...

  2. POJ 2393 贪心 简单题

    有一家生产酸奶的公司,连续n周,每周需要出货numi的单位,已经知道每一周生产单位酸奶的价格ci,并且,酸奶可以提前生产,但是存储费用是一周一单位s费用,问最少的花费. 对于要出货的酸奶,要不这一周生 ...

  3. HDU - 5999 The Third Cup is Free 贪心 简单题

    The Third Cup is Free Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  4. POJ 2260 Error Correction 模拟 贪心 简单题

    Error Correction Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6825   Accepted: 4289 ...

  5. hdu2187悼念512汶川大地震遇难同胞——老人是真饿了(贪心 简单题)

    传送门 简单题 #include<bits/stdc++.h> using namespace std; struct node { double dan,weight; }a[]; bo ...

  6. cf 620C Pearls in a Row(贪心)

    d.有一串数字,要把这些数字分成若干连续的段,每段必须至少包含2个相同的数字,怎么分才能分的段数最多? 比如 是1 2 1 3 1 2 1 那么 答案是 21 34 7 即最多分在2段,第一段是1~3 ...

  7. 贪心/思维题 Codeforces Round #310 (Div. 2) C. Case of Matryoshkas

    题目传送门 /* 题意:套娃娃,可以套一个单独的娃娃,或者把最后面的娃娃取出,最后使得0-1-2-...-(n-1),问最少要几步 贪心/思维题:娃娃的状态:取出+套上(2),套上(1), 已套上(0 ...

  8. 贪心/思维题 UVA 11292 The Dragon of Loowater

    题目传送门 /* 题意:n个头,m个士兵,问能否砍掉n个头 贪心/思维题:两个数组升序排序,用最弱的士兵砍掉当前的头 */ #include <cstdio> #include <c ...

  9. CF #296 (Div. 1) B. Clique Problem 贪心(构造)

    B. Clique Problem time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

随机推荐

  1. HDU 1087 Super Jumping! Jumping! Jumping

    HDU 1087 题目大意:给定一个序列,只能走比当前位置大的位置,不可回头,求能得到的和的最大值.(其实就是求最大上升(可不连续)子序列和) 解题思路:可以定义状态dp[i]表示以a[i]为结尾的上 ...

  2. 救援行动(save) (BFS)

    时间限制: 1 Sec  内存限制: 64 MB提交: 42  解决: 9[提交][状态][讨论版] 题目描述 Angel被人抓住关在一个迷宫了!迷宫的长.宽均不超过200,迷宫中有不可以越过的墙以及 ...

  3. php会话(session)生命周期概念介绍及设置更改和回收

    http://www.169it.com/article/8429580816135935852.html https://my.oschina.net/jiec/blog/227252  sessi ...

  4. flash全屏输入模式

    params.allowscriptaccess = "sameDomain"; params.allowfullscreen = "true"; params ...

  5. SpringMVC给外部资源加版本号避免缓存

    一.属性文件:version.properties ->内容:version=201608  二.java代码 public class configVersion implements Ser ...

  6. Thinking Clearly about Performance

    http://queue.acm.org/detail.cfm?id=1854041 The July/August issue of acmqueue is out now acmqueue is ...

  7. ie6 z-index(用父元素的优先级来解决)

    父元素的优先级决定了子元素的优先级

  8. Linux-理解ARP、网关、路由

    假设你叫小不点(本地主机),住在一个大院子(本地局域网)里,有很多邻居(网络邻居),门口传达室有个看大门的李大爷,李大爷就是你的网关.当你想跟院子里的某个伙伴玩,只要你在院子里大喊一声他的名字(pin ...

  9. Mysql源码目录结构

    Programs for handling SQL commands. The "core" of MySQL. These are the .c and .cc files in ...

  10. Python_Day6_反射_正则表达式之篇

    一.反射 定义:利用字符串形式去对象(模块)中操作(寻找/检查/删除/设置)成员 #getattr:获取模块中属性 #hasattr:检查模块中是否存在某个成员(函数) #delattr:删除模块中成 ...