Bookshelf
Time limit1sMemory limit1024 MB
Books must be restored to order 1..N; each operation removes one book and reinserts it anywhere, costing twice its weight, and the goal is the minimum total cost.
- Level
Medium7 of 10
- Topics
- Dynamic programming, Greedy, Sorting
- Solved
- No attempts yet
Problem
In year 20XX, the IOI will be held in the country where JOI lives. Upon hearing this news, JOI tried to run out of his room to tell his friends. But he was in such a hurry that he bumped into the bookshelf in his room, and all the books fell off the shelf. In a rush, JOI put all the fallen books back onto the shelf without paying any attention to their order, and left home. After returning, JOI needed to restore the books, which were in a terrible order, to their original order.
The bookshelf in JOI's room is N centimeters wide, and it holds N books, each 1 centimeter wide. The books are numbered from 1 to N, and originally the books were arranged on the shelf from left to right in the order 1, ..., N. The weight of book i is Ai grams.
JOI was tired and did not want to hold many books at once, so he decided to tidy the shelf using the following operation:
- Choose one book on the shelf and take it out.
- Then, move books adjacent to the gap left by the removed book, any number of times.
- Then, put the removed book back into the gap on the shelf.
JOI cannot take two or more books out of the shelf at the same time.
For example, if the books are arranged from left to right in the order 5, 3, 4, 1, 2, JOI can take out book 1, move book 4 and then book 3 to the right, and put book 1 back onto the shelf, making the books on the shelf be in the order 5, 1, 3, 4, 2 from left to right. (See the figure below.)

In this operation, when JOI takes a book of weight w grams out of the shelf, he consumes exactly w calories. Also, when JOI puts a book of weight w grams back onto the shelf, he consumes exactly w calories. Since the shelf is made of a smooth material, JOI may consume no calories when moving books inside the shelf.
Because JOI was tired, he decided to restore the books on the shelf to their original order while consuming as few calories as possible.
Given the number of books, the weight of each book, and the current order of the books on the shelf, write a program to find the minimum total calories JOI consumes to rearrange the books on the shelf into their original order.
Input
Read the following input from standard input.
- The first line contains the integer N.
- The following N lines contain information about the weights of the books. The (i + 1)-th line (1 ≤ i ≤ N) contains the integer Ai, the weight of book i.
- The following N lines contain information about the current order of the books on the shelf. The (j + N + 1)-th line (1 ≤ j ≤ N) contains the integer representing the number of the book currently in the j-th position from the left.
Output
Print on standard output, in one line, the integer representing the minimum total calories JOI consumes.
Constraints
- 1 ≤ N ≤ 100 000, the number of books
- 1 ≤ Ai ≤ 1 000 000 000, the weight of book i (grams)
Hint
In this input example, the books are initially arranged from left to right in the order 3, 4, 2, 1, and the weights of books 1, 2, 3, 4 are 1, 6, 4, 3 grams, respectively.
JOI first performs the following operations in order.
- Take out book 1.
- Move book 2, book 4, and book 3 to the right, in that order.
- Put book 1 back into the gap.
The books on the shelf are now in the order 1, 3, 4, 2 from left to right. Since book 1 weighs 1 gram, JOI consumes 1 × 2 = 2 calories.
Next, he performs the following operations in order.
- Take out book 2.
- Move book 4 and book 3 to the right, in that order.
- Put book 2 back into the gap.
The books on the shelf are now in the order 1, 2, 3, 4 from left to right. Since book 2 weighs 6 grams, JOI consumes 6 × 2 = 12 calories.
Therefore JOI can arrange the books from left to right in the order 1, 2, 3, 4 by consuming a total of 14 calories. It is impossible for JOI to consume fewer calories than this.