Problem:
 Score: 400 points    Problem Statement  Quickly after finishing the tutorial of the online game ATChat , you have decided to visit a particular place with N-1 players who happen to be there. These N players, including you, are numbered 1 through N , and the friendliness of Player i is A_i .  The N players will arrive at the place one by one in some order. To make sure nobody gets lost, you have set the following rule: players who have already arrived there should form a circle, and a player who has just arrived there should cut into the circle somewhere.  When each player, except the first one to arrive, arrives at the place, the player gets comfort equal to the smaller of the friendliness of the clockwise adjacent player and that of the counter-clockwise adjacent player. The first player to arrive there gets the comfort of 0 .  What is the maximum total comfort the N players can get by optimally choosing the order of arrivals and the positions in the circle to cut into?      Constraints   All values in input are integers.  2 \leq N \leq 2 \times 10^5  1 \leq A_i \leq 10^9         Input  Input is given from Standard Input in the following format:  N  A_1  A_2  \dots  A_N       Output  Print the maximum total comfort the N players can get.        Sample Input 1 4
2 2 1 3      Sample Output 1 7  By arriving at the place in the order Player 4, 2, 1, 3 , and cutting into the circle as shown in the figure, they can get the total comfort of 7 .   They cannot get the total comfort greater than 7 , so the answer is 7 .       Sample Input 2 7
1 1 1 1 1 1 1      Sample Output 2 6

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

a.sort(reverse=True)

score = - a[0]
for i in range(n):
    score += a[i // 2]
print(score)

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

score = 0
score += A[0]
for i in range(1,int(N/2)):
    score += A[i]*2
if N%2 == 1:
    score += A[int(N/2)]
print(score)

Solution (Python):
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)

Solution (Python):
N = int(input())
A = 2*list(map(int, input().split()))
A.sort()
print(sum(A[N:-1]))

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
a = a + a
a.sort(reverse=True)
ans = 0
for i in range(1,n):
    ans += a[i]

print(ans)

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

SUM=0
A=sorted(A)
COUNT=N-1

if COUNT==0:
    print(SUM)
    exit()

SUM+=A[-1]
del A[-1]
COUNT-=1

if COUNT==0:
    print(SUM)
    exit()

while True:
    for i in range(2):
        SUM+=A[-1]
        COUNT-=1
        if COUNT==0:
            print(SUM)
            exit()
    del A[-1]

Solution (Python):
import numpy as np
N = int(input())
A_list = list(map(int, input().split()))
A_np = np.array(A_list)
A_sort = np.sort(A_np)[::-1]
comfort = 0
for i in range(0,N-1):
  if i == 0:
    comfort = comfort + A_sort[i]
  else:
    comfort = comfort + A_sort[int((i+1)/2)]
print(comfort)

Solution (Python):
n=int(input())
a=[int(i) for i in input().split()]
a.sort()
ans=0
ans+=max(a)
a.pop()
n-=2
while n:

    if n>=2:
        ans+=a.pop()*2
        n-=2
    else:
        ans+=a.pop()
        n-=1
print(ans)

Solution (Python):
N = int(input())

A = list(map(int, input().split()))
A.sort()
if N % 2:
    start = N//2
    end = N-1
    print(sum(A[start:end]) * 2 + A[end] - A[start])
else:
    start = N//2
    end = N-1
    print(sum(A[start:end]) * 2 + A[end])

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

a.sort(reverse=True)
ans = a[0]
cnt = 0
j = 1
for i in range(2, n):
    if cnt < 2:
        ans += a[i-j-cnt]
        cnt += 1
    if cnt == 2:
        cnt = 0
        j += 1
print(ans)

Solution (Python):
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N = I()
    A = LI()

    A.sort(reverse=True)

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

    print(ans)

if __name__ == '__main__':
    resolve()

Solution (Python):
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()

Solution (Python):

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])

Solution (Python):
import heapq

N = int(input())
An = [int(e) for e in input().split()]

def quick_sort(arr):
    left = []
    right = []
    if len(arr) <= 1:
        return arr

    ref = sum([e for e in arr]) / len(arr)

    for e in arr:
        if e < ref:
            left.append(e)
        else:
            right.append(e)

    if len(left) == 0:
        return right

    left = quick_sort(left)
    right = quick_sort(right)

    return left + right

