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

from collections import deque

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

if n == 2:
    print(max(a))
    exit()

a.sort(reverse=True)

d = deque([a[1]]*2)

ans = a[0]
for i in a[2:]:
    d.append(i)
    d.append(i)
    ans += d.popleft()

print(ans)

Solution (Python):
n = int(input())
data = list(map(int, input().split()))
data.sort(reverse=True)
ans = data[0]
idx = 1
n -= 2
while n > 0:
    if n == 1:
        ans += data[idx]
    else:
        ans += data[idx]*2
    n-=2
    idx += 1
print(ans)

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

between=deque([a[0]])
score=0
for i in range(1,n):
  score+=between.popleft()
  between.extend([a[i],a[i]])
  
print(score)

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

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

print(ans)

Solution (Python):

import sys
input = sys.stdin.readline


def ceil(x):
    if x != int(x):
        x = int(x) + 1;
    return x;
    

def swaparr(arr, a,b):
    temp = arr[a];
    arr[a] = arr[b];
    arr[b] = temp


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


def nCr(n, k): 
    if(k > n - k): 
        k = n - k 
    res = 1
    for i in range(k): 
        res = res * (n - i) 
        res = res / (i + 1) 
    return int(res) 


def upper_bound(a, x, lo=0, hi = None):
    if hi == None:
        hi = len(a);
    while lo < hi:
        mid = (lo+hi)//2
        if a[mid] < x:
            lo = mid+1
        else:
            hi = mid
    return lo


def primefs(n):
    
    primes = {}
    while(n%2 == 0 and n > 0):
        primes[2] = primes.get(2, 0) + 1
        n = n//2
    for i in range(3, int(n**0.5)+2, 2):
        while(n%i == 0 and n > 0):
            primes[i] = primes.get(i, 0) + 1
            n = n//i
    if n > 2:
        primes[n] = primes.get(n, 0) + 1
    
    
    return primes


def power(x, y, p): 
    res = 1
    x = x % p  
    if (x == 0) : 
        return 0
    while (y > 0) : 
        if ((y & 1) == 1) : 
            res = (res * x) % p 
        y = y >> 1      
        x = (x * x) % p 
    return res 


def swap(a,b):
    temp = a
    a = b
    b = temp
    return a,b









def find(x, link):
    p = x;
    while( p != link[p]):
        p = link[p];
    
    while( x != p):
        nex = link[x];
        link[x] = p;
        x = nex;
    return p;




def union(x, y, link, size):
    x = find(x, link)
    y = find(y, link)
    if size[x] < size[y]:
        x,y = swap(x,y)
    if x != y:
        size[x] += size[y]
        link[y] = x


def sieve(n): 
    prime = [True for i in range(n+1)] 
    p = 2
    while (p * p <= n): 
        if (prime[p] == True): 
            for i in range(p * p, n+1, p):
                prime[i] = False
        p += 1
    return prime


MAXN = int(1e5 + 5)
def spf_sieve():
    spf[1] = 1;
    for i in range(2, MAXN):
        spf[i] = i;
    for i in range(4, MAXN, 2):
        spf[i] = 2;
    for i in range(3, ceil(MAXN ** 0.5), 2):
        if spf[i] == i:
            for j in range(i*i, MAXN, i):
                if spf[j] == j:
                    spf[j] = i;
    




def factoriazation(x):
    ret = {};
    while x != 1:
        ret[spf[x]] = ret.get(spf[x], 0) + 1;
        x = x//spf[x]
    return ret
    
    


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

def str_array():
    return input().strip().split();


MOD = int(1e9)+7;
CMOD = 998244353;
INF = float('inf'); NINF = -float('inf');







n = int(input()); a = sorted(int_array(), reverse = 1);
ans = a[0]; i = 1; n -= 2;
while(n):
    if n == 1:
        ans += a[i];
        n -= 1; continue;
    ans += 2 * a[i];
    i += 1; n -= 2;
print(ans);

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

Solution (Python):

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

def calculate(n, arr):
    arr = sorted(arr, reverse=True)
    ans = 0

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

        if endIndex < (n-1):
            endIndex += 1
            ans += arr[i]
        else:
            break

        if endIndex < (n-1):
            endIndex += 1
            ans += arr[i]
        else:
            break

    print(ans)


calculate(N, ARR)

