CodeEff/ECCO huggingface dataset:
# Original solution
n = int(eval(input()))

lst = list(map(int, input().split()))
lst = sorted(lst, reverse=True)
class Queue:
  def __init__(self):
    self.queue_lst = []
    self.first_flag = True
    self.count = 0
  
  def add(self, friend_val):
    if self.first_flag:
      self.queue_lst.append(friend_val)
      self.first_flag = False
    else:
      self.queue_lst.append(friend_val)
      self.queue_lst.append(friend_val)
      self.count += self.queue_lst.pop(0)
  
queue = Queue()

for i in lst:
  queue.add(i)



print((queue.count))

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

lst = list(map(int, input().split()))
lst = sorted(lst, reverse=True)
class Queue:
  def __init__(self):
    self.queue_lst = []
    self.first_flag = True
    self.count = 0
    self.index = 0
  
  def add(self, friend_val):
    if self.first_flag:
      self.queue_lst.append(friend_val)
      self.first_flag = False
    else:
      self.queue_lst.append(friend_val)
      self.queue_lst.append(friend_val)
      self.count += self.queue_lst[self.index]
      self.index += 1
  
queue = Queue()

for i in lst:
  queue.add(i)



print((queue.count))
# Original solution
N = int(eval(input()))
a = list(map(int, input().split()))
a.sort(reverse = True)
p=0
ppl = [a[0]]*2
best_index = 1
best_point = a[0]
for i in a[1:]:
    p += best_point
    ppl = ppl[:best_index]+[i]+ppl[best_index:]
    #print(ppl)
    point_list = [min(ppl[i],ppl[i+1]) for i in range(0,len(ppl)-1)]
    #print(point_list)
    best_point = max(point_list)
    for j,q  in enumerate(point_list):
        if q == best_point:
            best_index = j+1
            continue
            
print(p)

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

a.sort(reverse = True)
p = a[0]
for i in range(N-2): #-1は上記a[0]分、もう-1は最初の要素挿入は加点されないため。
    p += a[(i//2)+1]

print(p)
# Original solution
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product, groupby, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(eval(input()))
def MAP(): return list(map(int, input().split()))
def LIST(): return list(map(int, input().split()))
def ZIP(n): return list(zip(*(MAP() for _ in range(n))))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()
A.sort(reverse=True)
ans = A[0]
cnt = 1
for x in A[1:]:
    if cnt == N-1:
        break
    if cnt == N-2:
        ans += x
        cnt += 1
    else:
        ans += 2*x
        cnt += 2
print(ans)


# Optimized solution
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product, groupby, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(eval(input()))
def MAP(): return list(map(int, input().split()))
def LIST(): return list(map(int, input().split()))
def ZIP(n): return list(zip(*(MAP() for _ in range(n))))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()
A.sort(reverse=True)
ans = 0
for i in range(N-1):
    ans += A[0--i//2]
print(ans)
# Original solution
#!/usr/bin/env python3
import sys
from collections import deque

class MM:
    def __init__(self, c_max, c_cnt):
        self.c_max = c_max
        self.c_cnt = c_cnt

def solve(N: int, A: "List[int]"):
    sl = list(reversed(sorted(A)))
    score = 0
    c_max = sl[0]
    h = {sl[0]: 1}
    queue = deque()
    queue.append(c_max)
    for i in range(1, len(sl)):
        score += c_max

        h[c_max] -= 1
        if sl[i] in h:
            h[sl[i]] += 2
        else:
            h[sl[i]] = 2
            queue.append(sl[i])

        if h[c_max] == 0:
            queue.popleft()
            c_max = queue[0]
        # print(f'{i} {sl[i]} {c_max} {h}')
    print(score)
    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))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()


# Optimized solution
#!/usr/bin/env python3
import sys
from collections import deque

def solve(N: int, A: "List[int]"):
    sorted_list = list(reversed(sorted(A)))
    score = 0
    queue = deque()
    queue.append(sorted_list[0])
    cnt_map = {sorted_list[0]: 1}
    for i in range(1, len(sorted_list)):
        current_max = queue[0]
        score += current_max
        cnt_map[current_max] -= 1
        num = sorted_list[i]
        if num in cnt_map:
            cnt_map[num] += 2
        else:
            cnt_map[num] = 2
            queue.append(num)

        if cnt_map[current_max] == 0:
            queue.popleft()
    print(score)
    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))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()
