peterverebics/CodeNet_Python_118 huggingface dataset:
n = int(input())
lst = [int(i) for i in input().split()]

list.sort(lst)
print(lst)
lst2 = []
total = 0
for i in range(n):
  num = lst[i]
  if len(lst2) == 2:
    total += lst2[1]
    lst2.pop(1)
    lst2.append(num)
  if len(lst2) == 1:
    total += lst2[0]
    lst2.append(num)
  if not lst2:
    lst2.append(num)
  #print(total)
print(total)
n = int(input())
a = list(map(int, input().split()))
b = sorted(a, reverse=True)
ans = 0
for i in range(n):
    ans += b[(i+1)//2]
print(ans)
N=int(input())
A=list(map(int,input().split()))
A=sorted(A)
i=len(A)
i//=2
print(sum(A[-i-1:-1])*2+A[-1])
#!python3

import sys
iim = lambda: map(int, sys.stdin.readline().rstrip().split())

def resolve():
    it = map(int, sys.stdin.read().split())
    N = next(it)
    A = list(it)

    A.sort()
    if N ==2:
        print(A[-1])
        return

    print(sum(A)-A[0]-A[1]+A[-2])

if __name__ == "__main__":
    resolve()
N=int(input())
A=list(map(int,input().split()))
A=sorted(A)
i=len(A)
i//=2
print(sum(A[-i-1:-2])*2+A[-1])
N = int(input())
A = [int(i) for i in input().split()]
from collections import deque
A = sorted(A,reverse=True)
Q = deque(A[0:2])
ans = 0
ans += A[0]
for a in A[2:]:
    if Q[0] > Q[-1]:
        Q.appendleft(a)
        ans += Q[-1]
    else:
        Q.append(a)
        ans += Q[0]
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()
a = a[::-1]

if n % 2 == 0:
    n -= 1
    ans = a.pop()
    n -= 1
    ans += 2*sum(a[:n//2])
else:
    n -= 1
    ans = a.pop()
    n -= 1
    ans += 2*sum(a[:n//2]) + a[n//2]
print(ans)
import math
import random
import copy

N = int(input())
data = input().split()
man = [int(i) for i in data]
conf = []


for i in range(math.factorial(N)):
  man2 = copy.deepcopy(man)
  random.shuffle(man2)
  cnt = man2[(-1)%N]
  for j in range(2,N):
    cnt += min(man2[(j-1)%N],man2[(j+1)%N])
  conf.append(cnt)
    
MAX = max(conf)
print(MAX)
n = int(input())
a = list(map(int, input().split()))
a = list(reversed(sorted(a)))

result = a[0]
if n > 2:
	for i in range(2, n - 1):
		result += min(a[i - 1], a[i - 2])

result += a[1]

print(result)
# -*- coding: utf-8 -*-
import heapq as hq

n = int(input())
a = list(map(lambda x: 0 - int(x), input().split()))
a.sort()

rst = 0
circle = []
hq.heappush(circle, a[0])
for i in a[1:]:
    k = hq.heappop(circle)
    rst += k
    if k < i:
        hq.heappush(circle, i)
    else:
        # k == i
        hq.heappush(circle, i)
        hq.heappush(circle, k)

print(-rst)
N = int(input())
A = list(map(int, input().split()))
A = sorted(A)
print(sum(A)-A[0])
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 + 1]
else:
    ans = A[0] + sum(A[1:N // 2 ]) * 2
print(ans)
import sys
input = sys.stdin.readline
import heapq
def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort()
    ans = A[-1]
    ans_cnt = 0
    for i in range(N-1)[::-1]:
        ans += A[i]
        ans_cnt += 1
        if ans_cnt == N-2:
            break
        ans += A[i]
        ans_cnt += 1
        if ans_cnt == N-2:
            break
    print(ans)
if __name__ == "__main__":
    main()
n = int(input())
a = list(map(int,input().split()));
a.sort(reverse=True)
com = 0;
for i in range(n-1):
    com += a[i]
print(com);
import copy
from collections import deque

N = int(input())
A = list(map(int, input().split()))
A.sort(reverse = True)
d = deque()
c = 0
count = 2
ans = 0

if N == 2:
    print(A[0])
elif N == 3:
    print(A[0] + A[1])
else:
    ans = A[0] + A[1]
    for i in range(3, N-1):
        if c == 0:
            ans += A[count]
        elif c == 1:
            ans += A[count]
            count += 1
            c = 0
    ans += A[1]
    print(ans)
n = int(input())
a = [int(x) for x in input().split()]

a.sort(reverse=True)
print(sum(a[:-1]))
N = int(input())
A = map(int, input().split())

A = sorted(A)
A = A[::-1]
sumkoko = A.pop(0)
print(A)

sumkoko += sum(A[0:(N-1)//2]) * 2
if ((N-1)%2) == 1:
    sumkoko += A[(N-1)//2]

print(sumkoko)
import sys
from collections import deque
def I(): return int(sys.stdin.readline().rstrip())
def IL(): return map(int,sys.stdin.readline().rstrip().split())

def Main():
    n = I()
    a = list(IL())
    a.sort(reverse=True)
    a.pop()
    print(sum(a))
    return

if __name__=='__main__':
    Main()
from heapq import heapify, heappop, heappush
N = int(input())
A = list(map(int,input().split()))
A.sort(reverse = True)
#print(A)
ans = A[0]
Q = [(A[1],A[0])]
heapify(Q)
for i in range(2,N):
  nxt = A[i]
  s,t = heappop(Q)
  #s *= -1; t*= -1 #正に戻す
  ans += s
  heappush(Q,(nxt,s))
  heappush(Q,(nxt,t))
print(ans)
N = int(input())
A = [int(i) for i in input().split()]

A.sort()

score = 0

for i in A:
  N -= 1
  if(N==0):
    break
  score += i
  N -= 1
  if(N==0):
    break
  score += i
  
print(score)
'''
参考
https://yamakasa.net/atcoder-abc-173-d/
図
https://github.com/ymo999/AtCoder/blob/master/2020/20200705_ABC173/ABC173_D.pdf
'''
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)

ans = A[0]
idx = 1                 # 心地よさの和を算出するためのindex
for i in range(2, N):
    ans += A[idx]
    if i % 2 == 0:
        idx += 1

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

A = sorted(A, reverse=True)
print(sum(A)-A[len(A)-1])
n = int(input())
arr = [int(x) for x in input().split()]
arr.sort()
res, ans = [arr[0]], 0
for i in range(1, n - 1):
  res.append(arr[i])
  res.append(arr[i])
for i in range(n - 1):
  ans += res[i]
print(ans)
N=int(input())
A=[int(s) for s in input().split()]
A=sorted(A,reverse=True)
print(A[0]+A[1]*(N-2))
N = int(input())
A = list(map(int, input().split()))

A.sort(reverse=True)
ans = 0
tmp = 0
for i in range(N):
    if i == 0:
        continue
    elif i == 1:
        ans += A[0]
    elif i % 2 == 0:
        tmp = A[i-1]
        ans += tmp
    else:
        ans += tmp
print(ans)
from math import floor
n=int(input())
b=input().split()
a=[0 for i in range(n+1)]
for i in range(1,n+1):
  a[i]=int(b[i-1])

a[0]=10000000000

m=floor((n-1)/2)

a=sorted(a,reverse=True)

comfort=a[1]
if(n==2):
  print(comfort)
else:
  if(n%2 ==0):
    for i in range(2,m+2):
      comfort+=(2*a[i])
  else:
    for i in range(2,m+2):
      comfort+=(2*a[i])

    comfort+=a[m+2]

print(comfort)
N = int(input())

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

sorted_A = sorted(A, reverse=True)
print(sum(sorted_A[:-1]))
N = int(input())
A = list(map(int,input().split()))
A.sort(reverse=True)
ans = 0

if N % 2 == 1:
  ans += A[0]
  ans += A[N//2 + 1]
  for i in range(1,N//2):
    ans += A[i]*2
else:
  ans += A[0]
  for i in range(1,N//2):
    ans += A[i]*2
print(ans)
import subprocess
import os
import time


import sys
import math
from copy import deepcopy
from collections import deque, defaultdict
from operator import mul
from functools import reduce
sys.setrecursionlimit(20000000)

# input = sys.stdin.readline


def main():
    input()
    nums = [int(x) for x in input().split()]
    nums.sort(reverse=True)

    r = [nums.pop(0)]

    ans = 0
    while len(nums):
        n = nums.pop(0)
        i = r.index(max(r))
        ans += r[i]
        r = r[0:i] + [n] + [n] + r[i+1:-1]

    print(ans)


if __name__ == '__main__':
    main()
n=int(input())
a= list(map(int, input().split()))
a.sort()
ans=sum(a)-a[0]
print(ans)
n = int(input())
a = sorted(list(map(int, input().split())), reverse=True)

print(sum(a[:-1]))
from collections import deque

def main():
    n = int(input())
    l = list(map(int, input().split()))

    comfort = 0
    q = deque(sorted(l, reverse=True))
    while len(q) > 1:
        comfort += q[0]
        q.popleft()
    print(comfort)

if __name__ == '__main__':
    main()
def judge():
    ans = True
    return ans

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

print(b[-1] + (n-2)* b[-2])
import sys
import time
import math
from collections import deque
import heapq
import itertools
from decimal import Decimal
import bisect
from operator import itemgetter
MAX_INT = int(10e18)
MIN_INT = -MAX_INT
mod = 1000000000+7
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

N = I()
a = IL()
a.sort()
print(sum(a) - a[0])
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

n = ii()
a = li()

a.sort()

ans = sum(a)-a[0]
print(ans)
score = 0
turn = 0
n = int(input())
people = sorted(list(map(int, input().split())), reverse=True)

remain = n-1
while True:
    if remain >= 2**turn:
        score += people[turn] * (1+int(turn>0))
        remain -= 2 ** turn
        turn += 1
    else:
        score += people[turn] * remain
        break
    
print(score)
N = int(input())
Arr = list(map(int,input().split()))
Arr.sort()
for i in range(N-2,-1,-1):
    Arr[i] += Arr[i+1]
print(Arr[1])
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    N = int(input())
    A = list(map(int, input().split()))

    A.sort(reverse=1)
    d, m = divmod(N, 2)
    n = [2] * (d + m)
    n[0], n[-1] = 1, n[-1] - m
    ans = 0
    for i, k in enumerate(n):
        ans += A[i] * k
    print(ans)


main()
# -*- coding: utf-8 -*-

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

A.sort()

print(sum(A[1:]))
from heapq import *

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

r = 0
Q = [A[0]]
for v in A[1:]:
	c = heappop(Q)
	r += c
	heappush(Q, v)
	heappush(Q, v)

print(r)
N=int(input())
A=list(map(int, input().split()))
print(sum(A)-min(A))
n = input()
friendliness = [int(item) for item in input().split()]
friendliness.sort()
def of_ans():
  sum_ = 0
  for idx in range(len(friendliness)):
  	sum_ += friendliness[- (idx // 2)]
  return sum_
print(of_ans())
import heapq
n = int(input())
a_lst = list(map(int, input().split(" ")))

a_lst.sort(reverse=True)


result = 0
heap = []
heapq.heappush(heap,a_lst[0])

for i in a_lst[1:]:
	now_max = heapq.heappop(heap)
	result += now_max
	heapq.heappush(heap, i)
	heapq.heappush(heap, i)

print(result)
N = int(input())
A_i = list(map(int, input().split()))

ret = sum(A_i)-min(A_i)
forret = sorted(A_i)[1:]
print(sum(forret))
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
print(sum(A[:-1]))
n = int(input())
a = list(map(int, input().split()))
print(sum(a)-min(a))
N = int(input())
A = [int(x) for x in input().split(" ")]
sortedA = sorted(A)
result = 0
for x in sortedA[1:]:
  result += x

print(result)
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) < 1:
    heapq.heappush(pq, a[i])
    continue

  res += pq[0]
  heapq.heappush(pq, a[i])

print(res)
n = int(input())
a = list(map(int,input().split()))
a1 = sorted(a)
a1.pop(0)
print(sum(a1))
def resolve():
    n = int(input())
    a = sorted(list(map(int, input().split())))
    ans = sum(a[1:])
    print(ans)


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

print(sum(lst) - min(lst))
N=int(input())
A=map(int,input().split())
print(sum(A)-1)
N = int(input())
A = list(map(int,input().split()))
A.sort()
A.append(A[-1])
A = A[::-1]
sum = 0
for i in range(N-1):
    sum += min(A[i],A[i+1])
print(sum)
n = int(input())
a = list(map(int,input().split()))
comfort = 0

def mins(i):
  if i==len(b)-1:
    return min(b[0], b[i])
  else:
    return min(b[i], b[i+1])
              
if n==1:
  print(0)
elif n==2:
  print(a[0])
elif n>=3:
  confort = a[0] + min(a[0], a[1])
  b = [a[0], a[1], a[2]]
  for j in range(3, n):
    minlist = [mins(i) for i in range(len(b))]
    mx = max(minlist)
    confort += mx
    b.insert(b.index(mx), a[j])

  print(confort)
import sys
input = sys.stdin.readline

def li():
    return [int(x) for x in input().split()]

N = int(input())
A = li()

A.sort(reverse=True)
del A[-1]
result = sum(A)
print(result)
n = int(input())
a = list(map(int, input().split()))


a.sort(reverse = True)

ans = 0
c = 0
s = a[0]
while len(a)>=2 and s == a[0]:
	s = a.pop(0)
	ans += s

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

print(ans)
n = int(input())
a = list(map(int,input().split()))
a.sort(reverse=True)
num = 0
for i in range(1,n):
    num += a[i-1]
print(num)
def main():
    import sys
    def input(): return sys.stdin.readline().rstrip()
    n = int(input())
    a = list(map(int, input().split()))
    a.sort(reverse=True)
    if n %2== 0:
        print(a[0]+sum(a[1:n//2])*2)
    else:
        print(a[0]+sum(a[1:n//2+1])*2)



if __name__ == '__main__':
    main()
N=int(input())

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

B=sorted(A,reverse=True)

ans=sum(B[:-1])
print(ans)
n = int(input())
c = [int(x) for x in input().split()]
print(sum(c) - min(c))
import math
import sys
import os
from operator import mul

sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")

N = I()
A = LI()
ans = 0

A.sort(reverse=True)
# print(A)
half = math.ceil(len(A)/2)
# print(half)
for i in range(half):
    ans += A[i]

for i in range(1,half):
    ans += A[i]

print(ans)
from math import ceil
from collections import defaultdict as dd
import sys
input=sys.stdin.readline
#t=int(input())
#while t:
n=int(input())
l=list(map(int,input().split()))
l.sort(reverse=True)
ans=l[0]
ii=1
cou=0
for i in range(2,n):
    if(cou==2):
        ii+=1
        cou=0
    ans+=l[ii]
print(ans)
n = int(input())
A = sorted(list(map(int, input().split())))

ans = 0
if n >= len(A):
  ans = sum(A)-1
  
else:
  ans = sum(A[1:(n-1)//2+1])
  ans *= 2
  ans += A[0]
            
print(ans)
n=int(input()) 
c=list(map(int, input().split())) 

c.sort(reverse=True)
ans = 0

for i in range(1,len(c)):
  ans += c[len(c) - 1 - i//2]

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

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

total=0
for i in range(1,n):
  print(s[i//2])

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

# check
sort_A = sorted(A, reverse=True)
print(sum(sort_A[:-1]))
N=int(input())
A=list(map(int,input().split()))
A.sort(reverse=True)
print(sum(A)-A[-1])
n = int(input())
a = list(map(int, input().split()))
a = sorted(a)[::-1]
l = [0] * (n)
s = 0
e = n - 1

for i in range(n):
    if  i % 2 != 0:
        l[s] = a[i]
        s += 1
    else:
        l[e] = a[i]
        e -=1

cnt = l[0]
for i in range(1, n-1):
    cnt += l[i+1]
print(cnt)
N = int(input())
# = str(input())
# = map(int, input().split())
A = list(map(int, input().split()))
# = [int(input()) for i in range(N)]
# = [list(map(int, input().split())) for i in range(N)]

A = list(sorted(A))
del A[0]

print(sum(A))
import math
N=int(input())
A=list(map(int,input().split()))
A.sort(reverse=True)
ans=A[0]

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

if N%2==1:
    ans-=A[int((N-1)/2)-1]

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

for i in range(N-1):
    ans += A[i]
print(ans)
comfort=0
N=int(input())
mylist=list(map(int,input().split()))[:N]

mylist.sort()#O(nlogn)
mylist.reverse()#O(n)

if N>1:
    comfort=mylist[0]

for i in range(2,N):
    comfort+=min(mylist[i-1],mylist[i-2])
    
  
print(comfort)
n=int(input())
A=list(map(int,input().split()))
print(sum(A)-min(A))
N = int(input())

p = list(map(int, input().split()))
p.sort()
q = 0

for i in range(N):
	q += p[i]

print(q - p[0])
N = int(input())
A = list(map(int , input().split()))
B = sorted(A , reverse = True)
print(sum(B)-B[-1])
n = int(input())
a = list(map(int, input().split())) 

a.sort(reverse=True)

print(sum(a[:-1]))
N = int(input())
A = list(map(int, input().split()))
ans = 0
A = sorted(A, reverse = True)
for i in range(N-1):
  ans += A[i]
print(ans)
n  = int(input())
ai = list(reversed(sorted(map(int, input().split()))))

print(sum(ai[0:-1]))
a = int(input())
b = sorted(map(int,input().split()))
print(sum(b[1:]))
from collections import deque

n = int(input())
a = deque(sorted(list(map(int, input().split())), reverse=True))
count = 0
ans = 0
while n > count + 1:
    ans += a.popleft()
    count += 1
print(ans)
n = int(input())
a = list(map(int,input().split()))
print(sum(a) - min(a))
n = int(input())
a = list(map(int, input().split()))
ans = max(a)
a.sort(reverse = True)
index = 1
for i in range(n-2):
    if i % 2 == 1:
        index += 1
    ans += a[index]
print(ans)
N = int(input())
S = list(map(int,input().split()))

print(sum(S)-1)
n,*a=map(int,open(0).read().split())
a.sort()
b=a.pop(-1)
if n%2==0:
    print(sum(a[-(n-2)//2:])*2+b)
elif n==3:
    print(b+a[-1])
elif n==5:
    print(b+a[-1]*2)
elif n%2==1:
    print(sum(a[-(n-2)//2+1:])*2+b+a[-(n-2)//2])
n = int(input())
a = [int(i) for i in input().split()]
a.sort()
a = a[::-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 + 1])
n = int(input())
a = list(map(int, input().split()))
sorta = sorted(a, reverse=True)
c = sum(sorta) - sorta[-1]
print(c)
n = int(input())
arr = list(map(int,input().split()))
arr.sort()
print(sum(arr[1:]))
from sys import stdin
N = int(stdin.readline().rstrip())
A = [int(x) for x in stdin.readline().rstrip().split()]
A.sort()
print(sum(A[1:]))
N = int(input())
A = list(map(int,input().split()))
A.sort(reverse=True)
C = A[0]
for i in range(2,N):
  C = C+min(A[i-1],A[i-2])
print(C)
n = int(input())
A = list(map(int, input().split()))

# A.sort()
print(sum(A) - min(A))
n=int(input()) 
c=list(map(int, input().split())) 

c.sort()
print(sum(c)-c[0])
import itertools
import sys
input = sys.stdin.readline

N = int(input())
A = list(map(int, input().split()))
ans = 0
A.sort(reverse=True)
temp = 0
for i in range(N-1):
    temp += min(A[i-1], A[i+1])
ans = max(ans, temp)
print(ans)
n = int(input())
a = list(map(int,input().split()))
a.sort()
print(sum(a[1:]))
N = int(input())
A = sorted([int(n) for n in input().split()], reverse=True)
print(sum(A[:N-1]))
import sys; input = sys.stdin.readline
# n, m , k = map(int, input().split())
# matrix = [list(input().strip()) for _ in range(n)]

n = int(input())
lis = sorted(map(int, input().split()), reverse=True)
ans = lis[0]
i = 1
c = 1
while c<n:
	if n-1-c >= 2: ans += lis[i]*2; c += 2; i+=1
	else: break
print(ans)
n = int(input())
i = list(map(int, input().split()))

sorti = sorted(i, reverse=True)


#sorti = sorti + [sorti[0]]
sorti.insert(0, sorti[-1])

print(sorti)
    
ans = 0
for index, x in enumerate(sorti):

    if index == 0:
        continue
    if index == n:
        break
    
    if sorti[index-1] <= sorti[index+1]:
        ans += sorti[index+1]
    elif sorti[index+1] <= sorti[index-1]:
        ans += sorti[index-1]
    
    print(ans)

print(ans)
from math import floor
n=int(input())
b=input().split()
a=[0 for i in range(n+1)]
for i in range(1,n+1):
  a[i]=int(b[i-1])

a[0]=1000000000

m=floor((n-2)/2)

a=sorted(a,reverse=True)

comfort=a[1]
if(n==2):
  print(comfort)
else:
  if(n%2 ==0):
    for i in range(2,m+2):
      comfort+=(2*a[i])
  else:
    for i in range(2,m+2):
      comfort+=(2*a[i])

    comfort+=a[m+2]

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

a.sort(reverse=True)

ans=0

for i in range(1,n//2):
    ans=ans+a[i-1]
    print(ans,a[i-1])
for i in range(n//2,n-1):
    ans=ans+a[i-n//2+1]
    print(ans,a[i-n//2+1])
ans=ans+a[n//2]
print(ans)
import heapq
N = int(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)-2*heapq.heappop(heap)
    for _ in range(N-1-3):
        a = -heapq.heappop(heap)
        ans += a
print(ans)
from collections import deque

N = input()
A = sorted(list(map(int, input().split())), reverse=True)
s,d = 0,deque()

d.append(0)
for a in A:
	s += d.popleft()
	d.append(a)
	d.append(a)
print(s)
def main():
    n = int(input())
    a = list(map(int, input().split()))
    a.sort()
    ans = a[0]
    for i in range(1, n//2):
        ans += a[i]*2
    if n%2 == 1:
        ans += a[n//2]
    print(ans)

if __name__ == "__main__":
    main()
N = int(input())
A_list = list(map(int, input().split()))
A_list.sort(reverse=True)

print(sum(A_list) - A_list[-1])
n = int(input())

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

print(sum(num)-min(num))
import sys

N=int(sys.stdin.readline())

A=[int(i) for i in sys.stdin.readline().split()]

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)

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

print(ans)
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
 
print(sum(a[:n-1]))
import heapq

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

hp=[]

ans=-A[0]
heapq.heappush(hp,0)

for a in A:
    ans+=heapq.heappop(hp)
    heapq.heappush(hp,-a)
    heapq.heappush(hp,-a)
print(-ans)
n = int(input())
A = sorted(list(map(int, input().split())), reverse=True)

ans = 0
if n >= len(A):
  ans = sum(A)-1
  
else:
  ans = sum(A[1:n//2+1])
  print(ans)
  ans *= 2
  ans += A[0]
            
print(ans)
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
t=(len(a)-1)//2
ans=a[0]
if t%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)
N = int(input())
A = list(map(int, input().split()))

A.sort()
A.reverse()

happy = A[0]
circle = [A[0], A[1]]
for i in range(2, N):
    circle.insert(-1, A[i])
    happy +=  min(circle[-1], circle[-3])

print(happy)
# vim: fileencoding=utf-8


def main():
    n = int(input())
    li = list(map(int, input().split()))
    b = 0
    t = 0
    res = 0
    li.sort(reverse=True)
    for e in li[:-1]:
        if b != 0:
            res += b
            b = 0
        else:
            res += e
        if t == e:
            b = e
        t = e
    print(res)


if __name__ == "__main__":
    main()
n=int(input())
a=[0]*n
a=list(map(int,input().split()))
a=sorted(a)
ans=sum(a)-a[0]
print(ans)
N = input()
A = list(map(int, input().split()))
A.sort(reverse=True)
print(sum(A[:-1]))
#4問目
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse = True)
result = 0
print(sum(A) - A[-1])
n=int(input())
x=list(map(int,input().split()))
print(sum(x)-min(x))
# -*- coding: utf-8 -*-

N = int(input())

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

A_sort = sorted(A, reverse=True)

print(sum(A_sort[:-1]))
n = int(input())
liste = []
m = input()
liste = m.split(' ')
liste = [int(x) for x in liste]
liste.remove(min(liste))
print(sum(liste))
N = int(input())
l = list(map(int,input().split()))
l.sort()
print(sum(l[1:]))
N = int(input())
A = [int(x) for x in input().split()]

print(sum(sorted(A)[1:N]))
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse = True)

m = a.pop(0)
secBig = a.pop(0)
thirdBig = 0

answer = m

for i in range(n-2):
	answer += min(m, secBig)
	secBig, thirdBig = max(thirdBig, a[i]), min(thirdBig, a[i])

print(answer)
N = int(input())
A = list(map(int,input().split()))
A.sort(reverse=True)
C = A[0]+A[1]
for i in range(3,N):
  C = C+min(A[i-3],A[i-2])
print(C)
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>>1)<<1]
print(P)
input()
a=list(map(int,input().split()))
a.sort(reverse=True)
print(sum(a[:-1]))
N = int(input())
confort_list = list(map(int, input().split()))
confort_list.sort()
result = 0
for i in range(N-1):
    result += confort_list[i]
print(result)
n=int(input())
a=list(map(int,input().split(" ")))
b=sorted(a)

ans=sum(b)-b[0]
print(ans)
N = int(input())
A = sorted([int(a) for a in input().split()], reverse=True)
print(sum(A) - A[-1])