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 math
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[math.ceil(i/2)]
print(ans)

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

A = sorted(A,reverse=True)

if N <= 3:
  ans = sum(A[:-1])
elif N % 2 == 0:
  ans = A[0] + sum(A[1:(N-2)//2+1])*2
elif N % 2 == 1:
  ans = A[0] + sum(A[1:(N-2)//2+1])*2 + A[1+(N-2)//2]
  
print(ans)

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

ans = A[0]

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

print(ans)

Solution (Python):
import bisect,collections,copy,itertools,math,string
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())
def main():
    n = I()
    a = LI()
    a.sort(reverse=True)
    ans = a[0]
    n -= 1
    for i in range(n-1):
        index = i//2 + 1
        ans += a[index]
    print(ans)
main()

Solution (Python):

import sys
from itertools import chain















def solve(N: int, A: "List[int]"):
    A = sorted(A, reverse=True)
    if N == 1:
        return
    N -= 1
    if N % 2 == 0:
        return A[0] + 2 * sum(A[1 : N // 2]) + A[N // 2]
    else:
        return A[0] + 2 * sum(A[1 : (N - 1) // 2 + 1])


def main():
    tokens = chain(*(line.split() for line in sys.stdin))
    
    N = int(next(tokens))  
    A = list(map(int, (tokens)))
    answer = solve(N, A)
    print(answer)


if __name__ == "__main__":
    main()

Solution (Python):
def solve():
    N = int(input())
    A = [int(i) for i in input().split()]
    sorted_A = sorted(A, reverse=True)
    cnt = N
    ptr = 0
    is_first = True
    ans = 0
    while cnt > 0:
        ans += sorted_A[ptr]
        if is_first:
            is_first = False
        else:
            ptr +=1
            is_first = True
        cnt -= 1
    print(ans - sorted_A[0])

if __name__ == "__main__":
    solve()

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

Solution (Python):
from collections import deque
N = int(input())
A = list(map(int,input().split()))
Query = deque()
A.sort(reverse=True)
Query.append([A[0],A[1]])
Query.append([A[0],A[1]])
Answer = A[0]
for i in range(2,N):
    query = Query.popleft()
    Answer += min(query)
    Query.append([max(query),A[i]])
    Query.append([min(query),A[i]])
print(Answer)

Solution (Python):
input()
A=list(map(int,input().split()))
A.sort(reverse=True)
B=[]
ans=0
for i in A:
  if len(B)>=1:
    ans+=B[len(B)//2]
  B.append(i)
print(ans)

Solution (Python):
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy


import heapq
sys.setrecursionlimit(10**6)

INF = 10**18
import bisect
import statistics
mod = 10**9+7




N = int(input())

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

A = sorted(A, reverse=True)

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

print(ans)

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

A.sort(reverse=True)


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

print(ans)

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


ans = 0
for i in range(N):
    if i == 0:
        continue
    else:
        ans += A[i//2]
print(ans)

Solution (Python):



from fractions import gcd
from itertools import combinations,permutations,accumulate, product 

from collections import deque,defaultdict,Counter
import decimal
import re
import math












import sys
sys.setrecursionlimit(10000000)


mod = 998244353
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())



from fractions import gcd
from itertools import combinations,permutations,accumulate, product 

from collections import deque,defaultdict,Counter
import decimal
import re
import math












import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7


def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n = I()
A = sorted(readInts(), reverse=True)
ans = 0
dic = defaultdict(int)
dq = deque()
for i in range(n):
    if i == 0:
        dq.append(A[i])
    else:
        ans += dq.popleft()
        for j in range(2):
            dq.append(A[i])
print(ans)

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

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

ans = 0
cnt = 0
for i, a in enumerate(A):
    if i == 0:
        ans += a
        cnt += 1
    else:
        if n-1 - cnt >= 2:
            ans += a*2
            cnt += 2
        else:
            ans += a
            cnt += 1
    if cnt == n-1:
        break
print(ans)

Solution (Python):
N, *A = map(int, open(0).read().split())
A.sort(reverse=True)
ans = A[0]
for i in range(2, N):
    ans += A[i // 2]
print(ans)

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

deq = deque([A[0]])
ans = 0
for x in A[1:]:
    ans += deq.popleft()
    deq.append(x)
    deq.append(x)
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())    
al = list(map(int, input().split()))    
al.sort(reverse=True)

ans = al[0]
for i in range(n-2):
    ans += al[i//2+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 = list(map(int,input().split()))

A = sorted(A, reverse = True)

B = [A[0]] + [A[i] for i in range(1,N)] * 2

B = sorted(B, reverse = True)

ans = 0

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

Solution (Python):
n=int(input())
c= sorted(list(map(int, input().split())), reverse=True)
l = len(c)
m = l//2
ans = c[0]

if l%2 == 0:
    for i in range(1,m):
        ans += 2*c[i]
else:
    for i in range(1,m):
        ans += 2*c[i]
    ans += c[m]
print(ans)

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

Solution (Python):
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
  
  ans += v
  i += 1
  if i >= n:
    break
  
  ans += v
  i += 1

print(ans)

Solution (Python):
n = int(input())
a = sorted(list(map(int, input().split())))
ans = a.pop()
if n % 2:
    for i in range((n - 2) // 2):
        ans += a.pop() * 2
    ans += a.pop()
    print(ans)
else:
    for i in range((n - 2) // 2):
        ans += a.pop() * 2
    print(ans)

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

cnt = 0
for i in range(len(A)):
  if i == 0:
    continue
  
  cnt += A[int(i/2)]

print(cnt)

Solution (Python):



def next_line():
    return input()


def next_int():
    return int(input())


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


def next_int_array_multi_lines(size):
    return [int(input()) for _ in range(size)]


def next_str_array(size):
    return [input() for _ in range(size)]


def main():
    n = next_int()
    ar = list(reversed(sorted(next_int_array_one_line())))
    
    res = ar[0]
    n -= 2
    use = 1
    while n > 0:
        if n >= 2:
            res += ar[use] * 2
            n -= 2
            use += 1
        else:
            res += ar[use]
            n -= 1
    print(res)


if __name__ == '__main__':
    main()

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

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

Solution (Python):






N = int(input())



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





A = sorted(A, reverse=True)















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

print(tot)

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

B = sorted(A, reverse=True)
ans = 0

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

print(ans)

Solution (Python):
import sys
import bisect as bi
import math
from collections import defaultdict as dd
import heapq
input=sys.stdin.readline


mo=10**9+7
def cin():
    return map(int,sin().split())
def ain():            
    return list(map(int,sin().split()))
def sin():
    return input()
def inin():
    return int(input())









n=inin()
l=ain()
l=sorted(l,reverse=True)
if(n%2==0):
    if(n>=4):
        print(l[0]+0+2*sum(l[1:(n-2)//2+1]))
    else:
        print(l[0])
else:
    if(n>=5):
        print(l[0]+0+2*sum(l[1:(n-2)//2+1])+l[(n-2)//2+1])
    else:
        print(l[0]+l[1])

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
A=sorted(A,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()))
A=sorted(A)[::-1]
num=0
ans=0
cnt=1
i=0
while cnt<N:
    if i==0:
        ans+=A[i]
        i+=1
    else:
        if num==0:
            ans+=A[i]
            num+=1
        else:
            ans+=A[i]
            i+=1
            num=0
    cnt+=1
print(ans)

Solution (Python):
N = int(input())
A = sorted(map(int,input().split()),reverse = True)
answer = 0
i = 1
s = 0
answer = A[0]
if N == 2:
    anwer=A[0]
else:
    while True:
        s = (i+1)//2
        answer += A[s]
        i += 1
        if i == N-1:
            break
    
print(answer)

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

a.sort(reverse = True)
p = a[0]
for i in range(N-2): 
    p += a[(i//2)+1]

print(p)

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

Solution (Python):
import heapq

N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
L = []
ans = 0

heapq.heappush(L, -A[0])

for i in range(1, N):
    max = heapq.heappop(L)
    max *= -1
    ans += max
    heapq.heappush(L, -A[i])
    heapq.heappush(L, -A[i])

print(ans)

Solution (Python):
import heapq

N=int(input())
A=sorted(list(map(int,input().split())), reverse=True)
ans=0
H=[-1*A[0]]
heapq.heapify(H)
for a in A[1:]:
  ans-=heapq.heappop(H)
  heapq.heappush(H,-1*a)
  heapq.heappush(H,-1*a)
print(ans)

Solution (Python):

import sys

def main():
    N = int( sys.stdin.readline() )
    A_list = list(map(int, sys.stdin.readline().split()))

    A_list.sort(reverse=True)
    arrived_idx = 1
    comfort = A_list[0]


    for i in range(1, len(A_list)):
        if ( len(A_list) - 1 - arrived_idx ) >= 2:
            comfort += (A_list[i] * 2)
            arrived_idx += 2
        elif ( len(A_list) - 1 - arrived_idx ) == 1:
            comfort += A_list[i]
            break
        else:  
            break


    print(comfort)


if __name__ == "__main__":
    main()

Solution (Python):
import heapq

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

heapq.heapify(a)
b = list(map(lambda x: x * (-1), a))
heapq.heapify(b)
ans -= heapq.heappop(b)

for i in range(n//2-1):
    ans -= 2 * heapq.heappop(b)
if n % 2 == 1:
    ans -= heapq.heappop(b)
print(ans)

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

A.sort(reverse=True)

B=[]
B.append(A[0])
for i in range(1,N):
    B.append(A[i])
    B.append(A[i])

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

print(ans)

Solution (Python):
N = int(input())
A = sorted(map(int, input().split()), reverse=True)
ans = sum(A[:N//2]) + sum(A[1:-(-N//2)])
print(ans)

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

A.sort(reverse=True)

max_num, *p = A

node_num = N-1

q = [(2*i <= node_num) + (2*i+1 <= node_num) for i in range(1, node_num+1)]



r = max_num + sum(p_i*q_i for p_i, q_i in zip(p, q))
print(r)

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

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

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

    
    print(ans)

main()

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

lis = sorted(lis, reverse=True)

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

print(ans)

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

lis = sorted(lis, reverse=True)

ans = lis[0]
t = 0
for i in range(1,n-1):
    if i % 2 != 0:
        t += 1
    ans += lis[t]
        

print(ans)

Solution (Python):

import sys
input = sys.stdin.readline

def inp():
    return(int(input()))
    
def inlt():
    return(list(map(int,input().split())))
    
n = inp()
f = inlt()
f = list(reversed(sorted(f)))
if n%2:
    print(2*sum(f[0:(n+1)//2])-f[0]-f[(n+1)//2-1])
else:
    print(2*sum(f[0:n//2])-f[0])

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

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

Solution (Python):
import collections
n=int(input())
A=list(map(int,input().split()))
A.sort(reverse=True)
q=[]
for i in range(n):
  if i==0:
    q.append(A[i])
  else:
    q+=[A[i]]*2
print(sum(q[:n-1]))

Solution (Python):
def main():
    N = int(input())
    A = sorted(map(int, input().split()), reverse=True)
    print(A[0] + sum(A[1:(N - 2) // 2 + 1]) * 2 + A[(N - 2) // 2 + 1] * (N % 2))


if __name__ == "__main__":
    main()

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

A.sort(reverse=True)
ans = A[0]
cnt = 1
while cnt < n-1:
    ans += A[(cnt+1)//2]
    cnt += 1
print(ans)

Solution (Python):
import math
N = int(input())
A = list(map(int,input().split()))
A.sort(reverse = True)
score = A[0]
for i in range(1, math.ceil(N*0.5)):
    if N - i * 2 != 1:
        score += 2 * A[i]
    else:
        score += A[i]
print(score)

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

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

nums = []
nums = list(map(int, input().split()))
nums = sorted(nums, reverse=True)

cnt = 0

fir_num = nums[0]

del nums[0]

for i in range(n-2):
    fir_num += nums[int(i/2)]


print(fir_num)

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

Solution (Python):
def abc173d_chat_in_the_circle():
    n = int(input())
    a = sorted(map(int, input().split()), reverse=True)
    if n <= 1:
        print(0)
        return
    val = 0
    for i in range(0, n-1):
        idx = (i+1) // 2
        val += a[idx]
    print(val)

abc173d_chat_in_the_circle()

Solution (Python):
from collections import deque

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

    comfort = 0
    l_sorted = sorted(l, reverse=True)
    q = deque([l_sorted[0]])
    for i in range(1, n):
        comfort += q[0]
        q.popleft()
        for _ in range(2):
            q.append(l_sorted[i])
    print(comfort)

if __name__ == '__main__':
    main()

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[int((i+1)*0.5)]
    
print(ans)

Solution (Python):
N = int(input())
nums = [int(i) for i in input().split()]


nums = sorted(nums, reverse=True)


a_sum = 0
for i in range(1,N):
    index = i//2
    a_sum += nums[index]

print(a_sum)

Solution (Python):
import sys

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
    n = ni()
    a = sorted(nl(), reverse=True)
    print(sum(a[:n//2]) + sum(a[1:(n+1)//2]))
    return

solve()

Solution (Python):
import sys

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
    n = ni()
    a = sorted(nl(), reverse=True)
    print(sum(a[:n//2]) + sum(a[1:(n+1)//2]))
    return

solve()

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

s = sorted(a, reverse=True)
print(sum(s[i//2] for i in range(1, n)))

Solution (Python):
import sys

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
    n = ni()
    a = sorted(nl(), reverse=True)
    print(sum(a[:n//2]) + sum(a[1:(n+1)//2]))
    return

solve()

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

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

Solution (Python):
n = int(input())
arr = list(map(int,input().split()))
arr.sort(reverse = True)
ans = arr[0]
odd = False
mult = n-2
if mult%2!=0:
    mult-=1
    odd = True
mult//=2
ans += sum(arr[1:1+mult])*2
if odd:
    ans+=arr[1+mult]
print(ans)
"""
Sample:
7
1 2 3 4 5 6 7


2537146
7+6+6+5+5+4
"""

Solution (Python):
import bisect
import statistics
n = int(input())
a = list((int(x) for x in input().split()))

a.sort()
incl = []
score = 0

for i in range(len(a)):
    ain = a.pop()
    incl.append(ain)
    if i==0: continue
    if (len(incl)%2 == 1):
        score += incl[ int(len(incl)/2) ]
    else:
        
        score += incl[ int(len(incl)/2)-1 ]

print(score)

Solution (Python):

N = int(input())
K = list(map(int,input().split()))
K = sorted(K,reverse=True)
"""

before_data = 0
before_data_before = 0
ans = 0 
for i in K :
    ans += before_data
    before_data_before = before_data
    before_data = i
"""
ans = 0 
for i,l in enumerate(K) :
    if (i == 0 ):
        ans += 0
    else :
        ans += K[i // 2]
print(ans)

Solution (Python):

"""
N= 4
K = [2, 2, 1, 3]
"""

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





K = sorted(K,reverse=True)
"""

before_data = 0
before_data_before = 0
ans = 0 
for i in K :
    ans += before_data
    before_data_before = before_data
    before_data = i
"""
ans = 0 
for i,l in enumerate(K) :
    if (i == 0 ):
        ans += 0
    
    else :
        ans += K[i // 2]

    


print(ans)

Solution (Python):

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

arr = []
arr = sorted(A,reverse = True)

Friend_min_max_wa = 0
i =0
j = 0
for i in range(N -1):
    if i % 2 != 0 : j = j + 1
    Friend_min_max_wa = Friend_min_max_wa + arr[j]


print(Friend_min_max_wa)

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

Solution (Python):
'''
参考
https://yamakasa.net/atcoder-abc-173-d/
図
https://github.com/ymo999/AtCoder/blob/master/2020/20200705_ABC173/ABC173_D.pdf
'''
import math

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

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

print(ans)

Solution (Python):
def main():
    N = int(input())
    A = sorted(map(int, input().split()), reverse=True)
    print(A[0] + sum(A[1:(N - 2) // 2 + 1]) * 2 + A[(N - 2) // 2 + 1] * (N % 2))


if __name__ == "__main__":
    main()

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

Solution (Python):




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

A.sort()
ans=0

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

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

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

L = len(A) // 2

if len(A) % 2 == 0:
    
    ans = sum(A[:L]) + sum(A[1:L])
else:
    
    ans = sum(A[:L]) + sum(A[1:L+1])

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 += 2*sum(A[1:(N-2)//2+1])
else:
    ans += 2*sum(A[1:(N-2)//2+1])+A[(N-2)//2+1]

print(ans)

Solution (Python):
import math

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

s = sorted(a,reverse=True)

max = s[0]
for i in range(3,n+1):
  max += s[math.ceil(i/2)-1]
print(max)

Solution (Python):

from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


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


def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2


def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


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


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

    ans = A[0]
    n = 1
    for i in range(1, N):
        if n == N - 1:
            break
        ans += A[i]
        n += 1
        if n == N - 1:
            break
        ans += A[i]
        n += 1
        if n == N - 1:
            break

    print(ans)


def main():
    solve()


if __name__ == '__main__':
    main()

Solution (Python):


import itertools
import collections
from decimal import Decimal
from functools import reduce
import heapq

def main():
    n = int(input())
    a = input_list()
    aa = a + a
    aa.sort(reverse=True)
    ans = 0
    for i in range(1, n):
        ans += aa[i]
    print(ans)


def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a


def bfs(H, W, black_cells, dist):
    d = 0
    while black_cells:
        h, w = black_cells.popleft()
        d = dist[h][w]
        for dy, dx in ((1, 0), (0, 1), (-1, 0), (0, -1)):
            new_h = h + dy
            new_w = w + dx
            if new_h < 0 or H <= new_h or new_w < 0 or W <= new_w:
                continue
            if dist[new_h][new_w] == -1:
                dist[new_h][new_w] = d + 1
                black_cells.append((new_h, new_w))
    return d


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


def input_list_str():
    return list(map(str, input().split()))


if __name__ == "__main__":
    main()

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


if __name__ == '__main__':
    main()

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

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

Solution (Python):
n=int(input())
lst=list(map(int,input().split()))
lst=sorted(lst)
if(n>2):
    sm=0
    sm+=lst[-1]
    k=0
    i=2
    num=2
    while(num<n):
        k+=1
        sm+=lst[-i]
        if(k==2):
            k=0
            i+=1
        num+=1
elif(n==2):
    sm=lst[-1]

print(sm)

Solution (Python):
import numpy as np
n=int(input())
A=np.array(input().split(),np.int64)
A.sort()
A=A[::-1]
ans=int(0)
for i in range(n-1):
    ans+=A[(i+1)//2]
print(ans)

Solution (Python):
import heapq

def solve():
    N = int(input())
    A = list(sorted(map(int, input().split()), reverse=True))
    C = [-A[0]]

    ans = 0
    for a in A[1:]:
        K = heapq.heappop(C)
        ans += -K
        heapq.heappush(C, -a)
        heapq.heappush(C, -a)

    print(ans)



solve()

Solution (Python):


import math

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




A = sorted(A, reverse = True)



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

print(score)

Solution (Python):
N = int(input())
List = list(map(int,input().split()))
List += List
List = sorted(List)[::-1]
List = List[1:N]
print(sum(List))

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

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

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

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

Solution (Python):
ini = lambda : int(input())
inm = lambda : map(int,input().split())
inl = lambda : list(map(int,input().split()))
gcd = lambda x,y : gcd(y,x%y) if x%y else y

n = ini()
a = inl()
a = sorted(a,reverse = True)
c = 1
j = 0
ans = 0
for i in range(1,n):
        ans += a[j]
        c += 1
        if c == 2:
                c = 0
                j += 1
print(ans)

Solution (Python):
import queue

ans = 0

N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
q = queue.Queue()
q.put(0)
for a in range(len(A)):
    ans += q.get()
    q.put(A[a])
    if a != 0:
        q.put(A[a])

print(ans)

Solution (Python):
import math
from math import gcd
INF = float("inf")

import sys
input=sys.stdin.readline
import itertools
from collections import Counter

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

if __name__=="__main__":
    main()

Solution (Python):
import math
from math import gcd
INF = float("inf")

import sys
input=sys.stdin.readline
import itertools
from collections import Counter

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

if __name__=="__main__":
    main()

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

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

Solution (Python):


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

    
    
    
    
    dic = {}
    for a in As:
        if not a in dic: dic[a] = 0
        dic[a] += 1

    keys = []
    for key in dic.keys():
        keys.append(key)
    keys.sort(reverse=True)

    ans = 0
    n = N - 1
    dic[keys[0]] -= 1
    ans += keys[0]
    n -= 1

    for i, key in enumerate(keys):
        if dic[key] == 0:
            continue

        if (dic[key]*2) <= n:
            ans += key*(dic[key]*2)
            n -= dic[key]*2
        else:
            ans += key*n
            n -= n
            break

    print(ans)


if __name__ == "__main__":
    solve()

Solution (Python):


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

    
    
    
    dic = {}
    for a in As:
        if not a in dic: dic[a] = 0
        dic[a] += 1

    keys = []
    for key in dic.keys():
        keys.append(key)
    keys.sort(reverse=True)

    ans = 0
    n = N - 1
    dic[keys[0]] -= 1
    ans += keys[0]
    n -= 1

    for i, key in enumerate(keys):
        if dic[key] == 0:
            continue

        if (dic[key]*2) <= n:
            ans += key*(dic[key]*2)
            n -= dic[key]*2
        else:
            ans += key*n
            n -= n
            break

    print(ans)


if __name__ == "__main__":
    solve()

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

aa = a + a
aa.sort(reverse=True)

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

print(ans)