p = quick_sort(An)
v0 = p.pop()

q = [-v0]
heapq.heapify(q)

s = 0

while p:
    v = heapq.heappop(q)
    newv = p.pop()
    heapq.heappush(q, -newv)
    heapq.heappush(q, -newv)
    s -= v

print(s)

Solution (Python):
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)

Solution (Python):
N = int(input())
A = sorted(map(int, input().split()))

value = 0

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

print(value)

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

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
print( sum( a[(i+1)//2] for i in range(n-1)) )

Solution (Python):
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)

Solution (Python):
n=int(input())
a=sorted(list(map(int,input().split())),reverse=True)

t=n-1
ans=0
for i in range(n):
    lim=2
    if(i==0):lim=1
    for j in range(lim):
        if(t>0):
            ans+=a[i]
            t-=1
print(ans)

Solution (Python):
import sys
input = sys.stdin.buffer.readline

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

    a_sort = sorted(a, reverse=True)

    ans = a_sort[0]

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

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

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):

N = int(input())
A = sorted(list(map(int, input().split())), reverse=True) 
count = 0
score = 0
i = 0
while count < N-1:
  if i == 0:
    score += A[0]
    count += 1
    i += 1
    continue
  score += A[i]
  count += 1
  if count < N-1:
    score += A[i]
    count += 1
  i += 1
print(score)

Solution (Python):
import heapq

def solve(n, a):
    a = sorted(a, reverse=True)
    ans = a[0]
    que = [(-a[1], -a[0]), (-a[1], -a[0])]
    heapq.heapify(que)
    for i in range(2, n):
        x, y = heapq.heappop(que)
        ans += min(-x, -y)
        heapq.heappush(que, (-a[i], x))
        heapq.heappush(que, (-a[i], y))
    return ans

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

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
a = a + a
a.sort(reverse=True)
a = a[1:n]
print(sum(a))

Solution (Python):
n = int(input())
A = list(map(int,input().split()))
A.sort(reverse=True)
sumn = 0
for i in range(n-1):
    sumn += A[(i+1) // 2]
print(sumn)

Solution (Python):
import sys



import bisect


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)

Solution (Python):
import sys



import bisect


import itertools
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)

Solution (Python):
import sys



import bisect


import itertools
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)

Solution (Python):
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def s(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 1
ans = 0
inf = float("inf")

n = k()
a = l()

a.sort(reverse = True)

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

print(ans)

Solution (Python):
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)

Solution (Python):
import sys

DEBUG=False
if DEBUG:
    f=open("202007_1st/D_input.txt")
else:
    f=sys.stdin
N=int(f.readline().strip())
A=list(map(int,f.readline().split()))
A.sort(reverse=True)
ans=0
for _ in range(1,N):
    ans+=A[int(_/2)]
print(ans)
f.close()

Solution (Python):
from collections import deque
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)

ans=0
d=deque([a[0]])
for i in range(1,len(a)):
  d.append(a[i])
  d.append(a[i])
  ans+=d.popleft()
print(ans)

Solution (Python):
N = int(input())
A = sorted([int(x) for x in input().split(" ")])[::-1]

N -= 2
S = A[0]

i = 1
while 1:
    if N == 0:
        break 
    if N == 1:
        S += A[i]
        break
    S += 2 * A[i]
    N -= 2
    i += 1
print(S)

Solution (Python):





















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

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

print( ans )

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

Solution (Python):
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)

Solution (Python):
n = int(input())
l = list(map(int, input().split()))
l = l + l
l = sorted(l, reverse = 1)

print(sum(l[1:n]))

Solution (Python):
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])

Solution (Python):
count = int(input())
people=input().split(" ")
people1=[]
 
for i in range(count):
    people1.append(int(people[i]))
 
people1.sort(reverse=True)


ans=0
k=1
for i in range(count-1):
    if i == 0:
        ans = people1[0]
    elif i % 2 != 0:
        ans = ans+people1[k]
    else:
        ans = ans + people1[k]
        k=k+1
    i=i+1
print(ans)

