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
import heapq

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
printV = lambda x: print(*x, sep="\n")
printH = lambda x: print(" ".join(map(str,x)))
def IS(): return sys.stdin.readline()[:-1]
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
mo = 10**9+7

def main():
    N = II()
    A = LI()
    A.sort(reverse=True)
    ans = 0
    hq=[]
    heapq.heapify(hq)
    heapq.heappush(hq,A[0]*-1)
    for i,a in enumerate(A[1:]):
        ans+=heapq.heappop(hq)*-1
        heapq.heappush(hq,a*-1)
        heapq.heappush(hq,a*-1)
    print(ans)

    

if __name__ == '__main__':
    main()

Solution (Python):
def solve(ls, debug=0):
    n = len(ls)
    ls = sorted(ls)
    result = 0
    
    if n >= 2:
        result += ls[-1]
    
    for i in range(2, n):
        result += ls[-1 - i // 2]
    return result


def main(istr, ostr):
    n = int(istr.readline())
    ls = list(map(int, istr.readline().strip().split()))
    print(solve(ls), file=ostr)


if __name__ == "__main__":
    import sys

    main(sys.stdin, sys.stdout)

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

ans = 0

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

print(ans)

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

A = []
a, *an = map(int, input().split())
A.append(a)
A += an
A.sort()

Node = []
score = 0
Node.append(A.pop())


start = 0

for n in range(N - 1):
    score += Node[start]
    num = A.pop()
    start += 1
    
    Node.append(num)
    Node.append(num)


print(score)

Solution (Python):
from collections import deque

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

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

Solution (Python):
import math
n = int(input())
lst = list(map(int, input().split()))
sortlst = sorted(lst, reverse = True)

ans = 0
for i in range(n - 1):
	point =sortlst[math.ceil(i / 2)]

	ans += point

print(ans)

Solution (Python):
import math

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

ans = friendliness[0]

for i in range(1, N - 1):
    idx = math.ceil(i/2)
    ans += friendliness[idx]

print(ans)

Solution (Python):
n = int(input())
A = input()
A = A.split()
A = [int(i) for i in A]
A = sorted(A, reverse = True)

ans = 0

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

ans -= A[0]

if n&1:
  ans += A[int(n/2)]


print(ans)

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

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

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

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

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

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

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=list(map(int,input().split()))
B=sorted(A,reverse=True)
S=0
i=0
for j in range(1,N):
    if j%2==1:
        S+=B[i]
        i+=1
    else:
        S+=B[i]
print(S)

Solution (Python):
import numpy as np
N = int(input())

A = np.array([int(i) for i in input().split()])
A_sort = np.sort(A)


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

Solution (Python):
import math

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

    A.sort()

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

if __name__ == "__main__":
    resolve()

Solution (Python):
N = int(input())
table = sorted(list(map(int, input().split())), reverse=True)
ans = table[0]
rst = N - 2

for ti in table[1:]:
    if rst == 0:
        break
    if rst == 1:
        ans = ans + ti
        break
    ans = ans + 2 * ti
    rst = rst - 2

print(ans)

Solution (Python):
def main():
    N = int(input())
    A = list(map(int,input().split()))
    A.sort(reverse = True)
    ans = A[0]
    kazu = (N-2)//2
    amari = (N-2)%2
    for i in range(1,kazu+1,1):
        
        ans += (A[i]*2)

    if amari==1:
        ans += A[i+1]*amari

    print(ans)

main()

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

if n % 2 == 0:
    total = 0
    for i in A[::-1][:n//2]:
        total += i*2
    total = total - A[-1]
    print(total)
else:
    total = 0
    for i in A[::-1][:n//2+1]:
        total += i*2
    total = total - A[-1]
    total = total - A[-(n//2+1)]
    print(total)

Solution (Python):
import math

n = int(input())
players = [int(x) for x in input().split()]
players.sort(reverse = True)
comfortness = 0
for i in range(n-1):
    comfortness += players[math.ceil(i/2)]

print(comfortness)

Solution (Python):
from collections import deque
n = int(input())
a_ls = list(map(int, input().split()))
a_ls.sort(reverse = True)
queue = deque()
queue.append(a_ls[0])
for i in range(1,n):
    queue.append(a_ls[i])
    queue.append(a_ls[i])
ans = 0
for _ in range(n-1):
    ans += queue.popleft()
print(ans)

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

A.sort(reverse=True)
Aq=deque(A)

ans=Aq.popleft()
a=0
for i in range(1,N):
    if i%2==0:
        a=Aq.popleft()
    ans+=a

print(ans)

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

Solution (Python):
import sys
import collections
import bisect
import math
readline = sys.stdin.readline


def main():
    n = int(readline().rstrip())
    A = sorted(list(map(int, readline().rstrip().split())))
    a = sorted(A[0:n-1] * 2, reverse=True)
    print(max(A) + sum(a[0:n-2]))


if __name__ == '__main__':
    main()

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

a.sort(reverse=True)

ans = a[0]

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

print(ans)

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

a.sort(reverse=True)

ans = a[0]

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

print(ans)

Solution (Python):
n = int(input())
arr = list(map(int, input().split()))
arr.sort(reverse=True)
ans = arr[0]
i = 1
j = 1
while i < n-1:
    ans += arr[j]
    
    if i % 2 == 0:
        j += 1
    i += 1
print (ans)

Solution (Python):

import math





n = int(input())

target_1 = [int(x) for x in input().split()]









def count_sharp(target):
    return sum([x.count("


def bin_code(value, keta):
    return list(("{:0" + str(keta) + "b}").format(value))



target_1.sort()
point = 0
for idx, ins in enumerate(target_1):
    if idx != len(target_1) - 1:
        point += target_1[-1 - math.ceil(idx / 2)]


print(point)

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

A.sort(reverse=True)
ans = 0
tmp = 0
j = 0
for i in range(len(A)-1):
    if i == 0:
        ans += A[0]
        j += 1
    elif i % 2 == 0:
        ans += tmp
    else:
        tmp = A[j]
        ans+=tmp
        j += 1
print(ans)

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

Solution (Python):
n  = int(input())
ai = list(reversed(sorted(map(int, input().split()))))

added   = 2
mins    = [ ai[1], ai[1] ]
comfort = ai[0]

def double(xs):
    for x in xs:
        yield x
        yield x

while added < len(ai):
    pick = mins[ : len(ai) - added ]
    comfort += sum(pick)
    mins = list(double(ai[ added : added + len(pick) ]))
    added += len(pick)

print(comfort)

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

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

Solution (Python):


def solve():
    n = int(input())
    a = sorted(map(int, input().split()), reverse=True)
    a2 = [0] * len(a) * 2
    for i in range(len(a2)):
        a2[i] = a[i//2]
        if i >= n:
            break
    a2[0] = 0
    return sum(a2[:n])

print(solve())

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

    A.sort(reverse=True)

    res=A[0]

    counter=1

    while counter<=N-2:
        index = (counter + 1) // 2
        res+=A[index]
        counter+=1

    print(res)

if __name__=="__main__":
    main()

Solution (Python):
n=int(input())
A=list(map(int,input().split()))
AS=sorted(A,reverse=True)
B=sorted(AS+AS[1:],reverse=True)
print(sum(B[:n-1]))

Solution (Python):
import math
N=int(input())
A=[int(x) for x in input().split()]
ans=0
A=sorted(A)
ans+=A[N-1]
for i in range(1,N-1):
    ans+=A[N-1-math.ceil(i/2)]
print(ans)

Solution (Python):

N = int(input())
A = [int(i) for i in input().split()]

A.sort(reverse = True)

k = N-2
index = 1
ans = A[0]

while k > 0:
    if k > 1:
        k -= 2
        ans += A[index] * 2
    elif k == 1:
        k -= 1
        ans += A[index]
    index += 1

print("{}".format(ans))

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

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 heapq


def main():
    n = int(input())
    As = sorted(list(map(int, input().split())))[::-1]
    ans = 0
    q = []
    heapq.heappush(q, -As[0])
    for i in range(1, n):
        tmp = -heapq.heappop(q)
        ans += tmp
        heapq.heappush(q, -As[i])
        heapq.heappush(q, -As[i])
    print(ans)


if __name__ == "__main__":
    main()

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

i=1
s=A[0]
while i<n-1:
    i+=1
    s+=A[i//2]
print(s)

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

Solution (Python):
import sys
from collections import deque

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

A.sort(reverse=True)
max_value = A.pop(0)
diff = deque([max_value])
ans = 0
for i in A:
    ans = ans + diff.popleft()
    diff.append(i)
    diff.append(i)

print(ans)

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

Solution (Python):
n=input()
a=[int(i) for i in raw_input().split()]
a.sort()
s=a[n-1]
for i in range(n/2,n-1):
  s+=a[i]*2
if(n%2==1):
  s-=a[n/2]
print s

Solution (Python):
def main():
    N=int(input())
    A=sorted([int(_) for _ in input().split()])[::-1]
    ans = A[0]
    if N==2:
        print(ans)
    else:
        for i in range(1, N//2):
            ans += A[i]*2
        if N%2:
            ans += A[N//2]
        print(ans)
main()

Solution (Python):
import numpy as np









































def main3():
    N=input()
    N=int(N)
    A=input().split(" ")
    A=np.array([int(i) for i in A]).astype(np.uint64)
    l=[]
    A=np.sort(A)
    A=np.flip(A).astype(np.uint64)
    sum=0
    for i,num in enumerate(A):
        if i==0:
            l.append(num)
            continue
        l1=np.array(l,dtype=np.uint64)
        l2=np.roll(l1,1)
        l1=l1.reshape(-1,1)
        l2=l2.reshape(-1,1)
        l1=np.concatenate([l1,l2],1)
        l2=l1.min(1)
        arg=l2.argmax()
        l=l[:arg+1]+[num]+l[arg+1:]
        sum+=l2[arg]

    print(int(sum))


def correct():
    N=input()
    N=int(N)
    A=input().split(" ")
    A=np.array([int(i) for i in A]).astype(np.uint64)
    l=[]
    A=np.sort(A).astype(np.uint64)
    sum=0
    for i in range(N-1):
        sum+=A[N-(i+1)//2-1]
    print(int(sum))

if __name__ == "__main__":
    
    correct()

Solution (Python):
import sys

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

lst.sort(reverse = True)

ans = lst[0]
cnt = 0
idx = 1

for _ in range(1, n-1):
    if cnt <= 1:
        ans += lst[idx]
        cnt += 1
    else:
        idx += 1
        cnt = 1
        ans += lst[idx]


print(ans)

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

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

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

A = sorted(A, reverse=True)
print(A[0] + sum(A[1:N//2]) * 2 + A[N//2] * (N%2))

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

A.sort(reverse = True)

ans = A[0]

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

print(ans)

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 = list(map(int, input().split()))
a = sorted(a)[::-1]
ans = 2 * sum(a[1:n // 2]) + a[0]
if n % 2 == 1:
    ans += a[n // 2]

print(ans)

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

Solution (Python):
import math
import sys


def input():
    return sys.stdin.readline().strip()


sys.setrecursionlimit(20000000)

MOD = 10 ** 9 + 7
INF = float("inf")


def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort(reverse=True)
    answer = 0
    for i in range(1, N):

        answer += A[math.floor(i / 2)]

    print(answer)


if __name__ == "__main__":
    main()

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

Solution (Python):
N=int(input())
a=list(map(int, input().split()))
 
a=sorted(a)[::-1] 
ans =a[0]

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

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

print(ans)

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

Solution (Python):
n = int(input())
a = [int(x) for x in input().split()]

list.sort(a,reverse=True)

ans=a[0]

ans+=sum(a[1:(n//2)])*2+n%2*(a[n//2])

print(ans)

Solution (Python):
import time
startTimeProblem=time.time()

import fileinput, sys, itertools, functools
from math import *
from bisect import *
from heapq import *
from collections import *

def lcm(a, b):  
    return (a*b)/gcd(a, b)

class InputHelper:
    def __init__(self):
        self.myinput = fileinput.input()

    def isLocal(self):
        return not fileinput.isstdin()

    def int(self):
        return int(self.myinput.readline().rstrip())

    def ints(self):
        return [int(_) for _ in self.myinput.readline().rstrip().split()]

    def str(self):
        return self.myinput.readline().rstrip()

    def strs(self):
        return [_ for _ in self.myinput.readline().rstrip().split()]

class OutputHelper:
    def int(self, a):
        print(a)    

    def ints(self, a):  
        print(" ".join([str(_) for _ in a]))
    
    def intsNL(self, a):
        for _ in a:
            print(_)
    
    def str(self, s):
        print(s)

    def strs(self, s):
        print(" ".join([_ for _ in s]))

    def strsNL(self, s):
        for st in s:
            print(st)

class ListNode:
    def __init__(self, val):
        self.val = val
        self.next = None
        self.prev = None

In = InputHelper()
Out = OutputHelper()



N = In.int()
A = In.ints()

A.sort(reverse=True)

l = 0
ans = 0
tick = True
for i in range(len(A)-1):
    ans+=A[l]
    
    if i==0:
        l+=1
    else:        
        if tick:
            tick=not tick
        else:
            l+=1
            tick=not tick

Out.int(ans)
            



if len(sys.argv)>2 and sys.argv[2]=="TIMEIT":
    fin = (time.time()-startTimeProblem)*1000
    print("{:.2f}".format(fin) + "ms")

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
t=(len(a)-2)//2
ans=a[0]
if (len(a)-2)%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)

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

list.sort(x, reverse=True)
l = len(x)
if N == 1:
    print(0)
elif N == 2:
    print(x[0])
elif N%2==0:
    ans = 0
    for i in range(math.floor(N/2)):
        ans += 2 * x[i]
    print(ans - x[0])
else:
    ans = 0
    for i in range(math.floor(N/2)+1):
        ans += 2 * x[i]
    print(ans - x[0] - x[math.floor(N/2)])

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

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


print(ans)

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

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

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

print(point)

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

def main():
    n = int(input())
    a = [int(x) for x in input().split()]
    ans = 0
    a.sort(reverse=True)
    q = deque([])
    q.append(a[0])
    for i in range(1, n):
        ans += q.popleft()
        
        q.append(a[i])
        q.append(a[i])
    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
n=int(input())
a=[int(i) for i in input().split()]
a.sort()
ans=a.pop()
for i in range(2,n-1,2):
    ans+=2*a[-1]
    a.pop()
if n%2==1: ans+=a.pop()
print(ans)

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

AN.sort()
ans = 0
for n in range(1,N):
  ans += AN[N-1-(n//2)]
print(ans)

Solution (Python):
def main():
    n = int(input())
    a = list(map(int, input().split()))
    a.sort(key=lambda x: -x)
    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()

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

friend.sort(reverse = True)

ans = friend[0]
i = 1
ind = 1
while i < n - 1:
    for j in range(2):
        ans += friend[ind]
        i += 1
        if i == n-1: break
    ind += 1
print(ans)

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

Solution (Python):


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

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

Solution (Python):

from collections import deque


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

    Q = deque()

    ret = 0
    for i in range(-(-n//2)):
        if 0 < i:
            ret += min(Q[-1], Q[0])
        if i % 2:
            Q.appendleft(A[i])
        else:
            Q.append(A[i])

    for i in range(n-len(Q)):
        ret += min(Q[i], Q[(i+1) % len(Q)])

    return str(ret)


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))

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

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

A.sort()
A.reverse()
ans = 0
if N%2 == 0 :
  ans += A[0]
  for i in range(int(N/2) - 1) :
    ans += A[i+1] * 2
if N%2 == 1 :
  ans += A[0]
  ans += A[int(N/2)]
  for i in range(int(N/2) - 1) :
    ans += A[i+1] * 2
    
print(ans)

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

a = sorted(a)
a = list(reversed(a))

ans = 0
index = 0

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

print(ans)

Solution (Python):
import sys

import math
import collections

import bisect
import itertools
import fractions

import copy

import decimal

import queue



sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7


ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))



eps = 10 ** (-9)


def main():
    n = ni()
    a = na()

    a.sort(reverse=True)

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

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):

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]=10**10

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

a=sorted(a,reverse=True)

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

  if(n%2 ==0):
    print(comfort)
  else:
    print(comfort+a[m+2])

Solution (Python):
import sys 

def A():
  n = int(sys.stdin.readline().rstrip())
  charge = (n+999)//1000 * 1000 - n
  print(charge)


from collections import Counter
def B():
  n, *s = sys.stdin.read().split() 
  c = Counter(s)
  for v in 'AC, WA, TLE, RE'.split(', '):
    print(f'{v} x {c[v]}')


def C():
  h, w, k = map(int, sys.stdin.readline().split())
  c = [sys.stdin.readline().rstrip() for _ in range(h)]
  tot = 0
  for i in range(1<<h):
    for j in range(1<<w):
      cnt = 0
      for y in range(h):
        for x in range(w):
          if i>>y & 1 or j>>x & 1:
            continue 
          cnt += c[y][x] ==  '
      tot += cnt == k
  print(tot)


def D():
  n, *a = map(int, sys.stdin.read().split())
  a.sort(reverse=True)
  res = a[0] + sum(a[1:1+(n-2)//2])*2 + a[1+(n-2)//2]*(n & 1)
  print(res)


def E():
  pass


def F():
  pass

if __name__ == "__main__":
  
  
  
  D()
  E()
  F()

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

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

Solution (Python):


import bisect
import copy
import collections
import heapq
import itertools
import math
import numpy as np
from operator import itemgetter
import scipy.misc
import sys

"""

N, X = map(int, input().split())

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

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())
"""


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

def main():
    A.sort(reverse=True)
    dq = collections.deque(A)
    now = dq.popleft()
    hq = []
    heapq.heapify(hq)
    heapq.heappush(hq, (-now, now))    
    ans = 0
    for n in range(1,N):
        ans += heapq.heappop(hq)[1]
        now = dq.popleft()
        heapq.heappush(hq, (-now, now))
        heapq.heappush(hq, (-now, now))
    print(ans)
    return
    

if __name__ == '__main__':
    main()

Solution (Python):

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

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

print(sumkoko)

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

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

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random,resource

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    n = I()
    a = sorted(LI(), reverse=True)

    if n % 2 == 0:
        return sum(a[:n//2]) * 2 - a[0]

    return sum(a[:n//2+1]) * 2 - a[0] - a[n//2]


print(main())

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

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

a = sorted(a, reverse = True)
final = 0

if n % 2 == 0:
    ak = a[0:int(n/2)]
    for i in ak[1::]:
        final += i
    final *= 2
    final += ak[0]
else:
    ak = a[0:(int(n/2) + 1)]
    for i in ak[1::]:
        final += i
    final *= 2 
    final += ak[0]
    final -= ak[len(ak) - 1]


print(final)

Solution (Python):
def main():
    import sys
    readline = sys.stdin.buffer.readline
    sys.setrecursionlimit(10 ** 7)

    n = int(readline())
    a = list(map(int, readline().split()))
    a.sort(reverse=True)
    idx = 1
    ans = a[0]
    for aa in a[1:]:
        if idx + 2 >= n:
            if idx + 1 < n:
                ans += aa
            break
        idx += 2
        ans += aa * 2
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):

n = int(input())
a = list(map(int, input().split()))
a.sort()
ans = a[-1]
cnt = 1
while cnt < n - 1:
    ans += a[-1 * (1 + (1 + cnt) // 2)]
    cnt += 1

print(ans)

Solution (Python):
n = int(input())
a = list(reversed(sorted([int(i) for i in input().split()])))
ans = a[0]
n -= 2
i = 1
while n > 0:
    if n > 1:
        ans += a[i] * 2
        n -= 2
        i += 1
    else:
        ans += a[i]
        n -= 1
print(ans)

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

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

ans = A[0]
N -= 2

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

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

print(ans)

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):
N = int(input())
A = [int(i) for i in input().split()]


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


sum = 0
for i in range(len(A) - 1):
    if i % 2 == 1:
        sum += A[int((i+1)/2)]
    elif i % 2 == 0:
        sum += A[int(i/2)]
print(sum)

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

a.sort(reverse=True)

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

print(ans)

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