n=map(int,input()) num=[int(i) for i in input().split()] stk=[-1] for i in num: while stk[-1]>=i: stk.pop() print(stk[-1],end=' ') stk.append(i)