Solution (Python):
N = int(input())
A = sorted(list(map(int,input().split())),key=lambda x: -x)
import heapq
Q = [-A.pop(0)]
ans = 0
for a in A:
    tmp = -heapq.heappop(Q)
    ans += tmp
    heapq.heappush(Q,-a)
    heapq.heappush(Q,-a)
print(ans)

Solution (Python):
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])

Solution (Python):
n,*l=map(int,open(0).read().split())
l.sort(reverse=1)
print(sum(l[i//2] for i in range(1,n)))

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

Solution (Python):
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()

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
A.sort(reverse=True)
ans=0
li=[A[0]]
for i in range(1,N):
  ans+=li[i-1]
  li.append(A[i])
  li.append(A[i])
print(ans)

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

Solution (Python):
import bisect
import copy
import heapq
import sys
import itertools
import math
from functools import lru_cache
from collections import deque
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7 

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


def main():
    N = int(input())
    A = read_list()
    A.sort(reverse=True)
    
    res = 0
    for n in range(N):
        if n == 0:
            res += 0
        else:
            res += A[n // 2]

    print(res)


if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())
print(sum(sorted([int(x) for x in input().split()] * 2, reverse = True)[1:n]))

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
import sys
def input():
    return sys.stdin.readline()[:-1]


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

    
if __name__ == "__main__":
    main()

Solution (Python):

import sys, io, os, re
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from pprint import pprint
from math import sin, cos, pi, radians, sqrt, floor, ceil
from copy import copy, deepcopy
from collections import deque
from fractions import gcd
from functools import reduce
from itertools import groupby
from heapq import heapify, heappush, heappop



n1 = lambda: int(sys.stdin.readline().strip())
nn = lambda: list(map(int, sys.stdin.readline().strip().split()))
f1 = lambda: float(sys.stdin.readline().strip())
fn = lambda: list(map(float, sys.stdin.readline().strip().split()))
s1 = lambda: sys.stdin.readline().strip()
sn = lambda: list(sys.stdin.readline().strip().split())
nl = lambda n: [n1() for _ in range(n)]
fl = lambda n: [f1() for _ in range(n)]
sl = lambda n: [s1() for _ in range(n)]
nm = lambda n: [nn() for _ in range(n)]
fm = lambda n: [fn() for _ in range(n)]
sm = lambda n: [sn() for _ in range(n)]

def array1(n, d=0): return [d] * n
def array2(n, m, d=0): return [[d] * m for x in range(n)]
def array3(n, m, l, d=0): return [[[d] * l for y in xrange(m)] for x in xrange(n)]
def linc(A, d=1): return map(lambda x: x + d, A)
def ldec(A, d=1): return map(lambda x: x - d, A)

N = n1()
A = nn()

A.sort(reverse=True)

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

print(ans)

Solution (Python):
n = int(input())
friendly = list(map(int, input().split()))
friendly.sort(reverse=True)
if len(friendly)<=2:
    print(sum(friendly[0:n-1]))
else:
    ans = friendly[0]+sum(friendly[1:(n+1)//2-1])*2+friendly[(n+1)//2-1]*((n+1)%2+1)
    print(ans)

Solution (Python):
import heapq
N = int(input())
A = list(map(int, input().split()))
A = sorted(A, reverse=True)
ans = 0
q = []
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)

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

a.sort(reverse=True)
p = -(-n//2)
print(sum(a[:p])*2 - a[0] - (0 if n % 2 == 0 else a[p-1]))

Solution (Python):


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)

Solution (Python):
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
N = I()
A = LI()

A = sorted(A, reverse=True)
ans = A[0]
A = A[1:]
i = 0

while i < N-2:
	ans += A[i//2]
	i += 1

print(ans)

Solution (Python):
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)

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

Solution (Python):

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

    
    
    
        
    
        
        
    
        
        
    
    A = sorted(A, reverse = True)
    answer = 0
    for i in range(n-1):
        answer += A[(i+1)//2]
    print(answer)    

    
if __name__ == "__main__":
    main()

Solution (Python):
n = int(input())

a_list = list(map(int,input().split()))
for i in range(n):
  a_list[i] *= -1
import heapq  
heapq.heapify(a_list)
n = n-1
result = 0
target = heapq.heappop(a_list)
result += -(target)
n = n-1

while True:
  if n == 0:
    break
  target = heapq.heappop(a_list)
  result += -(target)
  n = n-1
  if n == 0:
    break
  result += -(target)
  n = n-1
  if n == 0:
    break
print(result)

Solution (Python):

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]))

Solution (Python):
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]))

