Codeforces500C(SummerTrainingDay01-G)
C. New Year Book Reading
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.
- He lifts all the books above book x.
- He pushes book x out of the stack.
- He puts down the lifted books without changing their order.
- 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.
Examples
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.

//2017-08-01
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm> using namespace std; const int N = ;
const int M = ;
int w[N], vis[N], b[M], n, m; int main(){
while(scanf("%d%d", &n, &m)!=EOF){
for(int i = ; i < n; i++)
scanf("%d", &w[i]);
for(int i = ; i < m; i++)
scanf("%d", &b[i]);
int ans = ;
for(int i = ; i < m; i++){
memset(vis, , sizeof(vis));
for(int j = i-; j >= ; j--){
if(b[i] == b[j])break;
if(!vis[b[j]-]){
ans += w[b[j]-];
vis[b[j]-] = ;
}
}
}
printf("%d\n", ans);
} return ;
}
Codeforces500C(SummerTrainingDay01-G)的更多相关文章
- Storyboards Tutorial 03
这一节主要介绍segues,static table view cells 和 Add Player screen 以及 a game picker screen. Introducing Segue ...
- 文件图标SVG
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink ...
- [转]Linux下g++编译与使用静态库(.a)和动态库(.os) (+修正与解释)
在windows环境下,我们通常在IDE如VS的工程中开发C++项目,对于生成和使用静态库(*.lib)与动态库(*.dll)可能都已经比较熟悉,但是,在linux环境下,则是另一套模式,对应的静态库 ...
- CentOS 6.6 升级GCC G++ (当前最新版本为v6.1.0) (完整)
---恢复内容开始--- CentOS 6.6 升级GCC G++ (当前最新GCC/G++版本为v6.1.0) 没有便捷方式, yum update.... yum install 或者 添加y ...
- Linux deepin 下sublimes配置g++ openGL
参考 :http://blog.csdn.net/u010129448/article/details/47754623 ubuntu 下gnome只要将代码中deepin-terminal改为gno ...
- [翻译svg教程]svg 中的g元素
svg 中的<g>元素用来组织svg元素.如果一组svg元素被g元素包裹了,你可以通过对g元素进行变换(transform),被g元素包裹的元素也将被变换,就好这些被svg包裹的元素是一个 ...
- 软件工程:黄金G点小游戏1.0
我们要做的是黄金G点小游戏: N个同学(N通常大于10),每人写一个0~100之间的有理数 (不包括0或100),交给裁判,裁判算出所有数字的平均值,然后乘以0.618(所谓黄金分割常数),得到G值. ...
- 2016huasacm暑假集训训练五 G - 湫湫系列故事——减肥记I
题目链接:http://acm.hust.edu.cn/vjudge/contest/126708#problem/G 这是一个01背包的模板题 AC代码: #include<stdio.h&g ...
- 毫秒级的时间处理上G的图片(生成缩略图)
测试环境: 测试图片(30M): 测试计时方法: Stopwatch sw1 = new Stopwatch(); sw1.Start(); //TODO...... sw1.Stop(); stri ...
- g++编译流程
测试程序test.cpp如下所示: #include <iostream> using namespace std; #define MAX 9 int main() { //just f ...
随机推荐
- cad2018卸载/安装失败/如何彻底卸载清除干净cad2018注册表和文件的方法
cad2018提示安装未完成,某些产品无法安装该怎样解决呢?一些朋友在win7或者win10系统下安装cad2018失败提示cad2018安装未完成,某些产品无法安装,也有时候想重新安装cad2018 ...
- wcf返回值报错解析
问题来源 最近在项目中使用wcf,因为是一个新手,对新的东西总是比较敬畏,不过一切都是进行得很顺利,运行的时候,突然报了错,编译器提示的错误大概是:“InvalidOperationException ...
- 【ElasticSearch】:elasticsearch.yml配置
ElasticSearch5的elasticsearch.yml配置 注意 elasticsearch.yml中的配置,冒号和后面配置值之间有空格 cluster.name: my-applicati ...
- mysql创建账号及管理权限
mysql创建账号及管理权限 0.mysql版本8.0.15,服务器版本:RHEL 6.5 1.创建用户名密码 mysql> use mysql; mysql> create user ' ...
- axios请求拦截及请求超时重新请求设置
自从使用Vue2之后,就使用官方推荐的axios的插件来调用API,在使用过程中,需要解决问题: 1. 请求带token校验 2. post请求请求体处理 3. 响应未登录跳转登录页处理 4. 响应错 ...
- 在vue里面使用iVew框架
iVew框架的文档 https://www.iviewui.com/docs/guide/install 这里使用的是 npm 来安装,在项目下执行下面命令npm install iview -- ...
- golang 闭包求斐波那契数列
题目是Go指南中的闭包求斐波那契数列 package main import "fmt" // 返回一个"返回int的函数" func fibonacci() ...
- node.js中的回调
同步和阻塞:这两个术语可以互换使用,指的是代码的执行会在函数返回之前停止.如果某个操作阻塞,那么脚本就无法继续,这意味着必须等待. 异步和非阻塞:这两个术语可以互换使用,指的是基于回调的.允许脚本并行 ...
- POJ 2707
#include<iostream> #include<stdio.h> #include<algorithm> using namespace std; int ...
- xamarin android 实现二维码带logo生成效果
MultiFormatWriter writer = new MultiFormatWriter(); Dictionary<EncodeHintType, object> hint = ...