절댓값이 가장 작은 값이 여러 개일 때 가장 작은 수를 출력해야 한다. 예를 들어, |5|와 |-5|가 있는 경우 -5를 출력해야 한다.
이를 처리하기 위해 튜플로 heap에 x와 x의 절댓값을 같이 저장했다. heapq.heappop(heap)은 abs(x)를 먼저 비교하고, abs(x)가 같다면 x의 값을 비교하여 작은 값을 삭제한다.
import sys
import heapq
n = int(sys.stdin.readline().strip())
heap = []
for _ in range(n):
x = int(sys.stdin.readline().strip())
if x == 0:
if heap:
# heapq.heappop(heap)는 (abs(x), x) 꼴이다.
print(heapq.heappop(heap)[1])
else:
print(0)
else:
heapq.heappush(heap, (abs(x), x))