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

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

Solution (Python):
from collections import deque

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

ans, cnt = 0, deque([a[0]])
for i in a[1:]:
    ans += cnt.popleft()
    cnt.extend([i,i])
print(ans)

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

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

Solution (Python):
import heapq as heapy
n = int(input())
arr = [int(x) for x in input().split()]
arr.sort(reverse=True)
friendliness = 0
circle = [arr[0]]
best = [[-arr[0], (arr[0], arr[0])]]
heapy.heapify(best)
i = 1
while i != n:
    p = heapy.heappop(best)
    left = p[1][0]
    right = p[1][1]
    p1 = [-min(left, arr[i]), (left, arr[i])]
    p2 = [-min(right, arr[i]), (arr[i], right)]
    heapy.heappush(best, p1)
    heapy.heappush(best, p2)
    friendliness += -p[0]
    i += 1
print(friendliness)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
A = sorted(A,reverse=True)
ans = 0
L = []
for i in range(N//2+1):
    if i == 0:
        L.append(A[i])
    elif i % 2 == 0:
        L.append(A[i])
        ans += min(L[0],L[-2])
    else:
        L.insert(0,A[i])
        ans += min(L[1],L[-1])
if N % 2 == 1:
    ans += sum(A[1:N//2+1])
else:
    ans += sum(A[1:N//2])
print(ans)

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

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

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

Solution (Python):
n,*a=map(int,open(0).read().split())
a.sort()
a=a[::-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()))
aa = a + a
ans = 0
aa.sort(reverse = True)
for i in range(1,n):
    ans += aa[i]
print(ans)

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

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

ctr = n-1
idx = 0
res = 0

while ctr > 0:
    if idx == 0:
        ctr -= 1
        res += a[idx]
    else:
        ctr -= 2
        if ctr == -1:
            res += a[idx]
        else:
            res += a[idx]*2
    idx += 1

print(res)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

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

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

print(ans)

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


def readstr():
    return input().strip()


def readint():
    return int(input())


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


def readstrs():
    return input().split()


def main():
    N = readint()
    A = sorted(list(readnums()), reverse=True)

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

    print(ans)


if __name__ == "__main__":
    main()

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

Solution (Python):
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
n = int(input())
a = sorted(list(map(int, input().split())))
print(2*sum(a[n//2:])-a[-1]-(a[n//2] if n%2 else 0))

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

Solution (Python):
import sys
sys.setrecursionlimit(300000)
from heapq import heappush, heappop

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI0(): return map(lambda s: int(s) - 1, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
def LMI0(): return list(map(lambda s: int(s) - 1, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


N = I()
A = LMI()
A.sort(reverse=True)

ans = 0
q = []
for i, a in enumerate(A):
    if i == 0:
        heappush(q, -a)
    else:
        m = -heappop(q)
        ans += m
        heappush(q, -a)
        heappush(q, -a)
print(ans)

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

for _ in range(n-1):
    ans+= A.pop()
print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
ans=a[0]
del a[0]

num=0
n-=2

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

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

ans = A.pop()
A.pop()
for _ in range(N - 2):
    ans += A.pop()

print(ans)

Solution (Python):
import heapq
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
ans = 0
tmp = []
heapq.heappush(tmp, -a[0])
for i in range(1, n):
    point = -heapq.heappop(tmp)
    ans += point
    heapq.heappush(tmp, -a[i])
    heapq.heappush(tmp, -a[i])
print(ans)

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

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

Solution (Python):
from sys import stdin, stdout, setrecursionlimit
from collections import deque, defaultdict, Counter
from heapq import heappush, heappop
import math

rl = lambda: stdin.readline()
rll = lambda: stdin.readline().split()
rli = lambda: map(int, stdin.readline().split())
rlf = lambda: map(float, stdin.readline().split())

INF, NINF = float('inf'), float('-inf')

def main():
	n = int(rl())
	A = list(rli())
	A.sort(reverse = True)
	comfort = A[0]
	cnt = 0
	for i in range(2, n):
		if i % 2 == 0:
			cnt += 1
		comfort += A[cnt]
	print(comfort)	

if __name__ == "__main__":
	main()

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
a = inpl()
a.sort(reverse = True)
res = 0
ind = 0
for i in range(1,n):
    res += a[ind]
    if i%2:
        ind += 1
print(res)

Solution (Python):
from sys import stdin
input = stdin.readline
X = int(input())
x_list = list(map(int, input().split()))
a = sorted(x_list,reverse = True)
ans = 0
for i in range(1,X):
  ans += a[i//2]
print(ans)

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())
l=list(map(int,input().split()))
d=[]
l.sort(reverse=True)
d.append(l[0])
for i in range(1,n):
    d.append(l[i])
    d.append(l[i])
max=0
for i in range(n-1):
    max+=d[i]
print(max)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)  
B = []  
cnt = 0
for a in A:
    if len(B) != 0:
        j = len(B) // 2  
        cnt += B[j]
    B.append(a)
print(cnt)

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

Solution (Python):
import heapq

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

Q = [[-A[1],-A[0]]]*2
heapq.heapify(Q)

ans = A[0]
for a in A[2:]:
    
    top = heapq.heappop(Q)
    ans += -top[0]

    heapq.heappush(Q, [-a,-top[0]])
    heapq.heappush(Q, [-a,-top[1]])
    
    
print(ans)

Solution (Python):
import heapq

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

Q = [[-A[1],-A[0]]]*2
heapq.heapify(Q)

ans = A[0]
for a in A[2:]:
    
    top = heapq.heappop(Q)
    ans += -top[0]

    heapq.heappush(Q, [-a,-top[0]])
    heapq.heappush(Q, [-a,-top[1]])
    
    
print(ans)

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

Solution (Python):
def main():
  N=int(input())
  A=list(map(int,input().split()))
  A.sort(reverse=True)
  ans=0
  for i in range(1,len(A)):
    ans+=A[i//2]
  print(ans)
  pass
if __name__ == '__main__':
  main()

Solution (Python):



import copy
from collections import deque

N = int(input())
A = list(map(int, input().split()))
A.sort(reverse = True)
d = deque()
c = 0
count = 2
ans = 0

if N == 2:
    print(A[0])
elif N == 3:
    print(A[0] + A[1])
else:
    ans = A[0] + A[1]
    for i in range(3, N-1):
        if c == 0:
            ans += A[count]
            c += 1
        elif c == 1:
            ans += A[count]
            count += 1
            c = 0
        
    ans += A[1]
    print(ans)

Solution (Python):
import copy
from collections import deque

N = int(input())
A = list(map(int, input().split()))
A.sort(reverse = True)
d = deque()
c = 0
count = 2
ans = 0

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

Solution (Python):
import heapq

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

ans = 0
q = []
heapq.heappush(q, -arr[0])
for i in range(1,n):
    tmp = heapq.heappop(q)*(-1)
    ans += tmp
    heapq.heappush(q, -arr[i])
    heapq.heappush(q, -arr[i])
print(ans)

Solution (Python):
n,*a=map(int,open(0).read().split())
a.sort(reverse=True)
p=[]
for i in range(n):
    p.append(a[i])
    p.append(a[i])
print(sum(p[:n])-p[0])

Solution (Python):

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

Solution (Python):
n = int(input())
lis = sorted(list(map(int, input().split())),reverse=True)
idx = 0
res = lis[0]
while True:
    if idx >= n-2:
        break
    res += lis[1+idx//2]
    idx += 1
print(res)

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

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

print(ans)

Solution (Python):
import numpy as np
import heapq

N = int(input())
A =  np.sort([int(x) for x in input().split(" ")])[::-1]

q = [(-A[1], (0, 1)), (-A[1], (0, 1))]
confort = A[0]
heapq.heapify(q)

i = 2
while N > i:
    m = heapq.heappop(q)
    
    confort -= m[0]
    heapq.heappush(q, (-A[i], (i, m[1][0])))
    heapq.heappush(q, (-A[i], (i, m[1][1])))
    i += 1
print(confort)

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

Solution (Python):

import sys
import collections


def II(): return int(sys.stdin.readline())


def MI(): return map(int, sys.stdin.readline().split())


def LI(): return list(map(int, sys.stdin.readline().split()))


def main():
    N = II()
    As = LI()
    As.sort()
    As.reverse()
    As = collections.deque(As)
    ans = []
    tmp = As.popleft()
    ans.append(tmp)

    for i in range(len(As)):
        tmp = As.popleft()
        ans.extend([tmp, tmp])

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




main()

Solution (Python):
from collections import deque

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

A.sort(reverse=True)

dp=deque([A[1]])
point=A[0]
cnt=0
for n in range(2,len(A)):
    if cnt==0:
        p=dp.popleft()
        cnt+=1
        dp.append(A[n])
        point+=p
    else:
        cnt=0
        dp.append(A[n])
        point+=p

print(point)

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)

Solution (Python):
import heapq
N = int(input())
arr = list(map(int,input().split()))
arr = sorted(arr, reverse = True)
ans = 0
q = []
heapq.heappush(q, -arr[0])

for n in range(1, N):
    tmp  = heapq.heappop(q)
    tmp *= -1

    ans += tmp  
    heapq.heappush(q, -arr[n])
    heapq.heappush(q, -arr[n]) 
print(ans)

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

Solution (Python):
from heapq import *
n = int(input())
A = [*map(lambda x: -int(x), input().split())]
heapify(A)
ans = -heappop(A)
for c in range(0, n-2, 2): ans += -heappop(A) * (1<<(c+1 < n-2))
print(ans)

Solution (Python):
from collections import defaultdict as dd
from collections import deque
import bisect
import heapq

def ri():
    return int(input())

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


def solve():
    n = ri()
    A = rl()
    A.sort(reverse=True)

    ans = 0
    pos = 0
    for i in range(1, len(A)):
        
        ans += A[pos]
        if i % 2 == 1:
            pos += 1
    print (ans)






mode = 's'

if mode == 'T':
    t = ri()
    for i in range(t):
        solve()
else:
    solve()

Solution (Python):
n =int(input())
a = [int(i) for i in input().split()]
a= sorted(a,reverse =True)
summ = a[0]
n-=2
i = 1
while(n>=2):
    summ+=2*a[i]
    i+=1
    n-=2
if n==1:
    summ+=a[i]
print(summ)

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

ret = 0
c_pointer = 0
comfort = []
tmp = A.pop()
comfort.append(tmp)

while len(A) > 0:
    tmp = A.pop()
    c_pointer += 1
    comfort.append(tmp)
    comfort.append(tmp)

print(sum(comfort[:c_pointer]))

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

Solution (Python):
from heapq import heapify, heappop,heappush
n=int(input())
a=list(map(int,input().split()))
a=[i*(-1) for i in a]
a.sort()

ans=0
q=[]
heappush(q,a[0])
for i in range(1,n):
    tmp = heappop(q)
    ans += tmp*(-1)
    heappush(q,a[i])
    heappush(q,a[i])
print(ans)

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

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

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

new=a+a

new=sorted(new, reverse=True)

ans=0

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

print(ans)

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

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

pq = []
res = 0
heapq.heapify(pq)

for i in range(n):
  if len(pq) == 0:
    heapq.heappush(pq, -a[i])
  else:
    score = -heapq.heappop(pq)
    res += score
    for _ in range(2):
      heapq.heappush(pq, -a[i])

print(res)

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

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

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 19
MOD = 10 ** 9 + 7
EPS = 10 ** -10

N = INT()
A = LIST()

A.sort(reverse=1)
que = [-A[0]]
ans = 0
for i in range(1, N):
    cur = -heappop(que)
    ans += cur
    heappush(que, -A[i])
    heappush(que, -A[i])
print(ans)

Solution (Python):
import sys
import queue

input_methods=['clipboard','file','key']
using_method=1
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=1000000007



def main():
	n = int(input())
	
	
	al = lin()
	al.sort(reverse = True)
	q = queue.PriorityQueue()
	q.put((-al[1], al[1]))
	q.put((-al[1], al[1]))
	score = al[0]
	for v in al[2:]:
		o, f = q.get()
		score += f
		q.put((-v, v))
		q.put((-v, v))
	return score
	

isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

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

Solution (Python):
import math
N = int(input())
Nlist = list(map(int, input().split()))
Nlist.sort(reverse=True)
x = math.ceil(N/2)
ans = sum(Nlist[0:x])*2-Nlist[0]
if N % 2 != 0:
    ans -= Nlist[x-1]
print(ans)

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

A = sorted(A)

conf = 0
for k in range(1, N):
    conf += A[N - 1 - k // 2]

print(conf)

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

N = int(readline().strip())
A = list(map(int, readline().split()))

A.sort(reverse=True)

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

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

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

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

cnt = a.pop()
tmp = a.pop()
l = 0
for i in range(n - 2):
    if l == 2:
        l = 0
        tmp = a.pop()
    cnt += tmp
    l += 1
print(cnt)

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

D()

Solution (Python):
N = int(input())
A = [int(_) for _ in input().split()]
ans = 0
AA = A + A
AA.sort(reverse=True)


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

print(ans)

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

Solution (Python):

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

a.sort(reverse=True)

q = [-a[0]]
heapq.heapify(q)
ans = 0
for i in range(1,n):
    t = heapq.heappop(q)
    ans += -t
    heapq.heappush(q,-a[i])
    heapq.heappush(q,-a[i])
    
print(ans)

Solution (Python):


from pprint import pprint
from bisect import bisect_left
import sys

sys.setrecursionlimit(10 ** 6)
INF = float('inf')


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






ans = 0
times = N - 1

for i in range(N):
    limit = 2
    if i == 0:
        limit = 1
    for _ in range(limit):
        if times > 0:
            ans += A[i]
            times -= 1

print(ans)

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


def main(input, print):
    n = int(input())
    a = list(map(int, input().split()))
    a.sort(reverse=True)
    ans = 0
    cnt = 0
    for i in range(n):
        lim = 1 if i == 0 else 2
        for j in range(lim):
            if cnt < n-1:
                ans += a[i]
                cnt += 1
    print(ans)


if __name__ == '__main__':
    main(sys.stdin.readline, print)

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

Solution (Python):
from collections import deque


def main():
    input()  
    array = [int(x) for x in input().split()]
    array.sort()
    coming = deque(array)

    edges = deque()
    noob = coming.pop()
    edges.append(noob)
    ans = 0
    while coming:
        ans += edges.popleft()
        noob = coming.pop()
        edges.append(noob)
        edges.append(noob)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque


def main():
    input()  
    coming = [int(x) for x in input().split()]
    coming.sort()
    coming = deque(coming)

    edges = deque()
    new = coming.pop()
    edges.append(new)
    ans = 0
    while coming:
        ans += edges.popleft()
        new = coming.pop()
        edges.append(new)
        edges.append(new)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque


def main():
    input()  
    array = [int(x) for x in input().split()]
    array.sort()
    coming = deque(array)

    edges = deque()
    new = coming.pop()
    edges.append(new)
    ans = 0
    while coming:
        ans += edges.popleft()
        new = coming.pop()
        edges.append(new)
        edges.append(new)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque


def main():
    input()  
    friendliness = [int(x) for x in input().split()]
    friendliness.sort()
    friendliness = deque(friendliness)

    edges = deque()
    popped = friendliness.pop()
    edges.append(popped)
    ans = 0
    while friendliness:
        ans += edges.popleft()
        popped = friendliness.pop()
        edges.append(popped)
        edges.append(popped)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque


def main():
    input()
    (edges := deque()).append(
        (friendliness := deque(sorted(int(x) for x in input().split()))).pop())
    ans = 0
    while friendliness:
        ans += edges.popleft()
        edges.append(popped := friendliness.pop())
        edges.append(popped)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque


def main():
    input()  
    friendliness = [int(x) for x in input().split()]
    friendliness.sort(reverse=True)
    edges = deque()
    edges.append(friendliness[0])
    ans = 0
    for fri in friendliness[1:]:
        ans += edges.popleft()
        edges.append(fri)
        edges.append(fri)
    print(ans)


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(1,n):
    ans+=a[i//2]
print(ans)

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


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

  max_ = A[0]
  max_iter = N-2
  iter_ = 0
  for i in range(1, N):
    for _ in range(2):
      if iter_ >= max_iter:
        print(max_)
        return
      max_ += A[i]
      iter_ += 1


if(__name__ == '__main__'):
  main()

Solution (Python):
from math import ceil

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

ans = 0

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


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

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

ans = 0
ans += a[0]

h = []
heapq.heapify(h)
heapq.heappush(h, -a[1])
heapq.heappush(h, -a[1])

for i in range(2, n):
    b = heapq.heappop(h)
    ans += (-b)
    heapq.heappush(h, -a[i])
    heapq.heappush(h, -a[i])

print(ans)

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

Solution (Python):

def resolve():
    N = int(input())
    A = sorted(map(int, input().split()), reverse=True)
    ans = 0
    t = N-1
    for i in range(N):
        lim = 2
        if i == 0:
            lim = 1
        for l in range(lim):
            if t > 0:
                ans += A[i]
                t -= 1
    print(ans)

if __name__ == "__main__":
    resolve()

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

A.sort(reverse=True)
wa=A[0]
i=0
amari=len(A)%2

while(1):

    if(amari==0):

        if(len(A)-1==1+2*i):
            print(wa)
            break
        wa=A[i+1]*2+wa


    elif(amari==1):
        if(len(A)-2==1+2*i):
            print(A[i+1]+wa)
            break
        wa=A[i+1]*2+wa

    i=i+1

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


resolve()

Solution (Python):

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

count = 0
result = 0

for i in A:
    count += 1
    if count == 1:
        result += i
    elif count < len(A):
        result += i * 2
        count += 1
    elif count == len(A):
        result += i
    else:
        break 

    
print(result)

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

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

Solution (Python):

l = int(input())

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

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

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

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

dic = {}
lis = []
for i in a:
    if i in dic:
        dic[i] = dic[i] + 1
    else:
        dic[i] = 1
        lis.append(i)

lis.sort()
lis.reverse()

ans = 0
n -= 1
ma = lis[0]
for i in lis:
    if i == ma:
        k = 2 * dic[i] - 1
    else:
        k = 2 * dic[i]
    if n - k >= 0:
        ans += i * k
    else:
        ans += i * n
        break
    n -= k

print(ans)

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

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

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

print(ans)

Solution (Python):
import sys
import heapq

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

N = ini()
A = inl()


def solve():
    A.sort(reverse=True)
    hp = [(-A[1], -A[0]), (-A[1], -A[0])]
    score = A[0]
    for i in range(2, N):
        asmall, abig = heapq.heappop(hp)
        score += -asmall
        heapq.heappush(hp, (-A[i], asmall))
        heapq.heappush(hp, (-A[i], abig))
    return score


print(solve())

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)  
B = []  
cnt = 0
for a in A:
    if len(B) != 0:
        j = len(B) // 2
        cnt += B[j]
    B.append(a)
print(cnt)

Solution (Python):
n = int(input())
arr = [int(x) for x in input().split()]
arr.sort()
arr = arr[::-1]
res, ans = [arr[0]], 0
for i in range(1, n - 1):
  res.append(arr[i])
  res.append(arr[i])
for i in range(n - 1):
  ans += res[i]
print(ans)

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
A.sort(reverse=True)
if N == 2:
    print(A[0])
else:
    tocome = N-2
    curval = A[0]
    i = 1
    while tocome > 1:
        curval += A[i]*2
        tocome -= 2
        i += 1
    if tocome == 1:
        curval += A[i]
    print(curval)

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

a.sort(reverse=True)

point = a[0]
cnt = 0
k = 1
for i in range(1, n-1):
    point += a[k]
    cnt += 1
    if cnt == 2:
        cnt = 0
        k += 1

print(point)

Solution (Python):
import os
import sys
from atexit import register
from io import BytesIO
sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
sys.stdout = BytesIO()
register(lambda: os.write(1, sys.stdout.getvalue()))
input = lambda: sys.stdin.readline().rstrip('\r\n')
raw_input = lambda: sys.stdin.readline().rstrip('\r\n')

n = int(input())
seq = sorted((int(x) for x in input().split()), reverse=True)
ans = seq[0]

i = 1
cnt = 2
cnt_in = 0
while cnt < n:
    ans += seq[i]
    cnt_in += 1
    if cnt_in == 2:
        cnt_in = 0
        i += 1
    cnt += 1

print(ans)

Solution (Python):
n, *lst = map(int, open(0).read().split())
lst.sort(reverse=1)
res = sum(lst[i//2] for i in range(1, n))
print(res)

Solution (Python):
from sys import stdin,stdout
def INPUT():return list(int(i) for i in stdin.readline().split())
import math
def inp():return stdin.readline()
def out(x):return stdout.write(x)
import math as M

n=int(input())
A=INPUT()
A.sort(reverse=True)
B=[0]*n
distinct=list(set(A))
distinct.sort(reverse=True)

ans=0
D={}
for ele in A:
    if ele in D:
        D[ele]+=1
    else:
        D[ele]=1

count=0
i=0
p=0
while(i<n):
    ele=distinct[p]
    count=D[ele]*2
    while(i<n and count>0):
        B[i]=ele
        count-=1
        i+=1
    p+=1



print(sum(B)-B[0])

Solution (Python):

def main():
    N, *A = map(int, open(0).read().split())
    A.sort(reverse=True)
    res = A[0] + 2 * sum(A[1 : N // 2]) + (A[N // 2] if N % 2 else 0)
    print(res)


if __name__ == "__main__":
    main()

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

ans += A[0]

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

print(ans)