절댓값 힙은 다음과 같은 연산을 지원하는 자료구조이다.
프로그램은 처음에 비어있는 배열에서 시작하게 된다.
import heapq as hq
import sys
input = sys.stdin.readline
min_heap = []
max_heap = []
for _ in range(int(input())):
x = int(input())
if x:
if x > 0:
hq.heappush(min_heap, x)
else:
hq.heappush(max_heap, -x)
else:
if min_heap:
if max_heap:
if min_heap[0] < abs(-max_heap[0]):
print(hq.heappop(min_heap))
else:
print(-hq.heappop(max_heap))
else:
print(hq.heappop(min_heap))
else:
if max_heap:
print(-hq.heappop(max_heap))
else:
print(0)