import sys
import heapq
input = sys.stdin.readline
N=int(input())
cards=[]
for _ in range(N):
heapq.heappush(cards,int(input())) # 최소 힙을 통해 정렬
time=0
while len(cards)!=1:
compared=heapq.heappop(cards)+heapq.heappop(cards) # 가장 작은 두 개를 섞음
time+=(compared)
heapq.heappush(cards,compared) # 섞은 것을 heappush를 통해 다른 것과의 비교가 진행되도록
print(time)
# 항상 가장 작은 두 카드 묶음을 꺼내야 하니 새로 더한 것도 비교군에 넣어야 한다.
풀긴 풀었지만, 우선순위 큐를 이용해야 한다는 것을 이미 들어버려서 금방 풀었다.
어떤 연산을 한 뒤 새로 나온 것과 다른 기존 것들과의 비교가 필요하면 우선순위 큐를 사용해야 하는 것 같다.