#参考了BSF的题解
#思路:深度优先搜索每一种走法,搜索完毕之后存到列表中

res = []#结果
#深搜
def dfs(wait, stack, out):#参数为等待进栈列表、栈列表、已出栈列表
    if not wait and not stack:#等待进栈为空且栈为空,全部已经出栈,将出栈列表转化为结果
        res.append(' '.join([str(i) for i in out]))
    if wait: #等待进栈不为空,可以进栈
        dfs(wait[1:], stack + [wait[0]], out)
    if stack: #栈不为空,可以出栈
        dfs(wait, stack[:-1], out + [stack[-1]])

n=int(input())
nums=[int(i) for i in input().split()]
dfs(nums, [], [])
for i in sorted(res):
    print(i)