import sys
import heapq
count = int(sys.stdin.readline().strip())
heap = list()
for _ in range(count):
data = int(sys.stdin.readline().strip())
if data:
heapq.heappush(heap, (-data, data))
else:
if heap:
print(heapq.heappop(heap)[1])
else:
print(0)