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()