import heapq
n = int(input())
card = []
for _ in range(n):
    heapq.heappush(card, int(input()))
result = 0
while len(card) != 1:
    one = heapq.heappop(card)
    two = heapq.heappop(card)
    temp = one + two
    result += temp
    heapq.heappush(card, temp)
print(result)

15aed5ef7fdcde836d717526a195f0c55e990cd9
<mask token>

<mask token>
for _ in range(n):
    heapq.heappush(card, int(input()))
<mask token>
while len(card) != 1:
    one = heapq.heappop(card)
    two = heapq.heappop(card)
    temp = one + two
    result += temp
    heapq.heappush(card, temp)
print(result)

<mask token>
n = int(input())
card = []
for _ in range(n):
    heapq.heappush(card, int(input()))
result = 0
while len(card) != 1:
    one = heapq.heappop(card)
    two = heapq.heappop(card)
    temp = one + two
    result += temp
    heapq.heappush(card, temp)
print(result)

import heapq
n = int(input())
card = []
for _ in range(n):
    heapq.heappush(card, int(input()))
result = 0
while len(card) != 1:
    one = heapq.heappop(card)
    two = heapq.heappop(card)
    temp = one + two
    result += temp
    heapq.heappush(card, temp)
print(result)
