LogiQuest

75. Next Permutation

MediumPermutation

Print the next lexicographically greater permutation of the array. If it is the last permutation, print the smallest (sorted ascending) one.

Input format

Line 1: n Line 2: n integers

Output format

The next permutation.

Example 1

Input

3
1 2 3

Output

1 3 2

Example 2

Input

3
3 2 1

Output

1 2 3

Constraints

1 ≤ n ≤ 10⁵

Read input from standard input and print the answer to standard output. Your code is judged against 6 test cases, most of them hidden.

Loading the MyLoginn IDE…