#d2113. 可重排列

可重排列

Multiset Permutations

Problem Description

Output all sequences in lexicographically increasing order such that the sequence contains p1p_1 copies of 11, p2p_2 copies of 22, …\dots, pnp_n copies of nn.

Input Format

The first line contains an integer nn.

The second line contains nn integers p1,p2,…,pnp_1, p_2, \dots, p_n.

Output Format

Output all valid sequences in lexicographically increasing order, one sequence per line. Numbers in a sequence are separated by spaces.

Sample Input

3
1 2 2

Sample Output

1 2 2 3 3
1 2 3 2 3
1 2 3 3 2
1 3 2 2 3
1 3 2 3 2
1 3 3 2 2
2 1 2 3 3
2 1 3 2 3
2 1 3 3 2
2 2 1 3 3
2 2 3 1 3
2 2 3 3 1
2 3 1 2 3
2 3 1 3 2
2 3 2 1 3
2 3 2 3 1
2 3 3 1 2
2 3 3 2 1
3 1 2 2 3
3 1 2 3 2
3 1 3 2 2
3 2 1 2 3
3 2 1 3 2
3 2 2 1 3
3 2 2 3 1
3 2 3 1 2
3 2 3 2 1
3 3 1 2 2
3 3 2 1 2
3 3 2 2 1

Constraints

For 100%100\% of the data, 1≤n≤91 \le n \le 9, 1≤pi≤91 \le p_i \le 9, and the total number of valid sequences does not exceed 10510^5.