Problem Statement
You are given a permutation P=(P1,dots,PN) of (1,dots,N), where (P1,dots,PN)neq(1,dots,N).
Assume that P is the K-th lexicographically smallest among all permutations of (1dots,N). Find the (K−1)-th lexicographically smallest permutation.
What are permutations?
A permutation of (1,dots,N) is an arrangement of (1,dots,N) into a sequence.
What is lexicographical order?
For sequences of length N, A=(A1,dots,AN) and B=(B1,dots,BN), A is said to be strictly lexicographically smaller than B if and only if there is an integer 1leqileqN that satisfies both of the following.
- (A1,ldots,Ai−1)=(B1,ldots,Bi−1).
- Ai<Bi.
Constraints
- 2leqNleq100
- 1leqPileqN,(1leqileqN)
- PineqPj,(ineqj)
- (P1,dots,PN)neq(1,dots,N)
- All values in the input are integers.
The input is given from Standard Input in the following format:
N
P1 ldots PN
Output
Let Q=(Q1,dots,QN) be the sought permutation. Print Q1,dots,QN in a single line in this order, separated by spaces.
3
3 1 2
Sample Output 1
2 3 1
Here are the permutations of (1,2,3) in ascending lexicographical order.
- (1,2,3)
- (1,3,2)
- (2,1,3)
- (2,3,1)
- (3,1,2)
- (3,2,1)
Therefore, P=(3,1,2) is the fifth smallest, so the sought permutation, which is the fourth smallest (5−1=4), is (2,3,1).
10
9 8 6 5 10 3 1 2 4 7
Sample Output 2
9 8 6 5 10 2 7 4 3 1