peterverebics/CodeNet_Python_118 huggingface dataset:
n = int(input())
a = [int(i) for i in input().split()]
a.sort(reverse=True)

ans = a[0]
j = 1
cnt = 0
for i in range(n - 2):
    ans += a[j]
    
    if cnt == 1:
        cnt = 0
        j += 1
    else:
        cnt += 1
        
print(ans)
n = int(input())
a = sorted(list(map(int, input().split())))[::-1]
ans = 0
for i in range(n):
  if i == 0:
    pass
  else:
    ans += a[i//2]
print(ans)
n=int(input())
*a,=map(int, input().split())
a.sort()
ans=a[-1]
for i in range(2,n//2+1):
    ans+=a[-i]*2
if n%2:
    ans+=a[n//2]
print(ans)
import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

N = int(input())

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

ans = 0
cur = 0
for i in range(N - 1):
    ans += A[cur]
    if i < 1:
        cur += 1
    elif i % 2 == 0:
        cur += 1

print(ans)
def main():
    n = int(input())
    al = list(map(int, input().split()))
    al.sort(reverse=True)
    ans = 0

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

    ans -= al[0]
    print(ans)


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

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

A = deque(A)
L = deque()

# print(A, L)
ans = A.popleft()
L.append(A.popleft())
F = True
for i in range(2, N):
    if F:
        ans += L[0]
        F = not F
    else:
        ans += L.popleft()
        L.append(A.popleft())
        F = not F
print(ans)
#D
n = int(input())
a = list(map(int,input().split()))
a.sort(reverse=True)
ans = [a[0]]
for i in range(1, n - n // 2 + 1):
    ans.append(a[i])
    ans.append(a[i])
print(sum(ans[:n - 1]))
N = int(input())
A = list(map(int,input().split()))
A = sorted(A, reverse = True)
ans = A[0]
A.remove(A[0])
B = []
for i in range(len(A)):
    B.append(A[i])
    B.append(A[i])

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

print(ans)
n=int(input())
a=sorted(list(map(int,input().split())),reverse=1)
b=0
for i in range(n-1):
    b+=a[(i+1)//2]
print(b)
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)
def main():
    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)    
    
if __name__ == '__main__':
    main()
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()

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

ans = 0
for i in range(1, N):
	ans += A[(N-1) - (i // 2)]
	
print(ans)
import sys
readline = sys.stdin.buffer.readline
read = sys.stdin.buffer.read


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

M = -(-N // 2)
ans = A[0]
for m in range(1, M):
    ans += A[m] * 2

if N % 2 != 0:
    ans -= A[M - 1]

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

if __name__ == "__main__":
    main()
n = int(input())

A = [int(x) for x in input().split()] #yoko

A.sort(reverse=True)
#print(A)
#A1 = A[1:n-1]
ans = A[0]

count = 1
guuki = 0
i = 1

while count < n-1:
    ans += A[i]

    guuki += 1
    count += 1
    if guuki != 1:
        guuki = 0
        i += 1




print(ans)
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
ans = 0
for x in range(1, N):
    if x % 2 != 0:
        ans += A[x//2]
    else:
        ans += A[x//2]
print(ans)
n = int(input())
arr = sorted(list(map(int,input().split())),reverse = True)
vals = [arr[0]]
import math
for i in range(1,math.ceil(n//2) + 1):
  vals.append(arr[i])
  vals.append(arr[i])
print(sum(vals[:n-1]))
N=int(input())
A=list(map(int,input().split()))
A=sorted(A,reverse=True)
ans=0
for i in range((N//2)+1):
  for j in range(2):
    ans+=A[i]

if N%2==1:
  ans-=A[i]
else:
  ans=ans-(A[i]*2)

if N//2!=0:
  ans-=A[0]

print(max(ans,0))
import heapq
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
ans = 0
tmp = []
heapq.heappush(tmp, -a[0])
for i in range(1, n):
    point = -heapq.heappop(tmp)
    ans += point
    heapq.heappush(tmp, -a[i])
    heapq.heappush(tmp, -a[i])
print(ans)
N = int(input())
A = 2*list(map(int, input().split()))
A.sort()
print(sum(A[N:-1]))
l = int(input())

a = map(int, input().split())
a = sorted(a, reverse=True)
#print(a)
if l%2==0:
	print(a[0] + sum(a[1:l//2])*2)
else:
	print(a[0] + sum(a[1:(l-1)//2])*2 + a[(l-1)//2])
N,*As=map(int,open(0).read().split());As.sort();print(As[-1]+sum(As[-2-i//2]for i in range(N-2)))
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)

sum = A[0]
index = 1

for i in range(1,N-1):
	sum += A[index]
	if i %2 == 0:
		index += 1

print(sum)
n=int(input())
a=list(map(int, input().split()))
a.sort(reverse=True)
ans=0
cnt=0
i=1
while cnt<n-1:
    ans+=a[(cnt+1)//2]
    cnt+=1
print(ans)
n = int(input())
a = list(map(int, input().split()))




s = a*2
s.sort()
s.pop()
s.reverse()
ans = 0
for i in range(n-1):
    ans += s[i]

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

A.sort()
A.reverse()
ans = 0
ans += A[0]

if len(A) % 2 == 0:
  tmp = 0
  for i in range(1, int((N-2)/2)+1):
    tmp += A[i] * 2
else:
  tmp = 0
  for i in range(1, int((N-1)/2)+1):
    tmp += A[i] * 2
  tmp -= A[int((N-1)/2)]

ans += tmp
print(ans)
n = int(input())
l = list(map(int, input().split()))
cnt = 2
l.sort(reverse = True)
ans = l[0]
for i in l[1: ]:
    if cnt == n: break
    ans += i
    cnt -= -1
    if cnt == n: break
    ans += i
    cnt -= -1
print(ans)
N = int(input())
A = list(map(int, input().split()))
A = A + A
A.sort(reverse=True)
print(sum(A[1:N]))
#! /usr/bin/env python3

import sys
import numpy as np
int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)

N, *A = map(int, read().split())

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

idx = 1
ans = A[0]
flag = False
for i in range(N - 2):
    ans += A[idx]
    if not flag:
        flag = True
        continue
    if flag:
        flag = False
        idx += 1
print(ans)
n = int(input())
an = list(map(int, input().split()))

an.sort(reverse=True)

ans = an[0]

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

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

print(ans)
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

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

a.sort(reverse=True)

from collections import deque
deq = deque([a[1], a[1]])

ans = a[0]

for i in range(2,n):
    ans += deq.popleft()
    deq.append(a[i])
    deq.append(a[i])

print(ans)
import itertools
import functools
import math
from collections import Counter
from itertools import combinations
import re


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)
#import numpy as np
#import math
#from decimal import *
#from numba import njit

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

main()
def readinput():
    n=int(input())
    a=list(map(int,input().split()))
    return n,a

def main(n,a):
    #print(a)
    a.sort(reverse=True)
    #print(a)
    sum=a[0]
    j=0
    k=1
    for i in range(2,n):
        sum+=a[k]
        #print(a[k])
        j+=1
        if j==2:
            j=0
            k+=1
    return sum

if __name__=='__main__':
    n,a=readinput()
    ans=main(n,a)
    print(ans)
N = int(input())
A = list(map(int, input().split()))

A.sort(reverse= True)

ans = 0
ind = 0
for i in range(N-1):
    if i == 0:
        ans += A[ind]
        ind += 1
    elif i % 2 == 1:
        ans += A[ind]
    else:
        ans += A[ind]
        ind += 1

print(ans)
n = int(input())
arr = [int(x) for x in input().split()]
arr = sorted(arr)
arr.reverse()
d = []
d.append(1)
current = 0
s = 0
for i in range(1,len(arr)):
    d[current] -= 1
    s += arr[current]
    if d[current] == 0:
        current += 1
    d.append(2)
print(s)
N = int(input())
A = [int(i) for i in input().split()]
A.sort()
S = -A[-1]

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

print(ans)
import heapq

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

ans = 0
ans += a[0]

h = []
heapq.heapify(h)
heapq.heappush(h, -a[1])
heapq.heappush(h, -a[1])

for i in range(2, n):
    b = heapq.heappop(h)
    ans += (-b)
    heapq.heappush(h, -a[i])
    heapq.heappush(h, -a[i])

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

A.sort(reverse=True)

ans = A[0]

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

print(ans)
n=int(input())
A=list(map(int,input().split()))
A.sort(reverse = True)
total=0
for i in range(n-1):
    total+=A[(i+1)//2]
print(total)
n = int(input())
a = list(map(int, input().split()))
ans = max(a)
a.sort(reverse = True)
index = 1
for i in range(n-2):
    ans += a[index]
    if i % 2 == 1:
        index += 1
print(ans)
n = int(input())
a = list(map(int,input().split()))
a.sort(reverse=True)
c = []
for i in range(n):
    c.append(i)
    c.append(i)
ans = 0
for i in range(n):
    ans += a[c[i]]

print(ans-a[0])
N=int(input())
A=list(map(int,input().split()))
A.sort()
A.reverse()
count=0
for i in range(1,N//2):
    count+=A[i]
if N%2==1:
    print(A[0]+count*2+A[N//2])
else:
    print(A[0]+count*2)
# coding: utf-8

def solve(*args: str) -> str:
    n = int(args[0])
    A = sorted(map(int, args[1].split()), reverse=True)

    return str(sum([A[i//2] for i in range(1, n)]))


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))
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)
def main():
    N = int(input())
    A = list(map(int, input().split()))

    # 大きい順
    A.sort()
    A = A[::-1]

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

    print(ans)

if __name__ == '__main__':
    main()
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
if n==2:
    exit(print(max(a)))
b=[a[0],a[1]]
ans=a[0]
a=a[1:]
from math import floor
for i in range(n-2):
    num=floor(i/2)
    ans+=a[num]
print(ans)
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int,input().split()))

A.sort(reverse=True)

M = (N-1)//2
ans = 0
ans += 2*sum(A[1:M+1])
ans += A[0]
if N % 2 == 1:
    ans -= A[M]
print(ans)
from heapq import heappush, heappop,heapify
n = int(input())
a = list(map(int,input().split()))
a.sort(reverse=True)
x = []
s = 0
s += a[0]
heappush(x,-a[1])
heappush(x,-a[1])
for i in range(2,n):
  y = heappop(x)
  s -= y
  heappush(x,-a[i])
  heappush(x,-a[i])
print(s)
N = int(input())
A = [int(x) for x in input().split()]
A.sort(reverse=True)
if N == 2:
    print(A[0])
else:
    tocome = N-2
    curval = A[0]
    i = 1
    while tocome > 1:
        curval += A[i]*2
        tocome -= 2
        i += 1
    if tocome == 1:
        curval += A[i]
    print(curval)
N = int(input())
A = list(map(int, input().split()))
sort_A = sorted(A, reverse=True)
Ans = 0

loop = N // 2
for i in range(loop):
  Ans = Ans + (sort_A[i] * 2) 

Ans = Ans - sort_A[0]

if N % 2 == 1:
  Ans = Ans + sort_A[(N-1) // 2]

print(Ans)
n = int(input())
a = list(map(int, input().split()))
a = list(reversed(sorted(a)))

result = a[0]
n -= 2

i = 1

while n > 0:
	result += a[i]
	n -= 1
	if n > 0:
		result += a[i]
		n -= 1

	i += 1

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

n, *aaa = map(int, sys.stdin.buffer.read().split())
aaa.sort(reverse=True)
ans = 0
for i in range(n - 1):
    ans += aaa[(i + 1) // 2]

print(ans)
import numpy as np
import math


def _run(N, As):
    As.sort(reverse=True)
    sum_value = 0
    for i in range(1, N):
        index = math.ceil((i - 1) / 2)
        sum_value += As[index]
    print(sum_value)
    # states = []
    # for i in range(N):
    #     max_value = 0
    #     max_index = 0
    #     for j in range(len(states)):
    #


def _main():
    N = int(input())
    As = list(map(int, input().split()))
    _run(N, As)


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


A.sort(reverse=True)
ans = A[0]
current_a = 1
cnt = 0
for i in range(n-2):
  cnt += 1
  ans += A[current_a]
  if cnt == 2:
    cnt = 0
    current_a += 1
print(ans)
import sys
def input(): return sys.stdin.readline().rstrip()
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()
N = int(input())
A = list(map(int, input().split()))

A.sort(reverse = True)

ans = A[0]
re = [A[1]]
n = 0
for num in A[2:]:
     re.append(num)
     if num < re[0]:
          n += 1
     ans += re[0]
     if n == 2:
         re.pop(0)
         n = 0

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

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

A.sort(reverse=True)
ans = 0

ans += A[0]

cnt = 1
i = 1
while cnt < N-2:
    ans += A[i] * 2
    i += 1
    cnt += 2
if cnt == N-2:
    ans += A[i]

print(ans)
n = int(input())
a = [int(i) for i in input().split()]
a.sort(reverse=True)
cnt = 0
for i in range(1, n):
  cnt += a[i // 2]
print(cnt)
def resolve():
    '''
    code here
    '''
    N = int(input())
    As = [int(item) for item in input().split()]
    As.sort()

    res = 0

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

    print(res)

if __name__ == "__main__":
    resolve()
n = int(input())
al = list(map(int, input().split()))
al.sort(reverse=True)


def ceil(a, b):
    return -(-a // b)


if n % 2 == 0:
    ans = sum(al[:n//2-1])
    top = al[:n//2]
    ans += top[-1]
    ans += sum(top[1:])
    print(ans)
else:
    ans = sum(al[:(n+1)//2-1])
    ans += sum(al[1:1+n//2])
    print(ans)
n=int(input())
arr=[int(i) for i in input().strip().split(" ")]
arr=sorted(arr)
arr=arr[::-1]
# newArr=[arr[0]]
ans=0
# for i in range(1,n//2):
#     newArr.append(arr[i])
#     newArr.append(arr[i])
for i in range(1,n):
    # print()
    ans+=arr[i//2]
print(ans)
N = int(input())
a = list(map(int,input().split()))
total=0

list.sort(a, reverse=True)

if N %2== 0:
	for i in range(int(N/2)):
		if i == 0:
			total += a[i]
		else:
			total += a[i]*2
else:
	for i in range(int((N-1)/2)):
		if i == 0:
			total += a[i]
		else:
			total += a[i]*2
	total += a[int((N-1)/2 )]
print(total)
import math
N = int(input())
A = list(map(int, input().split()))

A.sort(reverse=True)
# print(A)

ans = A[0]
for i in range(1, N-1):
    # print(math.ceil(i/2), A[math.ceil(i/2)])
    ans += A[math.ceil(i/2)]
print(ans)
import heapq
def main():
    n = int(input())
    a = list(map(int,input().split()))
    a.sort()
    heap = []
    heapq.heappush(heap,-a.pop())
    total = 0
    while a:
        score = -heapq.heappop(heap)
        total += score
        temp = -a.pop()
        heapq.heappush(heap,temp)
        heapq.heappush(heap,temp)
    print(total)



if __name__ == '__main__':
    main()
import sys
input = sys.stdin.readline

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

if N%2 == 0:
    ans = sum(A[1:(N-2)//2+1])*2
else:
    ans = sum(A[1:(N-2)//2+1])*2 + A[(N-2)//2+1]
ans += A[0]
print(ans)
from collections import deque
N = int(input())
A = list(map(int, input().split()))
#A=[1,1,1,1,1,1,1]
#N=7
A.sort(reverse=True)
dis=deque([A[0]])
num=0
for i in range(1,len(A)):
    num+=dis.popleft()
    #L.insert(ind+1,A[i])
    dis.append(A[i])
    dis.append(A[i])
print(num)
#print(L)
#print(dis)
N = int(input())
A = list(map(int,input().strip().split()))
A = sorted(A)[::-1]

if N == 1:
    print(0)
else:
    cur = N-2
    temp = max(A)
    for i in range(1,len(A)):
        if cur == 0:
            break
        temp += A[i]
        cur -= 1
        if cur == 0:
            break  
        temp += A[i]
        cur -= 1
    print(temp)
# 初期入力
import sys
input = sys.stdin.readline
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse =True)
A1 =A[1:] +A[1:]
A1.sort(reverse =True)
ans =[A[0]] +A1


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

print(ans)
import heapq

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

hp=[]

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

for a in A[1:]:
    ans+=heapq.heappop(hp)
    heapq.heappush(hp,-a)
    heapq.heappush(hp,-a)
print(-ans)
n = int(input())
a = sorted(list(map(int,input().split())))
cou = a.pop()
n1 = n-2
if n1%2==0:
  t = n1//2
  for i in range(t):
    q = a.pop()
    cou += q*2
else:
  t = n1//2
  for i in range(t):
    q = a.pop()
    cou += 2*q
  cou += a.pop()
print(cou)
from collections import deque
n=int(input())
a=list(map(int,input().split()))

a.sort(reverse=True)                    

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

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

sa = sorted(a,reverse=True)
ans = sa[0]
i = 2
for v in sa[1:]:
  if i >= n:
    break
  #print("i:{} ans:{}".format(i,ans))
  ans += v
  i += 1
  if i >= n:
    break
  #print("i:{} ans:{}".format(i,ans))
  ans += v
  i += 1

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

n = int(input())
A = list(map(int, input().split()))
A = [-a for a in A]

A.sort()

from heapq import *

ans = -A[0]

q = A[:2][:]
heapify(q)
q[0] = q[-1]

for a in A[2:]:
    ans += -q[0]
    t1 = heappop(q)
    t1 = max(t1, a)
    heappush(q, t1)
    heappush(q, a)
print(ans)
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
ans=0
for i in range(1,n):
  ans=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):
  #print(i//2, A[i//2])
  ans += A[i//2]
print(ans)
# -*- coding: utf-8 -*-

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

a_sorted = sorted(A, key=lambda x: -x)

circle = [a_sorted[0]]
ans = 0
for idx, a in enumerate(a_sorted[1:]):
    ans += circle[idx]
    circle.extend([a, a])

print(ans)
n=int(input())
a = list(map(int, input().split()))
b=sorted(a,reverse=True)
s=b[0]
t=2
if n==2:
  print(s)
else:
    for i in range(1,n):
        for _ in range(2):
            s+=b[i]
            t+=1
            if t==n:
                break
        else:
            continue
        break
    print(s)
n,ais = int(raw_input()), map(int, raw_input().split())
ais.sort(key = lambda x:-x)
n -=2
s = 0
s += ais[0]
i = 1
while(n):
	s += ais[i]
	n-=1
	if n:
		s += ais[i]
		n -=1
	i+=1

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

a.sort(reverse = True)

ans = 0
ans += a[0]
i = 1
j = 1
while i < n-1 :
    if i+2 <= n-1 :
        ans += 2*a[j]
        j += 1
        i += 2
    else :
        ans += a[j]
        j += 1
        i += 1

print(ans)
n=int(input())
l=list(map(int,input().split()))
d=[]
l.sort(reverse=True)
d.append(l[0])
for i in range(1,n):
    d.append(l[i])
    d.append(l[i])
max=0
for i in range(n-1):
    max+=d[i]
print(max)
n = int(input())
a = sorted(list(map(int,input().split())))[::-1]
ans = 0
for i in range(1,n):
  ans += a[i//2]
print(ans)
N=int(input())
a_list=list(map(int,input().split()))

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

sumn=0
if N%2==0:
    sumn+=a_list[0]
    sumn+=2*sum(a_list[1:N//2])
else:
    sumn+=a_list[0]
    sumn+=2*sum(a_list[1:N//2])
    sumn+=a_list[N//2]

print(sumn)
from math import ceil
N = int(input())
A = list(map(int,input().split()))
A.sort(reverse=True)
if N % 2 == 0: print(sum(A[:N // 2]) * 2 - A[0])
else: print(sum(A[:ceil(N / 2)]) + sum(A[:N // 2]) - A[0])
num=int(input())
s=[int(i) for i in input().split()]
s.sort(reverse=True)
array=[]
for (i,j) in enumerate(s):
    if i==0:
        array.append(j)
    else:
        array.append(j)
        array.append(j)
point=0
for i in range(num-1):
    if i==0:
        point+=array[i]
    else:
        point+=array[i]
print(point)
from collections import Counter

def m():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort(reverse=True)
    ans = A[0]
    c = Counter(A[1:])
    d = list(c.items())
    d.sort(reverse=True, key=lambda x:x[0])
    tmp = []
    for k,v in d:
        for i in range((v-1)+(v+1)):
            tmp.append(k)
    ans += sum(tmp[:N-2])
    return ans
print(m())
n = int(input())
a = list(map(int,input().split()))
a.sort(reverse = True)
comf = a[0]
counter = n-2
for i in range(1,n):
  if counter>1:
    comf += 2*a[i]
    counter -= 2
  elif counter == 1:
    comf += a[i]
    counter -= 1
  else:
    break
print(comf)
s=0
N=int(input())
L=list(map(int,input().split()))
L=sorted(L,reverse=True)
if N%2==0:
  s+=L[0]
  s+=2*sum(L[1:N//2])
else:
  s+=L[0]
  s+=2*sum(L[1:(N+1)//2])-L[((N+1)//2)-1]
print(s)
N = int(input())
A = list(map(int ,input().split()))
sA = sorted(A, reverse=True)
if N%2==0:
    print(sA[0]+2*sum(sA[1:N//2]))
else:
    print(sA[0]+2*sum(sA[1:(N-1)//2])+sA[(N-1)//2])
N = int(input())
A = list(map(int,input().split()))

A.sort(reverse=True)

from collections import deque
d = deque()
ans = 0

d.append(A[0])
for i in range(1,N):
    d.append(A[i])
    d.append(A[i])
    ans += d.popleft()

print(ans)
import math
n  =int(input())
a = [int(x) for x in input().split()]
a.sort(reverse=True)
ans = 0
i=0
ind = 0
while (i<n):
    if n-i == 1:
        ans+=a[ind]
        i+=1
    else:
        ans+=2*a[ind]
        i+=2
    ind+=1
    # print(ans,i)
ans-=a[0]
print(ans)
n = int(input())
a_list = sorted(map(int, input().split()),reverse=True)

ans = 0
ans += a_list.pop(0)
cnt = 2
times_2 = 0
times_1 = 0
while 1:
  if cnt == n:
    break
  if n - cnt >= 2:
    cnt += 2
    times_2 += 1
  else:
    cnt += 1
    times_1 += 1
      
ind = 0
for i in range(times_2):
  ans += 2 * a_list[i]
  ind += 1
  
for i in range(times_1):
  ans += a_list[ind + i]

print(ans)
from collections import deque
n=int(input())
A=list(map(int,input().split()))
A.sort()
A.reverse()
conf=[A[0]]
ans=0
con=deque(conf)
A.pop(0)


for elem in A:
    ans+=con.popleft()
    
    for _ in range(2):
        con.append(elem)

print(ans)