# 최소 힙 import sys import heapq input = sys.stdin.readline def solution(): n = int(input().rstrip()) h = [] for i in range(n): v = int(input().rstrip()) if v == 0: if len(h) == 0: print(0) continue print(heapq.heappop(h)) else: heapq.heappush(h, v) return solution() """ 걸린 시간: 5분 시간 복잡도: heappush와 heappop 모두 현재 트리의 높이 즉, logn만큼 든다. 트리 길이가 점점 길어져서 n까지 가는 것이기 때문에 평균적으로 O(logn)이다. n번 진행하기 때문에 전체 시간복잡도는 O(nlogn)이다. 해설: 대놓고 힙 써서 구현하라고 해서 쉬웠다. 0일때 pop하는데, 배열 길이가 0이면 0 출력하도록 설정 넣을때는 heappush, 뺄때는 heappop 하면 끝 """