Solution (Python):
conf = 0
num_char = int(input())
char = list(map(int, input().split()))
char.sort(reverse=True)
count = 0
conf_int = 0
for i in range(num_char):
  if count == 0:
    count = 1
  elif count == 1:
    count = 2
    conf = char[conf_int]
    count_int = 0
    conf_int = 1
  else:
    if count_int == 0 :
      conf += char[conf_int]
      count_int = 1
    else:
      conf += char[conf_int]
      conf_int += 1
      count_int = 0
print(conf)

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

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

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

def main():
    N = int(readline())
    A = [int(i) for i in readline().split()]
    A.sort(reverse=True)

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


if __name__ == "__main__":
    main()

Solution (Python):
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n=ni()
a=sorted(nl())[::-1]
 
ans=0

cur=0
now=1
for i in range(n-1):
  ans+=a[cur]
  if now==1:
    now=0
    cur+=1
  elif now==0:
    now=1


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) 
 tmp*=-1
 ans+=tmp 
 heapq.heappush(q,-arr[i]) 
 heapq.heappush(q,-arr[i])
print(ans)

Solution (Python):
import sys
from collections import deque
 
read = sys.stdin.buffer.read
 
N, *A = map(int, read().split())
A.sort(reverse=True)
 
deq = deque([A[0]])
ans = 0
 
for x in A[1:]:
    ans += deq.popleft()
    deq.append(x)
    deq.append(x)

print(ans)

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

ans = 0

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

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

ans = 0

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

Solution (Python):

