https://www.acmicpc.net/problem/1927
import sys
import heapq
input = sys.stdin.readline
n = int(input())
arr = []
for _ in range(n):
num = int(input())
if num != 0:
heapq.heappush(arr, num)
else:
try:
print(heapq.heappop(arr))
except:
print(0)