https://www.acmicpc.net/problem/1927
import sys
import heapq
input = sys.stdin.readline
n = int(input())
q = []
for _ in range(n):
x = int(input())
if x == 0:
if len(q) > 0:
print(heapq.heappop(q))
else:
print(0)
else:
heapq.heappush(q, x)