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. 记汉化zabbix后图形界面没有任何汉字的问题

    1.安装并汉化后zabbix,所有的图形界面都没有任何字图,如下图 2.郁闷不已,去/var/www/html/zabbix/fonts目录下面查看,发现之前上传字体的文件名后缀是.ttc,猜着一般见 ...

  2. 【Linux】将ens33修改为eth0 网卡方法

    1.编辑 grub 配置文件 vim /etc/sysconfig/grub # 其实是/etc/default/grub的软连接 # 为GRUB_CMDLINE_LINUX变量增加2个参数,添加的内 ...

  3. 【Linux】配置ssh留下的一些思考和大坑解决办法

    今天传包突然有问题,结果发现是ssh出现了问题,密钥也在里面,都是正常的,但是还有什么问题呢? 后来总结下需要注意点: 1.最开始你要检查.ssh/  这个文件夹的权限,看下权限是否为700或者为75 ...

  4. 【Oracle】Oracle 10g下载路径

    ORACLE 10g下载地址 下载方法: 直接复制下面的链接,打开迅雷,自动会识别下载的内容 Oracle Database 10g Release 2 (10.2.0.1.0) Enterprise ...

  5. 【转】自定义ALV控件的工具条按钮

    1 CLASS lcl_event_receiver DEFINITION DEFERRED. 2 3 DATA: itab TYPE TABLE OF spfli, 4 wa TYPE spfli. ...

  6. Flask源码流程分析(一)

    Flask源码流程分析: 1.项目启动: 1.实例化Flask对象 1. 重要的加载项: * url_rule_class = Rule * url_map_class = Map * session ...

  7. 订阅者模式,公众号、B站、快手用了都说好!

    大家好,今天和大家来聊一个新的设计模式--订阅者模式. 这个模式在我们的生活当中非常常见,可以说是几乎所有的媒体平台都用或多或少地用到了这个模式.比如公众号,我们来仔细梳理一下公众号这个平台当中的整个 ...

  8. 安装OpenDaylight及Openflow插件

    1. 安装 Java 和 Maven CentOS7: yum install java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64 ma ...

  9. CUDA 介绍

    1. 介绍 1.1 GPU vs. CPU GPU 使用更多的晶体管进行数据处理,而不是数据缓存和流控制,因此可以提供高度的并行计算. GPU 可以通过计算来隐藏内存访问延迟,而不是依赖于大量的数据缓 ...

  10. js input相关事件(转载)

    1.onfocus  当input 获取到焦点时触发. 2.onblur  当input失去焦点时触发,注意:这个事件触发的前提是已经获取了焦点再失去焦点的时候才会触发该事件,用于判断标签为空.3.o ...