백준 1715번: 카드 정렬하기 #Python

ColorlessDia·2025년 2월 5일

algorithm/baekjoon

목록 보기
444/836
import sys
import heapq as hq

input = sys.stdin.readline

heap = []

N = int(input())

for _ in range(N):
    size = int(input())

    hq.heappush(heap, size)

count = 0

while 1 < len(heap):
    A = hq.heappop(heap)
    B = hq.heappop(heap)

    count += A + B

    hq.heappush(heap, A + B)

print(count)

0개의 댓글