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

A.sort(reverse=True)

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

Solution (Python):
n=int(input())
a=list(map(int, input().split()))
a.sort(reverse=True)
ans=0
cnt=0
i=1
while cnt<n-1:
    ans+=a[(cnt+1)//2]
    cnt+=1
print(ans)

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

forret = sorted(A_i)
ret = forret[-1]
for i in range(N-2):
    ret = ret + forret[-1-1-(i//2)]
print(ret)

Solution (Python):
num = int(input())
a_i = list(map(int, input().split()))
list.sort(a_i, reverse=True)
if num <= 3:
    print(sum(a_i[:num - 1]))
else:
    ans = sum(a_i[:2])
    for x in range(3, num):
        if x == 3: ans += a_i[1]
        else: ans += a_i[x // 2]
    print(ans)

Solution (Python):


import math

import itertools
from collections import deque
from collections import Counter



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

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

A.sort(reverse = True)

ans = A[0]
re = [A[1]]
n = 0
for num in A[2:]:
     re.append(num)
     if num < re[0]:
          n += 1
     ans += re[0]
     if n == 2:
         re.pop(0)
         n = 0

print(ans)

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

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

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

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

Solution (Python):
from heapq import heappush, heappop, heapify
from collections import deque, defaultdict, Counter
import itertools
from itertools import permutations, combinations, accumulate, product, combinations_with_replacement
import sys
import bisect
import string
import math
import time


def I(): return int(input())
def S(): return input()
def MI(): return map(int, input().split())
def MS(): return map(str, input().split())
def LI(): return [int(i) for i in input().split()]
def LI_(): return [int(i)-1 for i in input().split()]
def StoI(): return [ord(i)-97 for i in input()]
def ItoS(nn): return chr(nn+97)
def input(): return sys.stdin.readline().rstrip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def print_matrix(mat):
    for i in range(len(mat)):
        print(*['IINF' if v == IINF else "{:0=4}".format(v) for v in mat[i]])


yn = {False: 'No', True: 'Yes'}
YN = {False: 'NO', True: 'YES'}
MOD = 10**9+7
inf = float('inf')
IINF = 10**19
l_alp = string.ascii_lowercase
u_alp = string.ascii_uppercase
ts = time.time()
sys.setrecursionlimit(10**6)
nums = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']
show_flg = False



def main():
    N = I()
    A = LI()
    A = sorted(A, reverse=True)
    max_a = max(A)
    ans = 0
    arr = []
    cnt = 0
    cur = 0
    v = max_a

    while cur < len(A) and v == A[cur]:
        cur += 1
        cnt += 1
        arr.append(v)
        arr.append(v)

    while cur < len(A):
        cnt = 0
        while cur < len(A) and v == A[cur]:
            cur += 1
            cnt += 1

        
        for i in range(cnt * 2):
            arr.append(v)
        if cur < len(A):
            v = A[cur]
        if len(arr) >= 2 * N:
            break

    
    
    
    print(sum(arr[1:N]))


if __name__ == '__main__':
    main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
S = sorted(A, reverse=True)
X = S[0]
N -= 2
i = 0
while N > 0:
    i += 1
    x = S[i]
    X += x
    if N == 1:
        break
    X += x
    N -= 2
print(X)

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


so = sorted(a)

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

Solution (Python):
n = int(input())
x=n
lol=[int(n) for n in input().split()]

ans = 0
lol.sort()
lol.reverse()
z=lol.copy()
ans +=lol[0]
lol.remove(max(lol))
n=n-2

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

Solution (Python):
from collections import deque

n = int(input())
d = deque()
a = [int(x) for x in input().split()]
a = sorted(a)

i = a.pop()
d.append(i)

ans = 0
while a:
    arrive = a.pop()
    get_ease = d.popleft()
    ans += get_ease
    d.append(arrive)
    d.append(arrive)
    
    
print(ans)

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

ans = 0
if len(a) % 2 == 0:
  b = a[:len(a)//2]
  ans = (2 * sum(b)) - b[0]
else:
  b = a[:len(a)//2 +1]
  ans = 2 * sum(b) - b[0] - b[-1]

print(ans)

Solution (Python):
from collections import deque

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

li.sort(reverse=True)

d = deque()
ans = 0

for i in range(n):
    if i == 0:
        d.append(li[i])
    elif i == 1:
        d.append(li[i])
        d.append(li[i])
        ans += d.popleft()
    else:
        d.append(li[i])
        d.append(li[i])
        ans += d.popleft()
print(ans)

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

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

Solution (Python):
n = input()
a = [int(x) for x in input().strip().split()]
count = {}
for x in a:
    if x not in count:
        count[x] = 0
    count[x] += 1
sort_keys = sorted(list(count.keys()), reverse=True)

result = 0
position = []
for idx, current in enumerate(sort_keys):
    value = count[current]
    if idx == 0:
        result += current * (value - 1)
        position.append([current, value])
    else:
        new_pos = 0
        while len(position) > 0 and value > 0:
            if position[0][1] > value:
                result += position[0][0] * value
                new_pos += value * 2
                position[0][1] -= value
                value = 0
            else:
                result += position[0][1] * position[0][0]
                new_pos += position[0][1] * 2
                value -= position[0][1]
                position.remove(position[0])
        if value > 0:
            new_pos += value
            result += current * value
        position.append([current, new_pos])
print(result)

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

N=int(input().rstrip('\n'))
As=[int(x) for x in input().rstrip('\n').split()]
As_s=sorted(As,reverse=True)
res = As_s[0]
num = 2
if N>2:
  for i in As_s[1:]:
    res += i
    num += 1
    if num ==N:
      break
    res += i
    num += 1
    if num==N:
      break
print(res)

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

Solution (Python):
def resolve():
    N = int(input())
    A = sorted(list(map(int, input().split())))
    import collections
    pointcands = collections.deque([A[-1]])
    ans = 0
    A = collections.deque(A[:-1])
    while A:
        point = pointcands.pop()
        ans += point
        arrived = A.pop()
        pointcands.appendleft(arrived)
        pointcands.appendleft(arrived)
    print(ans)



    

if '__main__' == __name__:
    resolve()

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

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

Solution (Python):
N = int(input())
lA = list(map(int,input().split()))
lA.sort(reverse=True)
cnt = lA[0]
for a in range(2,len(lA)):
  cnt += lA[a // 2]
print(cnt)

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

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

a.sort(reverse=True)
ans = a[0]

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

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

Solution (Python):
import sys, re
from math import ceil, floor, sqrt, pi, factorial, gcd
from copy import deepcopy
from collections import Counter, deque
from heapq import heapify, heappop, heappush
from itertools import accumulate, product, combinations, combinations_with_replacement
from bisect import bisect, bisect_left, bisect_right
from functools import reduce
from decimal import Decimal, getcontext

def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_list(N): return [s_list() for _ in range(N)]
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7
num_list = []
str_list = []

def main():
    n = i_input()
    num_list = i_list()

    co_list = Counter(num_list)

    count = 0

    num_list = list(set(num_list))
    num_list.sort(reverse=True)

    ans_list = []

    for i, num in enumerate(num_list):
        num_c = co_list[num]

        for _ in range(num_c*2):
            ans_list.append(num)
        if i == 0:
            ans_list.pop()

    for i in range(n-1):
        count += ans_list[i]
        
    print(count)
 
if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
scores = deque()

result = 0

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

scores.append(l[0])

for i in range(1, N):
    scores.append(l[i])
    scores.append(l[i])
    result += scores.popleft()

print(result)

Solution (Python):
import heapq

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

comf = [-a.pop()]
heapq.heapify(comf)
res = 0
for _ in range(n-1):
    v = a.pop()
    res -= heapq.heappop(comf)
    heapq.heappush(comf, -v)
    heapq.heappush(comf, -v)
print(res)

Solution (Python):
N = int(input())
a = [int(i) for i in input().split()]

a.sort()
f = 1
ans = 0
while N>0:
    n = a.pop()
    cnt = 1
    while len(a) != 0 and n == a[-1]:
        a.pop()
        cnt += 1
    if N - (cnt*2 - f) <= 0:
        ans += (N-f) * n
    else:
        ans += (cnt * 2 - f) * n
    N -= cnt * 2 
    f *= 0

print(ans)

Solution (Python):



N=int(input())
Alist=list(map(int,input().split()))
Alist.sort(reverse=True)
kokochiyosa=Alist[0]
for i in range(1,N-1):
    kokochiyosa+=Alist[(i+1)//2]

print(kokochiyosa)

Solution (Python):
from collections import deque


def main():
    n = int(input())
    A = [int(e) for e in input().split()]

    A.sort(reverse=True)
    D = deque([A[0]])
    ans = 0
    for i in range(1, n):
        D.extend([A[i], A[i]])
        ans += D.popleft()

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import Counter

def m():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort(reverse=True)
    ans = A[0]
    c = Counter(A[1:])
    d = list(c.items())
    d.sort(reverse=True, key=lambda x:x[0])
    tmp = []
    for k,v in d:
        for i in range((v-1)+(v+1)):
            tmp.append(k)
    ans += sum(tmp[:N-2])
    return ans
print(m())

Solution (Python):
import sys
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
read = sys.stdin.buffer.read
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

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

A.sort(reverse=True)


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

print(ans)

Solution (Python):
import sys


def main():
    def readline_and_rstrip() -> str:
        s = sys.stdin.readline()
        return s.rstrip()

    n = int(readline_and_rstrip())
    a = [int(_) for _ in readline_and_rstrip().split()]

    a.sort(reverse=True)

    if n == 2:
        print(a[0])
    elif n == 3:
        print(a[0] + a[1])
    elif (n - 2) % 2 == 0:
        middle_len = (n - 2) // 2
        a_middle = a[1:1 + middle_len]
        print(a[0] + sum(map(lambda x: x * 2, a_middle)))
    else:
        middle_len = (n - 2) // 2
        a_middle = a[1:1 + middle_len]
        print(a[0] + sum(map(lambda x: x * 2, a_middle)) + a[1 + middle_len])


if __name__ == '__main__':
    main()

Solution (Python):
import sys

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def main():
    n = I()
    A =LI()
    A.sort(reverse=True)
    ans = A[0] + sum(A[1:n//2])*2
    if n%2:
        ans += A[n//2]
    print(ans)

if __name__ == '__main__':
    main()

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

a.sort(reverse=True)

score = 0

m = int(n / 2)

for i in range(m):
  score += a[i] * 2

if n % 2 == 1:
  score += a[m]
  
score -= a[0]

print(score)

Solution (Python):
n = int(input())
a = [int(i) for i in input().split()]
a.sort(reverse=True)
b = a[0]
for i in range(2, n):
    b += a[i // 2]
print(b)

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

c = 0
q = deque([a[0]])
for i in a[1:]:
    m = q.pop()
    c += m
    q.appendleft(i)
    q.appendleft(i)
print(c)

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

sA = sorted(A, reverse=True)

if N % 2 == 0:
    res = sum(sA[:N//2]) * 2 - sA[0]
else:
    res = sum(sA[:N//2]) * 2 - sA[0] + sA[N//2]

print(res)

Solution (Python):
n = int(input())
arr = sorted([int(x) for x in input().split()])
total = 0
x = n-2
c = 0
for i in range(2, n):
    total += arr[x]
    c += 1
    if c == 2:
        c = 0
        x -= 1
print(total+arr[-1])

Solution (Python):
n = int(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)

Solution (Python):
import numpy as np
import math


def _run(N, As):
    As.sort(reverse=True)
    sum_value = 0
    for i in range(1, N):
        index = math.ceil((i - 1) / 2)
        sum_value += As[index]
    print(sum_value)
    
    
    
    
    
    


def _main():
    N = int(input())
    As = list(map(int, input().split()))
    _run(N, As)


if __name__ == "__main__":
    _main()

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

Solution (Python):

import sys
import math as mt
input=sys.stdin.buffer.readline  
t=1



for _ in range(t):
    n=int(input())
      
    
    l=list(map(int,input().split()))
    l.sort(reverse=True)
    ans=l[0]
    cnt=0
    ind=1
    for i in range(2,n):
        if cnt==2:
            cnt=0
            ind+=1
        ans+=l[ind]
        cnt+=1
    print(ans)

Solution (Python):

import numpy as np
import sys
from collections import deque
from collections import defaultdict
import heapq
import collections
import itertools
import bisect
import math


def minu_int(i):
    return -1*int(i)


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


def z():
    return int(sys.stdin.readline())


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


def C(line):
    return [sys.stdin.readline() for _ in range(line)]


def primes(n):
    is_prime = [True] * (n + 1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5) + 1):
        if not is_prime[i]:
            continue
        for j in range(i * 2, n + 1, i):
            is_prime[j] = False
    return [i for i in range(n + 1) if is_prime[i]]


N = z()
A = zz()

A = sorted(A, reverse=True)
if (N == 2):
    print(max(A))
    exit()
elif (N == 3):
    print(sum(A[:2]))
    exit()
else:
    ans = A[0]
    index = 1
    for i in range(1, N-1):
        
        ans += A[index]
        if (i % 2 == 0):
            index += 1
    print(ans)

Solution (Python):
import sys
read = sys.stdin.read
readline = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 8)
INF = float('inf')
MOD = 10 ** 9 + 7

def main():
    N = int(readline())
    A = list(map(int, readline().split()))
    A.sort(reverse=True)
    ans = 0
    conut = 1
    for i in range(N):
        if i==0:
            ans += A[i]
            conut += 1
            if conut==N:
                break
        else:
            ans += A[i]
            conut += 1
            if conut == N:
                break
            ans += A[i]
            conut += 1
            if conut==N:
                break
    print(ans)



if __name__ == '__main__':
    main()

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, acos, atan, asin, log, log10, gcd
from itertools import permutations, combinations, product, accumulate, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

from decimal import *
import heapq
def debug(*args):
    if debugmode:
        print(*args)
def input(): return sys.stdin.readline().strip()
def STR(): return input()
def INT(): return int(input())
def FLOAT(): return float(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
def lcm(a, b): return a * b // gcd(a, b)
sys.setrecursionlimit(10 ** 9)
inf = sys.maxsize
mod = 10 ** 9 + 7
dx = [0, 1, 0, -1, 1, -1, -1, 1]
dy = [1, 0, -1, 0, 1, -1, 1, -1]
debugmode = True

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

Solution (Python):
import math

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

    A_sorted = sorted(A, reverse=True)
    ans = 0

    for i in range(math.ceil(N/2) - 1):
        ans += A_sorted[i]

    for i in range(math.ceil(N/2) - 1):
        ans += A_sorted[1+i]
    if N % 2 == 0:
        ans += A_sorted[N//2 - 1]

    print(ans)
    
main()

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

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

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

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

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

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

print(ans)

Solution (Python):
import sys
N=int(sys.stdin.readline().strip())
A=map(int, sys.stdin.readline().split())
A.sort(reverse=True)
N-=1
ans=0
for i,a in enumerate(A):
    if i==0:
        ans+=a
        N-=1
    else:
        if 2<=N:
            ans+=a*2
            N-=2
        elif N==1:
            ans+=a
            N-=1
        elif N==0:
            print ans
            quit()

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

if alen %2 == 1:
  s = 2* sum(a[::-1][ :(alen+1)//2 ]) - a[-1] - a[::-1][alen//2]
else:
  s = 2* sum(a[::-1][ :(alen)//2 ]) - a[-1] 
print(s)

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

Solution (Python):

"""
@author: H_Hoshigi
"""
import heapq
def main():
    N = int(input())
    A_lsit = list(map(int, input().split()))
    A_lsit.sort(reverse=True)

    answer = A_lsit[0]
    A_heap = [-(A_lsit[1])]*2
    heapq.heapify(A_heap)
    for i in range(2, N):
        max_in_heap = -(heapq.heappop(A_heap))
        answer += max_in_heap
        heapq.heappush(A_heap, -(A_lsit[i]))
        heapq.heappush(A_heap, -(A_lsit[i]))
    print(answer)

if __name__ == "__main__":
    main()

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

answer = 0
consume = N
for a in A:
    if consume == N:
        consume -= 2
        if N > 1:
            answer += a
        continue
    elif consume > 1:
        answer += a * 2
        consume -= 2
        continue
    elif consume == 1:
        answer += a
        consume -= 1
        continue
    else:
        break

print(answer)

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

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

A.sort(reverse=True)
confort_index = 1
now_confort_count = 2
ans = A[0]
for i in range(2,N):
  ans += A[confort_index]
  now_confort_count -= 1
  if now_confort_count == 0:
    confort_index += 1
    now_confort_count = 2

print(ans)

Solution (Python):
n = int(input())
a_list = list(map(int, input().split(" ")))
sorted_a_list = sorted(a_list, reverse=True)
total_comfort = 0
if n%2==0:
    right_index = n/2
    for i, i_comfort in enumerate(sorted_a_list):
        if i==right_index:
            break
        if i==0:
            total_comfort+=i_comfort
        else:
            total_comfort+=i_comfort*2
else:
    right_index = (n+1)/2
    for i, i_comfort in enumerate(sorted_a_list):
        if i==right_index:
            break
        if i==0 or i==right_index-1:
            total_comfort+=i_comfort
        elif i==right_index-1:
            total_comfort+=i_comfort
        else:
            total_comfort+=i_comfort*2
print(total_comfort)

Solution (Python):
import itertools
import copy
from collections import deque
import heapq
n = int(input())
a = list(map(int,input().split()))
a.sort()
a = a[::-1]
p = a[0]
c = 1
ans = 0
cnt = 0
for i in range(n-1):
    ans += p
    if c == 0:
        c += 1
    else:
        c = 0
        cnt += 1
        p = a[cnt]
    
print(ans)

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

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

L = [A[0]]
i = 0
ans = 0
for a in A[1:]:
    ans += L[i]
    i += 1
    L.append(a)
    L.append(a)

print(ans)

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

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

A.sort(reverse=True)

h = []
heapq.heappush(h,(-1) * A[0])
ans = 0
for i in range(1,N):
  wk = heapq.heappop(h) * (-1)
  ans += wk
  heapq.heappush(h,(-1) * A[i])
  heapq.heappush(h,(-1) * A[i])
  
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())
A = list(map(int, input().split()))
A.sort(reverse=True)
ans = 0
ans += A[0]
for i in range((n-2) // 2):
    ans += 2 * A[i+1]
if n % 2 != 0:
    ans += A[(n-1)//2]
print(ans)

Solution (Python):
"""
AtCoder :: Beginner Contest 173 :: Chat in a Circle
https://atcoder.jp/contests/abc173/tasks/abc173_d
"""
import sys
import heapq

def solve(players):
    """Solve puzzle."""
    players.sort(reverse=True)
    circle = []
    heapq.heappush(circle, -players[1])
    heapq.heappush(circle, -players[1])

    comfort = players[0]
    for player in players[2:]:
        spot = -heapq.heappop(circle)
        comfort += spot
        heapq.heappush(circle, -player)
        heapq.heappush(circle, -player)
    return comfort


def main():
    """Main program."""
    player_count = int(sys.stdin.readline())
    players = [int(i) for i in sys.stdin.readline().split()]
    print(solve(players))


if __name__ == '__main__':
    main()

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

a.sort()
fir = a[n-1]

sco = 0
sit = deque([fir])
for i in range(n-1):
    sco += sit.popleft()
    sit.append(a[n-i-2])
    sit.append(a[n-i-2])
print(sco)

Solution (Python):
import numpy as np

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

A = sorted(A, reverse=True)
A = np.array(A)

if N % 2 == 0:
  
  end = (N - 2) // 2
  answer = A[0] + (A[1:end+1] * 2).sum()
else:
  
  end = (N - 2) // 2
  answer = A[0] + (A[1:end+1] * 2).sum() + A[end+1]

print(answer)

Solution (Python):
N  = int(input())
A = sorted([int(a) for a in input().split()], reverse=True)
cnt = N
if N == 2:
    ans = A[0]
else:
    ans = A[0]
    cnt = N - 2
    A = A[1:]
    chk = 0
    for a in A:
        if cnt == 0:
            break
        elif cnt == 1:
            ans += a
            break
        ans += 2*a
        cnt -= 2
print(ans)

Solution (Python):



from fractions import gcd
from itertools import combinations,permutations,accumulate 

from collections import deque,defaultdict,Counter
import decimal
import re











import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7

def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n = I()
A = sorted(readInts(),reverse=True)
ans = 0
dic = defaultdict(int)
dq = deque()
for i in range(n):
    
    if i == 0:
        dq.append(A[i])
    else:
        ans += dq.popleft()
        for j in range(2):
            dq.append(A[i])
print(ans)

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

n = int(input())

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

if n == 2:
    print(AS[0])
else:
    m = math.ceil(n / 2)
    ans = 0
    for i in range(m):
        if i == 0:
            ans += AS[0]
        elif i < m-1:
            ans += AS[i]*2
        else:
            if n % 2 == 0:
                ans += AS[i]*2
            else:
                ans += AS[i]
    print(ans)

Solution (Python):


import math
import numpy as np


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


A = sorted(A, reverse=True)
A = np.array(A)
ans = 2 * np.sum(A[:(N // 2)]) - A[0] + (N % 2) * A[math.floor(N / 2)]


print(ans)

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

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


sr = sorted(coms, reverse=True)

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

print(ans)

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


comfort = 0
for i in range(1, n):
    parent = i // 2
    comfort += as_[parent]

print(comfort)

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

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