파이썬의 heapq는 최소힙이기 때문에 값을 -로 치환해주면 최대힙으로 이용할 수 있다.
import heapq
import sys
input = sys.stdin.readline
N = int(input())
hq = []
for i in range(N):
n = int(input())
if n == 0:
if not hq:
print(0)
else:
print(-1*heapq.heappop(hq))
else:
heapq.heappush(hq, -1*n)