#P3482. [POI2009] SLO-Elephants

[POI2009] SLO-Elephants

题目描述

A parade of all elephants is to commence soon at the Byteotian zoo.

The zoo employees have encouraged these enormous animals to form a single line, as the manager wills it to be the initial figure of the parade.

Unfortunately, the manager himself came to the parade and did not quite like what he saw - he had intended an entirely different order of the elephants.

Therefore he enforced his ordering, claiming the animals would seem most majestic this way, and made the employees reorder the elephants accordingly.

As a pack of moving elephants can wreak havoc, the employees decided to have them rearranged by swapping one pair at a time. Luckily the animals need not stand next to each other in order to swap positions in the line. Making an elephant move, however, is not as easy as it sounds. In fact, the effort one has to put into it is proportional to the animal's mass. Hence, the effort involved in swapping a pair of elephants of respective masses m1m_1 and m2m_2 can be estimated by m1+m2m_1+m_2. What is the minimum effort involved in rearranging the elephants according to manager's will?

Write a programme that:

  • reads from the standard input the masses of all elephants from the zoo, along with their current and desired order in the line,
  • determines a sequence of elephant swaps leading from the initial to the desired order of animals in the line, such that this sequence minimises the summary effort involved in all the swaps,
  • prints out the summary effort on the standard output.

输入格式

The first line of the standard input contains a single integer nn (1n1,000,0001\le n\le 1{,}000{,}000) denoting the number of elephants in the zoo.

We assume that the elephants are numbered from 11 to nn to simplify things.

The second line holds nn integers mim_i (100mi6,500100\le m_i\le 6{,}500 dla 1in1\le i\le n) separated by single spaces and denoting the masses of respective elephants (in kilogrammes).

The third line of input contains nn pairwise different integers aia_i (1ain1\le a_i\le n) separated by single spaces and denoting the numbers of successive elephants in the initial ordering.

The fourth line holds nn pairwise different integers bib_i (1bin1 \le b_i \le n) separated by single spaces and denoting the numbers of successive elephants in the ordering desired by the zoo manager. You may assume that the sequences (ai)(a_i) and (bi)(b_i) differ.

输出格式

The first and only line of the standard output should contain a single integer denoting the minimum summary effort involved in reordering the elephants from the order represented by the sequence to the one represented by (bi)(b_i).

题目大意

对于一个 1N1-N 的排列 aa,每次你可以交换两个数 axa_xaya_y(xyx\neq y),代价为 Wax+WayW_{a_x}+W_{a_y}。若干次交换的代价为每次交换的代价之和。请问将 aia_i 变为 bib_i 所需的最小代价是多少。

对于 100%100\% 的数据,2N10000002\leq N\leq 1000000100wi6500100\leq w_i\leq 65001ai,bin1\leq a_i,b_i\leq n。保证 aia_i 各不相等,bib_i 各不相等。

6
2400 2000 1200 2400 1600 4000
1 4 5 3 6 2
5 3 2 4 6 1

11200