CodeEff/ECCO huggingface dataset:
# Original solution
N = int(eval(input()))
if N == 0:
    print((0))
    exit()
L = []
M = []

def bit_range(bit_length,maximum=0,minimal=0):
    for i in range(bit_length-1,-1,-1):
        if i % 2 == 0:
            maximum += pow(2,i)
        else:
            minimal += pow(-2,i)
    L.append(minimal)
    M.append(maximum)

for i in range(1,41):
    bit_range(i)

identify = []
for i in range(40):
    if i == 0:
        ap_list = [L[0],M[0]]
    elif i % 2 == 1:
        ap_list = [L[i],L[i-1]]
    elif i % 2 == 0:
        ap_list = [M[i-1],M[i]]
    identify.append(ap_list)

bit = [0]*40
for i in range(39,-1,-1):
    left = identify[i][0]
    right = identify[i][1]
    if i % 2 == 0 and left < N <= right:
        bit[39-i] = 1
        N -= pow(-2,i)
    elif i % 2 == 1 and left <= N < right:
        bit[39-i] = 1
        N -= pow(-2,i)

ans = "".join(str(e) for e in bit).lstrip("0")
print(ans)

# Optimized solution
N = int(eval(input()))
if N == 0:
    print((0))
    exit()

L = []
M = []

