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. 2016 Hunan Province Programming Contest

    2016 Hunan Province Programming Contest A. 2016 题意 \(1 \le a \le n, 1 \le b \le m\) ,其中\(1 \le n,m \ ...

  2. 修改host文件屏蔽视频广告和网站

    很多时候我们会需要屏蔽一些网站或者广告,类似XX网站,下木马病毒的网站,或者破解软件的时候.我们可以使用一些软件屏蔽,我这里是用windows系统自带的hosts文件来屏蔽的.这个文件有点类似精简版的 ...

  3. ARM体系的7种工作模式

    一.ARM体系的CPU有以下7种工作模式:   用户模式(usr)    大多数程序运行于用户模式 特权模式   系统模式(sys)   运行具有特权的操作系统任务 异常模式 中断模式(irq)   ...

  4. matlab 工具之各种降维方法工具包,下载及使用教程,有PCA, LDA, 等等。。。

    最近跑深度学习,提出的feature是4096维的,放到我们的程序里,跑得很慢,很慢.... 于是,一怒之下,就给他降维处理了,但是matlab 自带的什么pca( ), princomp( )函数, ...

  5. C# Development 13 Things Every C# Developer Should Know

    https://dzone.com/refcardz/csharp C#Development 13 Things Every C# Developer Should Know Written by ...

  6. 对象属性操作-包含kvc---ios

    #import <Foundation/Foundation.h> @class Author; @interface Books : NSObject{ @private NSStrin ...

  7. SET ROWCOUNT,SET NOCOUNT

    SET ROWCOUNT (Transact-SQL) 在停止特定查询之前要处理的行数(整数). 重要提示 在 SQL Server 的将来版本中,使用 SET ROWCOUNT 将不会影响 DELE ...

  8. jQuery 核心 - noConflict() 方法,jQuery 文档操作 - detach() 方法

    原文地址:http://www.w3school.com.cn/jquery/manipulation_detach.asp   实例 使用 noConflict() 方法为 jQuery 变量规定新 ...

  9. ThreadContext

    //#define UseThreadContext using System; using System.Collections.Generic; using System.Linq; using ...

  10. 将m个苹果放入n个盘子的问题【转】

    来自:http://blog.csdn.net/qq675927952/article/details/6312255 问题1: m----->相同, n---> 相同,可为空 将m个苹果 ...