import heapq



n=int(eval(input()))

arr=list(map(int,input().split()))

arr=sorted(arr,reverse=True)

ans=0

q=[]

heapq.heappush(q,-arr[0])

for i in range(1,n):

  tmp=heapq.heappop(q)

  tmp*=-1

  ans+=tmp

  heapq.heappush(q,-arr[i])

  heapq.heappush(q,-arr[i])

print(ans)
n=int(eval(input()))

arr=list(map(int,input().split()))

arr=sorted(arr,reverse=True)

ans=0

for i in range(1,n):

  ans+=arr[i//2]

print(ans)
import heapq

n = int(eval(input()))
arr = list(map(int, input().split()))
arr = sorted(arr, reverse=True)
ans = 0
q = []
heapq.heappush(q, -arr[0])
for i in range(1, n):
    tmp = heapq.heappop(q)
    tmp *= -1
    ans += tmp
    heapq.heappush(q, -arr[i])
    heapq.heappush(q, -arr[i])
print(ans)

n = int(eval(input()))
arr = list(map(int, input().split()))
arr = sorted(arr, reverse=True)
ans = 0
for i in range(1, n):
    ans += arr[i // 2]
print(ans)

-import heapq - -q = [] -heapq.heappush(q, -arr[0]) -    tmp = heapq.heappop(q) -    tmp *= -1 -    ans += tmp -    heapq.heappush(q, -arr[i]) -    heapq.heappush(q, -arr[i]) +    ans += arr[i // 2]