2757Jasmine and Permutation

2757   Jasmine and Permutation

题目描述

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 cant 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.

1
9
通过提交
时空限制2000ms/64mb
题目来源
评测方式在线评测
题目类型
难        度