import sys
import heapq
N = int(input())
heap = []
for _ in range(N):
num = int(sys.stdin.readline())
heapq.heappush(heap, num)
if len(heap) < 2:
print(0)
else:
result = 0
while len(heap) > 1:
temp1 = heapq.heappop(heap)
temp2 = heapq.heappop(heap)
new_temp = temp1 + temp2
result += new_temp
heapq.heappush(heap, new_temp)
print(result)