목차

파일 합치기 3

ps
링크acmicpc.net/…
출처BOJ
문제 번호13975
문제명파일 합치기 3
레벨골드 5
분류

그리디, 우선순위큐

시간복잡도O(nlogn)
인풋사이즈n <= 1,000,000
사용한 언어Python
제출기록150904KB / 5832ms
최고기록5076ms
해결날짜2021/03/09

풀이

코드

"""Solution code for "BOJ 13975. 파일 합치기 3".

- Problem link: https://www.acmicpc.net/problem/13975
- Solution link: http://www.teferi.net/ps/problems/boj/13975
"""

import heapq


def main():
    T = int(input())
    for _ in range(T):
        K = int(input())  # pylint: disable=unused-variable
        file_sizes = [int(x) for x in input().split()]

        cost = 0
        heapq.heapify(file_sizes)
        while True:
            try:
                f1 = heapq.heappop(file_sizes)
                f2 = heapq.heappop(file_sizes)
                cost += f1 + f2
                heapq.heappush(file_sizes, f1 + f2)
            except IndexError:
                break

        print(cost)


if __name__ == '__main__':
    main()