CodeEff/ECCO huggingface dataset:
# Original solution
n = int(eval(input()))

nums = []
nums = list(map(int, input().split()))
nums = sorted(nums, reverse=True)

cnt = 0

fir_num = nums[0]

del nums[0]

for i in range(n-2):
    fir_num += nums[0]
    cnt += 1
    if cnt == 2:
        del nums[0]
        cnt = 0

print(fir_num)









# Optimized solution
n = int(eval(input()))

nums = []
nums = list(map(int, input().split()))
nums = sorted(nums, reverse=True)

cnt = 0

fir_num = nums[0]

del nums[0]

for i in range(n-2):
    fir_num += nums[int(i/2)]


print(fir_num)
# Original solution
# -*- coding: utf-8 -*-

N = int(eval(input()))

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

A_sort = sorted(A, reverse=True)
sum_num = A_sort.pop(0)

i = 2
while i + 2 <= N:
    sum_num += A_sort.pop(0) * 2
    i += 2

if i + 1 <= N:
    sum_num += A_sort.pop(0)

print(sum_num)



# Optimized solution
# -*- coding: utf-8 -*-

N = int(eval(input()))

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

A_sort = sorted(A)
sum_num = A_sort.pop(-1)

i = 2
while i + 2 <= N:
    sum_num += A_sort.pop(-1) * 2
    i += 2

if i + 1 <= N:
    sum_num += A_sort.pop(-1)

print(sum_num)
# Original solution
from collections import deque
n = int(eval(input()))
A = [*list(map(int, input().split()))]
# print(n)
# print(A)

A.sort(reverse=True)

# print(A)

q = deque([A[0]])
B = []
for a in A[1:]:
    x = q.popleft()
    B.append(x)
    q.append(a)
    q.append(a)
# print(q)
# print(B)
print((sum(B)))


# Optimized solution
from collections import deque
eval(input())
A = [*list(map(int, input().split()))]
A.sort(reverse=True)
q = deque([A[0]])
P = []
for a in A[1:]:
    p = q.popleft()
    P.append(p)
    q.append(a); q.append(a)
print((sum(P)))
# Original solution
from heapq import *
n = int(eval(input()))
A = [*[-int(x) for x in input().split()]]
heapify(A)
ans = -heappop(A)
for c in range(0, n-2, 2): ans += -heappop(A) * (1<<(c+1 < n-2))
print(ans)


# Optimized solution
n = int(eval(input()))
A = sorted([*list(map(int, input().split()))], reverse=True)
ans = -A[0]
for i in range(n): ans += A[i//2]
print(ans)
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))
A.sort(reverse=True)
ans = A[0]
cur = 1
count = 1
while True:
    if count <= N - 3:
        ans += A[cur] * 2
        count += 2
        cur += 1
    else:
        ans += A[cur]
        count += 1
    if count == N - 1:
        break
print(ans)


# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))
A.sort(reverse=True)
ans = A[0]
cur = 1
count = 1
while True:
    if count <= N - 3:
        ans += A[cur] * 2
        count += 2
        cur += 1
    elif count == N - 2:
        ans += A[cur]
        count += 1
    elif count == N - 1:
        break
print(ans)
# Original solution
import sys

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
    n = ni()
    a = sorted(nl(), reverse=True)
    c = a[0]
    n -= 2
    for x in a[1:]:
        if n == 0:
            break
        if n == 1:
            c += x
            n -= 1
        else:
            c += x * 2
            n -= 2
    print(c)
    return

solve()


# Optimized solution
import sys

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
    n = ni()
    a = sorted(nl(), reverse=True)
    print(sum(a[:n//2]) + sum(a[1:(n+1)//2]))
    return

solve()
# Original solution
import sys
sys.setrecursionlimit(10 ** 9)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(eval(input()))
def MI(): return list(map(int, input().split()))
def MI1(): return list(map(int1, input().split()))
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(eval(input()))
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print((k.join(list(map(str, lst)))))
INF = float('inf')
# from math import ceil, floor, log2
from collections import deque
# from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations
# from heapq import heapify, heappop, heappush
# import numpy as np    # cumsum
# from bisect import bisect_left, bisect_right

def solve():
    N = II()
    A = LI()
    A = deque(sorted(A)[::-1])

    q = deque([A.popleft()])

    ans = 0
    while A:
        ans += q.popleft()
        a = A.popleft()
        q.extend([a, a])
    print(ans)

if __name__ == '__main__':
    solve()


# Optimized solution
import sys
sys.setrecursionlimit(10 ** 9)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(eval(input()))
def MI(): return list(map(int, input().split()))
def MI1(): return list(map(int1, input().split()))
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(eval(input()))
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print((k.join(list(map(str, lst)))))
INF = float('inf')
# from math import ceil, floor, log2
from collections import deque
# from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations
# from heapq import heapify, heappop, heappush
# import numpy as np    # cumsum
# from bisect import bisect_left, bisect_right

def solve():
    N = II()
    A = LI()
    A = deque(sorted(A, reverse=True))

    q = deque([A.popleft()])
    # print(q)
    ans = 0
    while A:
        ans += q.popleft()
        a = A.popleft()
        q.extend([a, a])
    print(ans)


if __name__ == '__main__':
    solve()
# Original solution
#!/usr/bin/env python3
import sys
import collections


def II(): return int(sys.stdin.readline())


def MI(): return list(map(int, sys.stdin.readline().split()))


def LI(): return list(map(int, sys.stdin.readline().split()))


def main():
    N = II()
    As = LI()
    As.sort()
    As.reverse()
    ans = []
    ans.append(As.pop(0))
    N -= 1

    for i in range(len(As)):
        tmp = As.pop(0)
        ans.extend([tmp, tmp])

    print((sum(ans[:N])))


# oj t -c "pypy3 main.py"
# acc s main.py -- --guess-python-interpreter pypy
main()


# Optimized solution
#!/usr/bin/env python3
import sys
import collections


def II(): return int(sys.stdin.readline())


def MI(): return list(map(int, sys.stdin.readline().split()))


def LI(): return list(map(int, sys.stdin.readline().split()))


def main():
    N = II()
    As = LI()
    As.sort()
    As.reverse()
    As = collections.deque(As)
    ans = []
    tmp = As.popleft()
    ans.append(tmp)

    for i in range(len(As)):
        tmp = As.popleft()
        ans.extend([tmp, tmp])

    print((sum(ans[:N-1])))
    # print(ans)


# oj t -c "pypy3 main.py"
# acc s main.py -- --guess-python-interpreter pypy
main()
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))

a.sort()
a.reverse()

ans = 0
seq = [a[0]]

seq.append(a[1])
ans += a[0]

l = 2
for i in range(2, n):
    pos = 0
    m_ans, M_ans = 0, 0
    for p in range(l):
        np = p + 1
        if np == l:
            np = 0
        m, M = min(seq[p], seq[np]), max(seq[p], seq[np])
        if m >= m_ans and M >= m_ans:
            pos = np
            m_ans = m
            M_ans = M
    ans += m_ans
    seq.insert(pos, a[i])
    l += 1
print(ans)


# Optimized solution
n = int(eval(input()))
a = list(map(int, input().split()))

a.sort()
a.reverse()

#ans = 0
#seq = [a[0]]
#
#seq.append(a[1])
#ans += a[0]
#
#l = 2
#for i in range(2, n):
#    pos = 0
#    m_ans, M_ans = 0, 0
#    for p in range(l):
#        np = p + 1
#        if np == l:
#            np = 0
#        m, M = min(seq[p], seq[np]), max(seq[p], seq[np])
#        if m >= m_ans and M >= m_ans:
#            pos = np
#            m_ans = m
#            M_ans = M
#    ans += m_ans
#    seq.insert(pos, a[i])
#    l += 1
#print(ans)

ans = 0
cur = 0
cnt = 1
for i in range(n - 1):
    if cnt == 2:
        cur += 1
        cnt = 0
    ans += a[cur]
    cnt += 1
print(ans)
# Original solution
n=int(eval(input()))
a=list(map(int,input().split(' ')))


def min_point(data):
    lgt = len(data)
    m=0
    ins=0
    for i in range(lgt):
        if i ==0:
            buf = min(data[i],data[lgt-1])
        else:
            buf = min(data[i],data[i-1])
        if buf > m:
            m=buf
            ins=i
    return [m,ins]

a.sort(reverse=True)
cnt=0
lst=[]
for i in range(n):
    if i == 0:
        lst.append(a[i])
    elif len(lst) == 1:
        cnt += lst[0]
        lst.append(a[i])
    else:
        mp = min_point(lst)
        cnt += mp[0]
        lst.insert(mp[1], a[i])

print(cnt)

# Optimized solution
n=int(eval(input()))
a=list(map(int,input().split(' ')))


a.sort(reverse=True)
cnt=0
for i in range(1,n):
    if i==1:
        cnt+=a[0]
    else:
        j = (i // 2)
        cnt += a[j]
print(cnt)
# Original solution
# -*- coding: utf-8 -*-

N, = list(map(int, input().split()))
A = list(map(int, input().split()))

A = sorted(A)[::-1]

ans = 0
for i, a in enumerate(A):
    if i==0:
        first = a
    elif i==1:
        ans += first
        pre = [A[1], A[1]]
    else:
        ans += pre[0]
        pre = pre[1:]
        pre.append(a)
        pre.append(a)

print(ans)


# Optimized solution
# -*- coding: utf-8 -*-

N, = list(map(int, input().split()))
A = list(map(int, input().split()))

A = sorted(A)[::-1]

ans = 0
for i, a in enumerate(A):
    if i==0:
        ans += a
        itr = 2
        if itr >= N:
            break
    else:
        ans += a
        itr += 1
        if itr >= N:
            break
        ans += a
        itr += 1
        if itr >= N:
            break

print(ans)
# Original solution
N=int(eval(input()))
A=list(map(int,input().split()))
list.sort(A)

l=[]
n=A.pop()
n2=A.pop()
sum=n
comfort={n2:[[n,n2],[n2,n]]}

while A:
  max=sorted(comfort.keys())[-1]
  if comfort.get(max)==None:print((0))
  n=comfort[max].pop()
  if comfort[max]==[]:del comfort[max]
  sum+=max
  n2=A.pop()
  if comfort.get(min(n[0],n2))==None:comfort[min(n[0],n2)]=[]
  if comfort.get(min(n[1],n2))==None:comfort[min(n[1],n2)]=[]
  comfort[min(n[0],n2)].append([n[0],n2])
  comfort[min(n[1],n2)].append([n[1],n2])
print(sum)

# Optimized solution
eval(input())
A=list(map(int,input().split()))
A.sort(reverse=True)
B=[]
ans=0
for i in A:
  if len(B)>=1:
    ans+=B[len(B)//2]
  B.append(i)
print(ans)
# Original solution
n = int(eval(input()))
a = list(map(int,input().split()))


a.sort(reverse=True)
cnt = a.pop(0)
for i in range(n-2):
    if i % 2 == 0:
        cnt += a[0]
    else:
        cnt += a.pop(0)

print(cnt)

# Optimized solution
n = int(eval(input()))
a = list(map(int,input().split()))


a.sort(reverse=True)
cnt = a.pop(0)
if (n-2) % 2 == 0:
    tmp = 2 * sum(a[0:(n-2)//2])
else:
    tmp = 2 * sum(a[0:(n-2)//2]) + a[(n-2)//2]
cnt += tmp
print(cnt)
# Original solution
conf = 0
num_char = int(eval(input()))
char = list(map(int, input().split()))
char.sort(reverse=True)
count = 0
conf_int = 0
for i in range(num_char):
  if count == 0:
    count = 1
  elif count == 1:
    count = 2
    conf = char[conf_int]
    count_int = 0
    conf_int = 1
  else:
    if count_int == 0 :
      conf += char[conf_int]
      count_int = 1
    else:
      conf += char[conf_int]
      conf_int += 1
      count_int = 0
print(conf)

# Optimized solution
N = int(eval(input()))

As = sorted(map(int, input().split()), reverse=True)

score = As[0]
counter = 0
index = 1

for i in range(2, N):
  score += As[index]
  if counter == 0:
    counter += 1
  else:
    counter -= 1
    index += 1
    
print(score)
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))

A.sort(reverse=True)
cnt = 1
idx = 0
ans = 0
while cnt <= N-1:
    ans += A[idx]
    if cnt % 2 == 1:
        #ans += A[idx]
        idx += 1
    #else:
        #ans += A[idx]
    cnt += 1
print(ans)

# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))
A.sort(reverse=True)
ans = A[0] #最大値だけ1回
for i in range(N-2):
    ans += A[i//2 + 1]
print(ans)
# Original solution
import heapq
N = int(eval(input()))
A = list(map(int,input().split()))
heap = []
for i in range(N):
    heapq.heappush(heap,-A[i])
if N==2:
    ans = -heapq.heappop(heap)
elif N==3:
    ans = -heapq.heappop(heap)-heapq.heappop(heap)
else:
    ans = 0
    ans = -heapq.heappop(heap)
    if N%2==0:
        for _ in range((N-2)//2):
            a = -heapq.heappop(heap)
            ans += 2*a
    else:
        for _ in range((N-3)//2):
            a = -heapq.heappop(heap)
            ans += 2*a
        a = -heapq.heappop(heap)
        ans += a
print(ans)

# Optimized solution
N = int(eval(input()))
A = sorted(list(map(int,input().split())),reverse=True)
if N%2==0:
    cnt = A[0]
    for i in range(1,N//2):
        cnt += 2*A[i]
    print(cnt)
else:
    cnt = A[0]
    for i in range(1,N//2):
        cnt += 2*A[i]
    cnt += A[N//2]
    print(cnt)
# Original solution
N = int(eval(input()))
A = sorted(list(map(int,input().split())),reverse=True)
if N%2==0:
    cnt = A[0]
    for i in range(1,N//2):
        cnt += 2*A[i]
    print(cnt)
else:
    cnt = A[0]
    for i in range(1,N//2):
        cnt += 2*A[i]
    cnt += A[N//2]
    print(cnt)

# Optimized solution
N = int(eval(input()))
A = sorted(list(map(int,input().split())),reverse=True)
cnt = A[0]
k = (N+1)//2-1
for i in range(1,1+k):
    cnt += 2*A[i]
if N%2==1:
    cnt -= A[k]
print(cnt)
# Original solution
import os
import sys
from io import BytesIO, IOBase
# from collections import defaultdict as dd
# from collections import deque as dq
# import itertools as it
# from math import sqrt, log, log2
# from fractions import Fraction
import heapq
def main():
    n = int(eval(input()))
    nums = list(map(int, input().split()))
    nums.sort(reverse=True)
    if n<=3:
        print((sum(nums[:n-1])))
        exit()
    

    hp = list()
    ans = 0
    # Push the first three intervals
    heapq.heappush(hp, (-nums[1], nums[0]))
    heapq.heappush(hp, (-nums[2], nums[0]))
    heapq.heappush(hp, (-nums[2], nums[1]))
    ans = sum(nums[:2])

    i = 3
    while i < n:
        comfort, big = heapq.heappop(hp)
        ans += -comfort

        small = -comfort

        curr = nums[i] # what enters the circle

        #push the smaller interval
        heapq.heappush(hp, (-curr, small))
        heapq.heappush(hp, (-curr, big))
        i += 1
    print(ans)






































# region fastio
BUFSIZE = 8192
class FastIO(IOBase):
    newlines = 0
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
 
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
 
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
 
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)

class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# endregion
 
if __name__ == "__main__":
    main()

# Optimized solution
n = int(eval(input()))
friend = list(map(int, input().split()))

friend.sort(reverse = True)

ans = friend[0]
i = 1
ind = 1
while i < n - 1:
    for j in range(2):
        ans += friend[ind]
        i += 1
        if i == n-1: break
    ind += 1
print(ans)
# Original solution
N=int(eval(input()))
A=list(map(int,input().split()))
A.sort(reverse=True)
ans=0
li=[A[0]]
for i in range(1,N):
  ans+=li[0]
  li.pop(0)
  li.append(A[i])
  li.append(A[i])
print(ans)


# Optimized solution
N=int(eval(input()))
A=list(map(int,input().split()))
A.sort(reverse=True)
ans=0
li=[A[0]]
for i in range(1,N):
  ans+=li[i-1]
  li.append(A[i])
  li.append(A[i])
print(ans)
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))

A.sort(reverse=True)
ans = sum(A[:N//2])*2-A[0]
if N%2==1:
  ans += A[N//2]
print(ans)

# Optimized solution
def solve():
  N = int(eval(input()))
  A = list(map(int, input().split()))
  A.sort(reverse=True)
  ans = A[0]
  cnt = 1
  for i in range(1,N-1):
    ans += A[i]
    cnt += 1
    if cnt==N-1:
      break
    ans += A[i]
    cnt += 1
    if cnt==N-1:
      break
  return ans
print((solve()))
# Original solution
N = int(eval(input()))
A = list(map(int,input().split()))
A = sorted(A,reverse=True)

ans = A[0]

for i in range(1,(N-2)//2+1):
  ans += A[i]*2
  
if N % 2 == 1:
  ans += A[(N-2)//2 + 1]

print(ans)

# Optimized solution
N = int(eval(input()))
A = list(map(int,input().split()))

A = sorted(A,reverse=True)

if N <= 3:
  ans = sum(A[:-1])
elif N % 2 == 0:
  ans = A[0] + sum(A[1:(N-2)//2+1])*2
elif N % 2 == 1:
  ans = A[0] + sum(A[1:(N-2)//2+1])*2 + A[1+(N-2)//2]
  
print(ans)
# Original solution
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)

# Optimized solution
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)
# Original solution
N = int(eval(input()))
A = list(map(int,input().split()))
A = sorted(A)
ans = [A[-1]]
com = 0
for i in range(2,N+1) :
    com += ans[-1]
    del ans[-1]
    ans.append(A[-i])
    ans.append(A[-i])
    ans = sorted(ans)
print(com)


    
    
    


# Optimized solution
N = int(eval(input()))
A = list(map(int,input().split()))
A = sorted(A)
ans = 0
for i in range(1,N) :
    ans += A[N-1 - i//2 ]
print(ans)
# Original solution
# 貪欲に大きい順に入れていって，入れる場所はもっとも周りが大きくなる場所
# この方針は正しいただし，値の取り出しだけではなく挿入が起こるので，優先度付きキュー（heap）を使う必要があるっ
N = int(eval(input()))
A = list(map(int,input().split()))
A.sort(reverse=True)
import heapq
C = []
# heapは最小値しか取り出せないことに注意
heapq.heapify(C)
ans = 0
# 初期条件として2つの値を挿入する
heapq.heappush(C,-A[0])
for i in range(1,N):
    tmp = heapq.heappop(C)
    ans += -tmp
    heapq.heappush(C, -A[i])
    heapq.heappush(C, -A[i])
print(ans)






# Optimized solution
N = int(eval(input()))
A = list(map(int,input().split()))
A.sort(reverse=True)
ans = 0
for i in range(N-1):
    if i == 0:
        ans += A[i]
    else:
        ans += A[1+(i-1)//2]
print(ans)
# Original solution
n = int(eval(input()))
a  = list(map(int,input().split()))

a.sort(reverse=True)
dic = {a[0]:1}
ans = 0

for i in range(1,n):
    h = a[i]
    max_h = list(dic)[0]
    
    
    
    if h == max_h:
        dic[h] += 1
        ans += h
    
    else:
        if h in dic:
            dic[h] += 2
            dic[max_h] -= 1
            ans += max_h
        
        else:
            dic[h] = 2
            dic[max_h] -= 1
            ans += max_h
        
        if dic[max_h] == 0:
            del dic[max_h]
    
    

print(ans)
            

# Optimized solution
n = int(eval(input()))
a  = list(map(int,input().split()))

a.sort(reverse=True)
dic = {a[0]:1}
ans = 0

for i in range(1,n):
    h = a[i]
    max_h = next(iter(dic))
    
    
    
    if h == max_h:
        dic[h] += 1
        ans += h
    
    else:
        if h in dic:
            dic[h] += 2
            dic[max_h] -= 1
            ans += max_h
        
        else:
            dic[h] = 2
            dic[max_h] -= 1
            ans += max_h
        
        if dic[max_h] == 0:
            del dic[max_h]
    
    

print(ans)
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))
A.sort(reverse=True)
ring = [A[0], A[1]]
ans = A[0]
for i in range(2, N):
    max_tmp = 0
    num_tmp = 0
    for j in range(len(ring) - 1):
        if max_tmp < min(ring[j], ring[j + 1]):
            max_tmp = min(ring[j], ring[j + 1])
            num_tmp = j + 1
    if max_tmp < min(ring[0], ring[-1]):
        max_tmp = min(ring[0], ring[-1])
        num_tmp = -1
    if num_tmp == -1:
        ring.append(A[i])
    else:
        ring.insert(num_tmp, A[i])
    ans += max_tmp
print(ans)

# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))
A.sort(reverse=True)
ans = A[0]
s = (N - 2) // 2
if s > 0:
    ans += sum(A[1:1 + s]) * 2
if (N - 2) % 2:
    ans += A[1 + s]
print(ans)
# Original solution
if __name__ == '__main__':
    n = int(input().strip())
    A = list(map(int, input().strip().split()))
    A.sort(reverse=True)
    score = 0
    if not len(A) < 2:
        min_max_list = [0] * n
        min_max_dict = {A[1]: 2}
        max_value = A[1]
        score = A[0]
        value_list = [A[1]]
        for num, a in enumerate(A[2:]):
            score += max_value
            min_max_dict[max_value] -= 1
            if a in min_max_dict:
                min_max_dict[a] += 2
            else:
                min_max_dict[a] = 2
                value_list.append(a)
            if min_max_dict[max_value] == 0:
                del(value_list[0])
                max_value = value_list[0]
    print(score)


# Optimized solution
if __name__ == '__main__':
    n = int(input().strip())
    A = list(map(int, input().strip().split()))
    A.sort(reverse=True)

    score = A[0] + sum([A[1+int(i/2)]for i in range(0, n-2)])

    print(score)
# Original solution
import collections

N=int(eval(input()))
A=list(map(int,input().split()))
A=sorted(A,reverse=True)
c=collections.Counter(A)
x=2*c[A[0]]-1
if x>N-1:
    print((sum(A)-A[0]))
    exit()
ans=A[0]*(2*c[A[0]]-1)
while x<N-1:
    A=A[c[A[0]]:]
    x+=2*c[A[0]]
    ans+=A[0]*2*c[A[0]]
y=x-N+1
ans-=y*A[0]
print(ans)






        




    









        

    









    

        




    















    



    





    






        






    
















        


























            
    


    


    
    














    


    














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