문제
코드
My answer
import sys
import heapq
input=sys.stdin.readline
n=int(input())
heap = []
for i in range(n):
tmp=int(input())
if(tmp!=0):
heapq.heappush(heap, tmp)
else:
if(heap==[]):
print(0)
else:
result=heapq.heappop(heap)
print(result)
Another answer
from heapq import*
h=[]
for i in[*open(0)][1:]:heappush(h,a)if(a:=int(i))else print(len(h)and heappop(h))
풀이
최소힙 모듈사용
728x90
반응형