import heapq n = int(input()) frequencies = list(map(int, input().split())) if n == 1: print(frequencies[0]) exit() # 使用最小堆 heap = frequencies[:] heapq.heapify(heap) total = 0 while len(heap) > 1: # 取出两个最小的频率 first = heapq.heappop(heap) second = heapq.heappop(heap) # 合并这两个频率 combined = first + second total += combined # 将合并后的频率放回堆中 heapq.heappush(heap, combined) print(total)