import numpy as np

import heapq



N = int(eval(input()))

A =  np.sort([int(x) for x in input().split(" ")])[::-1]



q = [(-A[1], (0, 1)), (-A[1], (0, 1))]

confort = A[0]

heapq.heapify(q)



i = 2

while N > i:

    m = heapq.heappop(q)

    # print(f"{m[1][0]}と{m[1][1]}の間に割り込む. 気持ち良さ {-m[0]}")

    confort -= m[0]

    heapq.heappush(q, (-A[i], (i, m[1][0])))

    heapq.heappush(q, (-A[i], (i, m[1][1])))

    i += 1

print(confort)

N = int(eval(input()))

A = sorted([int(x) for x in input().split(" ")])[::-1]



N -= 2

S = A[0]



i = 1

while 1:

    if N == 0:

        break 

    if N == 1:

        S += A[i]

        break

    S += 2 * A[i]

    N -= 2

    i += 1

print(S)
import numpy as np
import heapq

N = int(eval(input()))
A = np.sort([int(x) for x in input().split(" ")])[::-1]
q = [(-A[1], (0, 1)), (-A[1], (0, 1))]
confort = A[0]
heapq.heapify(q)
i = 2
while N > i:
    m = heapq.heappop(q)
    # print(f"{m[1][0]}と{m[1][1]}の間に割り込む. 気持ち良さ {-m[0]}")
    confort -= m[0]
    heapq.heappush(q, (-A[i], (i, m[1][0])))
    heapq.heappush(q, (-A[i], (i, m[1][1])))
    i += 1
print(confort)

N = int(eval(input()))
A = sorted([int(x) for x in input().split(" ")])[::-1]
N -= 2
S = A[0]
i = 1
while 1:
    if N == 0:
        break
    if N == 1:
        S += A[i]
        break
    S += 2 * A[i]
    N -= 2
    i += 1
print(S)

-import numpy as np -import heapq - -A = np.sort([int(x) for x in input().split(" ")])[::-1] -q = [(-A[1], (0, 1)), (-A[1], (0, 1))] -confort = A[0] -heapq.heapify(q) -i = 2 -while N > i: -    m = heapq.heappop(q) -    # print(f"{m[1][0]}と{m[1][1]}の間に割り込む. 気持ち良さ {-m[0]}") -    confort -= m[0] -    heapq.heappush(q, (-A[i], (i, m[1][0]))) -    heapq.heappush(q, (-A[i], (i, m[1][1]))) +A = sorted([int(x) for x in input().split(" ")])[::-1] +N -= 2 +S = A[0] +i = 1 +while 1: +    if N == 0: +        break +    if N == 1: +        S += A[i] +        break +    S += 2 * A[i] +    N -= 2 -print(confort) +print(S)