Problem:
 Score: 400 points    Problem Statement  Quickly after finishing the tutorial of the online game ATChat , you have decided to visit a particular place with N-1 players who happen to be there. These N players, including you, are numbered 1 through N , and the friendliness of Player i is A_i .  The N players will arrive at the place one by one in some order. To make sure nobody gets lost, you have set the following rule: players who have already arrived there should form a circle, and a player who has just arrived there should cut into the circle somewhere.  When each player, except the first one to arrive, arrives at the place, the player gets comfort equal to the smaller of the friendliness of the clockwise adjacent player and that of the counter-clockwise adjacent player. The first player to arrive there gets the comfort of 0 .  What is the maximum total comfort the N players can get by optimally choosing the order of arrivals and the positions in the circle to cut into?      Constraints   All values in input are integers.  2 \leq N \leq 2 \times 10^5  1 \leq A_i \leq 10^9         Input  Input is given from Standard Input in the following format:  N  A_1  A_2  \dots  A_N       Output  Print the maximum total comfort the N players can get.        Sample Input 1 4
2 2 1 3      Sample Output 1 7  By arriving at the place in the order Player 4, 2, 1, 3 , and cutting into the circle as shown in the figure, they can get the total comfort of 7 .   They cannot get the total comfort greater than 7 , so the answer is 7 .       Sample Input 2 7
1 1 1 1 1 1 1      Sample Output 2 6

Solution (Python):
import sys
from io import StringIO
import unittest
import os
import heapq


sys.setrecursionlimit(999999999)



def resolve(test_def_name=""):
    n = int(input())
    a_s = list(map(int, input().split()))

    a_s.sort(reverse=True)

    que = []
    heapq.heapify(que)

    ans = 0
    for cnt, a in enumerate(a_s):
        a = -a
        if cnt == 0:
            heapq.heappush(que, a)
            continue

            
        target = heapq.heappop(que)
        ans += -target
        heapq.heappush(que, a)
        heapq.heappush(que, a)

    print(ans)



class TestClass(unittest.TestCase):
    def assertIO(self, assert_input, output):
        stdout, sat_in = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(assert_input)
        resolve(sys._getframe().f_back.f_code.co_name)
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, sat_in
        self.assertEqual(out, output)

    def test_input_1(self):
        test_input = """4
2 2 1 3"""
        output = """7"""
        self.assertIO(test_input, output)

    def test_input_2(self):
        test_input = """7
1 1 1 1 1 1 1"""
        output = """6"""
        self.assertIO(test_input, output)

    
    def test_1original_1(self):
        test_input = """4
2 2 3 3"""
        output = """9"""
        self.assertIO(test_input, output)



if __name__ == "__main__":
    if os.environ.get("USERNAME") is None:
        
        resolve()

    else:
        
        unittest.main()

Solution (Python):
def input_int():
    return map(int, input().split())

def one_int():
    return int(input())

def one_str():
    return input()

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

import heapq

N=one_int()
A=many_int()

sort_A = sorted(A,reverse=True)



sukima = [-sort_A[0]]
heapq.heapify(sukima)

sort_A = sort_A[1:]
sums = 0
for a in sort_A:
    sums += heapq.heappop(sukima)
    heapq.heappush(sukima, -a)
    heapq.heappush(sukima, -a)

print(-sums)

Solution (Python):
N = int(input())
S = list(map(int,input().split()))
rev_sorted_S = list(reversed(sorted(S)))

