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

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


def read():
    N = int(input().strip())
    A = list(map(int, input().strip().split()))
    return N, A


def solve(N, A):
    A.sort(reverse=True)
    ans = 0
    for k in range(1, N):
        parent = k // 2
        ans += A[parent]
    return ans


if __name__ == '__main__':
    inputs = read()
    outputs = solve(*inputs)
    if outputs is not None:
        print("%s" % str(outputs))

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

sys.setrecursionlimit(1000000)
MOD = 10 ** 9 + 7
input = lambda: sys.stdin.readline().strip()
NI = lambda: int(input())
NMI = lambda: map(int, input().split())
NLI = lambda: list(NMI())
SI = lambda: input()


def make_grid(h, w, num): return [[int(num)] * w for _ in range(h)]


def main():
    N = NI()
    A = NLI()
    A = [a * (-1) for a in A]
    A.sort()
    C = []
    heapq.heapify(C)
    ans = 0
    for i, a in enumerate(A):
        if i == 0:
            heapq.heappush(C, a)
            continue

        ans -= heapq.heappop(C)
        heapq.heappush(C, a)
        heapq.heappush(C, a)
    print(ans)


if __name__ == "__main__":
    main()

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

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

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

alist.sort(reverse=True)

if n%2 == 0:
  result = alist[0] + 2*sum(alist[1:int(n/2)])
  print(result)
