cswork / OAOpen practice workspace

AmazonOA #237

Get Operations

Estimated HardJudge-ready

Topics (AI-labelled)GreedySimulation

Array values are distinct. One operation chooses a value x present in the array, places all elements less than x first and then all elements greater than or equal to x, keeping the current relative order within each group. The goal is ascending order: first minimize the number of operations, then take the lexicographically smallest threshold sequence; if the array is already sorted, output −1.

Input

The first line contains n; the second line contains the resource values. 1≤n≤50, 1≤value≤10^9, all values distinct.

Output

Output the threshold sequence separated by spaces; if no operation is needed, output only −1.

Example 1

Input:

6
6 4 3 5 2 1

Output:

2 3 4 6

Example 2

Input:

5
10 5 14 12 13

Output:

10 14

Example 3

Input:

3
1 2 3

Output:

-1

Example notes

The answers are 2 3 4 6, 10 14 and −1. The source's second example output 14 15 belongs to a different array, and 15 is not in the actual input; this site corrects it to 10 14 according to the given input.

Editorial

Loading solution…