Skip to main content
Tuesday (24.11.2026) at 20:00: ALGOR Contest: Round #4 (Special Challenge) starts!

~/algor $ cat problems/rekursiya-orin-almashtirishlar

All permutations

Medium 1000 ms 128 MB 56%
Share

You are given $n$ distinct integers. Print all their permutations (orders in which every number appears exactly once) in lexicographic order: first by the smaller first element, with equal first elements by the smaller second one, and so on.

The given numbers and the ordered list of all their permutations

Input

The first line contains a positive integer $n$. The second line contains $n$ distinct integers $a_{i}$ separated by spaces.

Output

$n!$ lines, each with $n$ integers separated by spaces.

Note

$1 \le n \le 8$
$-100 \le a_{i} \le 100$
the numbers $a_{i}$ are distinct

Examples

Input #1
3
2 1 3
Output #1
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1
Input #2
1
7
Output #2
7

Sign in to submit a solution

Signing up is free and takes a minute.