”Happy Valentine’s Day to all programming lovers!”
Little Square has received a gift for Valentine’s Day from his girlfriend, Princess Square. The gift consists of an array of integers between and . She also told Little Square that a permutation is perfect if for all . He knows that Princess Square loves the number , so in order to impress her, he will do the following.
- Write all perfect permutations of length on a paper.
- Sort them in increasing lexicographic order. (We say that a permutation is less than a permutation in lexicographic order if and only if and for some .)
- Select the -th permutation on the list and send it back to Princess Square as a gift.
But since it is already 8 pm and Valentine’s Day ends in 4 hours, he needs to do this very fast, so he asks for your help. Write a program which, given and the array , finds the -th perfect permutation of length in lexicographic order, and save Valentine’s Day!
Input data
The first line of input contains the integers and . The second line of input contains the integers , separated by white space.
Output data
The output must contain a single line, which contains the desired permutation , separated by white space. It is guaranteed that such a permutation exists for every test case.
Restrictions
Subtask 1 (9 points)
Subtask 2 (7 points)
Subtask 3 (15 points)
Subtask 4 (19 points)
Subtask 5 (14 points)
Subtask 6 (20 points)
Subtask 7 (16 points)
- No further restrictions
Examples
stdin
5 3
1 3 1 2 4
stdout
1 3 4 2 5
stdin
9 1
4 2 2 5 1 7 9 6 1
stdout
4 2 3 5 1 7 9 6 8
stdin
10 42
5 1 3 2 5 4 9 9 6 2
stdout
5 1 3 7 6 4 10 9 8 2
stdin
20 819011990
6 12 1 2 13 3 13 9 18 4 6 11 7 1 5 7 6 6 1 1
stdout
6 12 1 2 13 4 20 10 18 5 14 11 15 3 16 19 9 7 17 8
Explanations
First example: Little Square’s list is the following.
- ⟨1, 3, 2, 4, 5⟩
- ⟨1, 3, 2, 5, 4⟩
- ⟨1, 3, 4, 2, 5⟩
- ⟨1, 3, 5, 2, 4⟩
- ⟨1, 4, 2, 3, 5⟩
- ⟨1, 4, 3, 2, 5⟩
- ⟨1, 5, 2, 3, 4⟩
- ⟨1, 5, 3, 2, 4⟩
- ⟨2, 3, 1, 4, 5⟩
- ⟨2, 3, 1, 5, 4⟩
- ⟨2, 4, 1, 3, 5⟩
- ⟨2, 5, 1, 3, 4⟩
- ⟨3, 4, 1, 2, 5⟩
- ⟨3, 5, 1, 2, 4⟩
- ⟨4, 3, 1, 2, 5⟩
- ⟨5, 3, 1, 2, 4⟩
Thus we select the 3rd one i.e. .
Second example: The first few permutations in Little Square’s list are the following.
- ⟨4, 2, 3, 5, 1, 7, 9, 6, 8⟩
- ⟨4, 2, 3, 5, 1, 7, 9, 8, 6⟩
- ⟨4, 2, 3, 5, 1, 8, 9, 6, 7⟩
- ⟨4, 2, 3, 5, 1, 8, 9, 7, 6⟩
- ⟨4, 2, 3, 5, 6, 7, 9, 8, 1⟩
- ⟨4, 2, 3, 5, 6, 8, 9, 7, 1⟩
- ⟨4, 2, 3, 5, 7, 8, 9, 6, 1⟩
- ⟨4, 2, 3, 5, 8, 7, 9, 6, 1⟩
- ⟨4, 2, 3, 6, 1, 7, 9, 8, 5⟩
- ⟨4, 2, 3, 6, 1, 8, 9, 7, 5⟩
Thus we select the first one i.e. .