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 heapq

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

comfort = A[0]
edges = [-A[1], -A[1]]

for ai in A[2:]:
    m = heapq.heappop(edges) * -1
    comfort += m
    heapq.heappush(edges, -ai)
    heapq.heappush(edges, -ai)
print(comfort)

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

lst = sorted(lst, reverse=True)

print(sum(lst[0:n//2]) + sum(lst[1:(n - n//2)]))

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

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
a.sort()
if n==2:
    print(a[-1])
else:
    su=a[-1]
    d=1
    for i in range(n-2,-1,-1):
        su+=2*a[i]
        d+=2
        if d==n-1:
            break
        elif d>n-1:
            su-=a[i]
            break
    print(su)

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
edge = {}
edge[0, 1] = min(A[0], A[1])
edge[1, 0] = min(A[0], A[1])
ans = A[0]
max_edge_list = [(0, 1), (1, 0)]
idx = 0
for i in range(2, N):
    x, y = max_edge_list[idx]
    ans += edge[(x, y)]
    
    edge[x, i] = A[i]
    edge[i, y] = A[i]
    max_edge_list.append((x, i))
    max_edge_list.append((i, y))
    
    idx += 1
print(ans)

Solution (Python):
n = int(input())
A = sorted(list(map(int, input().split())), reverse=True)
a = A.pop()
Res = [a]

ans = 0
flg = True
pointer = 0
for i in range(n-1):
    ans += A[pointer]
    if flg == True:
        pointer += 1
        flg = False
    else:
        flg = True
print(ans)

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

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

print(result)

Solution (Python):
from collections import deque

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


A.sort(reverse=True)

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

    print(ans)

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

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

def main(n,a):
    
    a.sort(reverse=True)
    
    sum=a[0]
    j=0
    k=1
    for i in range(2,n):
        sum+=a[k]
        
        j+=1
        if j==2:
            j=0
            k+=1
    return sum

if __name__=='__main__':
    n,a=readinput()
    ans=main(n,a)
    print(ans)

Solution (Python):
import math

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


num = math.ceil(N/2)

a = Asrt[:num]

if N % 2 == 0:
    print(sum(a) * 2 - a[0])
else:
    print(sum(a) * 2 - a[0] - a[-1])

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

sum = A[0]
index = 1

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

print(sum)

Solution (Python):
n = int(input())
li = list(map(int,input().split()))
li.sort(reverse=True)
ans = [li[0]] + li[1:]*2
ans.sort(reverse=True)

print(sum(ans[:n-1]))

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

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

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

idx0 = 0
idx1 = N // 2

idx2 = 1
idx3 = (N + 1) // 2

print(sum(B[idx0:idx1]) + sum(B[idx2:idx3]))

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

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

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

s = input().split()
A = []
for i in range(N):
  A.append(int(s[i]))
  
result = 0
A.sort(reverse=True)
result += A[0]
A.pop(0)
if N % 2 == 0:
  result += sum(A[:(int(N/2)-1)])*2
else:
  result += (sum(A[:(int((N-1)/2)-1)])*2 + A[int((N-1)/2)-1])
    
print(result)

Solution (Python):


N = int(input())
A = list(int(a) for a in input().split())

A.sort(reverse=True)

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

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

A.sort(reverse=True)
ans = 0
for i in range(1, N):
    index = i // 2
    ans += A[index]
print(ans)

Solution (Python):
N = int(input())
players = list(map(int, input().strip().split(" ")))

players.sort(reverse=True)
total = 0
for i in range(1,N):
    j = int(i/2)
    total += players[j]
print(total)

Solution (Python):
import sys
import numpy as np

readline = sys.stdin.readline
read = sys.stdin.read

n = int(input())
a = np.array(readline().split(), dtype=np.int64)
a.sort()
a = a[::-1]
m = -(-n//2)
ans = np.sum(a[:m])*2 - a[0] if n % 2 == 0 else np.sum(a[:m])*2 - a[0] - a[m-1]
print(ans)

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



wait = deque()
wait.append(A[0])
ans = 0
for a in A[1:]:
    
    ans += wait.popleft()
    wait.extend([a, a])
print(ans)

Solution (Python):
from collections import deque
n=int(input())
a=list(map(int,input().split()))
a.sort()
a=a[::-1]
ans=0
x=deque([a[0]])
nn=n//2
for i in range(1,nn+1):
  ans+=min(x[0],x[-1])
  x.append(a[i])
  x.appendleft(a[i])
x=ans-a[0]
if n%2==0:
  ans+=x
  print(ans)
else:
  ans+=x+a[nn]
  print(ans)

Solution (Python):
n = int(input())
comforts = list(map(int,input().split()))
newcomforts = sorted(comforts, reverse=True)
x = 0
if n%2 == 0:
  for i in range(1, int(n/2)):
    x += newcomforts[i]
  print(x * 2 + newcomforts[0])
else:
  for i in range(1, int((n-1)/2)):
    x += newcomforts[i]
  print(x*2 + newcomforts[0] + newcomforts[int((n+1)/2-1)])

Solution (Python):
def main():
    import sys
    N=int(sys.stdin.readline())
    A=list(map(int,sys.stdin.readline().split()))
    if N==2:
        print(A[0])
        return

    
    A.sort(reverse=True)
    score=A[0]
    count=1
    
    
    for a in A[1:]:
        score+=a
        count+=1
        if count==N-1:
            break

        score+=a
        count+=1
        if count==N-1:
            break
    
    print(score)

main()

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

a.sort()
a.reverse()

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

if n % 2 ==1:
    cnt += a[(n-2)//2 +1]
print(cnt)

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

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

A.sort(reverse = True)

tot = A[0]
cnt = 2
j = 0
for i in range(1,n):
    if n >= cnt+2:
        tot +=A[i]*2
        cnt += 2
        j = i
        
if n >= cnt + 1:
    tot += A[j+1]
    cnt += 1
        
print(tot)

Solution (Python):
n = int(input())
s = [int(i) for i in input().split()]
s.sort(reverse=True)
ans = s[0]

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

print(ans)

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

Solution (Python):
import heapq

N = int(input())
A = sorted(list(map(int, input().split())),reverse=True)
B = [0]
heapq.heapify(B)
conf = 0
ans = 0
for i, a in enumerate(A):
    conf = heapq.heappop(B)
    if i == 0:
        heapq.heappush(B, -a)
    else:
        for _ in range(2):
            heapq.heappush(B, -a)
    ans += (-1) * conf
print(ans)

Solution (Python):

import heapq as hq

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

rst = 0
circle = []
hq.heappush(circle, a[0])
for i in a[1:]:
    k = hq.heappop(circle)
    rst += k
    if k < i:
        hq.heappush(circle, i)
        hq.heappush(circle, i)
    else:
        
        hq.heappush(circle, i)
        hq.heappush(circle, k)

print(-rst)

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

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

A.sort(reverse=True)
ans = 0
if N >= 2:
    ans += A[0]

if N >= 3:
    idx = 1
    for i in range(N-2):
        ans += A[idx]
        if i % 2 == 1:
            idx += 1

print(ans)

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

A.sort(reverse=True)

A_total = A[0]
if N == 2:
    print(A_total)
    exit()

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

if N%2 == 1:
    A_total += A[i+1]

print(A_total)

Solution (Python):
from sys import stdin
import sys
import itertools
import array
import textwrap
import math

N = int(input())
A = list(map(int, input().split()))
A.sort()
ans = A[-1]
c = len(A)-2
temp = len(A)-2
d = 0
for i in range(c):
    ans += A[temp]
    d += 1
    if d == 2:
        d = 0
        temp = temp-1
print(ans)

Solution (Python):
n=int(input())
from collections import deque
a=list(map(int,input().split()))
a.sort()
a.reverse()
c=0
d=0
e=0
b=deque(a)
c+=b.popleft()
f=(n-2)//2
for i in range(f):
  e=b.popleft()
  c+=2*e
if n%2==1:
  e=b.popleft()
  c+=e
print(c)

Solution (Python):
n = int(input())
l =list(map(int, input().split())) 
l.sort()
list_a = []
list_m =[0]*n*2
for i in l[::-1]:
  list_a.append(i)
count =list_a[0]
for i in range(0,n):
  list_m[2*(i-1)] = list_a[i]
  list_m[2*(i-1)+1] = list_a[i]
  
for i in range(n-2):
  count+=list_m[i]

print(count)

Solution (Python):
from heapq import heapify, heappop, heappush


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

A.sort(reverse=True)

heap = []
heapify(heap)

val = 0
heappush(heap, -A[1])
val += A[0]

for i, a in enumerate(A[2:]):
    n = heappop(heap)
    val += -n

    if i == 0:
        heappush(heap, n)

    heappush(heap, -a)
    heappush(heap, -a)


print(val)

Solution (Python):
import sys

n = int(input().strip())
a = [int(x) for x in input().strip().split(' ')]
a = sorted(a, reverse=True)

if n == 2:
    print(a[0])
    exit(0)

sum = a[0] + a[1]
for i in range(n-3):
    if i % 2 == 0:
        sum += a[int(i/2+1)]
    else:
        sum += a[int(i/2+2)]

print(sum)

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

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

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

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

out = 0
A.sort(reverse=True)
for i in range(n):
    if (i + 1) * 2 <= n:
        out += A[i] * 2
    elif (i + 1) * 2 - 1 <= n:
        out += A[i]

print(out - A[0])

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
a=sorted(a,reverse=True)
space=[a[0]]
t=0
coco=0
for i in range(1,n):
    coco += space[t]
    t += 1
    space.append(a[i])
    space.append(a[i])
print(coco)

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

A.sort(reverse = True)

ans = A[0]

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

print(ans)

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

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

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


A.sort(reverse=True)
confort = A[0]

if (n % 2 == 0):
    for i in range(1, int(n/2)):
        confort += 2*A[i]
else:
    for i in range(1, int((n+1)/2)):
        confort +=2*A[i]
    confort -= A[int((n+1)/2-1)]


print(confort)

Solution (Python):
import sys

def resolve():
    inp = sys.stdin.readlines()
    inp = list(map(lambda x: x.strip(), inp))
    N = int(inp[0])
    A = list(map(int, inp[1].split(' ')))
    A.sort(reverse=True)
    num = 0
    for i in range(N-1):
        num += A[(i+1)//2]

    print(num)
    
resolve()

Solution (Python):
import math
import collections
import bisect
from collections import deque
from copy import copy, deepcopy


def main():
    N = int(input())
    A = list(map(int, input().split()))
    sortedA = list(reversed(sorted(A)))
    ans = 0
    ans += sortedA[0]
    deq = deque()
    deq.append([sortedA[0], sortedA[1]])
    deq.append([sortedA[1], sortedA[0]])

    for i in range(2, N):
        tmp = deq.popleft()
        ans += min(tmp)
        deq.append([tmp[0], sortedA[i]])
        deq.append([sortedA[i], tmp[1]])

    print(ans)


if __name__ == '__main__':
    main()

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

A.sort()
from collections import deque

pot = deque([A.pop()])
com = 0
while A:
    com += pot.pop()
    new = A.pop()
    pot.appendleft(new)
    pot.appendleft(new)

print(com)

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

Solution (Python):








def solve(N, As):
    import math
    As = sorted(As, reverse=True)
    ans = As[0]
    for i in range(2, N):
        ans += As[math.ceil((i + 1) / 2) - 1]
    print(ans)



if __name__ == '__main__':
    
    N = int(input())
    
    As = [int(i) for i in input().split()]
    
    solve(N, As)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
aa=[]
for i in a:
  aa.append(i)
  aa.append(i)
g=aa.pop(0)
ans=0

for i in range(n-1):
  ans+=aa[i]
print(ans)

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.sort(reverse=True)

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

a.sort(reverse = True)

ans = 0
ans += a[0]
i = 1
j = 1
while i < n-1 :
    if i+2 <= n-1 :
        ans += 2*a[j]
        j += 1
        i += 2
    else :
        ans += a[j]
        j += 1
        i += 1

print(ans)

Solution (Python):
import sys

import math
import itertools
import random
def next(index, lis):
    for i in range(index, 0):
        if (i != 0):
            return i

n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
ans = a[0]
cnt = 1
tmp=1
while (True):
    if (cnt == n-1):
        break
    ans += a[tmp]
    cnt+=1
    if (cnt == n-1):
        break
    ans += a[tmp]
    cnt+=1
    tmp+=1
print(ans)

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

    cnt = 1
    ans = 0
    for x in A:
        if cnt == N: break
        if cnt == 1:
            ans += x
            cnt += 1
        else:
            if (use := N - cnt) >= 2:
                ans += x + x
                cnt += 2
            else:
                ans += x
                cnt += 1
                break
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
n=int(input())
a=sorted([int(i) for i in input().split()])[::-1]

if n==2:
    print(a[0])
else:
    b=deque([a[1],a[2]])
    ans=a[0]+a[1]
    minlist=[min(b)]
    j=1
    for i in range(n-3):
        if j==0:
            j+=1
            ans+=b[0]
            b+=[a[i+3]]
        else:
            j=0
            ans+=b[0]
            b.popleft()
            b+=[a[i+3]]

    print(ans)

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

A.sort(reverse=True)


score = A[0]
N -= 1

for i in A[1:]:
  N -= 1
  if(N==0):
    break
  score += i
  N -= 1
  if(N==0):
    break
  score += i
print(score)

Solution (Python):
import sys
read = sys.stdin.read
def main():
    n, *a = map(int, read().split())
    a.sort(reverse=True)
    r = a[0]
    end = int((n - 2) // 2)
    r += sum(a[1:end+1]) * 2
    if n % 2 == 1:
        r += a[end+1]
    print(r)

if __name__ == '__main__':
    main()

Solution (Python):

import sys
import math
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import itertools
import random
from collections import deque
from decimal import *
import queue

input = sys.stdin.readline

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007




def main():
	N = inputInt()
	A = inputList()

	A.sort()
	A = A[::-1]

	ansans = []
	pgt = queue.Queue()
	for i,val in enumerate(A):
		if i == 0:
			pgt.put(val)
		else:
			tmp = pgt.get()
			ansans.append(tmp)
			pgt.put(val)
			pgt.put(val)

	print(sum(ansans))




if __name__ == "__main__":
	main()

Solution (Python):

import sys


def solve(N: int, A: "List[int]"):
    A.sort(reverse=True)
    ans=0
    for i in range(N-1):
        ans+=A[(i+1)//2]
    print(ans)
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    A = [int(next(tokens)) for _ in range(N)]  
    solve(N, A)

if __name__ == '__main__':
    main()

Solution (Python):
import heapq
from collections import deque
import sys
input = sys.stdin.readline



def main():
    _ = int(input().strip())
    A_l = [-int(i) for i in input().strip().split()]
    heapq.heapify(A_l)

    used = deque()
    ans = 0
    
    m1 = -1 * heapq.heappop(A_l)
    used.append(m1)
    m2 = -1 * heapq.heappop(A_l)
    used.append(m2)
    used.append(m2)
    ans += used.popleft()

    while A_l:
        m = -1 * heapq.heappop(A_l)
        used.append(m)
        used.append(m)
        ans += used.popleft()
    return ans


if __name__ == "__main__":
    print(main())

Solution (Python):
import heapq

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

a.sort()
ans=0
b=[]
heapq.heapify(b)

if n==2:
    ans-=a[0]
else:
    ans-=sum(a[:2])
    
    heapq.heappush(b,a[1])
    for i in range(2,n-1):
        
        ans-=heapq.heappop(b)
        
        heapq.heappush(b,a[i])
        heapq.heappush(b,a[i])

print(ans)

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

max1 = K[0]
max2 = K[1]
if N == 2:
    print(max1)
else:
    max3 = K[2]
    count = max1 + max2
    cands = deque([max2, max3, max3])
    for k in K[3:]:
        cand = cands.popleft()
        count += cand
        cands.append(k)
        cands.append(k)
    print(count)

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

count = 0
arr.sort()
arr.reverse()
temp = arr.copy()
count +=arr[0]
arr.remove(max(arr))
x=n
n=n-2

if(n%2==0):
    for i in range(int(n/2)):
        count+=(2*arr[i])
    print(count)
else:
    for i in range(int((n-1)/2)):
        count+=(2*arr[i])
    count+=(temp[int(x/2)])
    print(count)

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

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

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

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

B=sorted(A,reverse=True)

ans=B[0]

sheets=[B[0]]

ans=0
for i in range(1,N):

    ans += sheets[i-1]
    sheets.append(B[i])
    sheets.append(B[i])

print(ans)

Solution (Python):
import sys

def II(): return int(input())
def MI(): return map(int,input().split())
def LI(): return list(map(int,input().split()))
def TI(): return tuple(map(int,input().split()))
def RN(N): return [input().strip() for i in range(N)]


def main():
    N = II()
    A = LI()
    As = sorted(A, reverse=True)
    ans = 0

    for i in range(N):
        j = i//2
        if i != 0:
            ans += As[j]
    
    print(ans)

if __name__ == "__main__":
	main()

Solution (Python):




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


a.sort()
a.reverse()

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

print(ans)

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

if N % 2 == 0:
  k = N // 2
  print(A_list[0] + 2*sum(A_list[1:k]))
else:
  k = N // 2
  print(A_list[0] + 2*sum(A_list[1:k]) + A_list[k])

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

a.sort(reverse=True)

point = a[0]

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

if n%2 == 1:
    point += a[i+1]

print(point)

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

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

print(ans)

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

Solution (Python):
import math
n = int(input())
a = list(map(int, input().split()))
list.sort(a, reverse = True)
c = math.ceil(n/2)
b = [2]*c + [0]*(n-c)    

b[0] -= 1
if n%2 == 1:
    b[c-1] -= 1

ans = 0
for i in range(n):
    ans += b[i]*a[i]
print(ans)

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

    A.sort(reverse = True)

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

    print(sum(B[:n-1]))


main()

Solution (Python):
import queue

N = int(input())

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

S.sort(reverse = True)



q = queue.Queue()
q.put(S[0])

ans = 0

for i in range(1, N):
    ans += q.get()
    
    q.put(S[i])
    q.put(S[i])

print(ans)

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

Solution (Python):
import heapq
def main():
    n = int(input())
    a = list(map(int,input().split()))
    a.sort()
    heap = []
    heapq.heappush(heap,-a.pop())
    total = 0
    while a:
        score = -heapq.heappop(heap)
        total += score
        temp = -a.pop()
        heapq.heappush(heap,temp)
        heapq.heappush(heap,temp)
    print(total)



if __name__ == '__main__':
    main()

Solution (Python):
import sys

def main():

    input = sys.stdin.readline

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

    ans = A[0]

    count = 2

    if count >= N:
        print(ans)
        return


    for i in range(1,N):
        count += 1
        ans += A[i]

        if count >= N:
            break

        count += 1
        ans += A[i]

        if count >= N:
            break

    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):


from collections import deque

input()
A = input().split(' ')
A = list(map(int, A))
A = sorted(A)[::-1]

d = deque()

ret = 0

for a in A:
    if len(d) == 0:
        d.append(a)
    else:
        ret += d.popleft()
        d.append(a)
        d.append(a)

print(ret)

Solution (Python):
from collections import deque

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

A.sort(reverse=True)
ans = 0

point = deque()
point.append(A.pop(0))
for i in A:
    ans += point.popleft()
    point.append(i)
    point.append(i)

print(ans)

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

Solution (Python):
import sys
import heapq
input = lambda: sys.stdin.readline().rstrip()
n = int(input())
a = [int(i) for i in input().split()]
a.sort()
hl = [a[-1] * -1]
a.pop()


heapq.heapify(hl)
ans = 0

while (a):
    now = a.pop()
    ans += heapq.heappop(hl) * (-1)
    for i in range(2):
        heapq.heappush(hl, now * -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(n-2):
    ans += A[(i//2) + 1]

print(ans)

Solution (Python):
import sys
import numpy as np
import random
from decimal import Decimal
import itertools
import re
import bisect
from collections import deque, Counter
from functools import lru_cache
from numba import jit

sys.setrecursionlimit(10**9)
INF = 10**13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
def SERIES(n): return np.fromstring(sys.stdin.buffer.read(), dtype=np.int32, sep=' ')
def GRID(h,w): return np.fromstring(sys.stdin.buffer.read(), dtype=np.int32, sep=' ').reshape(h,-1)[:,:w]
def GRIDfromString(h,w): return np.frombuffer(sys.stdin.buffer.read(), 'S1').reshape(h,-1)[:,:w]
MOD = 1000000007

def main():
	n = I()
	a_list = LI()
	a_list.sort()
	ans = a_list[-1]
	if n % 2 == 0:
		for i in range(1,(n-2)//2+1):
			ans += a_list[-1-i]*2
	else:
		for i in range(1,(n-3)//2+1):
			ans += a_list[-1-i]*2
		ans += a_list[-1-(n-3)//2-1]
	print(ans)


if __name__ == '__main__':
    main()

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

confortability = sorted(A,reverse=True)
neighbours = 0
idx = 0
ans = 0
for i, conf in enumerate(confortability):
    if i == 0:
        continue
    if neighbours < 1:
        ans += confortability[idx]
        neighbours += 1
    else:
        idx += 1
        ans += confortability[idx]
        neighbours = 0

print(ans)

Solution (Python):


def resolve():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip())
    a_list = [int(x) for x in input().rstrip().split(" ")]
    a_list.sort(reverse=True)
        
    from heapq import heappush, heappop

    hq = []
    joined = set()
    joined.add(0)
    heappush(hq, (-a_list[0], 0, 0))
    result = 0
            
    for i in range(1, n):

        comfort, left, right = heappop(hq)
        comfort = -comfort

        result += comfort

        heappush(hq, (-(a_list[left] if a_list[left] < a_list[i] else a_list[i]),left, i))
        heappush(hq, (-(a_list[right] if a_list[right] < a_list[i] else a_list[i]),i,right))
    
    print(result)


if __name__ == "__main__":
    resolve()

Solution (Python):

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

A.sort(reverse=True)

ans=A[0]

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

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

half = (N+1)//2
A.sort(reverse = True)

ans = sum(A[:(half-1)])

if (N-half) != half:
    ans += sum(A[1:half])
else:
    ans += sum(A[1:half]) + A[half-1]

print(ans)

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