Solution (Python):
N = int(input())
l = list(map(int,input().split()))
 
l.sort()
l.reverse()
 
ans = 0
ans += l[0]
 
for i in range(1,N-1):
  ans += l[(i+1)//2]
print(ans)

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

a.sort()
a.reverse()

ans = 0
ans += a[0]

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

Solution (Python):
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)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 9)

def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')

from collections import deque





def solve():
    N = II()
    A = LI()
    A = deque(sorted(A, reverse=True))

    q = deque([A.popleft()])
    
    ans = 0
    while A:
        ans += q.popleft()
        a = A.popleft()
        q.extend([a, a])
    print(ans)


if __name__ == '__main__':
    solve()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
limit = 0
ans = 0
count = 0
lim = 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)

Solution (Python):
from sys import stdin
def main():
    
    readline=stdin.readline
    n=int(readline())
    a=list(map(int,readline().split()))

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

        if cnt==2:
            cnt=0
            j+=1

    print(ans)

if __name__=="__main__":
    main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
sa = sorted(A, reverse=True)
ans = 0

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

print(ans)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
import sys
input = sys.stdin.readline


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

i = 0
ans = A[i]
count = 2

while True:	
	if count == N:
		break
	else:
		if count % 2 == 0:
			i += 1
		
		ans += A[i]
		count += 1

print(ans)

Solution (Python):
import sys

n = int(input())
list_input = list(map(int, sys.stdin.readline().split()))

list_input.sort(reverse=True)

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

print(ans)

Solution (Python):
N=int(input())
Ai=list(map(int,input().split()))


Ai.sort()

ans=0

for i in range (1,N):
    if i%2==0:
        ans+=Ai[-1]
    else:
        ans+=Ai.pop()
print(ans)

Solution (Python):
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)

Solution (Python):
import math
N=int(input())
A=list(map(int,input().split()))

A.sort()

K=math.floor(N/2)

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

if(N%2==0):
  print(S-A[N-1])
else:
  print(S-A[N-1]-A[K])

Solution (Python):
def main2():
    N = int(input())
    A = [int(a) for a in input().split()]
    A.sort(reverse=True)

    ans = 0
    count = N - 1

    for i in range(len(A)):
        lim = 0

        if i == 0:
            lim = 1
        else:
            lim = 2

        for j in range(lim):
            if count > 0:
                ans += A[i]
                count -= 1

    print(ans)

if __name__ == "__main__":
    main2()

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

Solution (Python):
n = int(input())
l = list(map(int, input().split(' ')))
l.sort()
s = l.pop()
while n > 3:
	s += l.pop() * 2
	n -= 2
if n == 3:
	s += l.pop()
print(s)

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

A.sort(reverse = True)
ans = 0
k = 0
for i in range(N-1):
    k += 1/2
    ans += A[int(k)]

print(ans)

Solution (Python):
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)

Solution (Python):
n = int(input())
que = [x*-1 for x in list(map(int,input().split()))]

que.sort()
ans = 0
que = list(que)
for i in range(1,n):
  ans -= que[i//2]

print(ans)

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

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
ans = a[0]
j = 1
for i in range(1, len(a) - 1):
    ans += a[j]
    if i % 2 == 0:
        j += 1

print(ans)

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

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

Solution (Python):
N = int(input())

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

ans = 0
cnt = 0

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

    if cnt == N-1:
        break
print(ans)

Solution (Python):
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)

Solution (Python):



def main():
    import sys
    input = sys.stdin.readline

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

    if n % 2 == 0:
        m = (n - 1) // 2
    else:
        m = (n - 2) // 2

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

    if n % 2 == 1:
        ans += a[m + 1]

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
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):
    endIndex = 0
    ans = 0

    arr = sorted(arr, reverse=True)
    for i in range(n):

        if i == 0:
            ans += arr[i]
            endIndex += 1
            continue

        endIndex += 1
        if endIndex >= n:
            break
        ans += arr[i]

        endIndex += 1
        if endIndex >= n:
            break
        ans += arr[i]

    print(ans)


