728x90

heapq 로 구현을 했고 heapq 의 사용법을 다시 정리해야 겠다. 

import sys
from collections import deque
import heapq
 
def check(k):
 
    answer = 0
 
 
    heapq.heapify(k)
 
    while k:
 
        if len(k)==1:
 
            break
 
        a = heapq.heappop(k)
        b = heapq.heappop(k)
 
        answer += (a+b)
 
        heapq.heappush(k,a+b)
 
    return answer 
 
 
 
 
 
 
 
T = int(input())
 
for i in range(T):
 
    q= int(sys.stdin.readline())
 
    k = list(map(int,sys.stdin.readline().split(" ")))
 
 
    print(check(k))
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
728x90

+ Recent posts