1927: 최소 힙
![](https://velog.velcdn.com/images/jw3418/post/4e891975-59eb-4e16-901d-7ab9a315f893/image.png)
![](https://velog.velcdn.com/images/jw3418/post/2b46de53-96f4-4960-bd33-eca36f541116/image.png)
![](https://velog.velcdn.com/images/jw3418/post/91cf9560-e5fd-49c2-b8f4-b892db277495/image.png)
import sys
import heapq
N = int(input())
heap = []
for _ in range(N):
cmd = int(sys.stdin.readline()[:-1])
if cmd == 0:
if len(heap) == 0:
print(0)
else:
print(heapq.heappop(heap))
else:
heapq.heappush(heap, cmd)
- python의 heapq 묘듈을 사용하여 구현
11279: 최대 힙
![](https://velog.velcdn.com/images/jw3418/post/1d54a5ff-cf09-4237-8e95-11fd3ba52c17/image.png)
import sys
import heapq
N = int(input())
heap = []
for _ in range(N):
cmd = int(sys.stdin.readline()[:-1])
if cmd == 0:
if len(heap) == 0:
print(0)
else:
print(-heapq.heappop(heap))
else:
heapq.heappush(heap, -cmd)
11286: 절댓값 힙
![](https://velog.velcdn.com/images/jw3418/post/49090fd8-a53d-4daa-8f21-800d25d8003d/image.png)
import sys
import heapq
N = int(input())
heap = []
for _ in range(N):
cmd = int(sys.stdin.readline()[:-1])
if cmd == 0:
if len(heap) == 0:
print(0)
else:
tmp = heapq.heappop(heap)
print(tmp[1])
else:
heapq.heappush(heap, [abs(cmd), cmd])