# Original solution
N = int(eval(input()))

A = []
a, *an = list(map(int, input().split()))
A.append(a)
A += an
A.sort()

Node = []
score = 0
Node.append(A.pop())

#print(A)

for n in range(N - 1):
    score += Node[0]
    num = A.pop()
    Node = Node[1:]
    Node.append(num)
    Node.append(num)

#print(Node)
print(score)

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

A = []
a, *an = list(map(int, input().split()))
A.append(a)
A += an
A.sort()

Node = []
score = 0
Node.append(A.pop())

#print(A)
start = 0

for n in range(N - 1):
    score += Node[start]
    num = A.pop()
    start += 1
    #Node = Node[1:]
    Node.append(num)
    Node.append(num)

#print(Node)
print(score)
# Original solution
n=int(eval(input()))
a=list(map(int,input().split()))
a.sort(reverse=True)
d=[a[0]]
ans=0
for i in range(1,n):
  tmp=max(d)
  ans=ans+tmp
  d.remove(tmp)
  d.append(a[i])
  d.append(a[i])
print(ans)


# Optimized solution
n=int(eval(input()))
a=list(map(int,input().split()))
a.sort(reverse=True)
ans=0
for i in range(1,n):
  ans=ans+a[i//2]
print(ans)
# Original solution
from collections import deque
N = int(eval(input()))
K = list(sorted(map(int, input().split()), reverse=True))

max1 = K[0]
max2 = K[1]
if N == 2:
    print(max1)
else:
    max3 = K[2]
    count = max1 + max2
    cands = [(max1, max2), (max2, max3)]
    for k in K[3:]:
        max_cand = (0, 0)
        max_i = -1
        for i, cand in enumerate(cands):
            if min(cand) > min(max_cand):
                max_cand = cand
                max_i = i
        hi, lo = max_cand
        count += lo
        cands.pop(max_i)
        cands.append((hi, k))
        cands.append((lo, k))
    print(count)


# Optimized solution
from collections import deque
N = int(eval(input()))
K = list(sorted(map(int, input().split()), reverse=True))

max1 = K[0]
max2 = K[1]
if N == 2:
    print(max1)
else:
    max3 = K[2]
    count = max1 + max2
    cands = deque([max2, max3, max3])
    for k in K[3:]:
        cand = cands.popleft()
        count += cand
        cands.append(k)
        cands.append(k)
    print(count)
# Original solution
N = int(eval(input()))
A = sorted([int(x) for x in input().split()], reverse=True)


ans = 0
for i in range(N):
    if i == 0:
        B = [A[0]]
        continue
    index, mx = 0, 0
    for j in range(len(B)):
        tmp = min(B[j-1], B[j])
        if mx < tmp:
            mx = tmp
            index = j
    B.insert(index, A[i])
    ans += mx
print(ans)


# Optimized solution
N = int(eval(input()))
A = sorted([int(x) for x in input().split()], reverse=True)


ans = 0
for i in range(N):
    if i == 0:
        continue
    else:
        ans += A[i//2]
print(ans)
# Original solution
N=int(eval(input()))

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

B=sorted(A,reverse=True)

ans=B[0]

C=[B[0],B[1]]

ans=B[0]
for i in range(2,N):
    jmax=1
    scoremax=min(C[0],C[1])
    for j in range(1,len(C)-1):
        score = min(C[j],C[j+1])
        if scoremax < score:
            scoremax = score
            jmax=j
    ans += scoremax
    C=C[0:jmax]+[B[i]]+C[jmax:]
    # print(C)

print(ans)

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

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

B=sorted(A,reverse=True)

ans=B[0]

sheets=[B[0]]

ans=0
for i in range(1,N):

    ans += sheets[i-1]
    sheets.append(B[i])
    sheets.append(B[i])

print(ans)
# Original solution
import collections
n=int(eval(input()))
A=list(map(int,input().split()))
A.sort(reverse=True)
q=[]
for i in range(n):
  if i==0:
    q.append(A[i])
  else:
    q+=[A[i]]*2
print((sum(q[:n-1])))

# Optimized solution
n=int(eval(input()))
A=list(map(int,input().split()))
A.sort(reverse=True)
q=[]
for i in range(n):
  if i==0:
    q.append(A[i])
  else:
    q+=[A[i]]*2
print((sum(q[:n-1])))
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))
A.sort(reverse = True)
Sum = 0
for i in range(len(A)):
    if i == 0:
        pass
    elif i == 1:
        Sum += A.pop(0)
    else:
        if i % 2 == 0:
            Sum += A[0]
        else:
            Sum += A.pop(0)
