- 이전 포스팅에 있었던 [[백준] 1927번 최소 힙]을 변형한 문제다.
heap
에heappush
수행할 때 마이너스 부호를 붙여 넣어준 다음 가장작은 원소를 출력하는heappop
을 수행할 때 앞에 마이너스 부호를 붙여준다.
import sys
input = sys.stdin.readline
import heapq
heap = []
N = int(input().strip())
for _ in range(N):
num = int(input().strip())
if num != 0:
heapq.heappush(heap, -num)
else:
if len(heap) == 0:
print(0)
else:
print(-heapq.heappop(heap))