C. New Year Book Reading codeforces

C. New Year Book Reading
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

New Year is coming, and Jaehyun decided to read many books during 2015, unlike this year. He hasn books numbered by integers from 1 ton. The weight of the i-th (1 ≤ i ≤ n) book iswi.

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 bookx, 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 thej-th (1 ≤ j ≤ m) day, he will read the book that is numbered with integerbj (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) andm (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 integersw1, w2, ..., wn (1 ≤ wi ≤ 100) — the weight of each book.

The third line contains m space separated integersb1, 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.


   
题目大意:
放书问题:
这位小朋友每天要看书 看m天,每天看一本,他一共有n本书,放书规则看上图,我就不解释了哈,
其实刚开始我也有点迷糊,知道学长一语点醒我,才发现有个规律我没找到,不就是放书吗,
他给出以每天看啥书,问刚开始应该怎么放置书籍可以让他搬书总重量最小,
如果把它看做很多子过程的话,那么每个子过程就是每天为了看一本书ban了多少重量;子过程贪心即可
第一天看的那本书一定是最上边的位置,这样使得ban第一天书时重量最少(第一个子过程完成)
第二天ban书时,如果不和第一天的书相同,那么第二天的书原来的位置应该在第二个位置,这样可以第二天ban的书的重量最少,以此类推,如果某天看的书以前看过,那么想要完成这个子过程,就需要算一下上一次看这本书到这一次时,中途看过几种书(注意,中途可能也有 一本书看了好多次,所以这一种书只加一次),这些书的重量和就是完成这个子过程所ban的重量,完成m个子过程。解决问题!
#include<iostream>
#include<string.h>
using namespace std;
int main()
{
	int m,n;
	int sum,sum1;
	int a[505];
	int b[1005],vis[505];
	while(cin>>n>>m)
	{
		int i,j;
		sum=0;
		for(i=0;i<n;i++)
			cin>>a[i];
		for(i=0;i<m;i++)
		cin>>b[i];
		for(i=0;i<m;i++)
		{
			memset(vis,0,sizeof(vis));
			for(j=i-1;j>=0;j--)
		{
			if(b[i]==b[j])
			break;
			if(!vis[b[j]-1])
			{
				sum+=a[b[j]-1];
				vis[b[j]-1]=1;
			}
		}	
	 } 
	cout<<sum<<endl;
}
	return 0;
}


            

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
大学生参加学科竞赛有着诸多好处,不仅有助于个人综合素质的提升,还能为未来职业发展奠定良好基础。以下是一些分析: 首先,学科竞赛是提高专业知识和技能水平的有效途径。通过参与竞赛,学生不仅能够深入学习相关专业知识,还能够接触到最新的科研成果和技术发展趋势。这有助于拓展学生的学科视野,使其对专业领域有更深刻的理解。在竞赛过程中,学生通常需要解决实际问题,这锻炼了他们独立思考和解决问题的能力。 其次,学科竞赛培养了学生的团队合作精神。许多竞赛项目需要团队协作来完成,这促使学生学会有效地与他人合作、协调分工。在团队合作中,学生们能够学到如何有效沟通、共同制定目标和分工合作,这对于日后进入职场具有重要意义。 此外,学科竞赛是提高学生综合能力的一种途径。竞赛项目通常会涉及到理论知识、实际操作和创新思维等多个方面,要求参赛者具备全面的素质。在竞赛过程中,学生不仅需要展现自己的专业知识,还需要具备创新意识和解决问题的能力。这种全面的综合能力培养对于未来从事各类职业都具有积极作用。 此外,学科竞赛可以为学生提供展示自我、树立信心的机会。通过比赛的舞台,学生有机会展现自己在专业领域的优势,得到他人的认可和赞誉。这对于培养学生的自信心和自我价值感非常重要,有助于他们更加积极主动地投入学习和未来的职业生涯。 最后,学科竞赛对于个人职业发展具有积极的助推作用。在竞赛中脱颖而出的学生通常能够引起企业、研究机构等用人单位的关注。获得竞赛奖项不仅可以作为个人履历的亮点,还可以为进入理想的工作岗位提供有力的支持。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值