# 백준
# 최대힙 문제
import heapq
import sys
input = sys.stdin.readline

n = int(input())
data = []
heapq.heapify(data)

for _ in range(0, n):
    x = int(input())
    if x == 0:
        if data:
            print(-heapq.heappop(data))
        else:
            print(0)
    else:
        heapq.heappush(data, -x)

8149fe0d2ddd7f814a3b5e7e49be663b4a342731
<mask token>

<mask token>
heapq.heapify(data)
for _ in range(0, n):
    x = int(input())
    if x == 0:
        if data:
            print(-heapq.heappop(data))
        else:
            print(0)
    else:
        heapq.heappush(data, -x)

<mask token>
input = sys.stdin.readline
n = int(input())
data = []
heapq.heapify(data)
for _ in range(0, n):
    x = int(input())
    if x == 0:
        if data:
            print(-heapq.heappop(data))
        else:
            print(0)
    else:
        heapq.heappush(data, -x)

import heapq
import sys
input = sys.stdin.readline
n = int(input())
data = []
heapq.heapify(data)
for _ in range(0, n):
    x = int(input())
    if x == 0:
        if data:
            print(-heapq.heappop(data))
        else:
            print(0)
    else:
        heapq.heappush(data, -x)

# 백준
# 최대힙 문제
import heapq
import sys
input = sys.stdin.readline

n = int(input())
data = []
heapq.heapify(data)

for _ in range(0, n):
    x = int(input())
    if x == 0:
        if data:
            print(-heapq.heappop(data))
        else:
            print(0)
    else:
        heapq.heappush(data, -x)
