PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#GD0000163. Polycarp khôi phục hoán vị (Polycarp Recovers the Permutation)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Polycarp khôi phục hoán vị (Polycarp Recovers the Permutation)

Polycarp Recovers the Permutation

Source: Codeforces

Version: Phuoc Hung OJ Extended

Problem Statement

Polycarp starts with a permutation pp and an empty sequence aa. At each step, he removes the smaller of the first and last elements of pp, adding it to the corresponding end of aa. For the final single element he may choose either end. Given the final permutation aa, recover any possible initial permutation pp or print -1.

Input

The first line contains nn. The second line contains the final permutation aa.

Output

Print -1 if impossible; otherwise print a permutation pp that produces exactly aa under the described process.

Subtasks

  • Subtask 1 (20 points): 1≤n≤81\le n\le 8; aa là hoán vị của 1,2,…,n1,2,\ldots,n.
  • Subtask 2 (30 points): 1≤n≤30001\le n\le 3000; aa là hoán vị của 1,2,…,n1,2,\ldots,n.
  • Subtask 3 (50 points): 1≤n≤2⋅1051\le n\le 2\cdot10^5; aa là hoán vị của 1,2,…,n1,2,\ldots,n.

Examples

Input

4
1 3 2 4

Output

2 3 1 4

Explanation

One valid initial permutation is [2,3,1,4][2,3,1,4]. Removing the first endpoint successively adds 22, then 33, then 11 to the left of aa, resulting in [1,3,2][1,3,2]. Finally, add 44 to the right, producing the required array.