해당 문제 링크
## 최소 힙 import heapq import sys input=sys.stdin.readline n=int(input()) h=[] for i in range(n): x=int(input()) if x==0: if len(h)==0: print(0) else: print(heapq.heappop(h)) else: heapq.heappush(h, x)
힙(Heap) 문법을 작성해 놓은 해당 사이트를 참고하면 이해하기 쉽다