import heapq import sys input = sys.stdin.readline n = int(input()) h = [] for _ in range(n): x = int(input()) if x != 0: heapq.heappush(h,(abs(x),x)) else: try: print(heapq.heappop(h)[1]) except: print(0) # heap에 들어간 heap[1]값을 빼야 절대값이 씌워진 수가 아닌 본래 우리가 넣었던 수를 pop할 수 있기 때문에 h[1]을 빼내주는 것이다.