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 = sorted(map(int, input().split()))[::-1]

ans, que = 0, [a[0]]
for i, ai in enumerate(a[1:]):
    ans += que[i]
    que += [ai, ai]
print(ans)

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

ans, que, cnt = 0, [a[0]], 0
for i in a[1:]:
    ans += que[cnt]
    que += [i, i]
    cnt += 1
print(ans)

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

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

print(s)

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

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

ans = a[0]

cnt = 1
i = 1
while cnt < n-1:
    cnt += 2
    ans += a[i] * 2
    i += 1

if cnt == n:
    ans -= a[i-1]

print(ans)

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

Solution (Python):
import heapq as hq

n = int(input())
a = [int(_) for _ in input().split()]

a.sort()
p = a.pop()
q = []
hq.heappush(q, (-p, -p))
ans = 0
for _ in range(n - 1):
    p = a.pop()
    b1, b2 = hq.heappop(q)
    ans += -b1
    hq.heappush(q, sorted((b1, -p), reverse=True))
    hq.heappush(q, sorted((b2, -p), reverse=True))
print(ans)

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

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()

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

A.sort(reverse=True)
N -= 2
ans = A[0]
A.pop(0)
if N % 2 == 0:
    for i in range(N//2):
        ans += A[i] * 2
else:
    for i in range(N//2):
        ans += A[i] * 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]

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

print(ans)

Solution (Python):
from sys import stdin

input = stdin.readline

N = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
res = a[0]
cnt = N - 2
cur = 1
while cnt:
    if cnt == 1:
        res += a[cur]
        break
    else:
        res += a[cur] * 2
        cur += 1
        cnt -= 2

print(res)

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

a.sort(reverse=True)

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

print(cnt)

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

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

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

list.sort(A, reverse=True)
ans = 0

for i in range(N-1):

    ans += A[math.ceil(i/2)]

print(ans)

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

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

Solution (Python):


N=int(input())
A=list(map(int,input().split()))
A.sort(reverse=True)
ind=[(i+1)//2 for i in range(N-1)]
print(sum([A[i] for i in ind]))

Solution (Python):
def main():
    N = int(input())
    A = sorted(list(map(int, input().split())))[::-1]
    if N % 2 == 1:
        print(sum(A[:N//2]) + sum(A[1:N//2+1]))
    else:
        print(sum(A[:N//2-1]) + sum(A[1:N//2]) + A[N//2-1])


if __name__ == '__main__':
    main()

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

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

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

x = A[N-1]

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

print(x)

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

a.sort()

if n % 2 == 0:
    suma = 2*sum(a[-int((n/2)):])-a[-1]
else:
    suma = 2*sum(a[-int(((n+1)/2)):])-a[-1]-a[-int(((n+1)/2))]

print(suma)

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

"""
Created on Mon Sep 14 20:47:07 2020

@author: ezwry
"""
import math

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

a.sort(reverse=1)

ans=[]

for i in range(n-1):
    jyun=math.ceil(i/2)
    ans.append(a[jyun])

answer=sum(ans)
print(answer)

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, reverse = True)
ans = 0

index_val = int(N//2)
ans += a[0]
for i in range(1, index_val):
    ans += (2 * a[i])
if N % 2 != 0:
    index_val = N//2
    ans += a[index_val]

print(ans)

Solution (Python):
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
from collections import deque
def main():
    n, *a = map(int, read().split())
    a.sort(reverse=True)
    a = deque(a)
    circle_joined = deque()
    next_score = a.popleft()
    r = 0
    flag = False
    while a:
        circle_joined.append(a.popleft())
        if flag:
            next_score = circle_joined.popleft()
            r += next_score
            flag = False
        else:
            r += next_score
            flag = True
    print(r)

if __name__ == '__main__':
    main()

Solution (Python):
n = int(input())
a_list = sorted([int(x) for x in input().split()])
ans = a_list.pop()
n -= 2
while n > 0:
    a = a_list.pop()
    ans += a
    n -= 1
    if n > 0:
        ans += a
        n -= 1
print(ans)

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

Solution (Python):

import sys, math, heapq
from itertools import permutations, combinations
from collections import defaultdict, Counter, deque
from math import factorial, gcd
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline()[:-1]
pl = lambda x: print(*x, sep="\n")

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

res = [A[0]]
for a in A[1:]:
    res.append(a)
    res.append(a)
print(sum(res[: N - 1]))

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

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

sortA = sorted(A)

result = 0
for i in range(1, N):
    result += sortA[N - (i // 2) - 1]

print(result)

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

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

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

print(sum)

Solution (Python):
from sys import stdin
n = int(stdin.readline())
a = list(map(int,stdin.readline().split()))
a.sort(reverse = True)
ans = a[0]
count = 0
cur = 1
for _ in range(n-2):
    ans += a[cur]
    count += 1
    if count == 2:
        count = 0
        cur += 1
print(ans)

Solution (Python):
import bisect, collections, copy, heapq, itertools, math, string
import sys
def I(): return int(sys.stdin.readline().rstrip())
def MI(): return map(int, sys.stdin.readline().rstrip().split())
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())

from  collections import defaultdict
from collections import Counter
import bisect
def main():
    N = I()
    A = LI()
    A.sort(reverse=True)
    ans = A[0]
    cnt = 1
    i = 1
    while cnt < N - 1:
        ans += A[i] * 2
        cnt += 2
        i += 1
    if cnt > N - 1:
        ans -= A[i - 1]
    print(ans)
if __name__ == "__main__":
    main()

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

Solution (Python):
n = int(input())
p_list = input().split()
p_list = map(int,p_list)
p_list = sorted(p_list,reverse=True)
ans_list = []
ans = 0
for i in range(0,len(p_list)):
  if i!=0:
    ans_list.append(p_list[i])
    ans_list.append(p_list[i])
  else:
    ans_list.append(p_list[i])
for i in range(0,n-1):
	ans += int(ans_list[i])
print(ans)

Solution (Python):
def main():
    n = int(input())
    a_list = sorted(list(map(int, input().split())), reverse=True)
    ans = a_list[0]

    cnt = 1
    i = 1
    while True:
        if cnt >= n - 2:
            break
        ans += a_list[i] * 2
        i += 1
        cnt += 2

    if cnt == n - 2:
        ans += a_list[i]

    print(ans)


if __name__ == "__main__":
    main()

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

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

print(ans)

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

Solution (Python):
import math

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

A.sort(reverse=True)

comfortPoint = A[0]

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

print(comfortPoint)

Solution (Python):
import math , sys 
from collections import deque

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

ans = Cs.pop(0)
i = 1
Q  = (N-2)//2
R = (N-2) % 2
ans += sum([Cs[i]*2 for i in range(Q)])
if R==1:
    ans+=Cs[Q]
print(ans)

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

A.sort(reverse = True)

ans = 0

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

print(ans)

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

len_A = len(A)
ans = 0
for i in range(1, len_A):
  ans += A[len_A-1-int(i/2)]

print(ans)

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

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

A.sort(reverse=True)
ans = 0

ans += A[0]

cnt = 1
i = 1

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

print(ans)

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

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

A.sort(reverse=True)
ans = 0

ans += A[0]

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

print(ans)

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())
A = sorted(list(map(int, input().split())), reverse=True)

ans = 0
cnt = 1

ans += A[0]


flag = False
for i in range(1, len(A)-1):
    ans += A[cnt]
    if flag == False:
        flag = True
    else:
        flag = False
        cnt += 1
print(ans)

Solution (Python):
n = int(input())
a = sorted(list(map(int, input().split())))
s = a[::]
ans = 0
c = 0
for i in range(n-1):
    if i == 0:
        ans += s.pop()
    else:
        if c == 0:
            ans += s[-1]
            c += 1
        elif c == 1:
            ans += s.pop()
            c = 0

print(ans)

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

print(sum(A[i//2] for i in range(1, N)))

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

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

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

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

Solution (Python):
import math

def solve():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort(reverse = True)
    
    A_half = math.ceil(N/2)
    A_pre = A[:A_half]
    A_post = A[A_half:]
    
    ret = sum(A_pre)
    for i in range(len(A_post)-1):
        ret += min(A_pre[i],A_pre[(i+1)%A_half])
        
    print(ret)
    
solve()

Solution (Python):
N = int(input())
An = list(map(int, input().split()))
An.sort(reverse=True)
answer = 0
t = N-1
for i, Ai in enumerate(An):
    lim = 2
    if i==0:
        lim = 1
    for j in range(lim):
        if t > 0:
            answer += Ai
        t -= 1
print(answer)

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

A.sort(reverse=True)

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

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

    k = n//2
    if n%2==0:
        print(A[0]+2*sum(A[1:k]))
    else:
        print(A[0]+2*sum(A[1:k])+A[k])
main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n = int(input())
    A = list(map(int,input().split()))
    A.sort(reverse = True)
    ans = A[0]
    for i in range(n-2):
        ans += A[i//2 + 1]
    print(ans)

if __name__=='__main__':
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
def main():
    n = int(input())
    A = list(map(int,input().split()))
    A.sort(reverse = True)
    ans = A[0]
    for i in range(n-2):
        ans += A[i//2 + 1]
    print(ans)

if __name__=='__main__':
    main()

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

if n % 2 == 0:
  cnt = n // 2
  cnf = a[0] + sum(a[1:cnt]) * 2

else:
  cnt = n //2
  cnf = a[0] + sum(a[1:cnt]) * 2 + a[cnt]

print(cnf)

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

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

score = As[0]
counter = 0
index = 1

for i in range(2, N):
  score += As[index]
  if counter == 0:
    counter += 1
  else:
    counter -= 1
    index += 1
    
print(score)

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

N-=1
i=1
koko=A[0]
f=0
while 1:
  for _ in range(2):
    N-=1
    if N==0:
      f=1
      break
    koko+=A[i]
  if f==1:
    break
  i+=1
print(koko)

Solution (Python):
import heapq

n=int(input())
A=list(map(int,input().split()))
for i in range(n):
  A[i]*=-1
A.sort()
ans=0
Q=[A[0]]
heapq.heapify(Q)
for i in range(1,n):
  v=heapq.heappop(Q)
  v*=-1
  ans+=v
  heapq.heappush(Q,A[i])
  heapq.heappush(Q,A[i])

print(ans)

Solution (Python):
def chat_in_a_circle():
    N = int(input())
    A = list(map(int,input().split()))
    A.sort()
    res = A.pop(-1)
    t = len(A) % 2 == 0
    a = 0
    for i in range(len(A) // 2):
        a = A.pop(-1)
        res += a*2
    if t:
        res -= a
        
    print(res)

chat_in_a_circle()

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 = [int(x) for x in input().split()]

ans = 0
A.sort(reverse=True)

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

print(ans)

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

a.sort()
ans = a[-1]
j = -2
flag = 0
for i in range(n-2):
    ans += a[j]
    if flag:
        j -= 1
        flag = 0
    else:
        flag = 1

print(ans)

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




'''
a=[]
b=[]
for i in range():
    A, B = map(int, input().split())
    a.append(A)   
    b.append(B)'''


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

ans=0
from math import floor
for i in range(1,n):
    ans+=a[n-1-floor(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):
import heapq


def main():
    _ = int(input())
    A = sorted(list(map(int, input().split())), reverse=True)
    q = []
    heapq.heapify(q)
    ans = A[0]
    tmp_score = min(A[0], A[1])
    heapq.heappush(q, (-tmp_score, A[0], A[1]))
    heapq.heappush(q, (-tmp_score, A[1], A[0]))

    for a in A[2:]:
        g = heapq.heappop(q)
        ans += -g[0]
        tmp_score1 = min(a, g[1])
        tmp_push1 = (-tmp_score1, a, g[1])

        tmp_score2 = min(a, g[2])
        tmp_push2 = (-tmp_score2, a, g[2])

        heapq.heappush(q, tmp_push1)
        heapq.heappush(q, tmp_push2)

    print(ans)


if __name__ == '__main__':
    main()

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(sorted(map(int,input().split())))[::-1]
ans = 0
for x in range(1,n):
    ans += a[x//2]
print(ans)

Solution (Python):

def main():
    N = int(input())
    a = list(map(int, input().split()))
    a.sort(reverse = True)
    ans = a[0]
    t = N - 2

    for i in range(1, N):
        for _ in range(2):
            if t > 0:
                ans += a[i]
                t -= 1
    print(ans)
        

if __name__ == "__main__":
    main()

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

a.sort(reverse=True)

ans=a[0]
ch=1

for i in range(1,n):
  if ch==n-1:
    break
  else:
    ans+=a[i]
    ch+=1
    
  if ch==n-1:
    break
  else:
    ans+=a[i]
    ch+=1
    
print(ans)

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

A.sort(reverse=True)

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

    if len(score) >= N:
        break

print(sum(score[:N-1]))

Solution (Python):
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):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
from bisect import bisect, bisect_left, bisect_right

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()

A.sort(reverse=True)
ans = A[0]
cnt = 1

for x in A[1:]:
    if cnt == N - 1:
        break
    if cnt == N - 2:
        ans += x
        cnt += 1
        
    else:
        ans += 2 * x
        cnt += 2
print(ans)

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

a.sort()

arrival = 1
ans = 0

while arrival < N:
    score = a.pop()
    if arrival == 1:
        ans += score
        arrival += 1
    else:
        ans += score*min(2, N - arrival)
        arrival += 2

print(ans)

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

a.sort()

arrival = 1
ans = 0

while arrival < N:
    score = a.pop()
    if arrival == 1:
        ans += score
        arrival += 1
    else:
        ans += score*min(2, N - arrival)
        arrival += 2

print(ans)

Solution (Python):
from collections import deque

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

A.sort(reverse=True)

a = deque(A)

arrival = 1
ans = 0

while arrival < N:
    score = a.popleft()
    if arrival == 1:
        ans += score
        arrival += 1
    else:
        ans += score*min(2, N - arrival)
        arrival += 2

print(ans)

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

Solution (Python):
import heapq

n=int(input())
a=list(map(int,input().split()))
a=list(map(lambda x : x*(-1),a))

heapq.heapify(a)
total=0
total+=(-1)*heapq.heappop(a)

if n%2==1 :
    for i in range(1,n//2+1) :
        if i==n//2 :
            total+=(-1)*heapq.heappop(a)
        else :
            total+=(-2)*heapq.heappop(a)
    print(total)

else :
    for i in range(1,n//2) :
        total+=(-2)*heapq.heappop(a)
    print(total)

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

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

Solution (Python):
from collections import deque

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

q = deque()
q.append(a[0])
ans = 0
for i in range(1, n):
    b = q.popleft()
    ans += b
    b = min(b, a[i])
    for _ in range(2):
        q.append(b)
print(ans)

Solution (Python):
m=len(bin(n:=int(input())))-4;A=sorted([*map(int,input().split())])[::-1];print(2*sum(A[:(x:=2**m-(-n+2**-~m)//2)])-A[0]-n%2*A[x-1])

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

lst.sort(reverse=True)
if n % 2 == 0:
  a = n//2-1
  s = lst[0]
  for i in range(a):
    s += 2*(lst[i+1])
  
else:
  b = (n-1) //2
  s = lst[0]
  for j in range(b):
    s += 2*(lst[j+1])
  s -= lst[b]
  
print(s)

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

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

Solution (Python):
from collections import deque

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

q = deque()
q.append(a[0])
ans = 0
for i in range(1, n):
    b = q.popleft()
    ans += b
    b = min(b, a[i])
    for _ in range(2):
        q.append(b)
print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))*2
A.sort()
print(sum(A[N:-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):
import math
import sys
import os
from operator import mul

sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")

N = I()
A = LI()

A.sort(reverse=True)
half, isOdd = divmod(len(A),2)

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

if isOdd == 1:
    ans += A[half]

print(ans)

Solution (Python):
import math
import sys
import os
from operator import mul

sys.setrecursionlimit(10**7)

def _S(): return sys.stdin.readline().rstrip()
def I(): return int(_S())
def LS(): return list(_S().split())
def LI(): return list(map(int,LS()))

if os.getenv("LOCAL"):
    inputFile = basename_without_ext = os.path.splitext(os.path.basename(__file__))[0]+'.txt'
    sys.stdin = open(inputFile, "r")
INF = float("inf")

N = I()
A = LI()

A.sort(reverse=True)


q, mod = divmod(len(A),2)

if mod == 0:
    ans = A[0]
    for i in range(1,q):
        ans += 2 * A[i]
else:
    ans = A[0]
    for i in range(1,q):
        ans += 2 * A[i]
    ans += A[q]

print(ans)

Solution (Python):
def main():
	N = int(input())
	friendliness = [int(a) for a in input().split(" ")]

	friendliness.sort(reverse=True)
	put = 1
	comfort = 0
	for i in range(len(friendliness)):
		if i == 0:
			put += 1
			comfort += friendliness[0]
		elif put + 2 <= len(friendliness):
			put += 2
			comfort += 2 * friendliness[i]
		else:
			comfort += (len(friendliness) - put) * friendliness[i]
			break

	print(comfort)

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[(i + 1) // 2]
    
print(ans)

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



n = int(input())
lis = sorted(map(int, input().split()), reverse=True)
ans = lis[0]
i = 1
c = 1
while c<n-1:
	if c < n-1: ans += lis[i]; c+=1
	if c < n-1: ans += lis[i]; c+=1
	i+=1
print(ans)

Solution (Python):
import queue

def main():
    n = int(input())
    A = list(map(int,input().split()))
    A = sorted(A,reverse=True)
    q = queue.PriorityQueue()
    q.put([-1*A[0],-1*A[0]])
    ans = 0
    for a in A[1:]:
        _min, _max = map(lambda x:-1*x,q.get())
        ans += _min
        q.put([-1*a,-1*_min])
        q.put([-1*a,-1*_max])
    print(ans)
    return 

if __name__ == "__main__":
    main()

Solution (Python):
import sys
import numpy as np
from math import ceil as C, floor as F, sqrt
from collections import defaultdict as D, Counter as CNT
from functools import reduce as R
import heapq as H
  
ALP = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
alp = 'abcdefghijklmnopqrstuvwxyz'
def _X(): return sys.stdin.readline().rstrip().split(' ')
def _S(ss): return tuple(ss) if len(ss) > 1 else ss[0]
def S(): return _S(_X())
def Ss(): return list(S())
def _I(ss): return tuple([int(s) for s in ss]) if isinstance(ss, tuple) else int(ss)
def I(): return _I(S())
def _Is(ss): return list(ss) if isinstance(ss, tuple) else [ss]
def Is(): return _Is(I())
def rec(h, w): return [[0] * w for i in range(h)] 

n = I()
xs = Is()

xs = sorted(xs, reverse=True)

seats = [-xs[0]]
H.heapify(seats)

ans = 0
for x in xs[1:]:
  ans -= H.heappop(seats)
  H.heappush(seats, -x)
  H.heappush(seats, -x)

print(ans)

Solution (Python):
n = int(input())
f = list(map(int,input().split()))
f.sort(reverse = True)
ans = 0
if n%2 == 0:
    for i in range(n//2):
        ans += f[i] * 2
    ans -= f[0]
else:
    for i in range(n//2):
        ans += f[i] * 2
    ans += f[n//2]-f[0]

print(ans)

Solution (Python):
import math

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

for i in range(1,n):
    ans += f[n-math.floor(i/2)-1]

print(ans)

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

Solution (Python):
def main():
    N = int(input())
    A = list(map(int, input().split()))
    A = sorted(A)
    A = A[::-1]

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

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort(reverse=True)
    ans = A[0] + sum(A[i//2+1] for i in range(N-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]
num = 2
idx = 1
while num<n:
    if num+1==n:
        ans += a[idx]
        num += 1
    else:
        ans += a[idx]*2
        num += 2
        idx += 1
print(ans)

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

Solution (Python):
n=int(input())
arr=[int(i) for i in input().strip().split(" ")]
arr=sorted(arr)
arr=arr[::-1]

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.sort(reverse=True)
cnt = n-2
ans = a[0]
for i in a[1:]:
  if cnt==0: break
  elif cnt>=2:
    cnt -= 2
    ans += (i*2)
  else:
    cnt -=1
    ans += i
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(2,N):
    ans += A[j]
    if i % 2 == 1:
        j += 1

print(ans)

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

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

alist = sorted(alist,reverse=True)
ans = alist[0]
count = 1
flag = True
for i in range(n-2):
    if flag:
        ans += alist[count]
        flag = False
    else:
        ans += alist[count]
        count += 1
        flag = True

print(ans)

Solution (Python):


N=int(input())

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

print(sum(A[1:N]))