#P441. 【例77.2】经验值

【例77.2】经验值

Description

Xiaoming is playing a game. In this game, there is a permutation of numbers from 11 to nn. We delete each number in a given order. Each time a number is deleted, Xiaoming gains experience points equal to the sum of the number to its left and the number to its right. Note that after deleting a number, the numbers on its left and right become directly connected. If there is no number on the left or right side, the experience gained from that side is 00.
Given the original permutation and the order in which Xiaoming deletes the numbers, please help calculate the total experience points Xiaoming will gain.

Input Format

The first line contains an integer nn (1n1051≤n≤10^5)
The second line contains nn integers, p1p_1,p2p_2,...,pnp_n representing the original permutation.
The third line contains nn integers, d1d_1,d2d_2,...,dnd_n; where did_i represents the number that Xiaoming deletes in the ii-th step.

Output Format

Output a single line containing an integer, representing the answer.

Sample

3
1 2 3
3 1 2
4