elif n%2 == 1:
  result = alist[0] + 2*sum(alist[1:int(n//2)]) + alist[n//2]
  print(result)

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

for i in range(1, n-1):
    cnt += As[(i+1)//2] 
print(cnt)

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

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

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

arr.sort(reverse=True)

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

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

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

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

if N%2==0:
    n = int((N-2)/2 + 1)
    ans = 2 * sum(Alist[:n]) - Alist[0]
    print(ans)

else:
    n = int((N-1)/2 + 1)
    ans = 2 * sum(Alist[:n]) - Alist[0] - Alist[n-1]
    print(ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
ans = A[0]
cur = 1
count = 1
while True:
    if count <= N - 3:
        ans += A[cur] * 2
        count += 2
        cur += 1
    elif count == N - 2:
        ans += A[cur]
        count += 1
    elif count == N - 1:
        break
print(ans)

Solution (Python):
n, *A = map(int, open(0).read().split())
A.sort(reverse=1)
if n % 2 == 0:
    print(sum(A[:n//2]) * 2 - A[0])
else:
    t = (n+1)//2
    print(sum(A[:t]) * 2 - A[0] - A[t-1])

Solution (Python):
import math
N = int(input())
As = list(map(int, input().split()))
As.sort(reverse = True)
ans = 0
if len(As) >=2:
    for n in range(1, N):
        m = math.floor(n/2)
        ans += As[m]
else:
    ans = 0
print(ans)

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

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

print(sum(ans))

Solution (Python):


import sys
input= lambda: sys.stdin.readline().rstrip()
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
def lispin(t=int):return list(pin(t))

import heapq
def resolve():
    N,=pin()
    A=[*pin()]
    A.sort(reverse=True)

    ans=0
    h=[]
    heapq.heappush(h,0)
    for i in range(1,N):
        temp=heapq.heappop(h)
    
        ans+=A[temp]
        heapq.heappush(h,i)
        heapq.heappush(h,i)
    print(ans)


resolve()

Solution (Python):
import sys
import math
from collections import defaultdict
from bisect import bisect_left, bisect_right

sys.setrecursionlimit(10**7)
def input():
    return sys.stdin.readline()[:-1]

mod = 10**9 + 7

def I(): return int(input())
def LI(): return list(map(int, input().split()))
def LIR(row,col):
    if row <= 0:
        return [[] for _ in range(col)]
    elif col == 1:
        return [I() for _ in range(row)]
    else:
        read_all = [LI() for _ in range(row)]
        return map(list, zip(*read_all))



N = I()
A = LI()

A.sort(reverse=True)
p = 0
c = 1
ans = 0
for i in range(1,N):
    ans += A[p]
    if c == 1:
        c = 0
        p += 1
    else:
        c = 1

print(ans)

Solution (Python):
n = int(input())
a = [int(i) for i in 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)
ans = a[0] + 2 * sum(a[1:n // 2])
if n % 2:
    ans += a[n // 2]
print(ans)

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

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

Solution (Python):
from math import *
n = int(input())
a = [int(i) for i in 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)

B = [A[0]]

for i in range(N-1):
    B.append(A[i+1])
    B.append(A[i+1])

B.sort(reverse=True)

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

Solution (Python):
from collections import deque

d= deque()

N = int(input())
L = map(int,input().split())
L = sorted(list(L))[::-1]
import heapq
ans = 0
ideal = []
ans += L[0]
del L[0]
heapq.heappush(ideal, -L[0])
heapq.heappush(ideal, -L[0])
del L[0]

for x in L:
    ans += -1 * heapq.heappop(ideal)
    heapq.heappush(ideal, -x)
    heapq.heappush(ideal, -x)

print(ans)

Solution (Python):
N = int(input())
S = sorted(list(map(int,input().split())),reverse=True)
kokoti = 0
if N == 1:
    print(0)
elif N==2:
    print(max(S))
else:
    kokoti += max(S)
    for x in range (1,N-1):
        if x%2 == 1:
            kokoti += S[x//2 + 1]
        else:
            kokoti += S[x//2]
    print(kokoti)

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

for i in range(1, n//2):
  ans += lst[i]*2
  
if n%2 == 1:
  ans += lst[n//2]
  
  
print(ans)

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

ans = 0

check_num = N // 2

ans += 2 * sum(A[:check_num]) - A[0]
if N & 1 == 1:
    ans += A[check_num]

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

import sys


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

    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):
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 collections
n = int(input())
a = list(map(int, input().split()))
a.sort()
a.reverse()
b = collections.Counter(a)
c = list(b.items())
now = 0
tnp = 1
l = 0
l += c[0][1]*2-1
if (l>=n-1):
    print(c[0][0]*(n-1))
    exit()
now = l*c[0][0]
while True:
    if (l+c[tnp][1]*2>=n-1):
        print(now + (n-l-1)*c[tnp][0])
        break
    else:
        l += c[tnp][1]*2
        now += c[tnp][0]*c[tnp][1]*2
        tnp += 1

Solution (Python):
import queue
import numpy as np
n = int(input())
aa = list(map(int, input().split()))
aa.sort(reverse=True)
aa = np.array(aa, dtype=int)*-1
q = queue.PriorityQueue()
q.put(aa[0])
cnt = 0
for i in aa[1:]:
    cnt += q.get()
    q.put(i)
    q.put(i)

print(-cnt)

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

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

ans = 0

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

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


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

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

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

a.sort(reverse=True)

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

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

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

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

ans = a[0]

tree = [a[1]]
for i, el in enumerate(a[2:]):
    tree.append(el)
    ans += tree[i//2]

print(ans)

Solution (Python):



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

n = int(input())
a = lmp()
a.sort(reverse=True)
ans = 0
u = [a[0]]
for i in range(1, n):
    u.append(a[i])
    u.append(a[i])
print(sum(u[:n-1]))

Solution (Python):
import sys
from collections import deque

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


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

    res = 0
    que = deque([A[0]])
    for i in range(1, n):
        a = que.popleft()
        res += a
        que.append(A[i])
        que.append(A[i])
    print(res)


if __name__ == '__main__':
    resolve()

Solution (Python):
import heapq

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

heapq.heapify(a)
ans=heapq.heappop(a)
n-=1

while True:
    n-=1
    if n==0:
        break
    temp=heapq.heappop(a)
    ans+=temp
    n-=1
    if n==0:
        break
    ans+=temp



print(-ans)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
A = list(reversed(sorted(A)))
new_A = []
for i in range(1, N//2+1):
    new_A.append(A[i])
    new_A.append(A[i])

new_A = [0] + [A[0]] + new_A

print(sum(new_A[:N]))

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

import collections
count=len(a)
ans=0
c = collections.Counter(a)
for i in c.keys():
    tmp = c[i]*2
    if count<=tmp:
        ans+=i*count
        break
    else:
        ans+=i*tmp
        count-=tmp
print(ans-max(a))

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

A = sorted(A,reverse=True)
ans = 0
ans += A[0]
if n % 2 == 0:
  j = n//2
  ans += sum(A[1:j])*2
  
else:
  j = n//2
  ans += sum(A[1:j])*2
  ans += A[j]
print(ans)

Solution (Python):
from sys import stdin
from sys import setrecursionlimit
from collections import Counter, deque, defaultdict
from math import floor, ceil
from bisect import bisect_left
from itertools import combinations
setrecursionlimit(100000)

INF = int(1e10)
MOD = int(1e9 + 7)

def main():
    from builtins import int, map
    N = int(input())
    A = list(map(int, input().split()))
    A.sort(reverse=True)
    total = 0
    for k in range(1, len(A)):
        
        total += A[k // 2]
    print(total)

if __name__ == '__main__':
    main()

Solution (Python):


import sys
stdin = sys.stdin
from collections import deque

def ns(): return stdin.readline().rstrip()
def ni(): return int(ns())
def na(): return list(map(int, stdin.readline().split()))

def main():
    n = ni()
    a = na()
    a.sort(reverse=True)
    apre = a[0]
    cnt = 1
    q = deque()
    ans = -a[0]
    f = 0
    for ai in a[1:]:
        if ai == apre:
            cnt += 1
        else:
            for i in range(cnt*2):
                q.append(apre)
            apre = ai
            cnt = 1
            f += (cnt+1)
        if f > n:
            break
    for i in range(cnt*2):
        q.append(apre)
    for i in range(n):
        ans += q.popleft()

    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
import math
n  =int(input())
a = [int(x) for x in input().split()]
a.sort(reverse=True)
ans = 0
i=0
ind = 0
while (i<n):
    if n-i == 1:
        ans+=a[ind]
        i+=1
    else:
        ans+=2*a[ind]
        i+=2
    ind+=1
    
ans-=a[0]
print(ans)

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

ans=a[0]
c=1
i=1
while c<n-1:
    ans+=a[i]
    c+=1
    if c>=n-1:
        break
    else:
        ans+=a[i]
        c+=1
    i+=1
print(ans)

Solution (Python):


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






n=I()
a=LI()
a.sort(reverse=True)


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

Solution (Python):
import queue

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

ans = A[0]
q.put( (A[0], A[1]) )
q.put( (A[0], A[1]) )
for i in range(2, len(A)):
  t = q.get()
  ans += min(t[0], t[1])
  q.put((t[0], A[i]))
  q.put((t[1], A[i]))
  
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.append(i)
    cnt.append(i)
print(ans)

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

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

ans = al[0]

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

print(ans)

Solution (Python):
import sys
def rs(): return sys.stdin.readline().rstrip()
def ri(): return int(rs())
def rs_(): return [_ for _ in rs().split()]
def ri_(): return [int(_) for _ in rs().split()]

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

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

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

print(ret)

Solution (Python):

from collections import defaultdict, deque, Counter, OrderedDict
import collections, heapq, itertools, bisect
import math, fractions
import sys, copy

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI1(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline().rstrip())
def LS(): return [list(x) for x in sys.stdin.readline().split()]
def S(): return list(sys.stdin.readline().rstrip())
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)]

sys.setrecursionlimit(1000000)
dire = [[1, 0], [0, 1], [-1, 0], [0, -1]]
dire8 = [[1, 0], [1, 1], [0, 1], [-1, 1], [-1, 0], [-1, -1], [0, -1], [1, -1]]
MOD = 1000000007

def main():
    N = I()
    A = sorted(LI(), reverse=True)
    q = []
    ans = A[0]
    q.append((-min(A[0], A[1]), A[0], A[1]))
    q.append((-min(A[0], A[1]), A[0], A[1]))


    for i in range(2, N):
        (minus_x, left, right) = heapq.heappop(q)
        ans += -minus_x
        heapq.heappush(q, (-min(left, A[i]), left, A[i]))
        heapq.heappush(q, (-min(right, A[i]), A[i], right))

    print(ans)


if __name__ == '__main__':
    main()

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

score = sum(A) - A[-1]

l = 0
r = N - 1
while True:
    r_score = A[r - 1]
    l_score = A[l + 1]
    if l_score > r_score:
        score += l_score - r_score
    else:
        break
    l += 1
    r -= 1
    if l >= r:
        break
print(score)

Solution (Python):
from collections import deque

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

A.sort(reverse=True)

comfort = 0
comfort_list = deque()

for a in A:
    if len(comfort_list) == 0:
        comfort_list.append(a)
    else:
        max_comfort = comfort_list.popleft()
        comfort += max_comfort
        comfort_list.append(min(a, max_comfort))
        comfort_list.append(min(a, max_comfort))

print(comfort)

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

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

def solve():
    cnt = 0
    ans = 0
    for i in range(N):
        c = 2 if i else 1
        for j in range(c):
            if cnt<N-1:
                ans += A[i]
            else:
                return ans
            cnt += 1
print(solve())

Solution (Python):
from collections import deque

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

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

print(ans)

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

Solution (Python):
import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N = int(readline())
A = list(map(int,readline().split()))
A = sorted(A,reverse=True)
if N % 2 == 0: 
    k = N // 2
    ans = sum(A[:k])*2-A[0]
else:
    k = N // 2 + 1
    ans = sum(A[:k])*2-A[0]-A[k-1]
print(ans)

Solution (Python):
import heapq

def main():
    n = int(input())
    arr = list(map(int,input().split()))
    arr.sort()
    total = 0
    heap = []
    while arr:
        x = arr.pop()
        if not heap:
            heapq.heappush(heap,(-x,x,x))
        else:
            val,left,right = heapq.heappop(heap)
            val = abs(val)
            total += val
            heapq.heappush(heap,(-min(x,left),left,x))
            heapq.heappush(heap,(-min(x,right),x,right))
            

    print(total)
        
    

main()

Solution (Python):

from collections import deque

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

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

comfort_queue = deque()
comfort_queue.append(A[0])

score=0
for i in range(1,N):
    comfort_queue.extend([A[i],A[i]])
    now_score = comfort_queue.popleft()
    score += now_score

print(score)

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

if n == 1:
    print(0)
    exit(0)

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

print(res)

Solution (Python):
import sys

sys.setrecursionlimit(1000000)

import bisect
import math
import collections
import heapq


def solve(N, As):
    As.sort()
    ans = []
    while len(ans) < N - 1:
        k = As.pop()
        if len(ans) == 0:
            ans.append(k)
        else:
            ans.append(k)
            ans.append(k)

    while len(ans) > N - 1:
        ans.pop()
    return sum(ans)





if __name__ == "__main__":
    N = int(input())
    As = list(map(int, input().split(" ")))
    print(solve(N, As))

Solution (Python):



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

    ans = A[0]
    rest = N-2
    i = 1
    while rest > 0:
        if i == len(A):
            break

        if rest >= 2:
            ans += 2 * A[i]
            rest -= 2
        else:
            ans += A[i]
            rest -= 1
        i += 1

    return ans


print(main())

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

A=sorted(A, reverse=True)
if N==2:
    print(A[0])
    exit()

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

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
stdin = sys.stdin


def ni(): return int(ns())


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


def naa(N): return [na() for _ in range(N)]


def ns(): return stdin.readline().rstrip()  


N = ni()

a_array = na()

a_array.sort(reverse=True)

ans = a_array[0]

idx = 1

depth = 1

start = 1
end = 2

while(N - 1 > idx):
    start = 2 ** (depth-1)
    end = 2 ** depth
    if idx + 2 ** depth < N:
        ans += sum(a_array[start:end]) * 2
        idx = idx + 2 ** depth
    else:
        rest = (N - 1) - idx
        ans += sum(a_array[start:(start+rest//2)]) * 2
        ans += (rest % 2) * a_array[start+rest//2]
        idx = N - 1
    depth += 1
    


print(ans)

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

Ali.sort(reverse=True)

p = sum(Ali[0:(N+1)//2])*2
p -= Ali[0]
if len(Ali)%2 == 1:
  p -= Ali[N//2]
  
print(p)

Solution (Python):
from collections import deque

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

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

if __name__ == '__main__':
    solve()

Solution (Python):
from heapq import *
import sys
from collections import *
from itertools import *
from decimal import *
import copy
from bisect import *
import math
sys.setrecursionlimit(4100000)
def gcd(a,b):
    if(a%b==0):return(b)
    return (gcd(b,a%b))
input=lambda :sys.stdin.readline().rstrip()
N=int(input())
A=list(map(int,input().split()))
A.sort()
X=A.pop()
N-=1
c=0
lst=deque()
count=1
while N:
    lst.append(A.pop())
    if count:
        count-=1
    else:
        X=lst.popleft()
        count=1
    c+=X
    N-=1
print(c)

Solution (Python):
from collections import deque
N = int(input())
A = list(map(int,input().rstrip().split(" ")))
A.sort(reverse = True)
ans = 0
que = deque()
que.append(A[0])
for i in range(1,len(A)):
    ans += que[0]
    que.popleft()
    que.append(A[i])
    que.append(A[i])
print(ans)

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

a.sort(reverse=True)

import heapq
heap=[a[0]*(-1)]

ans=0

for aa in a[1:]:
    tmp=heapq.heappop(heap)
    ans+=((-1)*tmp)
    heapq.heappush(heap,-1*aa)
    heapq.heappush(heap,-1*aa)

print(ans)

Solution (Python):
from sys import stdin, setrecursionlimit, stdout




def ii(): return int(stdin.readline())
def fi(): return float(stdin.readline())
def mi(): return map(int, stdin.readline().split())
def fmi(): return map(float, stdin.readline().split())
def li(): return list(mi())
def si(): return stdin.readline().rstrip()
def lsi(): return list(si())

res=['YES', 'NO']





test_case=1
while test_case:
    test_case-=1

    n=ii()
    a=li()
    a.sort()
    s=a[-1]
    i=n-2
    x=-1
    for j in range(n-2):
        s+=a[i]
        x+=1
        if x:
            x=-1
            i-=1
    print(s)

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

Solution (Python):
import sys
import math
import heapq
from collections import defaultdict, deque
from decimal import *
def r():
    return int(input())
def rm():
    return map(int,input().split())
def rl():
    return list(map(int,input().split()))

n=r()
a=rl()
a.sort(reverse=True)
b=[0,a[0]]
i=1
while len(b)<n and i<n:
    b.append(a[i])
    b.append(a[i])
    i+=1
print(sum(b[:n]))

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

A.sort(reverse=True)
result = 0

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

print(result)

Solution (Python):


import sys
sys.setrecursionlimit(300000)


def solve(N: int, A: "List[int]"):
    A.sort(reverse=True)
    ret = 0
    
    
    

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

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

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

s=a[0]
t=n//2

s+=sum(a[1:t])*2
if n%2!=0:
    s+=a[t]


print(s)

Solution (Python):
import queue

n=int(input())

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

A.sort(reverse=True)

q=queue.Queue()

q.put(A[0])

ans=0
for i in range(1,n):
    ans+=q.get()
    q.put(A[i])
    q.put(A[i])

print(ans)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
n = int(readline())
a = list(map(int,readline().split()))
a.sort(reverse=True)

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

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

print(total)

Solution (Python):
N = int(input())
K = sorted(map(int,input().split()),reverse=True)
A = []
for i in range(N-1):
    A.append(K[i+1])
    A.append(K[i])
B = sorted(A,reverse = True)
ans = sum(B[:N-1])
print(ans)

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

ls.sort()

ans = 0
if n%2 == 1:
    ans = sum(ls[-(-n//2):n-1]) * 2 + ls[n-1] + ls[n//2]
else:
    ans = sum(ls[n//2:n-1]) * 2 + ls[n-1]

print(ans)

Solution (Python):
n,*a=map(int,open(0).read().split())
a.sort()
ans=a.pop(-1)
n-=2
while n!=0:
    if n>1:
        ans+=a.pop(-1)*2
        n-=2
    if n==1:
        ans+=a.pop(-1)
        n-=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,len(a)):
    ans+=(a[(i)//2])
print(ans)

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

sm=lst[0]
sm+=2*sum(lst[1:n//2])
if n%2==1 : sm+=lst[n//2]

print(sm)

Solution (Python):
def main():

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

    A.sort(reverse = True)
    v = A[0]
    if N % 2 == 0:
        
        v += sum(A[1:1+(N-2)//2]) * 2
        
    else:
        v += sum(A[1:1+(N-2)//2]) * 2 + A[1+(N-2)//2]
    return v

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

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

A.sort(reverse=True)

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

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

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    N = I()
    A = sorted(LI())[::-1]
    c = [A[0]]
    for x in A[1:]:
        c.append(x)
        c.append(x)
    ans= 0
    for i in range(len(c)):
        ans += c[i]
        if i == N - 2:
            break
    print(ans)

main()