본문 바로가기

Programming/BOJ

[백준/Python] 1715번: 카드 정렬하기(최소힙)

import sys
import heapq


# input
N = int(input())

arr = []
for i in range(N):
    arr.append(int(input()))
    
# process
answer = 0
heapq.heapify(arr)
while True:
    if len(arr) == 1:
        break
    
    n1 = heapq.heappop(arr)
    n2 = heapq.heappop(arr)
    
    n3 = n1+n2
    answer += n3
    heapq.heappush(arr,n3)
    
    
print(answer)