peterverebics/CodeNet_Python_118 huggingface dataset:
N = int(input())
A = list(map(int, input().split()))
S = sorted(A, reverse=True)
X = S[0]
N -= 2
i = 0
while N > 0:
    i += 1
    x = S[i]
    X += x
    if N == 1:
        break
    X += x
    N -= 2
print(X)
from functools import reduce
from fractions import gcd
import math
import bisect
import itertools
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float("inf")


# 処理内容
def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort(reverse=True)

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


if __name__ == '__main__':
    main()
N=int(input())
A=list(map(int,input().split()))
A.sort(reverse=True)
ans=0
import heapq
hq=[]
for i in range(N):
    if i==0:
        heapq.heappush(hq,-A[i])
        continue
    else:
        tmp=heapq.heappop(hq)*-1
        ans+=tmp
        heapq.heappush(hq,-A[i])
        heapq.heappush(hq,-A[i])
print(ans)
from collections import deque
n=int(input())
a=sorted(list(map(int,input().split())),reverse=True)
if n==2:
	print(a[0])
	exit(0)
ans=a[0]
d=deque()
d.append(a[1])
d.append(a[1])
for i in range(2,n):
	x=a[i]
	ans+=d.popleft()
	d.append(x)
	d.append(x)
print(ans)
n = int(input())
li_a = list(map(int,input().split()))
li_a.sort(reverse=True)

