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.


这道题的主要是考虑怎么排序,其实只要按照取书本的顺序就行了,比如1 1 1 1 1 1 2,取两本书2 1,那么显然初始序列是1 2 时总价值最小,分析样例,如果3本书取书的顺序是3 1 2 ,那么这些步骤后书的排序顺序一定是2 3 1,但是初始序列是3 1 2的总价值最小,那么加上后面的也一定最小。
#include<stdio.h>

#include<string.h>

int w[600],a[600],c[1005];

int main()

{
int n,m,i,j,t,flag,sum,u,h;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(w,0,sizeof(w));
memset(a,0,sizeof(a));
memset(c,0,sizeof(c));
for(i=1;i<=n;i++){
scanf("%d",&w[i]);
}
t=0;
for(i=1;i<=m;i++){
scanf("%d",&c[i]);
if(t==0){
t++;a[t]=c[i];continue;
}
flag=1;
for(j=1;j<=t;j++){
if(c[i]==a[j]){
flag=0;break;
}
}
if(flag==0)continue;
else{
t++;a[t]=c[i];continue;
}
}
sum=0;
for(i=1;i<=m;i++){
/*for(j=1;j<=n;j++){
printf("%d ",a[j]);
}printf("\n");*/

if(c[i]==a[1])continue;
for(j=1;j<=t;j++){
if(c[i]==a[j]){
u=j;break;
}
sum=sum+w[a[j]];
}
h=a[u];
for(j=u;j>=2;j--){
a[j]=a[j-1];
}
a[1]=h;
}
printf("%d\n",sum);
}
return 0;

}

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

  1. Reading C type declarations(引用http://unixwiz.net/techtips/reading-cdecl.html)

    Even relatively new C programmers have no trouble reading simple C declarations such as int foo[5]; ...

  2. Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://localhost:9001/api/size/get. (Reason: CORS header 'Access-Control-Allow-Origin' missing).

    Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http:/ ...

  3. Git Learning - By reading ProGit

    Today I begin to learn to use Git. I learn from Pro Git. And I recommend it which is an excellent bo ...

  4. MySQL远程连接丢失问题解决方法Lost connection to MySQL server at ‘reading initial communication packet’, system error: 0

    最近远程连接mysql总是提示 Lost connection 很明显这是连接初始化阶段就丢失了连接的错误 其实问题很简单,都是MySQL的配置文件默认没有为远程连接配置好,只需要更改下MySQL的配 ...

  5. 论文阅读(Weilin Huang——【AAAI2016】Reading Scene Text in Deep Convolutional Sequences)

    Weilin Huang--[AAAI2016]Reading Scene Text in Deep Convolutional Sequences 目录 作者和相关链接 方法概括 创新点和贡献 方法 ...

  6. Flesch Reading Ease -POJ3371模拟

    Flesch Reading Ease Time Limit: 1000MS Memory Limit: 65536K Description Flesch Reading Ease, a reada ...

  7. Mac下遇到 'reading initial communication packet’ 问题

    今天在开发过程中,一个单位跑的好好的项目,在家中的Mac下运行时,遇到了下面这个错误:   "Lost connection to MySQL server at 'reading init ...

  8. A log about Reading the memroy of Other Process in C++/WIN API--ReadProcessMemory()

    Memory, is a complex module in Programing, especially on Windows. This time, I use cpp with win wind ...

  9. Error: Cannot open main configuration file '//start' for reading! 解决办法

    当执行service nagios start启动nagios时,报错:Error: Cannot open main configuration file '//start' for reading ...

  10. Spring mvc 中使用ftl引用共通文件出错 FreeMarker template error: Error reading included file "/WEB-INF/ftl/common/errormessage.ftl"

    初次接触spring mvc,想做一个小的练习项目,结果在ftl文件中引用其它的共通ftl文件时出错.

随机推荐

  1. 【高级排序算法】2、归并排序法的实现-Merge Sort

    简单记录 - bobo老师的玩转算法系列–玩转算法 -高级排序算法 Merge Sort 归并排序 Java实现归并排序 SortTestHelper 排序测试辅助类 package algo; im ...

  2. C语言补码(C语言学习笔记)

    记录 在学习C语言数据范围时了解到了补码的概念,记录一下什么是补码,补码怎么运算的 运算 原文链接:https://www.cnblogs.com/lsgsanxiao/p/5113305.html ...

  3. Windows安全加固

    Windows安全加固 # 账户管理和认证授权 # 1.1 账户 # 默认账户安全 # 禁用Guest账户. 禁用或删除其他无用账户(建议先禁用账户三个月,待确认没有问题后删除.) 操作步骤 本地用户 ...

  4. 手动验证MySQL Innodb RR级别加锁 需要注意的几个点

    记录几个坑 优化器在表行数比较少的时候 会使用全表扫描,会造成全表所有的行加锁,所以需要使用force index 强制使用索引 来实现gap-lock(间隙锁)的应用 next-lock 加锁 会锁 ...

  5. js 前端词典对象的属性和值读取

    通常服务端返回比较奇葩的数据对象,不知道该怎么将这个对象转换为可用实体,想了很久,突发奇想想到了这么个方法. 需求是这样:企业有多个产品,产品有分为很几个种类.服务端有获取产品的接口,和单独获取产品种 ...

  6. 查看内核打印信息指令dmesg

    linux系统启动的时候打印的的信息非常重要,有时候需要看这些信息但是又不想重启,可以用dmesg这条指令.

  7. Eureka详解系列(二)--如何使用Eureka(原生API,无Spring)

    简介 通过上一篇博客 Eureka详解系列(一)--先谈谈负载均衡器 ,我们知道了 Eureka 是什么以及为什么要使用它,今天,我们开始研究如何使用 Eureka. 在此之前,先说明一点.网上几乎所 ...

  8. H3C防火墙开启区域间互访

    配置ip和路由以及将端口放至Untrust之后,外网还是不通,需要以下命令 interzone policy default by-priority 或者下面: security-zone intra ...

  9. How does Circus stack compare to a classical stack?

    Frequently Asked Questions - Circus 0.15.0 documentation https://circus.readthedocs.io/en/latest/faq ...

  10. 支付回调地址 同步回调地址 异步回调地址 return_url和notify_url的区别

    [微信支付]JSAPI支付开发者文档 https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_16&index=10 退款结果通知 ...