def resolver(N, A):
    A.sort(reverse=True)
    ans = 0
    for i, _ in enumerate(A):
        if i == 0:
            continue
        elif i == 1:
            ans += A[0]
        else:
            j = (i // 2) 
            ans += A[j]
    
    return ans



def _resolver(N, A):
    A.sort(reverse=True)
    circle = []
    ans = 0

    for a in A:
        if len(circle) == 0:
            circle.append(a)
        elif len(circle) == 1:
            ans += circle[0]
            circle.append(a)
        else:
            idx = max(range(len(circle)), key=lambda i: min(circle[i-1], circle[i]))
            ans += min(circle[idx-1], circle[idx])
            circle.insert(idx, a)

    return ans

def main():

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


if __name__ == '__main__':
    main()

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

Solution (Python):
import heapq

N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
h = [-A[0]]
ans = 0
for i in range(1,N):
    ans -= heapq.heappop(h)
    heapq.heappush(h, -A[i])
    heapq.heappush(h, -A[i])
print(ans)

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

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

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

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

a.sort(reverse=True)

cnt = 1
ans = a[0]

if n == 2:
    print(ans)
    exit()

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

print(ans)

Solution (Python):
import sys

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

def solve():
    n = ni()
    a = sorted(nl(), reverse=True)
    c = a[0]
    n -= 2
    for x in a[1:]:
        if n == 0:
            break
        if n == 1:
            c += x
            n -= 1
        else:
            c += x * 2
            n -= 2
    print(c)
    return

solve()

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

Solution (Python):
N = int(input())
alist = list(map(int, input().split()))
alist.sort(reverse=True)
sumscore=alist[0]
for n in range(1,int(N/2)):
  sumscore = sumscore + alist[n]*2
if N%2==1:
  sumscore += alist[n+1]

print(sumscore)

Solution (Python):

n=int(input())
alist=list(map(int,input().split()))
blist=sorted(alist,reverse=True)
total=blist[0]
for i in range(1,int(n/2)):
  total+=blist[i]*2
if n%2:
  total+=blist[int(n/2)]
print(total)

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

A.sort(reverse=True)

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

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
A.sort(reverse=True)
C = A[0]
j=0
for i in range(1,N-1):
  if i%2 == 0:
    C = C+A[j]
  elif i%2 != 0:
    j=j+1
    C = C+A[j]
print(C)

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

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

A.sort(reverse=True)

ans=A[0]

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

if N%2 != 0:
    ans += A[N//2]
    
print(ans)

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

Solution (Python):
num=int(input())
s=[int(i) for i in input().split()]
s.sort(reverse=True)
array=[]
for i in range(num-1):
    if i==0:
        array.append(s[i])
    else:
        array.append(s[i])
        array.append(s[i])
point=0
for i in range(num-1):
    if i==0:
        point+=array[i]
    else:
        point+=array[i]
print(point)

Solution (Python):
num=int(input())
s=[int(i) for i in input().split()]
s.sort(reverse=True)
array=[]
for (i,j) in enumerate(s):
    if i==0:
        array.append(j)
    else:
        array.append(j)
        array.append(j)
point=0
for i in range(num-1):
    if i==0:
        point+=array[i]
    else:
        point+=array[i]
print(point)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
a.sort()
c = a[-1]
if n%2 == 0:
  n1 = n//2-1
  for i in range(n1):
    a1 = a[n-2-i]
    c += a1*2
else:
  n2 = n//2
  a3 = a[n-n2-1]
  c += a3
  for i in range(n2-1):
    a2 = a[n-2-i]
    c += a2*2
print(c)

Solution (Python):



def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort(reverse = True)
    ret = 0
    for i in range(1,N):
        ret += A[i//2]
    print(ret)

if __name__ == '__main__':
    main()

Solution (Python):
import heapq


def main():
    n = int(input())
    As = list(map(int, input().split()))
    As_sort = sorted(As, reverse=True)
    ans = 0
    q = []
    heapq.heappush(q, -As_sort[0])
    for i in range(1, n):
        tmp = heapq.heappop(q)
        tmp *= -1
        ans += tmp
        heapq.heappush(q, -As_sort[i])
        heapq.heappush(q, -As_sort[i])
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
def INT():
    return int(input())

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

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

N = INT()
A = LI()
A = A + sorted(A, reverse = True)[1 : ]
A.sort(reverse = True)
ans = 0

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

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

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

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import math

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

a_lst = sorted(a_lst, reverse=-1)
ans = 0
for i in range(1, n):
  ans += a_lst[math.floor(i / 2)]
print(ans)

Solution (Python):
import math
 
N = int(input())
A = list(map(int, input().split()))
A.sort()
A.reverse()
ans = 0
for i in range(1, N):
	ans += A[math.floor(i / 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)
    tmp *= -1
    ans += tmp
    heapq.heappush(q, -arr[i])
    heapq.heappush(q, -arr[i])
print(ans)

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

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

n = int(input())
a = sorted(LI())
ans = 0
for i in range(1, n):
    ans += a[n-1-(i//2)]
print(ans)

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

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

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

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

members.sort()
ans = 0
ans += members.pop()
for i in range(0, int((N-2)/2)):
    ans += members.pop()*2
if (N) % 2 == 1:
    ans += members.pop()

print(ans)

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


A.sort(reverse=True)
ans = A[0]
current_a = 1
cnt = 0
for i in range(n-2):
  cnt += 1
  ans += A[current_a]
  if cnt == 2:
    cnt = 0
    current_a += 1
print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split()))
aa = a + a
aa.sort()
aa.pop()
answer = 0
for i in range(n - 1):
  answer += aa.pop()
print(answer)

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

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

Solution (Python):
def main():
    n = int(input())
    friendliness = [int(x) for x in input().split()]
    friendliness.sort(reverse=True)
    gg = (friendliness[i // 2] for i in range(1, n))
    print(sum(gg))


if __name__ == '__main__':
    main()

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


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import time
import math
st = time.perf_counter()
def inpl():
    return list(map(int, input().split()))


N = int(input())
A = inpl()

A.sort()
A.reverse()

ans = 0
N -= 1
for i in range(len(A)):
	if i == 0: t = 1
	else : t = 2
	for _ in range(t):
		ans += A[i]
		N -= 1
		if N == 0:
			break
	if N == 0:
		break

print(ans)



ed = time.perf_counter()
print('time:', ed-st, file=sys.stderr)

Solution (Python):
N,*As=map(int,open(0).read().split());As.sort();print(As[-1]+sum(As[-2-i//2]for i in range(N-2)))

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

A.sort()
A.reverse()
ans = 0
ans += A[0]

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

ans += tmp
print(ans)

Solution (Python):
_MULTITEST = False

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

    a.sort()
    print(sum(
        a[n - (k//2) - 1] for k in range(1, n)
    ))


if __name__ == "__main__":
    t = (int(input()) if _MULTITEST else 1)
    for tt in range(t):
        solve()

Solution (Python):
import sys
import numpy as np

readline = sys.stdin.readline 

N = int(input())
A = np.array(readline().split(), np.int64)
A_sort = np.sort(A)[::-1] 

count = 0
limit = 1
t = 0
for i in range(N - 1):
    if i == 0:
        count += A_sort[0]
    else:
        count += A_sort[limit]
        t += 1
        if t % 2 == 0:
            limit += 1
        

print(count)

Solution (Python):
n = int(input())
a = sorted([int(i) for i in input().split()], reverse=True)
ans = 0
t = n - 1 
for i, v in enumerate(a):
  lim = 1 if i == 0 else 2 
  for j in range(lim):
    if t > 0:
      ans += a[i]
      t -= 1
print(ans)

Solution (Python):
"""
Atcoder Beginner Contest 173
D - Chat in a circle

""" 
n = int(input())
a = list(map(int,input().strip().split()))
a.sort(reverse=True)
ans = a[0]

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

print(ans)

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

ans = a[0]
cnt = 1
num = 0
for _ in range(n - 2):
    ans += a[cnt]
    if num == 0:
        num += 1
    else:
        num = 0
        cnt += 1
print(ans)

Solution (Python):
N, *A = map(int, open(0).read().split())
A.sort()
A = [0] + A
print(sum(A[N - (k // 2)] for k in range(1, N)))

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

Solution (Python):
from collections import deque
n=int(input())
a=sorted(list(map(int,input().split())),reverse=True)
if n==2:
	print(a[0])
	exit(0)
ans=a[0]
d=deque()
d.append(a[1])
d.append(a[1])
for i in range(2,n):
	x=a[i]
	ans+=d.popleft()
	d.append(x)
	d.append(x)
print(ans)

Solution (Python):
import sys
INF = 1 << 60
MOD = 10**9 + 7 
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
def resolve():
    n = int(input())
    A = list(map(int, input().split()))
    A += A
    A.sort()
    A.pop()
    ans = 0
    for _ in range(n - 1):
        ans += A.pop()
    print(ans)
resolve()

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

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
total=0
A.sort(reverse=True)
insert=N-1
for i in range(N):
    add=2
    if i==0:
        add=1
    for j in range(add):
        if insert>0:
            total+=A[i]
            insert-=1
print(total)

Solution (Python):
N=int(input())
a = list(map(int, input().split()))
a.sort(reverse = True)
n = N-2
i = 1
ans = a[0]
while n > 0:
    if n > 1:
        ans += a[i]*2
        i += 1
        n -= 2
    elif n == 1:
        ans += a[i]
        i += 1
        n -= 1
    else:
        pass
print(ans)

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

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

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

A.sort()

ans = 0
can = 1
will = n-1
for i in range(n-1):
    ans += A[will]
    can -= 1
    if can == 0:
        can = 2
        will -= 1
print(ans)

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

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

Solution (Python):
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

from collections import deque

n = ii()
a = li()

a.sort(reverse = True)

l = deque()
l.append(a[1])
l.append(a[1])

ans = a[0]

if n != 2:
    for i in range(2,n):
        ans += l.popleft()
        l.append(a[i])
        l.append(a[i])

print(ans)

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

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

Solution (Python):
from collections import deque
n=int(input())
A=list(map(int,input().split()))
A.sort()
A.reverse()
conf=[A[0]]
ans=0
con=deque(conf)
A.pop(0)


for elem in A:
    ans+=con.popleft()
    
    for _ in range(2):
        con.append(elem)

print(ans)

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

A.sort()
res = 0

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

print(res)

Solution (Python):
from collections import deque
n=int(input())
A=list(map(int,input().split()))
A.sort()
A.reverse()
conf=[A[0]]
ans=0
con=deque(conf)
A.pop(0)


for elem in A:
    ans+=con.popleft()
    
    for _ in range(2):
        con.append(elem)

print(ans)

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

Solution (Python):
import heapq


def get_tuple(a, b):
    if a > b:
        a, b = b, a
    return -a, b


def main(a, N):
    a.sort(reverse=True)
    h = []
    if N == 1:
        comfort = 0
    else:
        comfort = a[0]
        heapq.heappush(h, get_tuple(a[0], a[1]))
        heapq.heappush(h, get_tuple(a[0], a[1]))
        for i in range(2, N):
            val = heapq.heappop(h)
            comfort -= val[0]
            heapq.heappush(h, get_tuple(a[i], -val[0]))
            heapq.heappush(h, get_tuple(a[i], val[1]))
    print(comfort)


if __name__ == '__main__':
    N = int(input())
    a = list(map(int, input().split()))
    main(a, N)

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

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

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

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

A.sort(reverse = True)
ans = A[0]
count = 1
flg = False

if N == 2:
    print(ans)
    exit()
    
for i in range(1,N):
    for j in range(2):
        ans += A[i]
        count += 1
        if count >= N-1:
            flg = True
            break
    if flg == True:
            break
        
print(ans)

Solution (Python):
import collections

N = int(input())
Mylist = input().split()
Mylist = [int(s) for s in Mylist]
Mylist.sort()
ANS = 0
for i in range(1,N):
    ANS += Mylist[(N-1)-int(i/2)]

print(ANS)

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

A.sort(reverse = True)
ans = 0

from collections import deque
tmp = deque([A[0]])

for x in A[1:]:
    ans += tmp.popleft()
    tmp.append(x)
    tmp.append(x)
    
print(ans)

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

Solution (Python):
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    N = int(input())
    A = list(map(int, input().split()))

    A.sort(reverse=1)
    d, m = divmod(N, 2)
    n = [2] * (d + m)
    n[-1], n[0] = n[-1] - m, 1
    ans = 0
    for i, k in enumerate(n):
        ans += A[i] * k
    print(ans)


main()

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

Solution (Python):
import sys

input = sys.stdin.readline

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

A.sort(reverse=True)
ans = 0

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

print(ans)

Solution (Python):
n = int(input())
l=sorted(list(map(int,input().split())))
an=l[n-1]
x=l[n-2];cr=n-3;cc=0
for i in range(n-3,-1,-1):
  an+=x
  cc+=1
  if cc!=0 and cc%2==0:
    x=l[cr];cr-=1
  
print(an)

Solution (Python):
import sys
from collections import deque

input = sys.stdin.readline

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

A.sort(reverse=True)

deq = deque([A[0]])
ans = 0

for x in A[1:]:
    ans += deq.popleft()
    deq.append(x)
    deq.append(x)

print(ans)

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

def set_debug(debug_mode=False):
    if debug_mode:
        fin = open('input.txt', 'r')
        sys.stdin = fin


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


if __name__ == '__main__':
    

    
    t = 1

    for ti in range(1, t + 1):
        n = int(input())

        A = int_input()

        A.sort()

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

Solution (Python):
import sys
from collections import deque

read = sys.stdin.buffer.read

N, *A = map(int, read().split())
A.sort(reverse=True)

deq = deque([A[0]])
ans = 0

for x in A[1:]:
    ans += deq.popleft()
    deq.append(x)
    deq.append(x)

print(ans)

Solution (Python):

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

A.sort()

ans = 0
can = 1 
will = n-1 
for i in range(n-1):
    ans += A[will]
    can -= 1
    if can == 0:
        
        can = 2 
        will -= 1
print(ans)

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



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

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

res = A[0]
num = (n - 1) // 2
for i in range(1, num + 1):
    res += A[i] * 2

if n % 2 == 1:
    res -= A[num]

print(res)

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

readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))

n = readint()
a = readints()

a.sort(reverse=1)

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

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

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

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

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

print(ans)

Solution (Python):

import sys
from collections import deque

input = sys.stdin.readline


def S():
    return input().rstrip()


def I():
    return int(input())


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


n = I()
al = list(MI())

al.sort()
al.reverse()

ans = 0
q = deque()
for a in al:
    if q:
        ans += q.popleft()
        q.append(a)
        q.append(a)
    else:
        q.append(a)

print(ans)

Solution (Python):
import collections

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

q = collections.deque()
a.sort(reverse=True)

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

print(ans)

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

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

Solution (Python):
read = lambda:list(map(int, input().split()))

n = int(input())
a = read()
a.sort()
ans = 0
for i in range(1, n):
    ans += a[n - 1 - i // 2]
print(ans)

Solution (Python):
import heapq

N = int(input())

arr = [int(x) for x in 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)
    
    tmp *= -1
    ans += tmp
    
    heapq.heappush(q,-arr[i])
    heapq.heappush(q,-arr[i])
    
print(ans)