# [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 ![](http://main.edu.pl/images/OI16/slo-en-tex.1.png) and ![](http://main.edu.pl/images/OI16/slo-en-tex.2.png) can be estimated by ![](http://main.edu.pl/images/OI16/slo-en-tex.3.png). 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. 对于一个1-N的排列(ai)，每次你可以交换两个数ax与ay(x<>y)，代价为W(ax)+W(ay) 若干次交换的代价为每次交换的代价之和。请问将(ai)变为(bi)所需的最小代价是多少。

## 输入输出格式

### 输入格式

The first line of the standard input contains a single integer ![](http://main.edu.pl/images/OI16/slo-en-tex.4.png) (![](http://main.edu.pl/images/OI16/slo-en-tex.5.png)) denoting the number of elephants in the zoo. We assume that the elephants are numbered from ![](http://main.edu.pl/images/OI16/slo-en-tex.6.png) to ![](http://main.edu.pl/images/OI16/slo-en-tex.7.png) to simplify things. The second line holds ![](http://main.edu.pl/images/OI16/slo-en-tex.8.png) integers ![](http://main.edu.pl/images/OI16/slo-en-tex.9.png) (![](http://main.edu.pl/images/OI16/slo-en-tex.10.png) dla ![](http://main.edu.pl/images/OI16/slo-en-tex.11.png)) separated by single spaces and denoting the masses of respective elephants (in kilogrammes). The third line of input contains ![](http://main.edu.pl/images/OI16/slo-en-tex.12.png) pairwise different integers ![](http://main.edu.pl/images/OI16/slo-en-tex.13.png) (![](http://main.edu.pl/images/OI16/slo-en-tex.14.png)) separated by single spac…

### 输出格式

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 ![](http://main.edu.pl/images/OI16/slo-en-tex.20.png).

## 输入输出样例

### 输入样例 #1

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

11200