import heapq

N = 4

ARR = [2, 2, 1, 3]





N = 7

ARR = [1, 1, 1, 1, 1, 1, 1]



N = int(eval(input()))

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

def calculate(n,arr):



    arr = sorted(arr,reverse=True)



    arr = [-1 * ar for ar in arr]





    mmk = []



    ans = 0

    for i in range(n):

        if i == 0:

            heapq.heappush(mmk,arr[i])

            continue



        ans -= heapq.heappop(mmk)



        heapq.heappush(mmk,arr[i])

        heapq.heappush(mmk,arr[i])



    print(ans)









calculate(N, ARR)
N = 4

ARR = [2, 2, 1, 3]



N = 7

ARR = [1, 1, 1, 1, 1, 1, 1]



N = int(eval(input()))

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



# 3 2 2 1

def calculate(n, arr):

    endIndex = 0

    ans = 0



    arr = sorted(arr, reverse=True)

    for i in range(n):



        if i == 0:

            ans += arr[i]

            endIndex += 1

            continue



        endIndex += 1

        if endIndex >= n:

            break

        ans += arr[i]



        endIndex += 1

        if endIndex >= n:

            break

        ans += arr[i]



    print(ans)





calculate(N, ARR)

import heapq

N = 4
ARR = [2, 2, 1, 3]
N = 7
ARR = [1, 1, 1, 1, 1, 1, 1]
N = int(eval(input()))
ARR = list(map(int, input().split()))


def calculate(n, arr):
    arr = sorted(arr, reverse=True)
    arr = [-1 * ar for ar in arr]
    mmk = []
    ans = 0
    for i in range(n):
        if i == 0:
            heapq.heappush(mmk, arr[i])
            continue
        ans -= heapq.heappop(mmk)
        heapq.heappush(mmk, arr[i])
        heapq.heappush(mmk, arr[i])
    print(ans)


calculate(N, ARR)

N = 4
ARR = [2, 2, 1, 3]
N = 7
ARR = [1, 1, 1, 1, 1, 1, 1]
N = int(eval(input()))
ARR = list(map(int, input().split()))
# 3 2 2 1
def calculate(n, arr):
    endIndex = 0
    ans = 0
    arr = sorted(arr, reverse=True)
    for i in range(n):
        if i == 0:
            ans += arr[i]
            endIndex += 1
            continue
        endIndex += 1
        if endIndex >= n:
            break
        ans += arr[i]
        endIndex += 1
        if endIndex >= n:
            break
        ans += arr[i]
    print(ans)


calculate(N, ARR)

-import heapq - - - +# 3 2 2 1 +    endIndex = 0 +    ans = 0 -    arr = [-1 * ar for ar in arr] -    mmk = [] -    ans = 0 -            heapq.heappush(mmk, arr[i]) +            ans += arr[i] +            endIndex += 1 -        ans -= heapq.heappop(mmk) -        heapq.heappush(mmk, arr[i]) -        heapq.heappush(mmk, arr[i]) +        endIndex += 1 +        if endIndex >= n: +            break +        ans += arr[i] +        endIndex += 1 +        if endIndex >= n: +            break +        ans += arr[i]