import heapq
import sys
n = int(sys.stdin.readline().strip())
candlis = []
for i in range(n) :
heapq.heappush(candlis,int(sys.stdin.readline().strip()))
res = 0
while len(candlis)>1 :
now=(heapq.heappop(candlis)+heapq.heappop(candlis))
res+=now
heapq.heappush(candlis,now)
print(res)
2023-02-09 재풀이
import sys
import heapq
n = int(sys.stdin.readline().rstrip())
card = []
for i in range(n) :
heapq.heappush(card, int(sys.stdin.readline().rstrip()))
answer = 0
while card :
a = heapq.heappop(card)
if card :
b = heapq.heappop(card)
if len(card)==0 :
if n%2==0 :
answer+=(a+b)
break
answer+=(a+b)
heapq.heappush(card, (a+b))
print(answer)
from collections import deque
import sys
n = int(sys.stdin.readline().strip())
nlis = []
for i in range(n) :
nlis.append(int(sys.stdin.readline().strip()))
nlis.sort()
nlis=deque(nlis)
res = 0
while len(nlis)>1 :
now=nlis[0]+nlis[1]
res+=now
nlis.popleft(); nlis.popleft()
nlis.appendleft(now)
print(res)
heapq.heappush(candlis,int(sys.stdin.readline().strip()))
res = 0