[12865] 평범한 배낭

김은서·2022년 3월 21일
0

백준

목록 보기
48/48

Python Code

import sys
import heapq

n = int(input())
left_heap, right_heap = [], []
for i in range(n):
    num = int(sys.stdin.readline())

    if len(left_heap) == len(right_heap):
        heapq.heappush(left_heap, -num)
    else:
        heapq.heappush(right_heap, num)
    
    if right_heap and right_heap[0] < -left_heap[0]:
        left = heapq.heappop(left_heap)
        right = heapq.heappop(right_heap)

        heapq.heappush(left_heap, -right)
        heapq.heappush(right_heap, -left)
    
    print(-left_heap[0])
profile
Gracelog

0개의 댓글