首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【CodeForces】500C - New Year Book Reading(贪心)

【CodeForces】500C - New Year Book Reading(贪心)

作者头像
FishWang
发布2025-08-26 20:43:49
发布2025-08-26 20:43:49
12100
代码可运行
举报
运行总次数:0
代码可运行

点击打开题目

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 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.

Examples

input

代码语言:javascript
代码运行次数:0
运行
复制
3 5
1 2 3
1 3 2 3 1

output

代码语言:javascript
代码运行次数:0
运行
复制
12

Note

Here's a picture depicting the example. Each vertical column presents the stacked books.

昨天想了半天没有想出来解法。今天突然想到从最后一天开始往前找,比如样例:

代码语言:javascript
代码运行次数:0
运行
复制
3 5
1 2 3
1 3 2 3 1

我们倒着看,1 3 2 3 1(这个数据有点特殊,是个回文的),我们先把1放在最上面,现在只有一本1,然后把3放在1上面(这一点考虑清楚,由于3在1前面,所以不管3在哪里取1的时候3肯定还在上面,所以把3放在最上面肯定最省事),然后把2放在3上面——现在的序列为2 3 1——然后我们继续,下来又是3,我们把3拿出来,再放在2上面(现在序列变为3 2 1),最后是1,我们再把1取出来放在3上面,则最终序列为:1 3 2。

这一个贪心思路是正确的,但是实现起来很费时间啊。然后照这个思路会发现:第一个出现的数字就放在最上面就行了,比如 1 1 1 1 3 3 2 3 ,最终的序列为1 3 2。那么一个for循环就可以把顺序整理好,用一个vector维护就行了。

这是我解题的思考的过程。

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
#include <cstdio>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define LL long long
int main()
{
	int n,m;
	int w[500+11];
	int day[1011];
	bool vis[511];		//记录该书是否放好 
	CLR(vis,false);
	vector<int> L;
	scanf ("%d %d",&n,&m);
	for (int i = 1 ; i <= n ; i++)
		scanf ("%d",&w[i]);
	for (int i = 1 ; i <= m ; i++)
	{
		scanf ("%d",&day[i]);
		if (!vis[day[i]])
		{
			vis[day[i]] = true;
			L.push_back(day[i]);
		}
	}
	int ans = 0;
	for (int i = 1 ; i <= m ; i++)
	{
		for (int j = 0 ; j < L.size() ; j++)
		{
			if (day[i] == L[j])
			{
				L.erase(L.begin()+j);
				L.insert(L.begin(),day[i]);
				break;
			}
			else
				ans += w[L[j]];
		}
	}
	printf ("%d\n",ans);
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档