CF500C.New Year Book Reading
普及/提高-
通过率:0%
时间限制:2.00s
内存限制:256MB
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
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 w__i.
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 b__j (1 ≤ b__j ≤ 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?
新年将至,与今年不同,Jaehyun 决定在 2015 年阅读大量书籍。他共有 n 本书,编号为 1 到 n。第 i 本书(1≤i≤n)的重量为 wi。
由于 Jaehyun 的家空间有限,无法容纳书架,他只能将这 n 本书垂直堆叠存放。当他想阅读某本编号为 x 的书时,需按如下步骤操作:
- 他拿起位于书 x 上方的所有书;
- 他将书 x 从堆叠中抽出;
- 他将之前拿起的书按原有顺序放回(即不改变它们之间的相对顺序);
- 阅读完书 x 后,他将书 x 放到堆叠的最顶端。

他计划连续 m 天阅读书籍。在第 j 天(1≤j≤m),他将阅读编号为 bj 的书(1≤bj≤n)。为阅读该书,他必须执行上述过程。他有可能在多天中重复阅读同一本书。
制定该计划后,他意识到:在 m 天内,他需要抬起的书籍总重量过大。因此,他决定在新年到来前重新调整书籍的堆叠顺序,以最小化这 m 天内需抬起的书籍总重量。你可以假设书籍可以按任意可能的顺序堆叠。注意:在某一步中他正要阅读的那本书,在该步中不被视为被抬起。你能帮他实现这一目标吗?
输入格式
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 _w_1, _w_2, ..., w__n (1 ≤ w__i ≤ 100) — the weight of each book.
The third line contains m space separated integers _b_1, _b_2, ..., b__m (1 ≤ b__j ≤ n) — the order of books that he would read. Note that he can read the same book more than once.
第一行包含两个以空格分隔的整数 n(2 ≤ n ≤ 500)和 m(1 ≤ m ≤ 1000)——分别表示书的总数以及 Jaehyun 计划读书的天数。
第二行包含 n 个以空格分隔的整数 w1,w2,...,wn(1 ≤ wi ≤ 100)——表示每本书的重量。
第三行包含 m 个以空格分隔的整数 b1,b2,...,bm(1 ≤ bj ≤ n)——表示他每天要读的书的顺序。注意:他可能在不同天重复读同一本书。
输出格式
Print the minimum total weight of books he should lift, which can be achieved by rearranging the order of stacked books.
打印他需要搬动的书的最小总重量,该值可通过重新排列堆叠书籍的顺序来实现。
输入输出样例
输入#1
3 5 1 2 3 1 3 2 3 1
输出#1
12
说明/提示
Here's a picture depicting the example. Each vertical column presents the stacked books.

以下是一张展示该示例的图片。每一列竖直方向表示堆叠在一起的书。

输入解题思路,AI测评打分。不知道怎么写?