升序优先序列,优先pop出最小值
代码
from queue import PriorityQueue
n = int(input())
a = list(map(int,input().split()))
q = PriorityQueue()
for i in range(n):
q.put(a[i])
sum = 0
while q.qsize()>1:
a = q.get()
b = q.get()
q.put(a+b)
sum += a+b
print(sum)