import queue
pq=queue.PriorityQueue()
n=int(input())
a =list(map(int,input().split()))
for i in range(len(a)):
pq.put(a[i])
sum=0
while pq.qsize()>1:
t=pq.get()+pq.get()
sum+=t
pq.put(t)
print(sum)
import queue
pq=queue.PriorityQueue()
n=int(input())
a =list(map(int,input().split()))
for i in range(len(a)):
pq.put(a[i])
sum=0
while pq.qsize()>1:
t=pq.get()+pq.get()
sum+=t
pq.put(t)
print(sum)