ans = li_a[0]
if n % 2 == 0:
    for i in range(1,n//2):
        ans += 2*li_a[i]
else:
    for i in range(1,n//2):
        ans += 2*li_a[i]
    ans += li_a[n//2]

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

ans = -A[0]
for i in range(N):
    ans += A[i//2]
print(ans)
n=int(input())
ar=list(map(int,input().split()))
ar.sort(reverse=True)
ans=0
for i in range(1,n):
    ans+=ar[i//2]
print(ans)
import heapq

n=int(input())
A=list(map(int, input().split()))
for i in range(n):
    A[i]=A[i]*(-1)
heapq.heapify(A)
ans=0
for i in range(n):
    a=heapq.heappop(A)
    if i==0:
        B=[a]
        heapq.heapify(B)
    else:
        b=heapq.heappop(B)
        ans=ans+b
        heapq.heappush(B, a)
        heapq.heappush(B, a)
print(-ans)
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
ans=a[0]
del a[0]

num=0
n-=2

while n>0:
  if n==1:
    ans+=a[num]
    n-=1
    
  else:
    ans+=a[num]*2
    n-=2
    num+=1
    
print(ans)
N = int(input())
a = list(map(int, input().split()))

a.sort(reverse = True)
p = a[0]
for i in range(N-2): #-1は上記a[0]分、もう-1は最初の要素挿入は加点されないため。
    p += a[(i//2)+1]

print(p)
#!/usr/bin python3
# -*- coding: utf-8 -*-

def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort(reverse = True)
    ret = 0
    for i in range(1,N):
        ret += A[i//2]
    print(ret)

if __name__ == '__main__':
    main()
n=int(input())
l=list(map(int, input().split(' ')))

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

ans=l[0]
a=2

for i in range(1,n):
    ans+=(min(2,n-a))*l[i]
    a+=min(2,n-a)
    if a==n:
        break

print(ans)
n = int(input())
A = list(int(x) for x in input().split())
A.sort(reverse=True)
if n == 2:
    print(A[0])
    exit()

ans = A[0]
for i in range(2, n):
    ans += A[i//2]
print(ans)
n = int(input())
a = list(map(int, input().split(" ")))
a = sorted(a)[::-1]

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

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

friendliness.sort(reverse=True)
print(friendliness[0] + 2 * sum(friendliness[1:n//2]) + (n%2) * friendliness[n//2])
N = int(input())
a_ls=list(map(int, input().split()))

a=sorted(a_ls,reverse=True)

ans=0

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

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

print(ans)


# 2darray [[0] * 4 for i in range(3)]
# D - Chat in a Circle

N = int(input())
A = list(int(a) for a in input().split())

A.sort(reverse=True)

n = N-1
ans = A[0]
n -= 1
i = 1
while n > 0:
    if n == 1:
        ans += A[i]
        n -= 1
    else:
        ans += A[i] * 2
        n -= 2
    i += 1
print(ans)
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
num = [A[0]]
for a in A[1:]:
    num += [a] * 2

ans = sum(num[:N-1])
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)
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse = True)
ans = A[0]

counter = 0
idx = 1
for _ in range(N-2):
  ans += A[idx]
  counter += 1
  if counter == 2:
    idx += 1
    counter = 0
print(ans)
from math import *
n = int(input())
a = [int(i) for i in input().split()]
a.sort(reverse = True)
ans = 0
for i in range(1,n):
	ans+=a[i//2]
print(ans)
N=int(input())
A=list(map(int,input().split()))
A.sort(reverse=True)
P=A[0]
for i in range(2,N):
  P+=A[i//2]
print(P)
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
ans = 0
for i in range(n):
    ans += a[i//2]

ans -= a[0]
print(ans)
import sys
input =sys.stdin.readline

N=int(input().rstrip('\n'))
As=[int(x) for x in input().rstrip('\n').split()]
As_s=sorted(As,reverse=True)
res = As_s[0]
num = 2
if N>2:
  for i in As_s[1:]:
    res += i
    num += 1
    if num ==N:
      break
    res += i
    num += 1
    if num==N:
      break
print(res)
from collections import defaultdict as dd
import heapq
N = int(input())
a = list(map(int,input().split()))
res = 0
a = sorted(a, reverse=True)
dic=dd(list)
ls = [-a[0]]
heapq.heapify(ls)
for val in a[1:]:
    tmp = -heapq.heappop(ls)
    res += tmp
    heapq.heappush(ls,-val)
    heapq.heappush(ls,-val)
print(res)
def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort(reverse=True)
    r = A[0]
    for i in range(1, N//2):
        r += 2 * A[i]
    if N % 2 == 1:
        r += A[N//2]
    return r
print(main())
n=int(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)
N = int(input())
A = list(map(int, input().split()))
B = []
for a in A:
    B.append(a)
    B.append(a)
B.sort()
B.pop()
ans = 0
for i in range(N - 1):
    ans += B.pop()
print(ans)
N = int(input())
A = [int(x) for x in input().split()]
A.sort(reverse = True)
ans = A[0]
cnt = 2
for i in range(1, N):
    for j in range(2):
        cnt += 1
        if cnt > N: break
        ans += A[i]

print(ans)
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
from queue import deque
q = deque([a[0]])
ans = 0
for num in a[1:]:
    ans += q.popleft()
    q.append(num)
    q.append(num)
print(ans)
import heapq
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)

ans = 0
heap = []
heapq.heappush(heap, -a[0])
for i in range(1, n):
    tmp = heapq.heappop(heap)
    tmp *= -1
    ans += tmp
    heapq.heappush(heap, -a[i])
    heapq.heappush(heap, -a[i])
print(ans)
import queue
N = int(input())
U = input().split()
l = [0 for i in range(N)]
for i in range(N):
    l[i]= int(U[i])

l.sort()
l.reverse()
ans = 0
if N%2 == 0:
    for i in range(N//2):
        ans += 2*l[i]
    ans -= l[0]
else:
    for i in range(1,(N+1)//2-1):
        ans += 2*l[i]
    ans += l[0]+l[(N+1)//2-1]

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

a = list(map(int,input().split()))
a.sort()
ans = 0

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

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

a.sort(reverse=True)

import heapq
heap=[a[0]*(-1)]

ans=0

for aa in a[1:]:
    tmp=heapq.heappop(heap)
    ans+=((-1)*tmp)
    heapq.heappush(heap,-1*aa)
    heapq.heappush(heap,-1*aa)

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

newa = []

for num in a:
    newa.append(num)
    newa.append(num)

newa.sort(reverse=True)

ans=sum(newa[1:n])

print(ans)
n = int(input())
a = list(map(int, input().split()))
a.sort()

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

print(ans)
N=int(input())
A=sorted(list(map(int,input().split())))[::-1]

i=1
t=1
score=A[0]
while i<=N-1:
    if i+2<=N-1:
        score+=2*A[t]
        i+=2
        t+=1
    elif i+1<=N-1:
        score+=A[t]
        break
    else:
        break
    #print(i,t,score)
print(score)
n = int(input())
a = list(map(int, input().split()))
a = sorted(a)[::-1]
s = 0
for i in range(1, n):
    s += a[i//2]
print(s)
def solve():
    N = int(input())
    A = list(map(int,input().split()))
    A.sort(reverse=True)
    
    ans = 0
    for i in range(N-1):
        if i == 0:
            ans += A[0]
        else:
            if i % 2 == 0:
                ans += A[i//2]
            else:
                ans += A[i//2 + 1]
    
    print(ans)

if __name__ == '__main__':
    solve()
N = int(input())
A = sorted(map(int, input().split()), reverse=True)
if N % 2:
    print(sum(A[: N // 2]) + sum(A[1 : N // 2 + 1]))
else:
    print(sum(A[: N // 2 - 1]) + sum(A[1 : N // 2]) + A[N // 2 - 1])
n = int(input())
la = [int(w) for w in input().split()]
la.sort(reverse=True)

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

cnt = 0
j = 0
for i in range(n-1):
  if i%2 == 1:
    j += 1
  cnt += a[j]
print (cnt)
import heapq

def solve():
    N = int(input())
    A = list(sorted(map(int, input().split()), reverse=True))
    C = [-A[0]]

    ans = 0
    for a in A[1:]:
        K = heapq.heappop(C)
        ans += -K
        heapq.heappush(C, -a)
        heapq.heappush(C, -a)

    print(ans)



solve()
n= int(input())
n -= 1
arr= sorted(list(map(int,input().split())))[::-1]
ans = arr[0]
i = 1
while n > 1:

    if n > 2:
        ans += arr[i]*2
        i += 1
        n -= 2
    elif n ==2:
        ans += arr[i]
        break
                
print(ans)
import math
n = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)

ans = A[0] + 2*sum(A[1:math.floor(n/2)]) + (n%2)*A[math.floor(n/2)]
print(ans)
import math

def resolve():

    n = int(input())
    values = list(map(int, input().split(' ')))

    values.sort(reverse=True)

    ans = 0

    for i in range(1, n):

        ans += values[math.floor(i / 2)]

    print(ans)


resolve()
import heapq

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

arr.sort()
arr.reverse()

que = [0]
heapq.heapify(que)

ans = 0

for i in range(n):
    top = heapq.heappop(que)
    top = -top
    ans += top
    if i == 0:
        heapq.heappush(que, -arr[i])
    else:
        heapq.heappush(que,-arr[i])
        heapq.heappush(que,-arr[i])

    # print(que)

print(ans)
N=int(input())

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

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

print(point)
n = int(input())
a = list(map(int, input().split()))

a.sort(reverse=True)
ans = a[0]
for i in range(2, n):
    ans += a[i // 2]
    #print(ans)

print(ans)
import numpy as np


n = int(input())
ajs_str = input().split()
ajs = [int(aj) for aj in ajs_str]

ajs_sorted = np.sort(ajs)[::-1]

score = ajs_sorted[0]
count = 1
current_idx = 1


max_idx = int( (n-2) / 2 )

for idx in range(1, 1+max_idx):
  score += 2 * ajs_sorted[idx]

if (n-2) % 2 != 0:
  score += ajs_sorted[1+max_idx]

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

ans = a[0]
n -= 2
now = 1
while n > 0:
    if n >= 2:
        ans += a[now]*2
        now += 1
        n -= 2
    elif n == 1:
        ans += a[now]
        n -= 1
        now += 1
print(ans)
# -*- coding: utf-8 -*-

N, = 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)
import sys
input = sys.stdin.readline
from collections import *

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

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

res = 0
for i in range(N):
    res += A[i//2]
print(res-A[0])
n = int(input())
l = list(map(int, input().split()))
l = sorted(l, reverse = 1)


if n % 2 == 0:
    print(l[0] + sum(l[1:1+(n-2)//2])*2)
else:
    print(l[0] + sum(l[1:1+(n-2)//2])*2 + l[1+(n-2)//2])
def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort(reverse=True)
    if N % 2 == 0:
        return A[0] + sum(A[1:N // 2]) * 2
    return A[0] + sum(A[1:N // 2]) * 2 + A[N // 2]


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

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




if __name__ == "__main__":
    main()
input()
l = sorted(map(int, input().split(' ')))

answer = 0
spots = [l.pop()]
x = len(l)

for i in range(x):
	n = l.pop()
	answer += spots[i]
	spots.append(n)
	spots.append(n)

print(answer)
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
ans=a[0]
w=n//2
for aa in a[1:w]:
  ans+=aa*2
if n%2!=0:
  ans+=a[w]
print(ans)
N = int(input())
A = map(int, input().split())

sortA = sorted(A)

result = 0
for i in range(1, N):
    result += sortA[N - (i // 2) - 1]

print(result)
n = int(input()) -2
a = list(map(int, input().split()))

sa = sorted(a,reverse=True)

c = 1
ans = sa[0]


for i in range(1, len(sa)):
    if n <= 0:
        break
    elif n == 1:
        ans += sa[i]
        n -= 1
    else:
        ans += sa[i]*2
        n -= 2
        # print(sa[i])

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


if __name__ == "__main__":
    main()
from collections import deque

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

A_sorted = sorted(A, reverse=True)
ans = 0
queue = deque([A_sorted[0]])
for a in A_sorted[1:]:
    ans += queue.popleft()
    queue.append(a)
    queue.append(a)
print(ans)
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
ans = a[0]
num = 2
idx = 1
while num<n:
    if num+1==n:
        ans += a[idx]
        num += 1
    else:
        ans += a[idx]*2
        num += 2
        idx += 1
print(ans)
n = int(input())
c = list(map(int, input().split()))
c.sort(reverse = True)
a = 0
for i in range(int((n+1)//2)):
  a += c[i]
if n%2==0:
  print(a*2-c[0])
else:
  print(a*2-c[0]-c[n//2])
# -*- coding: utf-8 -*-
n=int(input())
alist=list(map(int,input().split()))
blist=sorted(alist,reverse=True)
total=blist[0]
for i in range(1,int(n/2)):
  total+=blist[i]*2
if n%2:
  total+=blist[int(n/2)]
print(total)
N = int(input())
A = sorted(list(map(int, input().split())), reverse=True)

ans = A[0]
cnt = 1
for a in A[1:]:
    if cnt + 1 < N:
        ans += a
        cnt += 1

    if cnt+ 1 < N:
        ans += a
        cnt += 1
print(ans)
num=int(input().rstrip())
m=input().rstrip().split()
main=[]
for i in m:
    main.append(int(i))
sample=sorted(main,reverse=True)
result=0
for i in range(2,num+1):
    if i%2==0:
        y=i/2-1
        result+=sample[int(y)]
    else:
        y=(i+1)/2-1
        result+=sample[int(y)]
print(result)
n = int(input())
a = list(map(int, input().split()))

a.sort(reverse=True)

n -= 1

idx = 0
score = a[idx]
n -= 1
idx += 1

while n > 0:
    if n > 1:
        score += a[idx] * 2
        n -= 2
        idx += 1
    else:
        score += a[idx]
        n -= 1

print(score)
# 入力
n = int(input())
A = list(map(int,input().split()))

A.sort()

ans = 0
can = 1 # lastの数が後何回足されうるか
will = n-1 # 次に足されるであろう数のindex
for i in range(n-1):
    ans += A[will]
    can -= 1
    if can == 0:
        # 足されきったら一つ小さい数へ
        can = 2 # 最大値以外は2回足されうる
        will -= 1
print(ans)
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse = True)
ans = 0
for i in range(n):
    if i==0:
        ans += 0
    elif i == 1:
        ans += a[0]
    else:
        ans += a[i//2]
print(ans)
# import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)


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

    As = sorted(A)

    res = 0

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

    print(res)



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

cnt = n-1
i = 0
ans = 0
while cnt:
    if i == 0:
        cnt -= 1
        ans += A[i]
        i += 1
    else:
        if cnt < 2:
            ans += A[i]*cnt
            cnt = 0
        else:
            ans += A[i]*2
            cnt -= 2
            i += 1
print(ans)
n = int(input())
a = list(map(int, input().split()))
a = sorted(a)[::-1]
ans = 2 * sum(a[1:n // 2]) + a[0]
if n % 2 == 1:
    ans += a[n // 2]

print(ans)
def INT():
    return int(input())

def MI():
    return map(int, input().split())

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

N = INT()
A = LI()
A = A + sorted(A, reverse = True)[1 : ]
A.sort(reverse = True)
ans = 0

for i in range(N - 1):
    ans += A[i]
    
print(ans)
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
t=(len(a)-2)//2
ans=a[0]
if (len(a)-2)%2==0:
  for i in range(1,t+1):
    ans+=a[i]*2
else:
  ans=ans+a[t+1]
  for i in range(1,t+1):
    ans+=a[i]*2
print(ans)
from sys import stdin
n = int(stdin.readline())
a = list(map(int,stdin.readline().split()))
a.sort(reverse = True)
ans = a[0]
count = 0
cur = 1
for _ in range(n-2):
    ans += a[cur]
    count += 1
    if count == 2:
        count = 0
        cur += 1
print(ans)
N = int(input())
A = list(map(int, input().split()))

A.sort(reverse=True)

a = min(A[0], A[1])
ans = A[0]
idx = 1
c = 1

while c <= N-1:
    if(c == N-1):
        break
    if(c == N-2):
        ans += a
        break
        
    ans += a*2
    c += 2

    idx += 1
    a = min(A[idx], A[idx-1])
         
print(ans)
import sys, math
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 10**9 + 7

import heapq

N = ri()
A = rl()

A.sort()

heap = []
a = A.pop()
heapq.heappush(heap, (-a, a, a))

ans = 0
while A:
	a = A.pop()
	b, l, r = heapq.heappop(heap)
	b = -b
	ans += b
	heapq.heappush(heap, (-min(a, l), l, a))
	heapq.heappush(heap, (-min(a, r), a, r))
print(ans)
n = int(input())
lst = [int(i) for i in input().split()]
lst = sorted(lst)
count = 0
ans = 0
if n % 2 == 0:
    c = n//2
else:
    c = n//2+1
while count < c:
    if count == 0:
        num = lst.pop(-1)
        ans += num
    else:
        num = lst.pop(-1)
        ans += num * 2
    count += 1
if n % 2 == 1:
    ans -= num
print(ans)
N = int(input())
A = list(map(int,input().split()))
A.sort(reverse = True)

#print(A)

if N%2 == 0:
    print(sum(A[:N//2]) * 2 - A[0])
if N%2 == 1:
    #print(A[:N//2+1],A[N//2+1])
    print(sum(A[:N//2+1]) * 2 - A[0] - A[N//2])
n = int(input())
A = list(map(int, input().split()))

out = 0
A.sort(reverse=True)
for i in range(n):
    if (i + 1) * 2 <= n:
        out += A[i] * 2
    elif (i + 1) * 2 - 1 <= n:
        out += A[i]

print(out - A[0])
n = input()
t = int(n)
num = t-2
list = input().rstrip().split(" ")
target = []
for i in list:
    target.append(int(i))
target.sort()
target.reverse()
ans = target[0]
i = 1
k = 0
while num > 0:
    ans += target[i]
    k += 1
    num -=1
    if k == 2:
        i += 1
        k = 0
print(ans)
n = int(input())
a = list(map(int, input().split()))
a = sorted(a, reverse=True)
if n == 2:
  print(a[0])
  exit()

idx = 1
s = a[0]

for i in range(2,n):
  s += a[idx]
  if i % 2 == 1:
    idx += 1
print(s)
# coding: utf-8
N=int(input())
A=list(map(int,input().split()))

A.sort()
ans=max(A)

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

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

print(ans)
N = int(input())
A = list(map(int, input().split()))
A = A * 2
A = sorted(A, reverse=True)
A.pop(0)
SUM = 0
for i in range(N-1):
  SUM += A[i]
print(SUM)
def main():
    n=int(input())
    A=list(map(int,input().split()))
    A.sort(reverse=True)
    if n%2==0:
        ans=sum(A[:(n+1)//2])*2-A[0]
    else:
        ans=sum(A[:(n+1)//2])*2-A[0]-A[n//2]
    print(ans)
    
if __name__=='__main__':
    main()
N=int(input())
a = list(map(int, input().split()))
a.sort(reverse = True)
n = N-2
i = 1
ans = a[0]
while n > 0:
    if n > 1:
        ans += a[i]*2
        i += 1
        n -= 2
    elif n == 1:
        ans += a[i]
        i += 1
        n -= 1
    else:
        pass
print(ans)
N = int(input())
A = list(map(int,input().split()))
A.sort()
A = A[::-1]
ans = 0
if N%2==0:
    for i in range(N//2):
        if i==0:
            ans += A[i]
        else:
            ans += 2*A[i]
else:
    for i in range(N//2):
        if i==0:
            ans += A[i]
        else:
            ans += 2*A[i]
    ans += A[N//2]
print(ans)
N = int(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)
# 2020/07/05
# AtCoder Beginner Contest 173 - D

# Input
n = int(input())
a = list(map(int,input().split()))

# Calc
a.sort()
a.reverse()

ans = a[0]
idx = 1
for i in range(2, n):
    ans = ans + a[idx]
    if i % 2 != 0:
        idx = idx + 1
        
# Output
print(ans)
n = int(input())
a = [int(i) for i in input().split()]
a.sort(reverse=True)
b = [a[0]]
for i in range(1, n):
    b.append(a[i])
    b.append(a[i])
print(sum(b[:n - 1]))
n=int(input())
a=a=list(map(int,input().split()))
a.sort(reverse=True)
ans=0
if n%2==0:
  for i in range(1,n//2):
    ans +=2*a[i]
  ans+=a[0]
else:
  for i in range(1,n//2):
    ans +=2*a[i]
  ans +=a[n//2]
  ans+=a[0]
print(ans)