#GD0000163. Polycarp khôi phục hoán vị (Polycarp Recovers the Permutation)
Bài tập thuộc lộ trình
Mục lục đề ⌄
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 and an empty sequence . At each step, he removes the smaller of the first and last elements of , adding it to the corresponding end of . For the final single element he may choose either end. Given the final permutation , recover any possible initial permutation or print -1.
Input
The first line contains . The second line contains the final permutation .
Output
Print -1 if impossible; otherwise print a permutation that produces exactly under the described process.
Subtasks
- Subtask 1 (20 points): ; là hoán vị của .
- Subtask 2 (30 points): ; là hoán vị của .
- Subtask 3 (50 points): ; là hoán vị của .
Examples
Input
4
1 3 2 4
Output
2 3 1 4
Explanation
One valid initial permutation is . Removing the first endpoint successively adds , then , then to the left of , resulting in . Finally, add to the right, producing the required array.