A launch console shows n dials, each set to an integer, forming an arrangement. Advance the arrangement to the next one in lexicographic order among all distinct arrangements of the same multiset of dial values. If the current arrangement is already the lexicographically largest, wrap around to the lexicographically smallest arrangement. Output the resulting arrangement.
Input format
Line 1: an integer n.
Line 2: n space-separated integers, the current arrangement.
Output format
n space-separated integers on one line: the next arrangement (wrapping to the smallest if already the largest).
Constraints
- 1 ≤ n ≤ 8
- 1 ≤ each value ≤ 1000000