백준 13975번: 파일 합치기 3 #Python

ColorlessDia·2025년 2월 6일

algorithm/baekjoon

목록 보기
445/808
import sys
import heapq as hq

input = sys.stdin.readline

T = int(input())

for _ in range(T):
    K = int(input())
    size_list = list(map(int, input().split()))

    hq.heapify(size_list)

    cost = 0

    while 1 < len(size_list):
        size_1 = hq.heappop(size_list)
        size_2 = hq.heappop(size_list)

        merge_size = size_1 + size_2

        cost += merge_size

        hq.heappush(size_list, merge_size)
    
    print(cost)

0개의 댓글