for length in range(1,41):
    if length % 2 == 0:
        l = -2 * pow(4,length//2) // 3
        m = (pow(4,length//2) - 1) // 3
    elif length % 2 == 1:
        l = -2 * (pow(4,(length-1)//2) - 1) // 3
        m = (pow(4,(length+1)//2) - 1) // 3
    L.append(l)
    M.append(m)

identify = []
for i in range(40):
    if i == 0:
        ap_list = [L[0],M[0]]
    elif i % 2 == 1:
        ap_list = [L[i],L[i-1]]
    elif i % 2 == 0:
        ap_list = [M[i-1],M[i]]
    identify.append(ap_list)

bit = [0]*40
for i in range(39,-1,-1):
    left = identify[i][0]
    right = identify[i][1]
    if i % 2 == 0 and left < N <= right:
        bit[39-i] = 1
        N -= pow(-2,i)
    elif i % 2 == 1 and left <= N < right:
        bit[39-i] = 1
        N -= pow(-2,i)

ans = "".join(str(e) for e in bit).lstrip("0")
print(ans)
# Original solution
N = int(eval(input()))
if N == 0:
    print((0))
    exit()

L = []
M = []

for length in range(1,41):
    if length % 2 == 0:
        l = -2 * pow(4,length//2) // 3
        m = (pow(4,length//2) - 1) // 3
    elif length % 2 == 1:
        l = -2 * (pow(4,(length-1)//2) - 1) // 3
        m = (pow(4,(length+1)//2) - 1) // 3
    L.append(l)
    M.append(m)

identify = []
for i in range(40):
    if i == 0:
        ap_list = [L[0],M[0]]
    elif i % 2 == 1:
        ap_list = [L[i],L[i-1]]
    elif i % 2 == 0:
        ap_list = [M[i-1],M[i]]
    identify.append(ap_list)

bit = [0]*40
for i in range(39,-1,-1):
    left = identify[i][0]
    right = identify[i][1]
    if i % 2 == 0 and left < N <= right:
        bit[39-i] = 1
        N -= pow(-2,i)
    elif i % 2 == 1 and left <= N < right:
        bit[39-i] = 1
        N -= pow(-2,i)

ans = "".join(str(e) for e in bit).lstrip("0")
print(ans)

# Optimized solution
N = int(eval(input()))

ans = "" 
if N == 0:
    ans = "0"

while N != 0:
    if N % 2 == 1:
        ans = "1" + ans
    else:
        ans = "0" + ans
    N //= 2
    N *= -1

print(ans)
# Original solution
#!/usr/bin/env python

n = int(eval(input()))

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

ans = ''
while abs(n) > 0:
    r = n%2 
    ans += str(r) 
    n //= 2
    n = -n

if abs(n)%2 == 1:
    print(ans)
else:
    print((ans[::-1]))


# Optimized solution
#!/usr/bin/env python

n = int(eval(input()))

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

ans = ''
while abs(n) > 0:
    r = n%2 
    ans = str(r) + ans 
    
    if r == 1:
        n -= 1
    n //= (-2)

print(ans)
# Original solution
N = int(eval(input()))
A = list(map(int,input().split()))
A.sort(reverse=True)
# print(A)
ans = A[0]
for i in range(2,N):
    ans += A[i//2]
print(ans)

# Optimized solution
def main():
    import sys
    input = sys.stdin.readline

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


if __name__ == '__main__':
    main()
# Original solution
n = int(eval(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]))

# Optimized solution
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
n = int(eval(input()))
a = sorted(list(map(int, input().split())))
print((2*sum(a[n//2:])-a[-1]-(a[n//2] if n%2 else 0)))
# Original solution
import sys
input = sys.stdin.readline
from collections import *

N = int(eval(input()))
A = list(map(int, input().split()))
A.sort(reverse=True)
l = [A[0]]

for Ai in A[1:]:
    l.append(Ai)
    l.append(Ai)
    
print((sum(l[:N-1])))

# Optimized solution
import sys
input = sys.stdin.readline

N = int(eval(input()))
A = list(map(int, input().split()))
A.sort(reverse=True)
l = [A[0]]

for i in range(1, N):
    l.append(A[i])
    l.append(A[i])

print((sum(l[:N-1])))
# Original solution
import itertools
import sys
input = sys.stdin.readline

N = int(eval(input()))
A = list(map(int, input().split()))
ans = 0
for L in itertools.permutations(A):
    temp = 0
    for i in range(1, N):
        temp += min(L[0], L[i-1])
    ans = max(ans, temp)
print(ans)


# Optimized solution
import itertools
import sys
input = sys.stdin.readline

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

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

print(ans)
# Original solution
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return list(map(int, input().split()))
sys.setrecursionlimit(10**9)

N = int(eval(input()))
As = list(mapint())
As.sort(reverse=True)
from heapq import heappush, heappop

first = (As.pop(0))
hq = []
if N<2:
    print((0))
else:
    ans = 0
    second = As.pop(0)
    ans += first
    heappush(hq, (-second, -first))
    heappush(hq, (-second, -first))
    for a in As:
        s, f = heappop(hq)
        ans -= s
        heappush(hq, (-a, s))
        heappush(hq, (-a, f))

print(ans)

# Optimized solution
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return list(map(int, input().split()))
sys.setrecursionlimit(10**9)

N = int(eval(input()))
As = list(mapint())

conf = 0
from heapq import heapify, heappop, heappush
As.sort(reverse=True)
m = As.pop(0)
s = As.pop(0)
conf += m
Q = []
heappush(Q, -s)
heappush(Q, -s)
for a in As:
    s = heappop(Q)
    conf -= s
    heappush(Q, -a)
    heappush(Q, -a)
print(conf)
# Original solution
# -*- coding: utf-8 -*-

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

A.sort(reverse=True)
#print(A)

ans = 0
list = []
for i in range(N):
  if ( i==0 ) :
    list.append(A[0])
  else:
    max_index = list.index(max(list))
    ans += list[max_index]
    list[max_index] = A[i]
    list.insert(max_index, A[i])
  #print(list,ans)

print(ans)

# Optimized solution
# -*- coding: utf-8 -*-

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

A.sort(reverse=True)
#print(A)

"""
ans = 0
list = []
for i in range(N):
  if ( i==0 ) :
    list.append(A[0])
  else:
    max_index = list.index(max(list))
    ans += list[max_index]
    list[max_index] = A[i]
    list.insert(max_index, A[i])
  #print(list,ans)
"""

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

print(ans)
# Original solution
N, *A = list(map(int, open(0).read().split()))

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


# Optimized solution

N = int(eval(input()))
X = list(map(int, input().split()))

X.sort(reverse=True)
ans = 0
cnt = 0
j = 0
for i in range(N):
    if i == 0:
        continue
    elif i == 1:
        ans += X[j]
        j += 1
    else:
        ans += X[j]
        cnt += 1
        if cnt == 2:
            j += 1
            cnt = 0

print(ans)
# Original solution
#!/usr/bin/env python3
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections
from decimal import Decimal # 10進数で考慮できる

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

N = int(eval(input()))
A = LI()

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

# Optimized solution
N = int(eval(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)
# Original solution
import heapq

class ErasableHeapq:
    def __init__(self):
        self.p = list()
        self.q = list()
        self.len = 0

    def insert(self, x):
        heapq.heappush(self.p, x)
        self.len += 1
        return

    def erase(self, x):
        heapq.heappush(self.q, x)
        self.len -= 1
        return

    def minimum(self):
        while self.q and self.p[0] == self.q[0]:
            heapq.heappop(self.p)
            heapq.heappop(self.q)
        return self.p[0]

    def __len__(self):
        return self.len

    def empty(self):
        return self.len == 0

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

    # 大きい順
    A.sort()
    sA = A[::-1]

    ans = sA[0]

    eq = ErasableHeapq()
    eq.insert((-sA[1], -sA[0]))
    eq.insert((-sA[1], -sA[0]))

    for a in sA[2:]:
        best_pair = eq.minimum()
        eq.erase(best_pair)
        # print(a, best_pair)
        ans += -best_pair[0]
        eq.insert((-a, best_pair[0]))
        eq.insert((-a, best_pair[1]))

    print(ans)

if __name__ == '__main__':
    main()

# Optimized solution
def main():
    N = int(eval(input()))
    A = list(map(int, input().split()))

    # 大きい順
    A.sort()
    A = A[::-1]

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

    print(ans)

if __name__ == '__main__':
    main()
# Original solution
n = int(eval(input()))
A = [int(x) for x in input().split()]

A.sort(reverse=True)

line = [A[0]]
comfort = 0
for a in A[1:]:
    diff_comfort = [min(line[(i + 1) % len(line)], line[i])
                    for i in range(len(line))]
    pos = min(list(range(len(line))), key=lambda i: diff_comfort[i])
    comfort += diff_comfort[pos]
    line.insert(pos + 1 if pos < len(line) - 1 else 0, a)
print(comfort)


# Optimized solution
n = int(eval(input()))
A = [int(x) for x in input().split()]
A.sort(reverse=True)
print((A[0] + sum(A[1:(n - 2) // 2 + 1]) * 2 +
      (0 if n % 2 == 0 else A[(n - 1) // 2])))
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))
A.sort(reverse=False)

#N = 12
#A = [12,9,9,6,6,5,5,4,3,2,1,1]

ret = 0
array = []
array.append(A.pop())
while len(A) > 0:
    #print('array', array)
    comfort = [0 for i in range(len(array))]
    for i in range(len(comfort)):
        comfort[i] = min(array[i-1], array[i])
    #print('comfort', comfort)
    val = max(comfort)
    ret += val
    #print('ret', ret)
    array.insert(comfort.index(val), A.pop())

print(ret)


# Optimized solution
N = int(eval(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])))
# Original solution
n = int(eval(input()))

a = list(map(int, input().split()))
a = sorted(a, reverse=True)
comfort = a[0:2]
sum = max(a[0], a[1])
for j in a[2:]:
    key = 0
    for i in range(len(comfort) - 1):
        comfort_value = min(comfort[1], comfort[-1])
        comfort_value_kari = min(comfort[i], comfort[i + 1])
        if comfort_value < comfort_value_kari:
            comfort_value = comfort_value_kari
            key = i
    comfort.insert(key, j)
    sum += comfort_value
print(sum)



# Optimized solution
import heapq
n = int(eval(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)
# Original solution
import math

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

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

print(ans)

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

print(ans)
# Original solution
N=int(eval(input()))
A=list(map(int,input().split()))
A.sort(reverse=True)

cnt=0
ans=[]
ans.append(A[0])
ans.append(A[1])
cnt+=ans[0]
for i in range(2,N):
    k=ans.index(A[int((i-1)/2)])
    if k==len(ans)-1:
        kb=k-1
        kn=0
    else:
        kb=k-1
        kn=k+1

    if ans[kb]>=ans[kn]:
        cnt+=min(ans[k],ans[kb])
        ans.insert(kb,A[i])
    elif ans[kb]<ans[kn]:
        cnt+=min(ans[k],ans[kn])
        ans.insert(kn,A[i])

print(cnt)

# Optimized solution
N=int(eval(input()))
A=list(map(int,input().split()))
A.sort()
A.insert(0,0)

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

print(ans)
# Original solution
n,*l=list(map(int,open(0).read().split()))
from heapq import *
l.sort()
hq=[-l.pop()]
heapify(hq)
a=0
for _ in range(n-1):
  m=heappop(hq)
  a-=m
  t=-l.pop()
  heappush(hq,t)
  heappush(hq,t)
print(a)

# Optimized solution
n,*l=list(map(int,open(0).read().split()))
from heapq import *
l.sort()
q=[-l.pop()]
a=0
for _ in range(n-1):
  m=heappop(q)
  a-=m
  t=-l.pop()
  heappush(q,t)
  heappush(q,t)
print(a)
# Original solution
n,*l=list(map(int,open(0).read().split()))
from heapq import *
l.sort()
q=[-l.pop()]
a=0
for _ in range(n-1):
  m=heappop(q)
  a-=m
  t=-l.pop()
  heappush(q,t)
  heappush(q,t)
print(a)

# Optimized solution
n,*l=list(map(int,open(0).read().split()));print((sum(sorted(l*2)[-2:~n:-1])))
# Original solution
n,*l=list(map(int,open(0).read().split()));print((sum(sorted(l*2)[-2:~n:-1])))

# Optimized solution
n,*l=list(map(int,open(0).read().split()))
l.sort(reverse=1)
print((sum(l[i//2] for i in range(1,n))))
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))
A.sort(reverse=True)
ans = A[0]
S = [A[0], A[1]]
for i in range(2, len(A)):
    m = min(A[0], A[-1])
    cnt = -1
    for j in range(len(A)-1):
        if min(A[j], A[j+1]) > m:
            m = min(A[j], A[j+1])
            cnt = j
    S = S[:cnt] + [A[i]] + S[cnt:]
    ans += m
print(ans)




# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))
A.sort(reverse=True)
ans = A[0]
N -= 2
for i in range(1, N):
    if N >= 2:
        ans += A[i]*2
        N -= 2
    elif N  == 1:
        ans += A[i]
        N -= 1
    else:
        break
print(ans)
# Original solution
n = int(eval(input()))
a = sorted(list(map(int, input().split())), reverse=True)
sum_com = a[0]
comfort = [a[1], a[1]]

for ai in a[2:]:
    sum_com += comfort[0]
    if comfort[0] == ai:
        comfort.append(ai)
    else:
        comfort.pop(0)
        comfort += [ai, ai]

print(sum_com)


# Optimized solution
n = int(eval(input()))
a = sorted(list(map(int, input().split())), reverse=True)
sum_com = 0
distinct_a = sorted(list(set(a)), reverse=True)
comfort = {i: 0 for i in distinct_a}
comfort[a[0]] = 1
max_com = 0

for ai in a[1:]:
    sum_com += distinct_a[max_com]
    if distinct_a[max_com] == ai:
        comfort[distinct_a[max_com]] += 1
    else:
        comfort[distinct_a[max_com]] -= 1
        comfort[ai] += 2
        if comfort[distinct_a[max_com]] == 0:
            max_com += 1

print(sum_com)
# Original solution
def nth_element(array, will):
    will -= 1
    n = len(array)
    left = 0
    right = n-1
    while right > left:
        i = left-1
        j = right
        v = array[right]
        while True:
            i += 1
            j -= 1
            while array[i]<v:i+=1
            while array[j]>v:j-=1
            if i>=j:break
            array[i],array[j] = array[j],array[i]
        array[i],array[right] = array[right],array[i]
        if i == will:break
        if i > will:
            right = i-1
        else:
            left = i+1
    return array

# from random import randrange
# for i in range(100):

n = int(eval(input()))
A = list(map(int,input().split()))
# n = randrange(2,10)
# A = [0]*n
# for j in range(n):
#     A[j] = randrange(1,1000)
k = 1 + (n-1)//2

nth_element(A,n-k+1)
# B = sorted(A)
ans = 0
M = 0
# m = B[-1]
for i in range(-1,n-1):
    ans += A[n - (i+1)//2 - 1]
    M = max(M, A[n- (i+1)//2 - 1])
    # ans -= B[n-(i+1)//2 - 1]
ans -= M
# ans += m
# if ans != 0:
#     print(n,A)
print(ans)


# Optimized solution
# 入力
n = int(eval(input()))
A = list(map(int,input().split()))

A.sort()

ans = 0
can = 1 # lastの数が後何回足されうるか
will = n-1 # 次に足されるであろう数のindex
for i in range(n-1):
    ans += A[will]
    can -= 1
    if can == 0:
        # 足されきったら一つ小さい数へ
        can = 2 # 最大値以外は2回足されうる
        will -= 1
print(ans)
# Original solution
n = int(eval(input()))
a = list(map(int,input().split()))

sa = sorted(a,reverse=True)
ans = 0
ls = [sa[0]]
for v in sa[1:]:
  i = ls.index(max(ls))
  ans+= ls[i]
  new_min = min(ls[i], v)
  ls = ls[:i] + [new_min, new_min] + ls[i+1:]
  #print(ls)

print(ans)


# Optimized solution
n = int(eval(input()))
a = list(map(int,input().split()))

sa = sorted(a,reverse=True)
ans = sa[0]
i = 2
for v in sa[1:]:
  if i >= n:
    break
  #print("i:{} ans:{}".format(i,ans))
  ans += v
  i += 1
  if i >= n:
    break
  #print("i:{} ans:{}".format(i,ans))
  ans += v
  i += 1

print(ans)
# Original solution
import itertools

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

conf_max = 0
for p in itertools.permutations(A):
    conf = 0
    for i in range(1, N):
        conf += min(p[i-1], p[0])
    if conf_max < conf:
        conf_max = conf

print(conf_max)

# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))
A.sort(reverse=True)

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

print(conf_max)
# Original solution
import heapq

n=int(eval(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)

# Optimized solution
n=int(eval(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)
# Original solution
# coding: utf-8
from collections import deque


def solve(*args: str) -> str:
    n = int(args[0])
    A = sorted(map(int, args[1].split()), reverse=True)

    Q = deque()

    ret = 0
    for i in range(-(-n//2)):
        if 0 < i:
            ret += min(Q[-1], Q[0])
        if i % 2:
            Q.appendleft(A[i])
        else:
            Q.append(A[i])

    for i in range(n-len(Q)):
        ret += min(Q[i], Q[(i+1) % len(Q)])

    return str(ret)


if __name__ == "__main__":
    print((solve(*(open(0).read().splitlines()))))


# Optimized solution
# coding: utf-8

def solve(*args: str) -> str:
    n = int(args[0])
    A = sorted(map(int, args[1].split()), reverse=True)

    return str(sum([A[i//2] for i in range(1, n)]))


if __name__ == "__main__":
    print((solve(*(open(0).read().splitlines()))))
# Original solution
n = int(eval(input()))
A = list(map(int, input().split()))
A.sort(reverse=True)

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


# Optimized solution
n = int(eval(input()))
A = list(map(int, input().split()))
A.sort(reverse=True)

ans = 0
cnt = 0
for i, a in enumerate(A):
    if i == 0:
        ans += a
        cnt += 1
    else:
        if n-1 - cnt >= 2:
            ans += a*2
            cnt += 2
        else:
            ans += a
            cnt += 1
    if cnt == n-1:
        break
print(ans)
# Original solution
#D - Chat in a Circle
N = int(eval(input()))
A = list(map(int,input().split()))
#ソート
#B = []
arr = A.copy()
left = 0
right = N -1
#B = sorted(A,reverse = True)

def quicksort(arr,left,right):
    index = partition(arr,left,right)
    if left < index- 1 :
        quicksort(arr,left,index - 1)
    if index < right:
        quicksort(arr,index,right)

def partition(arr,left,right):
    pivot = arr[int((left + right) / 2)]
    while left <= right:
        while arr[left]  > pivot:
            left  += 1
        while arr[right] < pivot:
            right -= 1
        if left <= right:
             arr[left],arr[right] = arr[right],arr[left]
             left += 1
             right -= 1
    return left

quicksort(arr,left,right)

Chat = []
Friend_min = 0
Friend_min_max = 0
Friend_min_max_wa = 0
max_no = 0
i =0
Chat.append(arr[0])
for i in range(N):
    for j in range(i):
        if (Chat[j] > Chat[j+1]):
            Friend_min = Chat[j+1]
        else:
            Friend_min = Chat[j]
        if Friend_min >= Friend_min_max:
            Friend_min_max = Friend_min
            max_no = j + 1
        Friend_min = 0
    Chat.insert(max_no,arr[i])
    Friend_min_max_wa = Friend_min_max_wa + Friend_min_max
    Friend_min_max = 0
    max_no = 0
#    print(Chat)    
# 出力
print(Friend_min_max_wa)

# Optimized solution
#D - Chat in a Circle
N = int(eval(input()))
A = list(map(int,input().split()))
#ソート
arr = []
arr = sorted(A,reverse = True)

Friend_min_max_wa = 0
i =0
j = 0
for i in range(N -1):
    if i % 2 != 0 : j = j + 1
    Friend_min_max_wa = Friend_min_max_wa + arr[j]

# 出力
print(Friend_min_max_wa)
# Original solution
from heapq import heapify, heappop, heappush
N = int(eval(input()))
A = list(map(int,input().split()))
A.sort(reverse = True)
#print(A)
ans = A[0]
Q = [(-A[1],-A[0]),(-A[1],-A[0])] #一つ目が大きいほうへ入れる
heapify(Q)
for i in range(2,N):
  nxt = A[i]
  s,t = heappop(Q)
  s *= -1; t*= -1 #正に戻す
  #print(s,ans)
  ans += s
  heappush(Q,(-nxt,-s))
  heappush(Q,(-nxt,-t))
  #print(Q)
print(ans)


         
         

# Optimized solution
N = int(eval(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])
#print(B)
ans = 0
for i in range(N-1):
  ans += B[i]
print(ans)
# Original solution
from collections import deque

n = int(eval(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)


# Optimized solution
n = int(eval(input()))
a = sorted(map(int, input().split()), reverse=True)

ans, que, cnt = 0, [a[0]], 0
for i in a[1:]:
    ans += que[cnt]
    que += [i, i]
    cnt += 1
print(ans)
# Original solution
import bisect
import statistics
n = int(eval(input()))
a = list((int(x) for x in input().split()))

a.sort()
incl = []
score = 0

for i in range(len(a)):
    ain = a.pop()
    incl.append(ain)
    if i==0: continue
    if (len(incl)%2 == 1):
        score += statistics.median(incl)
    else:
        # smaller of middle two
        score += incl[ int(len(incl)/2)-1 ]

print(score)

# Optimized solution
import bisect
import statistics
n = int(eval(input()))
a = list((int(x) for x in input().split()))

a.sort()
incl = []
score = 0

for i in range(len(a)):
    ain = a.pop()
    incl.append(ain)
    if i==0: continue
    if (len(incl)%2 == 1):
        score += incl[ int(len(incl)/2) ]
    else:
        # smaller of middle two
        score += incl[ int(len(incl)/2)-1 ]

print(score)