Farmer John's Favorite Permutation
시간 제한2초메모리 제한1024 MB
덱 양 끝에서 제거하며 남긴 N-1개의 힌트가 주어질 때, 이와 일치하는 가장 사전순으로 작은 순열을 구하거나 불가능하면 -1을 출력한다.
문제
Farmer John has a permutation of length (, containing each positive integer from to exactly once. However, Farmer Nhoj has broken into FJ's barn and disassembled . To not be too cruel, FN has written some hints that will help FJ reconstruct . While there is more than one element remaining in , FN does the following:
Let the remaining elements of be ,
- If , he writes down and removes from the permutation.
- Otherwise, he writes down and removes from the permutation.
At the end, Farmer Nhoj will have written down integers , in that order. Given , Farmer John wants to enlist your help to reconstruct the lexicographically minimum consistent with Farmer Nhoj's hints, or determine that Farmer Nhoj must have made a mistake. Recall that if you are given two permutations and , is lexicographically smaller than if at the first position where the two differ.
입력
Each input consists of independent test cases (). Each test case is described as follows:
The first line contains .
The second line contains integers ().
출력
Output lines, one for each test case.
If there is a permutation of consistent with , output the lexicographically smallest such . If no such exists, output .