https://www.acmicpc.net/problem/11279
import sys
import heapq
input = sys.stdin.readline
test_case = int(input())
heap = []
for _ in range(test_case):
x = int(input())
if x != 0:
heapq.heappush(heap, (-x, x))
else:
try:
print(heapq.heappop(heap)[1])
except:
print(0)