n = int(input())
nums = list(map(int, input().split()))
import heapq
heapq.heapify(nums)
res = 0
while len(nums) > 1:
    a, b = heapq.heappop(nums), heapq.heappop(nums)
    res += a + b
    heapq.heappush(nums, a + b)
print(res)