Jasmine has N numbers, each number is in the range of 1 to 9. She can write them from left to right in any order to make them form a new number. However, she thinks it’s too simple, so she adds some limits: For each number, there is exactly one forbidden position where it can’t be put.(We call the highest digit is at the 1st position, and the lowest digit is at the nth position)
Jasmine wants to get a maximum number, can you help her?
There are multiple cases.
For each case, the first line contains an integer N (1<=N<=15), indicating Jasmine has N numbers initially.
The second line contains N integers Ai (1<=Ai<=9), indicating each number Jasmine has.
The third line contains N integers Pi(1<=Pi<=N), indicating that the ith number can’t be put at the position Pi.
For each case, output the maximum number Jasmine can get. If the answer is not existed, output”-1”。
3 1 2 3 2 1 1 3 1 2 3 1 2 3 10 9 8 7 6 5 4 3 2 1 1 1 2 3 4 1 2 3 4 1 2 1 3 1
132 312 8967543211 -1
In the first sample, we know the 1st number 1 can’t be put at the second position, the 2ed number 2 and the third number 3 can’t be put at the first position. In this case, the maximum new number Jasmine can get is 132.