Polycarp Recovers the Permutation solution codeforces
Polycarp wrote on a whiteboard an arrayof length , which is a permutation of numbers from to . In other words, in each number from to occurs exactly once.
He also prepared a resulting array, which is initially empty (that is, it has a length of ).
After that, he did exactly steps. Each step looked like this:
- Look at the leftmost and rightmost elements of , and pick the smaller of the two.
- If you picked the leftmost element of , append it to the left of ; otherwise, if you picked the rightmost element of , append it to the right of .
- The picked element is erased from . Polycarp Recovers the Permutation solution codeforces
Note that on the last step,has a length of and its minimum element is both leftmost and rightmost. In this case, Polycarp can choose what role the minimum element plays. In other words, this element can be added to both on the left and on the right (at the discretion of Polycarp).
Let’s look at an example. Let, . Initially . Then:
- During the first step, the minimum is on the right (with a value of ), so after this step, and (he added the value to the right).
- During the second step, the minimum is on the left (with a value of ), so after this step, and (he added the value to the left).
- During the third step, the minimum is on the left (with a value of ), so after this step, and (he added the value to the left).
- During the fourth step, the minimum is both left and right (this value is ). Let’s say Polycarp chose the right option. After this step, and (he added the value to the right).
Thus, a possible value ofafter steps could be .
You are given the final value of the resulting array any possible initial value for that can result the given , or determine that there is no solution.. Find
The first line of the input contains an integer( ) — the number of test cases in the test.
Each test case consists of two lines. The first of them contains an integer( ) — the length of the array . The second line contains integers ( ) — the elements of the array . All elements of the array are distinct numbers.
It is guaranteed that the sum of the valuesover all test cases in the test does not exceed .
Print -1 on the line.lines, each of the lines must contain the answer to the corresponding set of input data: numbers — any of the possible initial values of the array , which will lead to the given array . All elements of are distinct integers from to . Thus, if there are several solutions, print any. If there is no solution, then print
4 4 1 3 2 4 1 1 5 1 3 5 4 2 3 3 2 1
Polycarp Recovers the Permutation solution codeforces output
3 1 4 2 1 -1 2 3 1
The first test case in the example is clarified in the main section of the problem statement. There may be other correct answers for this test set.
In the second test case,. Thus, there is only one permutation that can be the answer: . Indeed, this is the answer to this test case.
In the third test case of the example, no matter what permutation you take as, after applying the steps, the result will differ from .