import heapq import ctypes a=int(input()) b=input().split() for i in range(len(b)): b[i]=int(b[i]) heapq.heapify(b) # print(b) sum=0 while len(b) != 1: c,d=heapq.nsmallest(2,b) # heapq.heappop(b) # heapq.heappop(b) b.remove(c) b.remove(d) sum+=c+d heapq.heappush(b,c+d) print(sum)