ans = 0
for n in range(1,N):
  if n == 1:
    ans += max(S)
  else:
    ans += rev_sorted_S[n//2]
print(ans)

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

Solution (Python):
def resolve():
    n = int(input())
    A = list(map(int, input().split()))
    A.sort()
    ans = 0
    ans += A[-1]
    if n % 2 == 0:
        s = sum(A[n - n//2:-1]) * 2
    else:
        s = sum(A[n - n//2:-1]) * 2 + A[n - n//2 - 1]
    print(ans + s)

resolve()

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

counter = 0
idx = 1
for _ in range(N-2):
  ans += A[idx]
  counter += 1
  if counter == 2:
    idx += 1
    counter = 0
print(ans)

Solution (Python):
n = int(input())
a = sorted(list(map(int,input().split())))
cou = a.pop()
n1 = n-2
if n1%2==0:
  t = n1//2
  for i in range(t):
    q = a.pop()
    cou += q*2
else:
  t = n1//2
  for i in range(t):
    q = a.pop()
    cou += 2*q
  cou += a.pop()
print(cou)

Solution (Python):
N=int(input())
As = list(map(int,input().split()))
As.sort()
As=As[::-1]
ans = As.pop(0)
N=N-2
if N !=0:
    if N%2==0:
        ans +=sum(As[:N//2])*2
    else:
        ans +=sum(As[:(N-1)//2])*2
        ans +=As[(N-1)//2]

print(ans)

Solution (Python):
import heapq

N = int(input())
A = list(map(int, input().split()))
A = list(map(lambda x: x * (-1), A))

heapq.heapify(A)

ans = 0
N -= 2
ans += heapq.heappop(A) * (-1)

if N == 0:
    print(ans)
    exit()
elif N == 1:
    ans += A[0]
    print(ans)
    exit()
elif N >= 2:
    q = N // 2
    r = N % 2
    for i in range(1, q + 1):
        ans += 2 * heapq.heappop(A) * (-1)
    if r == 1:
        ans += heapq.heappop(A) * (-1)
    print(ans)

Solution (Python):
def main1():
    N=int(input())
    A=list(map(int,input().split()))
    A.sort(reverse=True)
    cn=1
    ans=A[0]
    tmp=0
    if N%2==0:
        while cn<-(-N//2):
            ans+=A[cn]*2
            cn=cn+1
        print(ans)
    else:
        while cn<-(-N//2)-1:
            ans+=A[cn]*2
            cn=cn+1
        print(ans+A[cn])
main1()

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

a.sort(reverse = True)

ans = 0

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

print(ans)

Solution (Python):
N=int(input())
A=list(map(int,input().split()))
import heapq
A=[-i for i in A]
heapq.heapify(A)
x=heapq.heappop(A)
ans=0
ans+=-x
l=0
if N==2:
    print(ans)
else:
    while True:
        x=heapq.heappop(A)
        ans+=-x
        l+=1
        if l==N-2:
            break
        ans+=-x
        l+=1
        if l==N-2:
            break
    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(a[0]+a[n//2]+2*sum(a[1:n//2]))

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

num = n - 2

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

a = sorted(a, reverse=True)

ans = a[0]
t = 1
while num > 0:
    if num >=2 :
        ans += a[t]*2
        t +=1
        num -= 2
    else :
        ans += a[t]
        num -=1

print(ans)

Solution (Python):
from collections import deque

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

a.sort()

total = 0
q = deque()
q.append(a[n -1])
for i in reversed(range(n - 1)):
    q.append(a[i])
    q.append(a[i])
    total += q.popleft()

print(total)

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

Solution (Python):
n=int(input())
seq=list(map(int,input().split()))
seq.sort()
t=n//2
s=seq[t:]
if 1==1:
        x=seq[-1]
        d=sum(s)-x
        d=d*2
        d+=x
        
        if n%2==0:
                print(d)
        else:
                print(d-seq[t])

Solution (Python):
N=int(input())
a=input().split()
A=[int(b) for b in a]
AS=sorted(A,reverse=True)
N=len(AS)
NN=N//2
if N%2==0:
    answer = 0
    answer += AS[0]
    answer += sum(AS[1:NN]) * 2
    print(answer)
else:
    answer=0
    answer+=AS[0]
    answer += sum(AS[1:NN]) * 2
    answer+=AS[NN]
    print(answer)

Solution (Python):
N=int(input())
S=list(map(int,input().split()))
S.sort()
S.reverse()
tmp=0
ans=0
if len(S)==2:
    print(S[0])
    
else:
    ans+=S[0]

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

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

box = sorted(box, reverse=True)
li = [box[0]]
for b in box[1:]:
    li.append(b)
    li.append(b)

li = li[:N-1]
print(sum(li))

Solution (Python):






import sys
input = sys.stdin.readline

import math

import bisect
import heapq
from collections import defaultdict
from collections import deque
from collections import Counter
from functools import lru_cache





MOD = 10**9+7
INF = float('inf')
AZ = "abcdefghijklmnopqrstuvwxyz"






def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]


def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()


def DD(arg): return defaultdict(arg)


def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n): return kaijo_memo[n]
  if(len(kaijo_memo) == 0): kaijo_memo.append(1)
  while(len(kaijo_memo) <= n): kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n): return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0): gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n): gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if n == r: return 1
  if n < r or r < 0: return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1: arr.append([temp, 1])
  if arr==[]: arr.append([n, 1])
  return arr


def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors


def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum


def gcd(a, b):
    while b: a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd (a, b)


def count_bit(n):
  count = 0
  while n:
    n &= n-1
    count += 1
  return count


def base_10_to_n(X, n):
  if X//n: return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

def base_10_to_n_without_0(X, n):
  X -= 1
  if X//n: return base_10_to_n_without_0(X//n, n)+[X%n]
  return [X%n]


def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count





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

q = [-A[1],-A[1]]
ans = A[0]
for a in A[2:]:
  ans -= q[0]
  heapq.heappop(q)
  heapq.heappush(q,-a)
  heapq.heappush(q,-a)
print(ans)

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

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

Solution (Python):
from collections import deque

def main(N, A):
    
    l = A[:2]
    
    ans = A[0]

    d = deque([A[1], A[1]])

    for a in A[2:]:
        ans += d.popleft()
        d.extend([a, a])
    print(ans)



if __name__ == '__main__':
    N = int(input())
    A = list(map(int, input().split()))
    A.sort(reverse = True)
    main(N, A)

Solution (Python):
from heapq import*


n = int(input())
A = sorted([-int(x) for x in input().split()], reverse = True)

B = []
a = A.pop()
heappush(B, a)

ans = 0
while A:
    a = A.pop()
    b = heappop(B)
    ans += -b
    heappush(B, a); heappush(B, a)

print(ans)

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

nums = sorted([int(i) for i in input().split()], reverse=True)

cnt = 0
for i, v in enumerate(nums):
    if i != 0:
        cnt += nums[i//2]
        
print(cnt)

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

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

print(ans)

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

alist.sort(reverse=True)
counter = 0
for i in range(n):
    if i == 0:
        counter += 0
    else:
        counter += alist[i//2]
print(counter)

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

n, *a = map(int, read().split())

a2 = sorted((a + a), reverse=True)
a2[0] = 0


print(sum(a2[:n:]))

Solution (Python):
from collections import deque


n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
d = deque()
d.append(a[0])
ans = 0

for i in range(1, n):
    x = d.popleft()
    ans += x
    d.append(a[i])
    d.append(a[i])

print(ans)

Solution (Python):





import math
import fractions
import sys
import bisect
import heapq  
import collections
from collections import Counter
from collections import deque
import pprint
import itertools

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""


def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

    if temp != 1:
        arr.append([temp, 1])

    if arr == []:
        arr.append([n, 1])

    return arr



def power(a, n, mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod



def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n - 1
    while (tmp >= l):
        a = a * tmp % mod
        tmp -= 1
    return a


inf = 10 ** 18
mod = 10 ** 9 + 7

n = ir()
a = lr()
a.sort(reverse=True)
ans = a[0]
if (n-1)%2 == 1:
    for i in range(n//2-1):
        ans += a[i+1]*2
else:
    for i in range(n//2-1):
        ans += a[i+1]*2
    ans += a[n//2]
print(ans)

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

Solution (Python):
from collections import deque

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

Q = deque([0])
comfort = 0
i = 0
while Q:
    if i == n:
        break
    comfort += Q.popleft()
    if i == 0:
        Q.append(a[i])
    elif i > 0:
        Q.append(a[i])
        Q.append(a[i])
    i += 1

print(comfort)

Solution (Python):
N = int(input())
AList = list(map(int, input().split()))
sList = sorted(AList, reverse=True)
ans = sList[0]
idx = 1
for i in range(1, N-1):
  ans += sList[idx]
  if i%2 == 0:
    idx += 1

print(ans)

Solution (Python):
import bisect
N=int(input())
A=list(map(int,input().split()))
A.sort(reverse=True)
ans_list=[0]*(N-1)
ans_list[0]=A[0]
count=0
ind=1
for i in range(1,len(ans_list)):
    if count==2:
        count=0
        ind+=1
    ans_list[i]=A[ind]
    count+=1
print(sum(ans_list))

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





idx = []
for i in range(N + 1 // 2):
    idx.append(i)
    idx.append(i)

idx = idx[::-1]
idx = idx[1:N]

ans = 0
for i in idx:
    ans += A[i]
print(ans)

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

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

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

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

li.sort()
if N%2==0:
  print(2*sum(li[int(-N/2):-1])+li[-1])
else:
  print(2*sum(li[-(N//2):-1])+li[-1]+li[-(N//2)-1])

Solution (Python):
import heapq

N = int(input())
A = list(map(int,input().split()))
ans = max(A)
A = [-1*item for item in A]
heapq.heapify(A)
heapq.heappop(A)
flag = 1
for i in range(N-2):
    if flag == 1:
        a = heapq.heappop(A)
        ans += -1*a
        flag = 0
    else:
        ans += -1*a
        flag = 1
print(ans)

Solution (Python):
from collections import deque

def solve():
    ret = 0
    A.sort(reverse=True)
    stack = deque()
    for a in A:
        if not stack:
            stack.append(a)
            continue
        ret += stack.popleft()
        stack.extend([a, a])
    print(ret)

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

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

p = [0, l[0]]
for i in l[1:]:
    p.append(i)
    p.append(i)
    
print(sum(p[:n]))

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

a.sort(reverse=True)
dic = {a[0]:1}
ans = 0

for i in range(1,n):
    h = a[i]
    max_h = next(iter(dic))
    
    
    
    if h == max_h:
        dic[h] += 1
        ans += h
    
    else:
        if h in dic:
            dic[h] += 2
            dic[max_h] -= 1
            ans += max_h
        
        else:
            dic[h] = 2
            dic[max_h] -= 1
            ans += max_h
        
        if dic[max_h] == 0:
            del dic[max_h]
    
    

print(ans)

Solution (Python):

n=int(input())
x=[int(x) for x in input().split()]
x.sort(reverse=True)
ans=x[0]
c=1
for i in range(1,n):
    if c+2<n:
        c+=2
        ans+=2*x[i]
    else:break
if c<n-1:
    ans+=x[i]
print(ans)

Solution (Python):
n = int(input())
a_list = list(map(int, input().split()))
a_list.sort()
a_list.reverse()
index = 1
counter = 0
ans = a_list[0]

for i in range(2, n):
    ans += a_list[index]
    counter += 1
    if counter == 2:
        counter = 0
        index += 1

print(ans)

Solution (Python):
""" D
    https://atcoder.jp/contests/abc173/tasks/abc173_d
"""
import sys
import math
from functools import reduce
from bisect import bisect_left

def readString():
    return sys.stdin.readline()

def readInteger():
    return int(readString())

def readStringSet(n):
    return sys.stdin.readline().split(" ")[:n]

def readIntegerSet(n):
    return list(map(int, readStringSet(n)))

def readIntegerMatrix(n, m):
    return reduce(lambda acc, _: acc + [readIntegerSet(m)], range(0, n), [])

def main(N, A):
    A.sort(reverse=True)

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

    return sum

if __name__ == "__main__":
    _N = readInteger()
    _A = readIntegerSet(_N)

    print(main(_N, _A))

Solution (Python):


from collections import deque


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


a_nums = sorted(a_nums, reverse=True)
point_list = deque([a_nums[0]])
point = 0


for i in range(1, N):
    a = a_nums[i]
    point += point_list.popleft()
    point_list.append(a)
    point_list.append(a)


print(point)

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

Solution (Python):



def main():
    n = int(input())
    li = list(map(int, input().split()))
    res = 0
    li = li + li
    li.sort()
    tmp = int(len(li) / 2)
    print(sum(li[-tmp:-1]))


if __name__ == "__main__":
    main()

Solution (Python):
if __name__ == '__main__':
    n = int(input())
    a = [int(i) for i in input().split()]
    a.sort(reverse=True)
    ans = 0
    count = 0
    for i in a:
        if ans ==0:
            ans+=i
            count+=1
        else:
            if count == n-2:
                ans+=i
                break
            elif count == n-3:
                ans+=i*2
                break
            else:
                ans+=i*2
                count+=2

    if n ==2:
        ans= max(a)

    print(ans)

Solution (Python):

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

input = sys.stdin.readline

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

inf = float('inf')
mod = 1000000007




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

	A.sort()
	A = A[::-1]
	ans = 0
	maxs = 0
	cnt = 0
	fst = True

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

			pgt.put(val)
			pgt.put(val)
	print(sum(ansans))




if __name__ == "__main__":
	main()

Solution (Python):
import collections

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

As.sort(reverse = True)



circle_que = collections.deque()

circle_que.append(As[0])

ans = 0
for A in As[1:]:
    circle_que.append(A)
    circle_que.append(A)

    ans += circle_que.popleft()
    

print(ans)

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

a.sort(reverse=True)
middle = []
if (n % 2 == 0):
    middle = a[:n // 2]
    

    sum = sum(middle)-middle[-1]
    for i in range(n // 2-1):
        sum += middle[i + 1]
    sum += middle[-1]
else:
    middle = a[:n // 2+1]
    

    sum = sum(middle)-middle[-1]
    for i in range(n // 2):
        sum += middle[i+1]
print(sum)

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

A.sort(reverse=True)
ans = A[0]
i = 1
j = 1
while i < N-1:
    ans += A[j]
    i += 1
    if i < N-1:
        ans += A[j]
    i += 1
    j += 1
print(ans)

Solution (Python):
n=int(input())
m=[int(x) for x in input().split()]
m=sorted(m, reverse=True)
u=sorted(list(set(m)), reverse=True)
d={}
for i in m:
  if i in d:
    d[i]+=1
  else:
    d[i]=1
for i in d:
  if i != u[0]:
    d[i]*=2
pos=0
c=u[pos]*(d[u[pos]]-1)
posm=d[u[pos]]
for i in range(posm, len(m)):
  c+=u[pos]
  d[u[pos]]-=1
  if d[u[pos]] == 0:
    pos+=1
print(c)

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

a.sort()
ans = a[-1]
flg = False
ind = -2

for i in range(n - 2):
    ans += a[ind]
    if flg:
        ind -= 1
    flg = not flg

print(ans)

Solution (Python):

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

input = sys.stdin.readline

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

inf = float('inf')
mod = 1000000007




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

	A.sort()
	A = A[::-1]
	ans = 0
	maxs = 0
	cnt = 0
	fst = True

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

			pgt.put(val)
			pgt.put(val)
	print(sum(ansans))




if __name__ == "__main__":
	main()

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

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

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

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

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

A.sort(reverse=True)

res = 0

res += A.pop(0)

count = 1

for i in range(N):
    for _ in range(2):
        if count < N - 1:
            res += A[i]
            count += 1

print(res)

Solution (Python):

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

input = sys.stdin.readline

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

inf = float('inf')
mod = 1000000007




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

	A.sort()
	A = A[::-1]
	ans = 0
	maxs = 0
	cnt = 0
	fst = True

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

			pgt.put(val)

			if tmp == val:
				pgt.put(val)
			else:
				pgt.put(val)

	
	print(sum(ansans))




if __name__ == "__main__":
	main()

Solution (Python):
import queue

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

edge_que = queue.Queue()
edge_que.put(A_list[0])
score = 0
for i, a in enumerate(A_list[1:]):
    score += edge_que.get()
    edge_que.put(a)
    edge_que.put(a)
print(score)

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


a.sort(reverse=True)
cnt=0
for i in range(1,n):
    if i==1:
        cnt+=a[0]
    else:
        j = (i // 2)
        cnt += a[j]
print(cnt)

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

Solution (Python):
from collections import deque

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

A_sorted = sorted(A, reverse=True)
ans = 0
queue = deque([A_sorted[0]])
for a in A_sorted[1:]:
    ans += queue.popleft()
    queue.append(a)
    queue.append(a)
print(ans)

Solution (Python):
import collections

N = int(input())
A = sorted([int(a) for a in input().split()], reverse=True)

result = 0
max_a = A[0]
hash = {A[0]: 1}
next_max_a = collections.deque()
for i in range(1, len(A)):
    a = A[i]
    hash[max_a] -= 1
    result+=max_a

    if not(a in hash):
        hash[a] = 0
        next_max_a.append(a)
    hash[a] += 2

    if hash[max_a] <= 0:
        hash.pop(max_a)
        max_a = next_max_a.popleft()
print(result)

Solution (Python):
from heapq import heapify,heappush,heappop
N = int(input())
A = list(map(int, input().split()))

A.sort()
a = A.pop()
q = []
heappush(q,-a)
ans = 0
while A:
  
  b = A.pop()
  a = -heappop(q)
  ans += a
  heappush(q,-b)
  heappush(q,-b)
  
print(ans)

Solution (Python):
def main():

    N = int(input())
    lst = sorted([int(i) for i in input().split()], reverse=True)
    ans = lst[0]

    n, m = divmod(N - 2, 2)
    ans += sum(lst[1:n + 1]) * 2
    if m:
        ans += lst[n + 1]

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def d_chat_in_a_circle():
    N = int(input())
    A = sorted([int(i) for i in input().split()], reverse=True)

    ans = [A[0]]
    j = 1
    while True:
        ans.extend([A[j]] * 2)
        j += 1
        if len(ans) > N - 1:
            ans.pop()
            break
    if N % 2 == 0:
        ans.pop()
    return sum(ans)

print(d_chat_in_a_circle())

Solution (Python):
n=int(input())
a = list(map(int, input().split()))
b=sorted(a,reverse=True)
s=b[0]
t=2
if n==2:
  print(s)
else:
    for i in range(1,n):
        for _ in range(2):
            s+=b[i]
            t+=1
            if t==n:
                break
        else:
            continue
        break
    print(s)

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

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

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

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

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

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

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

A.sort(reverse=True)



ans=0
ans+=A[0]

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

print(ans)

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

sorted_list = sorted(A_LIST)[::-1]

n_half = N // 2 + N % 2
pts = 0
for a in sorted_list[:n_half - 1]:
    pts += a

circle = sorted_list[1:n_half] + [sorted_list[n_half - 1]]
    
for i in range(N - n_half):
    pts += circle[i]
        
print(pts)