peterverebics/CodeNet_Python_118 huggingface dataset:
n=int(input())
a=list(map(int, input().split()))

new=a+a

new=sorted(new, reverse=True)

ans=0

for i in range(1, n):
    ans+=new[i]

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

ans = a[0]

nn = (n-2)//2
nnn = (n-2)%2

for i in range(nn):
    ans = ans + a[i+1]*2

if nnn == 1:
    ans = ans + a[i+2]

print(ans)
n=int(input())
l=list(map(int,input().split()))
l.sort(reverse=True)
s=l[0]
n-=2
for i in range(1,n):
	if n>1:
		s+=l[i]*2
		n-=2
	elif n==1:
		s+=l[i]
		n-=1
	else:
		break
print(s)
import math

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

K = int(math.log2(N-1))

if N == 2:
    print(A[0])
else:
    ans = A[0]
    ind = 1
    num = 0
    for i in range(N-2):
        if num < 2:
            ans += A[ind]
            num += 1
        else:
            ind += 1
            ans += A[ind]
            num = 1
        
    print(ans)
N = int(input())
A = sorted(list(map(int, input().split())), reverse=True)
 
ans = A[0]
for i in range(1, N-1):
    ans += A[(i+1)//2]
print(ans)
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[i]
    else:
        ans += A[1+(i-1)//2]
print(ans)
n=int(input())
arr=list(map(int,input().split()))
arr=sorted(arr,reverse=True)
ans=0
for i in range(1,n):
  ans+=arr[i//2]
print(ans)
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)

ans = 0
for i in range(n-1):
  num = i // 2
  num += 1
  if i == 0:
    ans += a[i]
  else:
    ans += a[i-num+1]
print(ans)
n=int(input())
a=sorted(list(map(int,input().split())),reverse=True)
num=[0]*(3*n)
num[1]=a[0]
for i in range(1,n):
    num[2*i],num[2*i+1]=a[i],a[i]
ans=0
for i in range(n):
    ans+=num[i]
print(ans)
n = int(input())
print(sum(sorted([int(x) for x in input().split()] * 2, reverse = True)[1:n]))
# coding: utf-8
# Your code here!
n = int(input())
x = list(map(int,input().split()))
x.sort(reverse=True)
count = 0
for i in range(n-1):
    count += x[int((i+1)/2)]
print(count)
n = int(input())
a = list(map(int, input().split()))

a.sort(reverse=True)

cnt = 1
ans = a[0]

if n == 2:
    print(ans)
    exit()

for i in range(1, n):
    cnt += 1
    ans += a[i]
    if cnt == n - 1:
        break
    else:
        cnt += 1
        ans += a[i]
    if cnt == n - 1:
        break

print(ans)
import sys
#import time
#from collections import deque, Counter, defaultdict
#from fractions import gcd
import bisect
#import heapq
#import math
import itertools
import numpy as np
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
inf = 10**18
MOD = 1000000007
ri = lambda : int(input())
rs = lambda : input().strip()
rl = lambda : list(map(int, input().split()))


n = ri()
a = rl()
a=a+a
a.sort()

a.pop()
ans=0
for i in range(n-1):
    ans+=a[-i-1]
print(ans)
N=int(input())
A=list(int(x) for x in input().split())
A.sort()
sum = 0
for i in range(N-1):
  k=i+1
  sum+=A[N-int(k/2)-1]
print(sum)
n = int(input())
a = sorted(list(map(int, input().split())), reverse=True)
sum_com = 0
distinct_a = sorted(list(set(a)), reverse=True)
comfort = {i: 0 for i in distinct_a}
comfort[a[0]] = 1
max_com = 0

for ai in a[1:]:
    sum_com += distinct_a[max_com]
    if distinct_a[max_com] == ai:
        comfort[distinct_a[max_com]] += 1
    else:
        comfort[distinct_a[max_com]] -= 1
        comfort[ai] += 2
        if comfort[distinct_a[max_com]] == 0:
            max_com += 1

print(sum_com)
import math
n = int(input())
a = list(map(int,input().split()))
b = sorted(a,reverse=True)

x = math.ceil(len(b)/2)
y = b[:x]
z = sum(y*2)-y[0]
if len(b)%2==1:
    print(z-y[-1])
else:
    print(z)
n=int(input())
l=list(map(int,input().split()))
l.sort()
s=0
c=0
s+=l[-1]
t=0
for i in range(n-2,n//2-1,-1):
    s+=l[i]*2
    #print(l[i]*2)
if (n%2):
    s-=l[n//2]
    #print(l[n//2])
print(s)
from collections import deque 
n = int(input())
a = list(map(int,input().split()))

a.sort()
fir = a[n-1]

sco = 0
sit = deque([fir])
for i in range(n-1):
    sco += sit.popleft()
    sit.append(a[n-i-2])
    sit.append(a[n-i-2])
print(sco)
import copy
n = int(input())
a = list(map(int,input().split()))
a.sort(reverse=True)
count = 0
for i in range(len(a)-1):
    count += a[(i+1)//2]
print(count)
def main():
    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)
if __name__ == '__main__':
    main()
from collections import deque

n = int(input())
a = [int(_) for _ in input().split()]

aa = deque(sorted(a, reverse=True))
ans = aa.popleft()
for i in range(n-2):
    if i % 2 == 0:
        comf = aa.popleft()
    ans += comf
    #print(ans)
print(ans)
N = int(input())
a = list(map(int,input().split()))


a_sort = sorted(a, reverse=True)

a_sum = a_sort[0]

if (N-2) % 2 == 0:
    for i in range(int((N-2)/2)):
        a_sum += 2*a_sort[i+1]
    print(a_sum)
else:
    for i in range((N-2)//2):
        a_sum += 2*a_sort[i+1]
    print(a_sum+a_sort[(N-2)//2 + 1])
import sys
input = sys.stdin.readline

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

'''
a=[]
b=[]
for i in range():
    A, B = map(int, input().split())
    a.append(A)   
    b.append(B)'''


n=int(input())
a=list(map(int,input().split()))
a.sort()
#print(a)
ans=0
from math import floor
for i in range(1,n):
    ans+=a[n-1-floor(i/2)]
    #print(ans)
print(ans)
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))
A = np.sort(A)[::-1]

div, mod = divmod(N, 2)

print(2 * np.sum(A[:div]) + (A[div] if mod==1 else 0) - A[0])
n = int(input())
A = sorted(map(int, input().split()), 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)
n=int(input())
a=list(map(int,input().split()))
a.sort()
a.reverse()
ans=0
for i in range(1,n):
  ans+=a[i//2]
print(ans)
n = int(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)
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])

B.sort(reverse=True)

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

if N == 2:
    print(max(A))
    exit(0)

A.sort(reverse=True)

ans = A[0]
cur = 1
for ix in range(len(A[2:])):
    ans += A[cur]
    if ix & 1:
        cur += 1

print(ans)
import sys
sys.setrecursionlimit(10**7)

readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))

n = readint()
a = readints()

a.sort(reverse=1)

if n%2==0:
    print(a[0]+sum(a[1:n//2])*2)
else:
    print(a[0]+sum(a[1:n//2])*2+a[n//2])
N = int(input())
A = list(map(int,input().split()))
A.sort(reverse=True)
i = 1
ans = A[0]
N -= 2
while N>0:
    if N>1:
        ans += 2*A[i]
        N -= 2
        i += 1
    else:
        ans += A[i]
        break
print(ans)
# Contest No.: ABC173
# Problem No.: D
# Solver:      JEMINI
# Date:        20200705

import sys
import heapq

def main():
    n = int(input())    
    nums = list(map(int, sys.stdin.readline().split()))

    nums.sort()
    ans = 0

    # initial
    temp = nums.pop()
    ans += temp
    n -= 2

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

    if n % 2:
        ans += nums[-(n // 2) - 1]
    
    
    print(ans)

    return

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

a.sort(reverse=True)

ans = a[0]

mid = int(len(a) / 2)
for i in range(1, mid):
    ans = ans + a[i] * 2
if len(a) % 2 == 1:
    ans = ans + a[mid]

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

s = sorted(a, reverse=True)
print(sum(s[i//2] for i in range(1, n)))
n = int(input())
a = list(map(int, input().split()))

a.sort(reverse=True)
ans = a[0]
cnt = n - 2
i = 1
while cnt:
    use = min(2, cnt)
    ans += a[i] * use
    cnt -= use
    i += 1

print(ans)
n = input()
friendliness = [int(item) for item in input().split()]
friendliness.sort()
def of_ans():
    sum_ = 0
    idx = 1
    while idx < len(friendliness):
        sum_ += friendliness[len(friendliness) - (idx // 2) - 1]
        idx += 1
    return sum_
print(of_ans())
from collections import deque

n = int(input())
a = [int(e) for e in input().split()]

a.sort()

total = 0
q = deque()
q.append(a[n -1])
for i in reversed(range(n - 1)):
    q.append(a[i])
    q.append(a[i])
    total += q.popleft()

print(total)
import math
N = int (input())
x = list(map(int, input().split()))
sum = 0
red = sorted(x)[::-1]
count = math.floor(N/2)
for i in range(count):
    sum += red[i] * 2
sum -= red[0]
desire = N - count*2
sum += desire * red[count]
print(sum)
from collections import deque
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
ans=0
q=deque()
q.append(a[0])
for i in range(1,n):
    x=q.popleft()
    ans+=x
    q.append(a[i])
    q.append(a[i])
print(ans)
import heapq
N = 4
ARR = [2, 2, 1, 3]

N = 7
ARR = [1, 1, 1, 1, 1, 1, 1]

N = int(input())

ARR = list(map(int,input().split()))
def calculate(n, arr):
    result = []

    arr = sorted(arr,reverse=True)

    heapq.heappush(result,-1 * arr[0])

    ans = 0
    for i in range(1,n):
        ans -= heapq.heappop(result)

        heapq.heappush(result,-1 * arr[i])
        heapq.heappush(result, -1 * arr[i])

    print(ans)

calculate(N, ARR)
n = int(input()) - 1
a = list(map(int, input().split()))
a.sort(reverse = True)
if n % 2 == 1:
    t = n//2 + 1
    ans = sum(a[:t]) * 2 - a[0]
else:
    t = n // 2
    ans = sum(a[:t]) * 2 - a[0] + a[t]
print(ans)
N = int(input())
A = list(map(int, input().split()))
A.sort()
A.reverse()
if (N-1) % 2 == 0:
    ans = sum(A[1:(N-1)//2]) * 2 + A[0] + A[(N-1) // 2]
else:
    ans = A[0] + sum(A[1:N // 2 ]) * 2
print(ans)
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)

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

print(conf_max)
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
if N == 2:
    print(A[0])
    exit()
ans = 0
second_half = N // 2
first_half = N - second_half
for i in range(first_half - 1):
    ans += A[i]
if N % 2 == 1:
    for i in range(second_half):
        ans += A[i + 1]
else:
    for i in range(second_half - 1):
        ans += A[i + 1]
    ans += A[second_half - 1]
print(ans)
from collections import deque
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
dis=deque([A[0]])
num=0
for i in range(1,len(A)):
    num+=dis.popleft()
    dis.append(A[i])
    dis.append(A[i])
print(num)
N = int(input())
A = list(map(int,input().split()))
A.sort()
k = N - 2
a = A.pop()
ans = a
n = k // 2
for i in range(n):
    a = A.pop()
    ans += 2*a
if k - 2*n == 1:
    ans += A[-1]

print(ans)
#!/usr/bin/python
# -*- coding: utf-8 -*-

import numpy as np

def int_mtx(N):
    x = []
    for _ in range(N):
        x.append(list(map(int,input().split())))
    return np.array(x)

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

list.sort(A, reverse=True)

ans = A[0]

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

print(ans)
n,a=open(0)
print(sum(sorted(map(int,a.split()*2))[-int(n):-1]))
"""
N= 4
K = [2, 2, 1, 3]
"""

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





K = sorted(K,reverse=True)
"""
#print(K)
before_data = 0
before_data_before = 0
ans = 0 
for i in K :
    ans += before_data
    before_data_before = before_data
    before_data = i
"""
ans = 0 
for i,l in enumerate(K) :
    if (i == 0 ):
        ans += 0
    
    else :
        ans += K[i // 2]

    


print(ans)
import heapq

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

first = A.pop()
Q = [(-first,first,first)]

ans = 0
for i in range(N-1):
    a = A.pop()
    b,c,d = heapq.heappop(Q)
    ans -= b
    heapq.heappush(Q,(-a,c,a))
    heapq.heappush(Q,(-a,d,a))

print(ans)
n = int(input())
a = sorted(list(map(int,input().split())),reverse=True)
ans = 0
for i in range(1,n):
    if(i<4):
        ans += a[i-1]
    elif(i<6):
        ans += a[i-3]
    else:
        ans += a[i//2]
print(ans)
from math import ceil

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

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

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

A.sort(reverse=True)

score = [A[0]]
for i in range(1, N):
    for j in range(2):
        score.append(A[i])

    if len(score) >= N:
        break

print(sum(score[:N-1]))
N = int(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)
from collections import deque

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

A.sort(reverse=True)

dp=deque([A[1]])
point=A[0]
cnt=0
for n in range(2,len(A)):
    if cnt==0:
        p=dp.popleft()
        cnt+=1
        dp.append(A[n])
        point+=p
    else:
        cnt=0
        dp.append(A[n])
        point+=p

print(point)
N = int(input())

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

A.sort()
A.reverse()

ans = 0

if len(A) == 2:
  print(A[0])
  exit()
count = 0
for i in range(len(A)):
  if i == 0:
    ans += A[i]
    count += 1
  else:
    ans += A[i]
    count += 1
    if count == len(A)-1:
      break
    count += 1
    ans += A[i]
    if count == len(A)-1:
      break

print(ans)
from math import ceil
n=int(input())
lst=list(map(int, input().split()))
cut=ceil(n/2)
lst=sorted(lst,reverse=True)
arr=lst[:cut]
f=arr[0]
if n%2==0:
    for i in range(1,len(arr)):
        f+=2*arr[i]
else:
    for i in range(1,len(arr)):
        if i==len(arr)-1:
            f+=arr[i]
        else:
            f+=2*arr[i]
print(f)
N = int(input())
A = list(map(int, input().split()))

A.sort()
A = A[::-1]

ans = A[0]
if N%2 ==0:
    for i in range((N-2)//2):
        for j in range(2):
            ans += A[i+1]
else:
    for i in range((N-3)//2):
        for j in range(2):
            ans += A[i+1]
    ans += A[(N-3)//2+1]
print(ans)
N = int(input())
A = [int(x) for x in input().split()]
A.sort(reverse=True)

ans = A[0]
N -= 2

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

if N & 1:
    ans += A[N // 2 + 1]

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

a.sort(reverse=True)

score = 0

m = int(n / 2)

for i in range(m):
  score += a[i] * 2

if n % 2 == 1:
  score += a[m]
  
score -= a[0]

print(score)
n = int(input())
a = [int(x) for x 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)
ans=a[0]
n-=2
for i in a[1:]:
  if n == 0:
    break
  if n == 1:
    ans += i
    break
  ans += i*2
  n -= 2
print(ans)
n = int(input())
A = list(map(int, input().split()))
if n == 2:
  print(max(A))
else:
  A.sort(reverse=True)
  ans = A[0]
  for a in A[1:n//2]:
    ans += 2 * a
  if n % 2 == 1:
    ans += A[n//2]
  print(ans)
n = int(input())
arr = list(map(int,input().split()))
arr.sort(reverse=True)

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

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-math.floor((i+1)/2)-1]
print(ans)
import queue

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

edge_que = queue.Queue()
edge_que.put(A_list[0])
score = 0
for i, a in enumerate(A_list[1:]):
    score += edge_que.get()
    edge_que.put(a)
    edge_que.put(a)
print(score)
N = int(input())
a = list(map(int, input().split()))

a.sort()

arrival = 1
ans = 0

while arrival < N:
    score = a.pop()
    if arrival == 1:
        ans += score
        arrival += 1
    else:
        ans += score*min(2, N - arrival)
        arrival += 2

print(ans)
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
n = int(readline())
a = list(map(int,readline().split()))
a.sort(reverse=True)

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

import heapq
a.sort(reverse=True)
q = []
ans = 0
heapq.heappush(q, -a[0])
for i in range(1, n):
    tmp = heapq.heappop(q)
    tmp *= -1
    ans += tmp
    heapq.heappush(q, -a[i])
    heapq.heappush(q, -a[i])
print(ans)
import sys

def resolve():
    inp = sys.stdin.readlines()
    inp = list(map(lambda x: x.strip(), inp))
    N = int(inp[0])
    A = list(map(int, inp[1].split(' ')))
    A.sort(reverse=True)
    num = 0
    for i in range(N-1):
        num += A[(i+1)//2]

    print(num)
    
resolve()
N = int(input())
alist = list(map(int, input().split()))
alist.sort(reverse=True)
sumscore=alist[0]
for n in range(1,int(N/2)):
  sumscore = sumscore + alist[n]*2
if N%2==1:
  sumscore += alist[n+1]

print(sumscore)
n=int(input())
a=sorted([int(i) for i in input().split()],reverse=True)
ans=0
for i in range(1,n):
  ans+=a[i//2]
  if i==n-1:
    print(ans)
    exit()
print(ans)
n, *A = map(int, open(0).read().split())
A.sort(reverse=1)
if n % 2 == 0:
    print(sum(A[:n//2]) * 2 - A[0])
else:
    t = (n+1)//2
    print(sum(A[:t]) * 2 - A[0] - A[t-1])
N=int(input())
A=list(map(int,input().split()))
A.sort(reverse=True)
ans=A[0]
if N%2==1:
  n=int((N-1)/2)
  ans+=A[n]
  for i in range(1,n):
    ans+=2*A[i]
elif N==2:
  ans+=0
elif N%2==0 and N>2:
  n=int((N-2)/2)
  for i in range(1,n+1):
    ans+=2*A[i]
print(ans)
n = int(input())
list =list(map(int, input().split()))

list.sort(reverse=True)

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

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

print(ans)
conf = 0
num_char = int(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)
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
i = 0
ans = sum([A[i] for i in range(N//2)])*2
if N%2 == 1:
    ans += A[N//2]
ans -= A[0]
print(ans)
n = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
result = 0
count = 1
while count != n:
    result += A[count // 2]
    count += 1

print(result)
from collections import deque
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)

sm=a[0]
i=1
c=n-2
while c>0:
  if c>=2:
    sm+=2*a[i]
    c-=2
  elif c==1:
    sm+=a[i]
    break
  else:
    break
 
  i+=1
print(sm)
#!/usr/bin/python3
import sys
input = lambda: sys.stdin.readline().strip()
n = int(input())
a = sorted((int(x) for x in input().split()), reverse=True)
print(sum(a[1 + i // 2] for i in range(n - 2)) + a[0])
n = int(input())
l = list(map(int, input().split()))
l = sorted(l, reverse=True)

p = [0, l[0]]
for i in l[1:]:
    p.append(i)
    p.append(i)
    
print(sum(p[:n]))
N = int(input())
A = list(map(int, input().split()))

A.sort(reverse=True)

Sum = 0

for i in range(N-1):
    if i == 0:
        Sum += A[i]
    else:
        Sum += A[(i+1)//2]
        #print(A[(i+1)//2])

print(Sum)
"""
Atcoder Beginner Contest 173
D - Chat in a circle

""" 
n = int(input())
a = list(map(int,input().strip().split()))
a.sort(reverse=True)
ans = a[0]

i=0
for t in range(n-2):
    i+= 1 if t%2==0 else 0
    ans+=a[i]

print(ans)
from collections import deque
scores = deque()

result = 0

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

scores.append(l[0])

for i in range(1, N):
    scores.append(l[i])
    scores.append(l[i])
    result += scores.popleft()

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

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

print(ans)
n,*l=map(int,open(0).read().split());print(sum(sorted(l*2)[-2:~n:-1]))
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)

s = 0
for i in range(1, n):
    s += a[int(i/2)]
    
print(s)
n = int(input())
a = list(map(int,input().split()))
b = sorted(a,reverse=True)

ans = 0

for i in range(1,n):
    ans += b[i // 2]
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 itertools
import sys
input = sys.stdin.readline

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)
from heapq import *
n = int(input())
A = [*map(lambda x: -int(x), 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)
N = int(input())
A = sorted(map(int, input().split()), reverse=True)
ans = sum(A[:N//2]) + sum(A[1:-(-N//2)])
print(ans)
n = int(input())

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

A.sort(reverse = True)

tot = A[0]
cnt = 2
j = 0
for i in range(1,n):
    if n >= cnt+2:
        tot +=A[i]*2
        cnt += 2
        j = i
        
if n >= cnt + 1:
    tot += A[j+1]
    cnt += 1
        
print(tot)
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

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

a.sort(reverse=True)
ans = 0
t = n - 1
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)
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse = True)
ans = a[0]
if n % 2 == 0:
    ans += 2 * sum(a[1 : n // 2])
else:
    ans += 2 * sum(a[1 : (n - 1) // 2])
    ans += a[(n - 1) // 2]
print(ans)
from heapq import *
N=int(input())
A=list(map(int,input().split()))
A.sort(reverse=True)
que=[]
heapify(que)
ans=0
heappush(que,-A[0])
for i in range(1,N):
    p=-heappop(que)
    ans+=p
    heappush(que,-A[i])
    heappush(que,-A[i])
print(ans)
import heapq
n = int(input())

arr = list(map(int, input().split()))
arr = sorted(arr, reverse=True)
ans = 0
q = []
heapq.heappush(q, -arr[0])
for i in range(1, n):
    tmp = heapq.heappop(q)
    tmp *= -1
    ans += tmp
    heapq.heappush(q, -arr[i])
    heapq.heappush(q, -arr[i])
print(ans)
import sys
readline = sys.stdin.readline
from collections import deque

def solve():
    N = int(readline())
    A = list(map(int, readline().split()))

    A.sort(reverse=True)

    q = deque()

    q.append(A[0])

    ans = 0
    for i in range(1, len(A)):
        v = q.popleft()
        ans += v
        q.append(A[i])
        q.append(A[i])

    print(ans)

solve()
import numpy as np
n=int(input())
A=np.array(input().split(),np.int64)
A.sort()
A=A[::-1]
ans=int(0)
for i in range(n-1):
    ans+=A[(i+1)//2]
print(ans)
N = int(input())
A = list(map(lambda x: int(x), input().split(' ')))
A.sort(reverse=True)
score = 0
for i in range(N - 1):
  score += A[(i + 1) // 2]
print(score)
n = int(input())
a = list(map(int, input().split()))
list.sort(a, reverse=True)

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