print(Sum)

# Optimized solution
N = int(eval(input()))
A = list(map(int, input().split()))
A.sort()
Sum = 0
for i in range(len(A)):
    if i == 0:
        pass
    elif i == 1:
        Sum += A.pop()
    else:
        if i % 2 == 0:
            temp = A.pop()
            Sum += temp
        else:
            Sum += temp
print(Sum)
# Original solution
import sys
from collections import deque

input = sys.stdin.readline

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


# Optimized solution
import sys

input = sys.stdin.readline

N = int(eval(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)
# Original solution
n = int(eval(input()))
a = list(map(int,input().split()))
a.sort(reverse=True)

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

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

ans = 0
for i in range(1,N):
    ans += A[i//2]
print(ans)
# Original solution
n=int(eval(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)

# Optimized solution
n=int(eval(input()))
a=sorted(list(map(int,input().split())),reverse=1)
b=0
for i in range(n-1):
    b+=a[(i+1)//2]
print(b)
# Original solution
n = int(eval(input()))
a = list(map(int, input().split()))
a.sort()

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

# Optimized solution
n = int(eval(input()))
a = sorted(list(map(int, input().split())))[::-1]
print((a[0] + sum(a[1:n // 2]) * 2 + sum(a[n // 2:n // 2 + n % 2])))
# Original solution
from collections import deque

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

A.sort(reverse=True)

a = deque(A)

arrival = 1
ans = 0

while arrival < N:
    score = a.popleft()
    if arrival == 1:
        ans += score
        arrival += 1
    else:
        ans += score*min(2, N - arrival)
        arrival += 2

print(ans)

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

a.sort()

arrival = 1
ans = 0

while arrival < N:
    score = a.pop()
    if arrival == 1:
        ans += score
        arrival += 1
    else:
        ans += score*min(2, N - arrival)
        arrival += 2

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

A.sort(reverse = True)
circle = [A[0]]
ans = 0

for i in range(1,N):
    max = 0
    l = len(circle)
    for j in range(l):
        if max < min(circle[j],circle[(j+1)%l]):
            ins = j
            max = min(circle[j],circle[(j+1)%l])
    circle.insert(ins,A[i])
    ans += max

print(ans)


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

A.sort(reverse = True)


ans2 = A[0]
cnt = 0
j = 1
for i in range(1,N-1):
    if cnt == 2:
        j += 1
        cnt = 0
    ans2 += A[j]
    cnt += 1

print(ans2)
# Original solution
# -*- coding: utf-8 -*-

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

a_sorted = sorted(A, key=lambda x: -x)

circle = [a_sorted[0]]
ans = 0
for a in a_sorted:
    tmp = 0
    tmp_idx = -1
    for i in range(len(circle) - 1):
        cand = min(circle[i], circle[i + 1])
        if cand > tmp:
            tmp = cand
            tmp_idx = i
    ans += tmp
    circle.insert(tmp_idx + 1, a)

print(ans)


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

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

a_sorted = sorted(A, key=lambda x: -x)

circle = [a_sorted[0]]
ans = 0
for idx, a in enumerate(a_sorted[1:]):
    ans += circle[idx]
    circle.extend([a, a])

print(ans)
# Original solution
n = int(eval(input()))
friendly = [int(i) for i in input().split()]
friendly.sort(reverse=True)
place = []
place.append(friendly[0])
place=place*2
ans=0
for i in friendly[1:]:
    comfortable=[min(place[i], place[i+1]) for i in range(len(place)-1)]
    ans += max(comfortable)
    place.insert(comfortable.index(max(comfortable))+1, i)
print(ans)

# Optimized solution
n = int(eval(input()))
friendly = list(map(int, input().split()))
friendly.sort(reverse=True)
if len(friendly)<=2:
    print((sum(friendly[0:n-1])))
else:
    ans = friendly[0]+sum(friendly[1:(n+1)//2-1])*2+friendly[(n+1)//2-1]*((n+1)%2+1)
    print(ans)
# Original solution
N = int(eval(input()))
alist = list(map(int, input().split()))
alist.sort(reverse=True)
sumscore=0
posit = list()
posit.append(alist[0])
for n in range(1,N):
  sumscore = sumscore + posit[0]
  posit = posit[1:]
  posit.append(alist[n])
  posit.append(alist[n])

print(sumscore)

# Optimized solution
N = int(eval(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)
# Original solution
eval(input())
l = sorted(map(int, input().split(' ')))

answer = 0
spots = [l.pop()]

while l:
	n = l.pop()
	answer += spots[0]
	spots = spots[1:] + [n] * 2

print(answer)


# Optimized solution
eval(input())
l = sorted(map(int, input().split(' ')))

answer = 0
spots = [l.pop()]
x = len(l)

for i in range(x):
	n = l.pop()
	answer += spots[i]
	spots.append(n)
	spots.append(n)

print(answer)
# Original solution
def MergeSort(num_list):
  n = len(num_list)
  
  if len(num_list) == 1:
    return num_list
  
  #1. 整列されていないリストを２つのサブリストに分割する
  mid = int(round(n/2))
  
  left = num_list[:mid]
  right = num_list[mid:]
  

  
  #2. 分割したリスト（サブリスト）を整列する。サブリストもマージソートを使って整列させる。（再帰関数）
  l = MergeSort(left)
  r = MergeSort(right)
  
  #3. 2つのサブリストをマージして１つの整列済みリストにする
  return Merge(l,r)
  
# 整列済みリストA、Bを使って、整列済みリストCを作成する
def Merge(A,B):
  C = []
#   print("A",A)
#   print("B",B)
  
  # 1. AとBの先頭の数字を比較し、小さい方をCに追加し追加した値はリストから削除する
  # 2. 手順1をどちらか一方のリストが空になるまで繰り返す。
  while (len(A) > 0) and (len(B) > 0):
    if A[0] < B[0]:
      C.append(A[0])
      del A[0]
    else:
      C.append(B[0])
      del B[0]
  
  # 3. 残った要素をCの末尾に追加する
  if len(A) > 0:
    C.extend(A)
  else:
    C.extend(B)
 
#   print("C",C)
  return C

n = int(eval(input()))
array =list(map(int, input().split()))

list = MergeSort(array)

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




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

list.sort(reverse=True)

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

ans = 0
B = []
for a in A:
    if len(B)==0:
        ans += 0
        B.append(a)
    elif len(B)==1:
        ans += B[0]
        B.append(a)
    else:
        max_ = min(B[0],B[-1])
        idx = len(B)-1
        for i in range(len(B)-1):
            if min(B[i],B[i+1]) > max_:
                max_ = min(B[i],B[i+1])
                idx = i
        ans += max_
        B = B[:idx] + [a] + B[idx:]

print(ans)

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

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

print(ans)
# Original solution
n = int(eval(input()))
lis = list(map(int, input().split()))

lis = sorted(lis, reverse=True)

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

print(ans)

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

lis = sorted(lis, reverse=True)

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

print(ans)
# Original solution
from collections import deque


def main():
    eval(input())  # n
    friendliness = [int(x) for x in input().split()]
    friendliness.sort()
    friendliness = deque(friendliness)
    edges = deque()
    popped = friendliness.pop()
    edges.append(popped)
    ans = 0
    while friendliness:
        ans += edges.pop()
        popped = friendliness.pop()
        edges.append(popped)
        edges.append(popped)
        edges = deque(sorted(edges))
    print(ans)


if __name__ == '__main__':
    main()


# Optimized solution
from collections import deque


def main():
    n = int(eval(input()))  # n
    friendliness = [int(x) for x in input().split()]
    friendliness.sort()
    friendliness = deque(friendliness)
    edges = deque()
    popped = friendliness.pop()
    edges.append(popped)
    ans = 0
    counter = 0
    while friendliness:
        ans += edges.popleft()
        counter += 1
        popped = friendliness.pop()
        edges.append(popped)
        edges.append(popped)
    print(ans)


if __name__ == '__main__':
    main()
# Original solution
from collections import deque


def main():
    eval(input())  # n
    friendliness = [int(x) for x in input().split()]
    friendliness.sort()
    friendliness = deque(friendliness)
    edges = deque()
    popped = friendliness.pop()
    edges.append(popped)
    ans = 0
    while friendliness:
        ans += edges.popleft()
        popped = friendliness.pop()
        edges.append(popped)
        edges.append(popped)
    print(ans)


if __name__ == '__main__':
    main()


# Optimized solution
from collections import deque


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


if __name__ == '__main__':
    main()
# Original solution
from collections import deque


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


if __name__ == '__main__':
    main()


# Optimized solution
from collections import deque


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


if __name__ == '__main__':
    main()
# Original solution
from collections import deque


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


if __name__ == '__main__':
    main()


# Optimized solution
from collections import deque


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

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


if __name__ == '__main__':
    main()
# Original solution
from collections import deque


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

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


if __name__ == '__main__':
    main()


# Optimized solution
def main():
    n = int(eval(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()
# Original solution
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
def s(): return eval(input())
def i(): return int(eval(input()))
def S(): return input().split()
def I(): return list(map(int,input().split()))
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10**9)
INF = 10**9
mod = 10**9+7

N = i()
A = l()
A.sort(reverse=True)
ans = 0
lis = [A[0]]
A = A[1:]
for i in range(N-1):
    lis.append(A[i])
    lis.append(A[i])
for i in range(N-1):
    ans += lis[i]
print(ans)

# Optimized solution
N=int(eval(input()))
l=sorted(list(map(int,input().split())))
l.reverse()
s=l[0]+N%2*l[N//2]
for i in range(N//2-1):
  s+=l[i+1]*2
print(s)
# Original solution
N = int(eval(input()))
A = list(map(int, input().split()))

A.sort(reverse=True)
ans = 0
tmp = 0
for i in range(N):
    if i == 0:
        continue
    elif i == 1:
        ans += A.pop(0)
    elif i % 2 == 0:
        tmp = A.pop(0)
        ans += tmp
    else:
        ans += tmp
print(ans)

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

A.sort()
ans = 0
tmp = 0
for i in range(N):
    if i == 0:
        continue
    elif i == 1:
        ans += A.pop()
    elif i % 2 == 0:
        tmp = A.pop()
        ans += tmp
    else:
        ans += tmp
print(ans)