Two teams must be selected from a pool of players. You, as the captain of the first team, will start first. Afterwards, the captain of the second team (call him captain America) will select any of the remaining players, and so on, by turns. Apart from selecting first, you have several additional advantatges:
You know the exact objective value of every player.
You know that captain America will always select the remaining player that he likes the most, that is, his best friend not yet selected, irrespectively of his objective value.
You know the degree of friendship of captain America with all the players.
Please chose your team members optimally, in order to maximize the sum of their values.
Input consists of several cases, each with , followed by the value of each player, followed by the order of preference of captain America for each player. You can assume , that is even, that the objective values are integers between 1 and , and that the preference values are a permutation of the numbers between 1 and , where 1 means most preferred, and means last preferred.
For example, consider the first line in the sample: Supose that the names of the four players are , , and in order. has value 1, has value 10, has value 100, and has value 1000. However, the preference order of captain America is , , and .
For every case, print the maximum possible sum of values for your team.
For the first example in the sample, the optimal strategy is to start selecting , allowing to go to the other team, and then selecting , for a total sum of 1100.
Input
4 1 10 100 1000 3 2 1 4 4 5 5 5 5 1 2 3 4 6 12 8 5 7 15 10 4 6 3 5 1 2
Output
1100 10 35