A parade of all the elephants is about to begin at the Byteotian zoo. The zoo staff lined the huge animals up in a single row, since this row is meant to open the parade.
The manager, however, came to the parade in person and disliked the order he saw. He insisted on his own arrangement, claiming the elephants would look most majestic that way, and told the staff to reorder them accordingly.
Because a herd of moving elephants can cause chaos, the staff decided to rearrange them by swapping one pair at a time. Fortunately, two elephants do not need to stand next to each other to swap places in the row. Moving an elephant is not as easy as it sounds, though: the effort it takes is proportional to the animal's mass. So the effort of swapping two elephants of masses m1 and m2 can be estimated as m1+m2. What is the minimum total effort needed to rearrange the elephants into the order the manager wants?
Write a program that:
The first line contains a single integer n (2≤n≤106), the number of elephants in the zoo. For convenience the elephants are numbered from 1 to n. The second line holds n integers mi (100≤mi≤6500 for 1≤i≤n), separated by single spaces, giving the mass of each elephant in kilograms.
The third line contains n pairwise distinct integers ai (1≤ai≤n), separated by single spaces, listing the elephants in their initial order in the row. The fourth line contains n pairwise distinct integers bi (1≤bi≤n), separated by single spaces, listing the elephants in the order the manager wants. You may assume the sequences (ai) and (bi) are different.
Print a single integer: the minimum total effort needed to rearrange the elephants from the order given by (ai) into the order given by (bi).
For the first (public) test case, one optimal rearrangement uses these three swaps:
The three efforts add up to 3600+3600+4000=11200.