Skip to content

Instantly share code, notes, and snippets.

@ewmson
Created October 30, 2016 01:17
Show Gist options
  • Save ewmson/5b16c8907e62aadda4e14d162570d7a8 to your computer and use it in GitHub Desktop.
Save ewmson/5b16c8907e62aadda4e14d162570d7a8 to your computer and use it in GitHub Desktop.
import heapq
T = int(input())
for _ in range(T):
N = int(input())
canv = list(sorted(map(int,input().split())))
heapq.heapify(canv)
amount = 0
while len(canv) != 1:
val = heapq.heappop(canv) + heapq.heappop(canv)
heapq.heappush(canv,val)
amount += val
print(amount)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment