peterverebics/CodeNet_Python_118 huggingface dataset:
# -*- coding: utf-8 -*-
# モジュールのインポート
import math

# 標準入力を取得
N = int(input())
A = list(map(int, input().split()))

# 求解処理
A = sorted(A, reverse=True)
ans = 0
for n in range(1, N + 1):
    if n == 1:
        continue
    ans += A[math.ceil(n / 2) - 1]

# 結果出力
print(ans)
def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort(reverse=True)
    ans = -A[0]
    cnt = 0
    for i in range(N):
        ans += A[i//2]
    print(ans)


if __name__ == "__main__":
    main()
n=int(input())
A=sorted(list(map(int,input().split())))
ans=0
if n==1 : print(0);exit()
for i in range(n-1):
    # if i==0 : ans+=A[-1];continue
    ans+=A[-((i+1)//2+1)]

print(ans)
N = int(input())
*A, = map(int, input().split())
A.sort(reverse=True)
k = (N+1)//2

ans = sum(A[0:k])*2-A[0]
if N & 1:
    ans -= A[k-1]
print(ans)
N = int(input())
lsA = list(map(int,input().split()))
lsA += lsA
lsA.sort(reverse=True)

lsA.pop(0)
ans = sum(lsA[:N-1])
print(ans)
import sys
from collections import defaultdict
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10**8)


def geta(fn=lambda s: s.decode()):
    return map(fn, readline().split())


def gete(fn=lambda s: s.decode()):
    return fn(readline().rstrip())


def main():
    n = gete(int)
    a = list(geta(int))

    a.sort(reverse=True)

    ans = 2 * sum(a[:(n+1)//2]) - a[0]

    if n & 1 == 1:
        ans -= a[(n+1)//2-1]

    print(ans)


if __name__ == "__main__":
    main()
N = int(input())
A = list(map(int,input().split()))

A.sort(reverse = True)
ans = 0

from collections import deque
tmp = deque([A[0]])

for x in A[1:]:
    ans += tmp.popleft()
    tmp.append(x)
    tmp.append(x)
    
print(ans)
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()
N = int(input())
A = list(map(int, input().split()))
B = sorted(A)[::-1]

idx0 = 0
idx1 = N // 2

idx2 = 1
idx3 = (N + 1) // 2

print(sum(B[idx0:idx1]) + sum(B[idx2:idx3]))
n = int(input())
A = sorted([*map(int, input().split())], reverse=True)
ans = -A[0]
for i in range(n): ans += A[i//2]
print(ans)
N = int(input())
arr = sorted(list(map(int, input().split())),reverse=True)
arr2 = [arr[0]]

for i in range(1,N,1):
    arr2.append(arr[i])
    arr2.append(arr[i])


ans = 0
for i in range(N-1):
    ans += arr2[i]

print(ans)
n=int(input())
A=list(map(int,input().split()))
A.sort(reverse=True)
q=[]
for i in range(n):
  if i==0:
    q.append(A[i])
  else:
    q+=[A[i]]*2
print(sum(q[:n-1]))
import math
N=int(input())
A=list(map(int,input().split()))

A.sort()

K=math.floor(N/2)

S=sum( A[K:N] ) *2

if(N%2==0):
  print(S-A[N-1])
else:
  print(S-A[N-1]-A[K])
n = int(input())
a = sorted(list(map(int, input().split())),reverse=True)
i = 0
cnt = 1
ans = a[0]
a.pop(0)
while i < n-1 or cnt < n - 1:
    for j in range(2):
        if cnt == n - 1:
            break
        cnt += 1
        ans += a[i]
        #print(cnt,ans)
    i += 1
print(ans)
n = int(input())
a_list = list(map(int, input().split()))
a_list.sort(reverse=True)
# print(a_list)
ans = a_list[0]
if n % 2 == 1:
    ans += 2 * sum(a_list[1:(n-1)//2]) + a_list[(n-1)//2]
else:
    ans += 2 * sum(a_list[1:n//2])
print(ans)
from collections import deque


def main():
    input()
    (edges := deque()).append(
        (friendliness := deque(sorted(int(x) for x in input().split()))).pop())
    ans = 0
    while friendliness:
        ans += edges.popleft()
        edges.append(popped := friendliness.pop())
        edges.append(popped)
    print(ans)


if __name__ == '__main__':
    main()
N = int(input())
A = list(map(int,input().split()))
A.sort(reverse=True)
ans = A[0]
for i in range(1,(N - 1)//2 + 1):
    ans += A[i] * 2
if N%2 == 1:
    ans -= A[(N - 1)//2]
print(ans)
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
t = n-1
ans = 0
for i in range(n):
    lim = 2
    if i == 0:
        lim = 1
    for j in range(lim):
       if t > 0:
           ans += a[i] 
           t -= 1

print(ans)
ini = lambda : int(input())
inm = lambda : map(int,input().split())
inl = lambda : list(map(int,input().split()))
gcd = lambda x,y : gcd(y,x%y) if x%y else y

n = ini()
a = inl()
a = sorted(a,reverse = True)
c = 1
j = 0
ans = 0
for i in range(1,n):
        ans += a[j]
        c += 1
        if c == 2:
                c = 0
                j += 1
print(ans)
input()
a=list(map(int,input().split()))
a.sort(reverse=True)

import collections
count=len(a)
ans=0
c = collections.Counter(a)
for i in c.keys():
    tmp = c[i]*2
    if count<=tmp:
        ans+=i*count
        break
    else:
        ans+=i*tmp
        count-=tmp
print(ans-max(a))
n = int(input())
A = list(map(int,input().split()))
A.sort(reverse=True)
B = sorted(A+A[1:],reverse=True)
print(sum(B[:n-1]))
#from collections import deque,defaultdict
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True # and False
BIG = 10**18
R = 10**9 + 7
#R = 998244353

def ddprint(x):
  if DBG:
    print(x)

n = inn()
a = inl()
a.sort(reverse=True)
sm = 0
for i in range(1,n):
    sm += a[i//2]
print(sm)
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
ans = 0
for i in range(N-1):
  ans += A[(i+1)//2]
  
print(ans)
N = int(input())
A = list(map(int, input().split()))
A.sort()
A = A[::-1]
if N % 2 == 0:
  M = N // 2
  ans = sum(A[:M]) * 2 - A[0]
else:
  M = (N - 1) // 2
  ans = sum(A[:M]) * 2 - A[0] + A[M]
  
print(ans)
n = int(input())
A = list(map(int,input().split()))

A.sort(reverse = True)

ans = 0
can = 1
last = 0
for i in range(n-1):
    ans += A[last]
    can -= 1
    if can == 0:
        can = 2
        last += 1
print(ans)
N = int(input())

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

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

print(ans)
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
ans = a[0]
small_idx = 1
small_idx_count = 0
for i in range(2, n):
    ans += a[small_idx]
    small_idx_count += 1
    if small_idx_count == 2:
        small_idx += 1
        small_idx_count = 0
    #print("small_idx is", small_idx)
    #print("small_idx_count is", small_idx_count)
print(ans)
n=int(input())
alist=list(map(int,input().split()))

alist.sort(reverse=True)
counter = 0
for i in range(n):
    if i == 0:
        counter += 0
    else:
        counter += alist[i//2]
print(counter)
# -*- coding: utf-8 -*-
# モジュールのインポート
import math
import numpy as np

# 標準入力を取得
N = int(input())
A = list(map(int, input().split()))

# 求解処理
A = sorted(A, reverse=True)
A = np.array(A)
ans = 2 * np.sum(A[:(N // 2)]) - A[0] + (N % 2) * A[math.floor(N / 2)]

# 結果出力
print(ans)
def main():
  N=int(input())
  A=list(map(int,input().split()))
  A.sort(reverse=True)
  ans=0
  for i in range(1,len(A)):
    ans+=A[i//2]
  print(ans)
  pass
if __name__ == '__main__':
  main()
def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort(reverse=True)
    if N % 2 == 0:
        ans = A[0] + 2*sum(A[1:N // 2])
    else:
        ans = A[0] + 2*sum(A[1:N // 2]) + A[N // 2]
    print(ans)
    return


if __name__ == "__main__":
    main()
n=int(input())
a=list(map(int,input().split()))
ans=0

a.sort(reverse=True)
ans+=int(a[0])
n-=2
where=1
kaisu=0
while n>0:
  ans+=int(a[where])
  kaisu+=1
  n-=1
  if kaisu==2:
    kaisu=0
    where+=1

print(str(ans))
n=int(input())
arr=[int(i) for i in input().split()]
arr.sort(reverse=True)
i=0
j=1
res=arr[0]
n-=2
while n:
    k=min(2,n)
    res+=arr[j]*k
    n-=k
    j+=1
print(res)
n=int(input())
a=list(map(int, input().split()))
a.sort(reverse=True)
if n==2:
    print(a[0])
elif n%2==0:
    print(2*sum(a[:n//2])-a[0])
else:
    print(2*sum(a[:(n+1)//2])-a[0]-a[(n+1)//2-1])
N = int(input())
A = list(map(int,input().split()))
A.sort(reverse=True)
B = [A[0]]
for i in range(N-1):
  B.append(A[i+1])
  B.append(A[i+1])
#print(B)
ans = 0
for i in range(N-1):
  ans += B[i]
print(ans)
import sys
sys.setrecursionlimit(10**6)


def main(input, print):
    n = int(input())
    a = list(map(int, input().split()))
    a.sort(reverse=True)
    ans = 0
    cnt = 0
    for i in range(n):
        lim = 1 if i == 0 else 2
        for j in range(lim):
            if cnt < n-1:
                ans += a[i]
                cnt += 1
    print(ans)


if __name__ == '__main__':
    main(sys.stdin.readline, print)
import math
N = int(input())
A = list(map(int, input().split()))

A.sort()
ans = 0
for i in range(N - 1):
  ans += A[N - math.ceil(i / 2) - 1]
print(ans)
N = int(input())
A = list(map(int, input().split()))
A = A + A
A.sort()

ans = A.pop()
A.pop()
for _ in range(N - 2):
    ans += A.pop()

print(ans)
n = int(input())
As = list(map(int, input().split()))
As.sort(reverse=True)
cnt = As[0]
# print(As)
for i in range(1, n-1):
    cnt += As[(i+1)//2] 
print(cnt)
N = int(input())
A = list(map(int, input().split()))
A = sorted(A, reverse = True)
ans = 0
for i in range(1, N):
  x = int(i / 2)
  ans += A[x]
  #print(x, ans)
print(ans)
n = int(input())

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

a = sorted(a, reverse = True)
final = 0

if n % 2 == 0:
    ak = a[0:int(n/2)]
    for i in ak[1::]:
        final += i
    final *= 2
    final += ak[0]
else:
    ak = a[0:(int(n/2) + 1)]
    for i in ak[1::]:
        final += i
    final *= 2 
    final += ak[0]
    final -= ak[len(ak) - 1]


print(final)
n = int(input())

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

a.sort(reverse = True)

ans = a[0]

for i in range(n-2):
    ans += a[i//2+1]

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

A.sort()
res = 0

for i in range(1, N):
    res += A[N-(i//2)-1]

print(res)
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
ans=a[0]
cnt=n-2
for i in range(1,n):
    if cnt>1: ans+=a[i]*2;cnt-=2
    elif cnt==1: ans+=a[i];break
    else: break
print(ans)
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=False)

ret = 0
c_pointer = 0
comfort = []
tmp = A.pop()
comfort.append(tmp)

while len(A) > 0:
    tmp = A.pop()
    c_pointer += 1
    comfort.append(tmp)
    comfort.append(tmp)

print(sum(comfort[:c_pointer]))
n = int(input())
a = list(map(int, input().split()))
a = sorted(a, reverse=True)
ans = a[0]
a_other = a[1:]
for i in range(n-2):
    idx = i//2
    ans += a_other[idx]
print(ans)
import numpy as np

n = int(input())
a = input().split()
npa=np.array(a, dtype=int)

npa1=np.sort(npa)[::-1]

ninzuu=n-1
if (ninzuu % 2 != 0):
  ninzuu2=int((ninzuu-1)/2)
  atai=npa1[0]+sum(npa1[1:ninzuu2+1])*2
else:
  ninzuu2=int((ninzuu)/2)
  atai=npa1[0]+sum(npa1[1:ninzuu2])*2+npa1[ninzuu2]

print(atai)
N = int(input())
A = sorted(list(map(int, input().split())), reverse=True)
B = [A[0]]
for a in A[1:]:
    B += [a, a]
print(sum(B[:len(A[:-1])]))
N = int(input())
A = list(map(int,input().split()))
l = len(A) - 1
A.sort(reverse=True)
counter = A[0]
num = 1
while l > 2:
    counter += A[num] * 2
    num += 1
    l -= 2
if l == 2:
    counter += A[num]
print(counter)
N = int(input())
A = list(map(int, input().split()))
A = list(reversed(sorted(A)))
new_A = []
for i in range(1, N//2+1):
    new_A.append(A[i])
    new_A.append(A[i])

new_A = [0] + [A[0]] + new_A

print(sum(new_A[:N]))
N = int(input())
A = list(map(int, input().split()))

A.sort(reverse=True)
sum = 0
if N % 2 == 0:
    for i in range(1, N // 2, 1):
        sum = sum + A[i]
    answer = sum * 2 + A[0]
else:
    for i in range(1, N // 2, 1):
        sum = sum + A[i]
    answer = sum * 2 + A[0] + A[N // 2]

print(answer)
import heapq
n = int(input())
a = list(map(int,input().split()))
a.sort(reverse=True)

pq = []
res = 0
heapq.heapify(pq)

for i in range(n):
  if len(pq) == 0:
    heapq.heappush(pq, -a[i])
  else:
    score = -heapq.heappop(pq)
    res += score
    for _ in range(2):
      heapq.heappush(pq, -a[i])

print(res)
N = int(input())
A = sorted(list(map(int, input().split())), reverse=True)
ans = max(A)
for i in range(2, N):
    if i % 2:
        ans += A[(i - 1) // 2]
    else:
        ans += A[i // 2]
print(ans)
from heapq import heappush, heappop

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

pq = [-A[0]]
ans = 0

for a in A[1:]:
    v = -heappop(pq)
    ans += v
    heappush(pq, -a)
    heappush(pq, -a)

print(ans)
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=1)
ans=a[0]
i=0
t=1
while t<n-1:
  i+=1
  for _ in range(2):
    t+=1
    ans+=a[i]
    if t>=n-1:break
print(ans)
from collections import deque
N=int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
q = deque([A[0]])
ans = 0
for i in range(1,N):
  ans += q.popleft()
  q.append( A[i] )
  q.append( A[i] )
print(ans)
import math
n = int(input())
a = list(map(int, input().split()))
b = sorted(a)
ans = 0
for i in range(n-1):
  ans += b[n-1-(math.ceil(i/2))]
print(ans)
from sys import stdin
input = stdin.readline
X = int(input())
x_list = list(map(int, input().split()))
a = sorted(x_list,reverse = True)
ans = 0
for i in range(1,X):
  ans += a[i//2]
print(ans)
n = int(input())
A = list(map(int,input().split()))

A.sort(reverse=True)

ans = A[0]
for i in range(n-2):
    ans += A[(i//2) + 1]

print(ans)
N = int(input())
numbers = list(map(int, input().split()))
s = sorted(numbers, reverse = True)
total = 0
total += s[0]
s.pop(0)
z = N-2
if N%2 ==0:
    for i in range(N//2 -1):
        total += 2*s[i]
else:
    for i in range((N-3)//2):
        total += 2*s[i]
    total += s[(N-3)//2]
print(total)
N = int(input())
A = list(map(int,input().split()))

A = sorted(A,reverse=True)

ans=0

ans+=A[0]
N-=2
ptr=1

while N>0:
    if N>=2:
        ans+=A[ptr]*2
        N-=2
    else:
        ans+=A[ptr]
        N-=1
    ptr+=1
print(ans)
n=int(input())
a=list(map(int, input().split()))  #リスト入力 「a1 a2 a3 ...」みたいな配列のような入力のとき
a.sort(reverse=True)

asum=0
for i in range(1,n):
    ii=(i+2)//2
#    print(ii,a[ii-1])
    asum+=a[ii-1]
    
print(asum)
from collections import defaultdict as dd
from collections import deque
import bisect
import heapq

def ri():
    return int(input())

def rl():
    return list(map(int, input().split()))


def solve():
    n = ri()
    A = rl()
    A.sort(reverse=True)

    ans = 0
    pos = 0
    for i in range(1, len(A)):
        # print (i, pos, A[pos])
        ans += A[pos]
        if i % 2 == 1:
            pos += 1
    print (ans)






mode = 's'

if mode == 'T':
    t = ri()
    for i in range(t):
        solve()
else:
    solve()
n = int(input())
S = list(map(int, input().split()))
s = sorted(S, reverse=True)

if n %2 ==0:
  l = n/2
  m = s[0]
  s = s[1:int(l)]
  p = sum(s)
  o = p*2 + m 
  print(o)
else:
  l = (n+1)/2
  m = s[0]
  q = s[int(l)-1]
  s = s[1:int(l-1)]
  p = sum(s)
  o = p*2 + m +q
  print(o)
from sys import stdin
N = int(stdin.readline().rstrip())
ran = [int(x) for x in stdin.readline().rstrip().split()]
ran.sort(reverse=True)

gen = len(ran) - 2
goukei = ran[0]
if gen % 2 == 0:
  for i in range(int(gen/2)):
    goukei += 2 * ran[i + 1]
else:
  for i in range(int((gen-1)/2)):
    goukei += 2 * ran[i + 1]
  goukei += ran[i + 2]

print(goukei)
#import numpy as np
#import functools
#import operator
#from itertools import combinations as comb
#from itertools import combinations_with_replacement as comb_with
#from itertools import permutations as perm
#import collections as C #most_common
#import math
#import sympy

N = int(input())
#H,W,K= map(int,input().split())
A = list(map(int,input().split()))
#S = str(input())
#T = str(input())
A.sort(reverse=True)
ans=0
num=0
c=1
for i in range(N-1):
    ans+=A[num]
    #print(ans)
    c+=1
    if c==2:
        num+=1
        c=0
print(ans)
a=int(input())
b=list(map(int,input().split()))
b.sort(reverse=True)
ans=0
for i in range(a-1):
  i=(i+1)//2
  ans+=b[i]
print(ans)
import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")

n = int(input())
a = [int(x) for x in input().split()]
a.sort()
ans = a.pop()
a = a[::-1]

if n % 2 == 0:
    n -= 1

    n -= 1
    ans += 2*sum(a[:n//2])

else:
    n -= 1

    n -= 1
    ans += 2*sum(a[:n//2]) + a[n//2]
print(ans)
N = int(input())
A = [int(a) for a in input().split()] *2
A.sort()
A.reverse()
print(sum(A[1:N]))
n, l = int(input()), sorted( list(map(int, input().split())) )[::-1]
if n%2:
    print( l[0] + 2*sum(l[1:n//2]) + l[n//2] )
else:
    print( l[0] + 2*sum(l[1:n//2]) )
N = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
total = a[0]
index = 1
cnt = 0
for i in range(N - 2):
    if cnt == 2:
        index += 1
        cnt = 0
    total += a[index]
    cnt += 1

print(total)
N = int(input())
A = [int(i) for i in input().split()]
# 全部のパターンだとN**2
# 仕組み的に低い人ほどあとにきた方がうまくいきそうに見える
A.sort(reverse=1)
s = A[0]
for i in range(N-2):
    p = i
    if i % 2 == 1:
        p -= 1
    s += A[p//2+1]
print(s)
#! /usr/bin/env python3

# from math import gcd
from fractions import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations, combinations_with_replacement

N = int(input())
A = [int(_) for _ in input().split()]
A = sorted(A, reverse=True)

ret = A[0]
ct = 0
ni = []
for i in range(1, N-1):

    ret += A[(i+1) // 2]

print(ret)
n = int(input())
a = list(sorted(map(int, input().split()), reverse=True))

ret = a[0]
now = 1
for i in range(1, n - 1):
    ret += a[now]
    if i % 2 == 0:
        now += 1    

print(ret)
n = int(input())
alst = list(map(int, input().split()))
alst.sort(reverse = True)
ans = 0
pos = 0
cnt = 0
while cnt < n - 1:
    cnt += 1
    ans += alst[pos]
    if cnt % 2 == 1:
        pos += 1
print(ans)
n=int(input())-1
a=sorted(list(map(int,input().split())))
b=n//2+1
ans=sum(a[-b:])*2-a[-1]

if n%2==0:
  print(ans-a[-b])
else:
  print(ans)
n=int(input())-2
a=list(map(int,input().split()))
a.sort(reverse=True)
ind=1
ans=a[0]
co=0
while n>0:
  co+=1
  ans+=a[ind]
  if co==2:
    co=0
    ind+=1
  n-=1
print(ans)
import sys
import collections
ri = lambda: int(sys.stdin.readline())
rl = lambda: list(map(int, sys.stdin.readline().split()))

n = ri()
li = sorted(rl(), reverse=True)
q = collections.deque([max(li)])
score = 0
for i in li[1:]:
    a = q.popleft()
    score += a
    q.append(i)
    q.append(i)
print(score)
N = int(input())
X = list(map(int, input().split()))

X.sort(reverse=True)
ans = 0
cnt = 0
j = 0
for i in range(N):
    if i == 0:
        continue
    elif i == 1:
        ans += X[j]
        j += 1
    else:
        ans += X[j]
        cnt += 1
        if cnt == 2:
            j += 1
            cnt = 0

print(ans)
n = int(input())
a = list(map(int,input().split())) 
ans = 0
a.sort(reverse=True)
ans += a[0]
k = 0
if n%2 == 0:
    for i in range((n-1)//2):
        ans += 2*a[i+1]
else:
    for k in range((n-1)//2-1):
        ans += 2*a[k+1]
    ans += a[k+2]
if n == 3:
    ans += a[1]
    ans -= a[2]
print(ans)
n = int(input())
a = [int(i) for i in input().split()]

a.sort(reverse=True)
ans = a[0]

tmp = n-2

for i in range(n):
    if tmp == 0:
        break
    if tmp >= 2:
        ans += a[i+1]*2
        tmp -= 2
    elif tmp == 1:
        ans += a[i+1]
        tmp -= 1
    #print(ans,tmp)
        
print(ans)
n, *a = map(int, open(0).read().split())
a.sort(reverse=True)
ans = a[0]
if n%2 == 0:
    for i in range(1, n//2):
        ans += a[i] * 2
else:
    for i in range(1, int(n/2)):
        ans += a[i] * 2
    ans += a[i+1]

print(ans)
n = int(input())
a = list(map(int, input().split()))
a = sorted(a, reverse=True)

if n % 2 == 0:
  cnt = n // 2
  cnf = a[0] + sum(a[1:cnt]) * 2

else:
  cnt = n //2
  cnf = a[0] + sum(a[1:cnt]) * 2 + a[cnt]

print(cnf)
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return input()
def i(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10**9)
INF = 10**9
mod = 10**9+7

N = i()
A = l()
A.sort(reverse=True)
ans = 0
lis = [A[0]]
A = A[1:]
for i in range(N-1):
    lis.append(A[i])
    lis.append(A[i])
for i in range(N-1):
    ans += lis[i]
print(ans)
n = int(input())

a = list(map(int, input().split()))
a.sort(reverse=True)

ans = 0

for i in range(1, n):
    ans += a[i//2]

print(ans)
import heapq

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

Q = [[-A[1],-A[0]]]*2
heapq.heapify(Q)

ans = A[0]
for a in A[2:]:
    #print(Q)
    top = heapq.heappop(Q)
    ans += -top[0]

    heapq.heappush(Q, [-a,-top[0]])
    heapq.heappush(Q, [-a,-top[1]])
    #print(ans)
    
print(ans)
import sys
import math
import fractions
from collections import deque
from collections import defaultdict
sys.setrecursionlimit(10**7)

N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
point = 0
point += A[0]

cnt = 1
for a in A[1:]:
    for _ in range(2):
        point += a
        cnt += 1
        if cnt == N-1:
            break
    if cnt == N-1:
        break

if N == 2:
    print(A[0])
else:
    print(point)
(N,) = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
A = sorted(A, reverse=True)

n1 = 1 + ((N - 2) // 2)
n2 = N - n1

print(sum(A[:n1]) + sum(A[1:n2]))
#!/usr/bin/env python3
n = int(input())
a = list(map(int, input().split()))
a.sort()
ans = a[-1]
cnt = 1
while cnt < n - 1:
    ans += a[-1 * (1 + (1 + cnt) // 2)]
    cnt += 1

print(ans)
import heapq as heapy
n = int(input())
arr = [int(x) for x in input().split()]
arr.sort(reverse=True)
friendliness = 0
circle = [arr[0]]
best = [[-arr[0], (arr[0], arr[0])]]
heapy.heapify(best)
i = 1
while i != n:
    p = heapy.heappop(best)
    left = p[1][0]
    right = p[1][1]
    p1 = [-min(left, arr[i]), (left, arr[i])]
    p2 = [-min(right, arr[i]), (arr[i], right)]
    heapy.heappush(best, p1)
    heapy.heappush(best, p2)
    friendliness += -p[0]
    i += 1
print(friendliness)
N = int(input())
A = list(map(int, input().split()))

A.sort()
A.reverse()
ans = 0
if N%2 == 0 :
  ans += A[0]
  for i in range(int(N/2) - 1) :
    ans += A[i+1] * 2
if N%2 == 1 :
  ans += A[0]
  ans += A[int(N/2)]
  for i in range(int(N/2) - 1) :
    ans += A[i+1] * 2
    
print(ans)
N = int(input())
l = list(map(int,input().split()))
 
l.sort()
l.reverse()
 
ans = 0
ans += l[0]
 
for i in range(1,N-1):
  ans += l[(i+1)//2]
print(ans)
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=1)
from heapq import heappop,heappush
h=[]
heappush(h,-a[0])
ans=0
for i in a[1:]:
  ans-=heappop(h)
  heappush(h,-i)
  heappush(h,-i)
print(ans)
n = int(input())
a = [int(x) for x in input().split()]
a.sort(reverse=True)

k = 1
index = 0
ans = 0
for i in range(n-1):
  k -= 1
  ans += a[index]
  if k == 0:
    index += 1
    k = 2

print(ans)
#!python3

from heapq import heappop, heappush

LI = lambda: list(map(int, input().split()))

# input
N = int(input())
A = LI()


def main():
    A.sort(reverse=True)
    hq = [-A[0]]
    ans = 0
    for a in A[1:]:
        ans -= heappop(hq)
        heappush(hq, -a)
        heappush(hq, -a)
    print(ans)


if __name__ == "__main__":
    main()
N = int(input())
A = list(map(int,input().split()))
A.sort(reverse = True)
ans = 0
K = -(-N//2)
for i in range(K):
    ans += A[i]
for i in range(1,N-K):
    ans += A[i]
print(ans)
x = int(input())
conf = list(map(int, input().split()))

conf.sort(reverse=True)

list=[]
list.append(conf[0])
conf.pop(0)
ans=0
re=1

for c in conf:
    ans+=list[0]
    list.append(c)
    re+=1
    if re==2:
        list.pop(0)
        re=0

print(ans)
import numpy as np

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

A = sorted(A, reverse=True)
A = np.array(A)

if N % 2 == 0:
  # # 偶数の場合
  end = (N - 2) // 2
  answer = A[0] + (A[1:end+1] * 2).sum()
else:
  # 奇数の場合
  end = (N - 2) // 2
  answer = A[0] + (A[1:end+1] * 2).sum() + A[end+1]

print(answer)