python
파이썬에서 heapq로 최소 힙 - 우선순위 큐 구현
언어 수집가
2021. 2. 25. 14:09
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import heapq | |
n = int(input()) | |
arr = [] | |
for i in range(n): | |
command = int(sys.stdin.readline().rstrip()) | |
if command != 0: | |
heapq.heappush(arr, command) | |
else: | |
if arr: | |
print(heapq.heappop(arr)) | |
else: | |
print(0) |