import sys

n = []
input_list = []
res = []

def dfs(wait, stack, out):
    if not wait and not stack:
        res.append(' '.join(map(str, out)))
        
        
    if wait:
        dfs(wait[1:], stack + [wait[0]], out)
    if stack:
        dfs(wait, stack[:-1], out + [stack[-1]])
for line in sys.stdin:
    
    if len(line) == 2:
        n = line
    else:
        input_list = list(map(int, line.split()))
#print(input_list)
dfs(input_list, [], [])
for i in sorted(res):
    print(i)