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




import sys
input = sys.stdin.readline



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

    As = sorted(A)

    res = 0

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

    print(res)



main()

Solution (Python):
n = int(input())
l = list(map(int, input().split()))
l.sort(reverse = True)
anz = l[0]
for i in range(1,n//2):
	anz += (2*l[i])
if n&1:
	anz += l[n//2]
print(anz)

Solution (Python):

import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import*
from io import BytesIO, IOBase
def vsInput():
    sys.stdin = open('input.txt', 'r')
    sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192
class FastIO(IOBase):
    newlines = 0
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def value():return tuple(map(int,input().split()))
def array():return [int(i) for i in input().split()]
def Int():return int(input())
def Str():return input()
def arrayS():return [i for i in input().split()]




n=Int()
a=array()
a.sort(reverse=True)

series=[a[0]]
have=1
need=n-1

for i in range(1,n):
    if(need>=have+2):
        series.append(a[i]*2)
        have+=2
    elif(need>have):
        have+=1
        series.append(a[i])
    else:
        break

print(sum(series))

Solution (Python):
from queue import deque
N = int(input())
A = list(map(int, input().split()))
ans = 0

A.sort(reverse=True)
q = deque()
q.append(A[0])

for i, a in enumerate(A):
    if i != 0:
        q.append(a)
        q.append(a)

for i in range(N-1):
    ans += q.popleft()

print(ans)

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

Solution (Python):
from collections import deque
input()
A = sorted([*map(int, input().split())], reverse=True)
q = deque([A[0]])
ans = 0
for a in A[1:]:
    ans += q.popleft()
    q.append(a); q.append(a)
print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
ans=a[0]
point=1
k=1
while point<n-1:
    ans+=2*a[k]
    k+=1
    point+=2
if point>n-1:
    ans-=a[k-1]

print(ans)

Solution (Python):
n = int(input())
arr = list(map(int , input().split()))
arr.sort(reverse = True)
ptr = 1
s = arr[0]
n-=2
while n:
    s+=arr[ptr]
    n-=1
    if n == 0: break
    s+=arr[ptr]
    n-=1
    ptr+=1
print(s)

Solution (Python):
import sys;input=lambda:sys.stdin.readline()
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=1)
b=[]
for i in range(n):
  b.append(a[i])
  if i:
    b.append(a[i])
print(sum(b[:n-1]))

Solution (Python):
from collections import deque
input()
A = [*map(int, input().split())]
A.sort(reverse=True)
q = deque([A[0]])
P = []
for a in A[1:]:
    p = q.popleft()
    P.append(p)
    q.append(a); q.append(a)
print(sum(P))

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

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

a.sort(reverse = True)

ans = a[0]

r = n-2
i = 1
while r > 1:
    ans += 2*a[i]
    i += 1
    r -= 2
if r == 0:
    print(ans)
else:
    print(ans + a[i])

Solution (Python):
from collections import deque
n = int(input())
A = [*map(int, input().split())]



A.sort(reverse=True)



q = deque([A[0]])
B = []
for a in A[1:]:
    x = q.popleft()
    B.append(x)
    q.append(a)
    q.append(a)


print(sum(B))

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
for i in range(N):
    if i == 1:
        ans += A[0]
    elif i > 1:
        ans += A[i//2]
print(ans)

Solution (Python):
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

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

Solution (Python):
def solve():
    N = int(input())
    A = list(map(int, input().split()))
    A = sorted(A * 2, reverse=True)
    print(sum(A[1:N]))


if __name__ == "__main__":
    solve()

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

Solution (Python):
M = int(input())
li = list(map(int, input().split()))
li = sorted(li * 2, reverse=True)
print(sum(li[1:M]))

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

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

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

Solution (Python):
n = int(input())
arr = sorted([int(x) for x in input().split(" ")], reverse=True)
S = arr[0]
iter = 1
c = False
for i in range(n - 2):
    S += arr[iter]
    if c == False:
        c = True
        continue
    if c == True:
        iter += 1
        c = False
        continue
        
print(S)

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

ans = 0
for i, A_i in enumerate(A):
  if i == 0:
    ans += A_i
    continue
  elif 2 * i >= N:
    break
  elif 2 * i == N - 1:
    ans += A_i
    break
  else:
    ans += 2 * A_i

print(ans)

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

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

ans = 0

for i in range(N):
    if not i == 0:
        ans += B[i // 2]
        
print(ans)

Solution (Python):
import math


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

    A.sort(reverse=True)

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

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
n=int(input())
inp = list(map(int,input().split()))
inp.sort(reverse=True)
ans=inp[0]
x=len(inp)-1
flag=0
i=1
while x>1:
    if flag==0 or flag==1:
        ans+=inp[i]
        flag+=1
    if flag==2:
        flag=0
        i+=1
    x-=1
print (ans)

Solution (Python):
N = int(input())
A = sorted(list(map(int,input().split())))
s = A[N-1]
v = N - 2
for i in range(N-2,-1,-1):
    s += A[i] * min(v,2)
    v -= min(v,2)
print(s)

Solution (Python):
n,*a=map(int,open(0).read().split())
a=sorted(a)[::-1]
print(sum(a[:n//2])*2-a[0]+a[n//2]*(n%2!=0))

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

sorted=sorted(a,reverse=True)

ans=sorted[0]

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

if len(a)%2==1:
  ans+=sorted[1 + len(sorted[2:])//2]
  
print(ans)

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

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

def mi(): return map(int,input().split())
def ii(): return int(input())
def isp(): return input().split()
def deb(text): print("-------\n{}\n-------".format(text))

INF=10**20
def main():
    N=ii()
    A=list(mi())

    A.sort(reverse=True)
    B = [A[0]]
    for i in range(1,N):
        B.append(A[i])
        B.append(A[i])
    
    print(sum(B[:N-1]))


if __name__ == "__main__":
    main()

Solution (Python):













from sys import stdin
from operator import itemgetter
from collections import deque, Counter
import math
import pprint
from functools import reduce
import numpy as np
import random
import bisect
import copy



MOD = 1000000007
INF = float('inf')
alpha = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"]

def keta(kazu): 
    kazu_str = str(kazu)
    kazu_list = [int(kazu_str[i]) for i in range(0, len(kazu_str))]
    return kazu_list

def gcd(*numbers): 
    return reduce(math.gcd, numbers)

def combination(m,n): 
    
    if n > m:
        return 'すまん'
    return math.factorial(m) // (math.factorial(m-n) * math.factorial(n))

def pow_k(x,n):  
    if n == 0:
        return 1
    K = 1
    while n > 1:
        if n % 2 != 0:
            K *= x
        x *= x
        n //= 2
    return K * x

def fact(n): 
    arr = {}
    temp = n
    for i in range(2,int(n**0.5)+1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr[i] = cnt
    if temp != 1:
        arr[temp] = 1
    if arr == {}:
        arr[n] = 1
    return arr


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

main()

Solution (Python):
import os
import sys
from collections import defaultdict, Counter
from itertools import product, permutations,combinations, accumulate
from operator import itemgetter
from bisect import bisect_left,bisect
from heapq import heappop,heappush,heapify
from math import ceil, floor, sqrt, gcd
from copy import deepcopy
from functools import reduce

def main():

    n = int(input())
    lists = list(map(int, input().split()))
    lists.sort(reverse=True)
    ans = 0
    count = n-1
    for i in range(n):
        if i == 0:
            ans += lists[i]
            count -= 1
        else:
            for j in range(2):
                if count > 0:
                    ans += lists[i]
                    count -= 1
        

    print(ans)

        

if __name__ == "__main__":
    main()

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

ans = 0

t = N - 1

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

print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
A.sort()
p = 0
ans = 0
for i in range(len(A)):
    if i == 0:
        continue
    if i == 1 or i % 2 == 0:
        p = A.pop()
    ans += p
print(ans)

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

lsA.pop(0)
ans = sum(lsA[:N-1])
print(ans)

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

ans = A.pop()
n -= 2

while n > 0:
    if n >= 2:
        ans += A.pop() * 2
        n -= 2
    elif n == 1:
        ans += A.pop()
        n -= 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):

import sys

sysread = sys.stdin.readline
read = sys.stdin.read
sys.setrecursionlimit(10 ** 7)
from heapq import heappop, heappush









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

    val_list = []
    heappush(val_list, (-min(A[0], A[1]), A[1], A[0], 1))
    heappush(val_list, (-min(A[0], A[1]), A[1], A[0], 1))
    ans = A[0]

    for a in A[2:]:
        val, a1, a2, edge = heappop(val_list)
        ans += -val
        if edge != 1:
            heappush(val_list, (-a, a, a1, 0))
            heappush(val_list, (-a, a, a2, 0))
        else:
            heappush(val_list, (-a, a, a1, 1))
            heappush(val_list, (-a, a, a2, 0))
    print(ans)



if __name__ == "__main__":
    run()

Solution (Python):

from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")


def solve():
    n = II()
    a = LI()
    a.sort()
    ans = 0
    a = a[::-1]
    for i in range(1, n):
        ans += a[i // 2]
    print(ans)
    return



if __name__ == '__main__':
    solve()

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
rl = sys.stdin.readline


def solve():
    N = int(rl())
    A = list(map(int, rl().split()))
    
    A.sort(reverse=True)
    B = [A[0]]
    for ai in A[1:]:
        B += [ai, ai]
    ans = sum(B[:N - 1])
    print(ans)


if __name__ == '__main__':
    solve()

Solution (Python):
def resolve():
    import sys
    sys.setrecursionlimit(10 ** 6)  
    to_index = lambda x: int(x) - 1  
    print_list_in_2D = lambda x: print(*x, sep="\n")  

    
    def input_int(): return int(input())

    def map_int_input(): return map(int, input())

    MII = map_int_input

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

    def MII_to_index(): return map(to_index, input())

    def MII_split_to_index(): return map(to_index, input().split())

    
    def list_int_inputs(): return list(map(int, input()))

    LII = list_int_inputs

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

    
    def LII_2D(rows_number): return [LII() for _ in range(rows_number)]

    def LII_split_2D(rows_number): return [LII_split() for _ in range(rows_number)]

    from collections import deque
    N = input_int()
    friendlinesses = LII_split()

    s_fris = sorted(friendlinesses, reverse=True)

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

Solution (Python):
n=int(input())
l=sorted(list(map(int,input().split())))
l.reverse()
if(n%2==0):
    print(l[0]+2*sum(l[1:n//2]))
else:
    print(l[0]+(2*sum(l[1:(n//2)+1]))-l[n//2])

Solution (Python):


import math


def get_input() -> tuple:
    """
    標準入力を取得する.

    Returns:\n
        tuple: 標準入力
    """
    N = int(input())
    A = list(map(int, input().split()))

    return N, A


def main(N: int, A: list) -> None:
    """
    メイン処理.

    Args:\n
        N (int): プレイヤー数（2 <= N <= 2*10**5）
        A (list): フレンドリーさ
    """
    
    A = sorted(A, reverse=True)
    ans = 0
    for n in range(1, N + 1):
        if n == 1:
            continue
        ans += A[math.ceil(n / 2) - 1]

    
    print(ans)


if __name__ == "__main__":
    
    N, A = get_input()

    
    main(N, A)

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


if __name__ == '__main__':
    main()

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

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

a1 = sorted(a, reverse=True)
com = 0

if n == 2:
    com = a1[0]
elif n%2 ==0:
    c = int(n/2)
    com = sum(a1[0:c])
    com += sum(a1[1:c])
else:
    c = int(n//2)
    com += sum(a1[0:c+1])
    com += sum(a1[1:c])
    
print(com)

Solution (Python):


import math


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


A = sorted(A, reverse=True)
ans = 0
for n in range(1, N + 1):
    if n == 1:
        continue
    ans += A[math.ceil(n / 2) - 1]


print(ans)

Solution (Python):
N = int(input())
arr = sorted(list(map(int, input().split())))
ans,cnt = 0, 1
for i in range(1,N):
    ans += arr[-1]
    cnt += 1
    if cnt == 2:
        cnt = 0
        arr.pop()
print(ans)

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

a.sort(reverse=True)
con_sum = a[0]
j = 0

for i in range(1,n-1):
    if i % 2 == 1:
        j += 1        
        con_sum += a[j]
    else:
        con_sum += a[j]

print(con_sum)

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

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

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

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

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

c = sorted(c)
ans = 0

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

print(ans)

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

a.sort(reverse=True)

min_comfort = max(a)
sum_comfort = max(a)
i = 0
j = 1
counter=0
while i < n - 2:
    tmp = a[j]
    sum_comfort += tmp
    counter+=1
    if counter ==2:
        counter=0
        j+=1
    i+=1
    
print(sum_comfort)

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

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

Solution (Python):
n = int(input())
A = tuple(map(int, input().split()))
A = sorted(A, reverse=True)
from collections import deque
q = deque([])
ans = A[0]

for a in A[1:n-1]:
    q.append(a)
    q.append(a)
    ans += q.popleft()
print(ans)

Solution (Python):
N=int(input())
aalist=list(map(int,input().split()))
alist=sorted(aalist, reverse=True)
ans=alist[0]
s=0
if N%2==0:
    for i in range(1,(N-2)//2+1):
        ans+=2*alist[i]
elif N==3:
    ans+=alist[1]
else:
    for i in range(1,(N-3)//2+1):
        ans+=2*alist[i]
    ans+=alist[(N-3)//2+1]
print(ans)

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


left = N-1
index = 0
ans = 0
while left > 0:
    if index == 0:
        ans += A[index]
        left -= 1
        index += 1
    else:
        if left == 1:
            ans += A[index]
            left -= 1
            index += 1

        else:
            ans += A[index]*2
            left -= 2
            index += 1
print(ans)

Solution (Python):
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
import copy

class PriorityQueue:

    
    def __init__(self,heap):
        self.heap = heap
        heapify(self.heap)
    
    
    def push(self,item):
        heappush(self.heap,item)
    
    
    def pop(self):
        return heappop(self.heap)
    
    
    def pushpop(self,item):
        return heappushpop(self.heap,item)
    
    
    def __call__(self):
        return self.heap
    
    
    def __len__(self):
        return len(self.heap)


if __name__ == "__main__":
    n = int(input())
    a = list(map(int,input().split()))
    a.sort(reverse=True)
    heap = []
    q = PriorityQueue(heap)
    q.push(-a[0])
    ans = 0
    for i in range(n-1):
        p = q.pop()
        ans += -p
        q.push(-a[i+1])
        q.push(-a[i+1])
    print(ans)

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

A = sorted(A)
ans =0
for i in range(1,len(A)):
  ans += A[len(A) - 1 - i//2]
           
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-int(i/2)-1]
print(ans)

Solution (Python):
def d_chat_in_a_circle():
    N = int(input())
    A = sorted([int(i) for i in input().split()])
    return sum(A[(N - 1) - (k // 2)] for k in range(1, N))

print(d_chat_in_a_circle())

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

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

maxval = a[0]
cc = {maxval: 1}

ans = 0
idx = 0

for i in range(1, n):
    if a[i] in cc:
        cc[a[i]] += 2
    else:
        cc[a[i]] = 2
    cc[maxval] -= 1
    ans += maxval
    if cc[maxval] == 0:
        while 1:
            idx += 1
            if maxval > a[idx]:
                maxval = a[idx]
                break

print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product, groupby, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()
A.sort(reverse=True)
ans = 0
for i in range(N-1):
    ans += A[0--i//2]
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):
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)