calculate(N, ARR)

Solution (Python):


N = int(input())
A = list(map(int,input().split()))
A.sort(reverse=True)
import heapq
C = []

heapq.heapify(C)
ans = 0

heapq.heappush(C,-A[0])
for i in range(1,N):
    tmp = heapq.heappop(C)
    ans += -tmp
    heapq.heappush(C, -A[i])
    heapq.heappush(C, -A[i])
print(ans)

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

ans = A[0]

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

Solution (Python):
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)

Solution (Python):
N=int(input())
A=sorted(map(int,input().split()))[::-1]
from heapq import heappop,heappush
class dictmax():
    def __init__(self):
        self.d={}
        self.h=[]
    def add(self,k,v):
        if k not in self.d:
            heappush(self.h,-k)
            self.d[k]=0
        self.d[k]+=v
        if self.d[k]==0:
            del self.d[k]
    def max(self):
        while -self.h[0] not in self.d:
            heappop(self.h)
        return -self.h[0]
d=dictmax()
d.add(A[0],1)
r=0
for a in A[1:]:
    m=d.max()
    r+=m
    d.add(m,-1)
    d.add(a,2)
print(r)

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

a.sort(reverse=True)

ans=0
index=0
for i in range(len(a)-1):
    if i == 0:
        ans+=a[i]
    else:
        if i%2!=0:
            index+=1
        ans+=a[index]
print(ans)

Solution (Python):
import numpy as np

N,*A = map(int,open(0).read().split())
a = sorted(A,reverse=True)

arr = np.array(a)
bai = arr * 2
ruisekiwa = np.cumsum(bai[1:])
if N == 2:
    print(a[0])
    exit()
if N % 2 == 0:
    print(ruisekiwa[(N-1)//2 - 1] + a[0])
else:
    print(ruisekiwa[(N-1)//2 - 2] + a[(N-1)//2] + a[0])

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
ans = 0
a.sort(reverse=True)

ans += a[0]

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

if n % 2 != 0:
    ans += a[n//2]

print(ans)

Solution (Python):
from collections import deque
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
ans = 0
d = deque([A[0]])
for x in A[1:]:
  ans += d.popleft()
  d.append(x)
  d.append(x)
print(ans)

Solution (Python):
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):

    arr = sorted(arr,reverse=True)

    arr = [-1 * ar for ar in arr]


    mmk = []

    ans = 0
    for i in range(n):
        if i == 0:
            heapq.heappush(mmk,arr[i])
            continue

        ans -= heapq.heappop(mmk)

        heapq.heappush(mmk,arr[i])
        heapq.heappush(mmk,arr[i])

    print(ans)




calculate(N, ARR)

Solution (Python):


from sys import stdin
input=stdin.readline

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

print(HalfDead())

Solution (Python):
N = int(input())
a = [int(i) for i in input().split()]
a.sort(reverse = True)
if N == 3:
  print(a[0] + a[1])
ans1 = 2*sum(a[0:N//2]) - a[0]
if N%2 == 0:
  print(ans1)
if N%2 == 1 and N != 3:
  print(ans1 + a[N//2])

Solution (Python):
from collections import deque
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)

Solution (Python):
a = int(input())
b = list(map(int,input().split()))
ans = 0
c = 0
list.sort(b, reverse=True)
ans += b.pop(0)
b.extend(b)
list.sort(b, reverse=True)

ans += sum(b[0:a-2])
print(str(ans))

Solution (Python):
n = int(input())
a = map(int,input().split())
a = sorted(a)

result = 0
spots = [a.pop()]

for i in range(n-1):
	x = a.pop()
	result += spots[i]
	spots.append(x)
	spots.append(x)
	
print(result)

Solution (Python):
n = int(input())
f = list(sorted(map(int, input().split()), reverse=True))
score = 0

for i in range(1, n):
    score += f[i//2]
print(score)

Solution (Python):
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)