All tasks
Hardmatharrays

Next Permutation

Time limit 2 s · Memory 64 MBAcceptance: —Solved by: 0

Statement

The first line contains n, the second line contains a permutation of the numbers 1 to n. Print the next permutation in lexicographic order on one line separated by spaces. If the given permutation is the last one (decreasing), print -1.

Examples

Example 1
Input
3
1 2 3
Output
1 3 2
Example 2
Input
3
3 2 1
Output
-1

Constraints

  • 1 ≤ n ≤ 10^5
Solve this task to unlock the editorial.

AI tutor

Stuck? Get a nudge — the tutor explains the idea, it never hands you the solution.