引自:https://blog.nowcoder.net/n/c966740408d945b8bccf72ffdc5dd636

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]])
        
        
n, nums = int(input()), list(map(int, input().split()))
dfs(nums, [], [])
for i in sorted(res):
    print(i)