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

a.sort(reverse=True)
cnt = 1
ans = a[0]
for ai in a[1:]:
    if cnt + 2 >= n-1:
        ans += ai * (n-1-cnt)
        break
    ans += 2 * ai
    cnt += 2

print(ans)

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

ans = A[0]
if N >= 3:
    a = (N - 2) // 2
    b = (N - 2)  % 2


    if b == 0:
        for i in range(a):
            ans += (A[i+1] * 2)
    else:
        for i in range(a):
            ans += (A[i+1] * 2)
        ans += A[a+1]
print(ans)

Solution (Python):


import sys
iim = lambda: map(int, sys.stdin.readline().rstrip().split())

def resolve():
    it = map(int, sys.stdin.read().split())
    N = next(it)
    A = list(it)

    A.sort(reverse=True)
    ans = A[0] + 2*sum(A[1:(N-1)//2+1])
    if N % 2:
        ans -= A[(N-1)//2]

    print(ans)

if __name__ == "__main__":
    resolve()

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

A.sort()
A.reverse()

list = []
ans = 0
list.append(A[0])
for i in range(1, len(A)):
        ans += list[i - 1]
        list.append(A[i])
        list.append(A[i])

print(ans)

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

A.sort(reverse= True)

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

print(ans)

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

Solution (Python):
N = int(input())
FRI_STRING=input().split()
FRI = [int(s) for s in FRI_STRING]
 
import numpy as np
ARR = np.array(FRI)
ARR = np.sort(ARR)
ARR = ARR[::-1]
 
count = 0
sum = 0
for i in range(len(FRI)-1):
  
  sum = sum + ARR[count]
  if i % 2 == 0:
    count = count + 1
  
    
print(sum)

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

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

main()

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

Solution (Python):

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(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))  
    A = [int(next(tokens)) for _ in range(N)]  
    solve(N, A)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
import math


from sys import stdin,stdout
from collections import deque,defaultdict
from math import ceil,floor,inf,sqrt,factorial,gcd,log2
from copy import deepcopy
ii1=lambda:int(stdin.readline().strip())
is1=lambda:stdin.readline().strip()
iia=lambda:list(map(int,stdin.readline().strip().split()))
isa=lambda:stdin.readline().strip().split()
mod=1000000007

n=ii1()
arr=iia()
ans=0
arr.sort(reverse=True)
for i in range(1,n):
    ans+=arr[i//2]
print(ans)

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

Solution (Python):

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

        if c_max == sl[i]:
            h[c_max] += 1
        else:
            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(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))  
    A = [int(next(tokens)) for _ in range(N)]  
    solve(N, A)

if __name__ == '__main__':
    main()

Solution (Python):

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

Solution (Python):

import heapq

N = int(input())
A = [int(s) for s in input().split()]
A_s = zip(list(range(N)), A)
A_s = sorted(A_s, key=lambda x: x[1], reverse=True)

kokoti = 0
kokoti += A_s[0][1]


h = []
heapq.heapify(h)  
heapq.heappush(h, (-A_s[1][1]))
heapq.heappush(h, (-A_s[1][1]))

for i in range(2, N):
    x = heapq.heappop(h)  
    kokoti += (-x)
    
    
    heapq.heappush(h, (-A_s[i][1]))  
    heapq.heappush(h, (-A_s[i][1]))

print(kokoti)

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

ans = 0
A.sort(reverse=True)

from collections import deque
d = deque(A)
for i in range(1,N):
    if i == 1:
        ans += d.popleft()
        continue
    if i % 2 == 0:
        ans += d[0]
    else:
        ans += d.popleft()

print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
A_new = sorted(A, reverse=True)
A_feel = []
sum = 0
for i in range(N):
  if A_feel == []:
    count = 1
  else:
    count = 2
  for j in range(count):
    A_feel.append(A_new[i])

for i in range(N-1):
  sum += A_feel[i]
  
print(sum)

Solution (Python):
from collections import deque


def main():
    input()  
    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()

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

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

print (ans)

Solution (Python):
from collections import deque


def main():
    input()  
    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()

Solution (Python):
import sys
import itertools


def resolve(in_):
    n = int(in_.readline())
    a = list(map(int, in_.readline().split()))
    assert len(a) == n
    a.sort(reverse=True)
    ans = a[0]
    ans += sum(itertools.islice(itertools.chain.from_iterable((x, x) for x in a[1:]), n - 2))

    return ans


def main():
    answer = resolve(sys.stdin.buffer)
    print(answer)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque


def main():
    input()  
    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()

Solution (Python):
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def exit(*argv, **kwarg):
    print(*argv, **kwarg)
    sys.exit()


def mina(*argv, sub=1): return list(map(lambda x: x - sub, argv))



def a_int(): return int(read())


def ints(): return list(map(int, read().split()))


def read_col(H):
    '''H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) 
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_tuple(H):
    '''H is number of rows'''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_matrix(H):
    '''H is number of rows'''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    


MOD = 10**9 + 7
INF = 2**31  

from collections import defaultdict, Counter, deque
from operator import itemgetter, xor, add
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  
from functools import reduce
from math import gcd


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





N = a_int()
A = ints()
A.sort()
ans = A.pop()
N -= 2
while N > 1:
    N -= 2
    ans += 2 * A.pop()
if N == 1:
    ans += A.pop()
print(ans)

Solution (Python):
n = int(input())
a = list(map(int, input().split(' ')))
 
def merge_sort(arr):
    if len(arr) <= 1:
        return arr
 
    mid = len(arr) // 2
    
    left = arr[:mid]
    right = arr[mid:]
 
    
    left = merge_sort(left)
    right = merge_sort(right)
 
    
    return merge(left, right)
 
 
def merge(left, right):
    merged = []
    l_i, r_i = 0, 0
 
    
    while l_i < len(left) and r_i < len(right):
        
        if left[l_i] >= right[r_i]:
            merged.append(left[l_i])
            l_i += 1
        else:
            merged.append(right[r_i])
            r_i += 1
 
    
    if l_i < len(left):
        merged.extend(left[l_i:])
    if r_i < len(right):
        merged.extend(right[r_i:])
    return merged
 
s = merge_sort(a)
ans=s[0]
for i in range(1,n//2):
    ans += s[i]*2
if n%2 == 1:
    ans += s[n//2]
 
print(ans)

Solution (Python):
from collections import deque


def main():
    n = int(input())  
    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()

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

Solution (Python):


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

A.sort(reverse=True)


"""
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])
  
"""

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

print(ans)

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

Solution (Python):
from sys import stdin
N = int(stdin.readline().rstrip())
ran = [int(x) for x in stdin.readline().rstrip().split()]
ran.sort(reverse=True)

gen = len(ran) - 2
goukei = ran[0]
if gen % 2 == 0:
  for i in range(int(gen/2)):
    goukei += 2 * ran[i + 1]
else:
  for i in range(int((gen-1)/2)):
    goukei += 2 * ran[i + 1]
  goukei += ran[i + 2]

print(goukei)

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

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

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

A.sort(reverse=True)


ans = A[0]

count = 1
guuki = 0
i = 1

while count < n-1:
    ans += A[i]

    guuki += 1
    count += 1
    if guuki != 1:
        guuki = 0
        i += 1




print(ans)

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

def li():
    return [int(x) for x in input().split()]

N = int(input())
A = li()

A.sort(reverse=True)

B = [A[i] for i in range(N) if i < N//2]










if N % 2 == 1:
    B = [A[i] for i in range(N) if i < N // 2 + 1]
    sm = B[0] + B[-1] + sum(B[i]*2 for i in range(N//2) if 0 < i)
    print(sm)
else:
    sm = B[0] + sum(B[i]*2 for i in range(N//2) if 0 < i)
    print(sm)

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)
				cnt += 1
			else:
				pgt.put(val)
				if cnt == N:
					pgt.put(val)

	
	print(sum(ansans))




if __name__ == "__main__":
	main()

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

Solution (Python):


import collections
N = int(input())
A = sorted(list(map(int, input().split())))[::-1]

ret = A[0]

cnt = 2
for i in range(1, N):
    for j in range(2):
        if cnt == N:
            print(ret)
            exit()
        ret += A[i]
        cnt += 1

print(ret)

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

print(ans)

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

current = 1
count = 0
comfort = Alist[0]
for i in range(2,N):
  if (count < 2):
    count += 1
    comfort += Alist[current]
  else:
    count = 1
    current += 1
    comfort += Alist[current]
    
    

print(comfort)

Solution (Python):
from collections import deque

n = int(input())
a = list(map(int,input().split()))
a.sort(reverse=True)
d = deque()
ans = a[0]
d.append(a[1])
d.append(a[1])
for i in a[2:]:
    ans+=d.popleft()
    d.append(i)
    d.append(i)
print(ans)

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

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

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

    if N % 2 == 1:
        ans -= A[N // 2]
    
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

import sys
import math

def main():
  
  X = int(input())

  if X % 2 ==0:
    
    arr = list(map(int, input().split()))
    arr = sorted(arr, reverse=True)

    nu = X/2
    arr2 = arr[0:int(nu)]
    print(sum(arr2)*2-arr2[0])
  else:
    arr = list(map(int, input().split()))
    arr = sorted(arr, reverse=True)

    nu = int(math.floor(X/2))
    arr2 = arr[0:nu]
    print(sum(arr2)*2+arr[nu]-arr2[0])

  
  
  sys.exit()

if __name__ == '__main__':
    main()

Solution (Python):



from fractions import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations, combinations_with_replacement

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

ret = A[0]
ct = 0
ni = []
for i in range(1, N-1):

    ret += A[(i+1) // 2]

print(ret)

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

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
a.sort() 
cnt = 0 
ans = a[-1]*-1 
c = 1
while cnt!=n:
    cnt+=1
    if c==1:
        k=a.pop()
    ans += k
    c *= -1

print(ans)

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

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

Solution (Python):
import math
from functools import reduce
from collections import deque
import sys
sys.setrecursionlimit(10**7)

def input():
    return sys.stdin.readline().strip()


def get_nums_l():
    return [ int(s) for s in input().split(" ")]


def get_all_int():
    return map(int, open(0).read().split())

def log(*args):
    print("DEBUG:", *args, file=sys.stderr)

def main():
    INF = 999999999999999999999999
    MOD = 10**9+7

    n,*A = get_all_int()
    A = list(A)
    A.sort(reverse=True)

    c = 1
    ans = A[0]
    i = 1
    flag = False
    while(c < n-1):
        ans += A[i]
        c += 1
        if flag:
            i+=1
            flag = False
        else:
            flag = True
    
    print(ans)


main()

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

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

Solution (Python):

def main():
  n = int(input())

  arr = sorted(map(int, input().split()), reverse=True)

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

  print(ans)


main()

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
a.sort()
cnt=0
ans=-1*a[-1]
c = 1
while cnt!=n:
    cnt+=1
    if c==1:
        k=a.pop()
    ans+=k
    c*=-1
 
print(ans)

Solution (Python):
N = int(input())
A = [int(i) for i in input().split()]
A = sorted(A,reverse=True)
ans = A[0]
if N==2:
    print(ans)
else:
    A = A[1:]
    for i in range(N-2):
        idx = i//2
        ans += A[idx]
    print(ans)

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

A.sort(reverse=True)

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

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

A = sorted(A, reverse=True)

result = A[0]
cnt =1
position = 1
flag = 0
while cnt<N-1:
    result += A[position]
    cnt += 1
    if cnt == N-1:
        break
    result += A[position]
    cnt +=1
    position += 1

print(result)

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

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

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

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

Solution (Python):


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

a_l.sort(reverse=True)
ans = -a_l[0]
now = a_l[0]
count = 1
for i in range(n):
    if i % 2 == 0:
        ans += now
    else:
        ans += now
        now = a_l[count]
        count += 1
print(ans)

Solution (Python):
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
if n==2:
    exit(print(max(a)))
b=[a[0],a[1]]
ans=a[0]
a=a[1:]
from math import floor
for i in range(n-2):
    num=floor(i/2)
    ans+=a[num]
print(ans)

Solution (Python):
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(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)
        
        ans += -best_pair[0]
        eq.insert((-a, best_pair[0]))
        eq.insert((-a, best_pair[1]))

    print(ans)

if __name__ == '__main__':
    main()

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

a=sorted(a,reverse=True)
if n==2:
    print(max(a))
    exit(0)

ans=a[0]
a=a[1:]
from math import floor

for i in range(n-2):
    index=floor(i/2)
    ans+=a[index]

print(ans)

Solution (Python):
n = int(input())
l = list(map(int,input().split()))
l_sort = sorted(l,reverse=True)
l_len = len(l)
if(l_len%2==0):
    ans = sum(l_sort[:(l_len//2)-1])+sum(l_sort[1:(l_len//2)])+l_sort[(l_len//2)-1]
else:
    ans = sum(l_sort[:(l_len//2)])+sum(l_sort[1:(l_len//2)+1])

print(ans)

Solution (Python):
n = int(input())
a = list(map(int,input().split()))
a.sort()
cnt=0
ans=-1*a[-1]
c = 1
while cnt!=n:
    cnt+=1
    if c==1:
        k=a.pop()
    ans+=k
    c*=-1

print(ans)

Solution (Python):
def main():
    N = int(input())
    A = list(map(int, input().split()))
    A = sorted(A)
    B = A[::-1]
    C = N-1
    ans = 0
    if N%2 == 0:
      ans += B[0]
      for i in range(N//2-1):
        ans += 2*B[i+1]
    else:
      ans += B[0]
      ans += B[N//2]
      for i in range(N//2-1):
        ans += 2*B[i+1]
    print(ans)    
    
if __name__ == '__main__':
    main()

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

sum=l[-1]
for i in range(1,n//2):
  sum+=2*l[-1-i]
if n%2!=0:
  sum+=l[n//2]
print(sum)

Solution (Python):
import numpy as np

N = int(input())
a = list(map(int,input().split()))
A = np.sort(np.array(a+a))

print(A[N:-1].sum())

Solution (Python):
import numpy as np

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

Solution (Python):
n = int(input())
a = sorted(list(map(int, input().split())),reverse=True)
i = 0
cnt = 1
ans = a[0]
a.pop(0)
while i < n-1 or cnt < n - 1:
    for j in range(2):
        if cnt == n - 1:
            break
        cnt += 1
        ans += a[i]
        
    i += 1
print(ans)

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

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

A.sort()
A.reverse()

ans = 0

if len(A) == 2:
  print(A[0])
  exit()
count = 0
for i in range(len(A)):
  if i == 0:
    ans += A[i]
    count += 1
  else:
    ans += A[i]
    count += 1
    if count == len(A)-1:
      break
    count += 1
    ans += A[i]
    if count == len(A)-1:
      break

print(ans)

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

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

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

n = int(input())
A = list(map(int, input().split()))
A = [-a for a in A]

A.sort()

from heapq import *

ans = -A[0]

q = A[:2][:]
heapify(q)
q[0] = q[-1]

for a in A[2:]:
    ans += -q[0]
    t1 = heappop(q)
    t1 = max(t1, a)
    heappush(q, t1)
    heappush(q, a)
print(ans)

Solution (Python):
from collections import deque
import sys

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

ali.sort(reverse=True)

comforts = deque([ali[1], ali[1]])
answer = ali[0]
if len(ali) < 3:
    print(answer)
    sys.exit()

for i in ali[2:]:
    answer += comforts.pop()
    comforts.appendleft(i)
    comforts.appendleft(i)

print(answer)

Solution (Python):
import sys
import os
import itertools
import numpy as np
from collections import deque

def main():
    if os.getenv("ENV")=='LOCAL':
        sys.stdin = open(os.path.dirname(__file__)+'/input.txt', 'r')

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

    A.sort(reverse=True)
    
    
    A_set=list(set(A))
    A_set.sort(reverse=True)
    set_p=0
    

    ans=0
    
    
    arc={k: 0 for k in A_set}
    
    
    arc[A_set[0]]=arc[A_set[0]]+1
    max=A_set[0]    
    for a in A[1:]:
        
        arc[a]+=2

        ans+=max
        arc[max]-=1
        if arc[max]==0:
            set_p+=1
            max=A_set[set_p]


    print(ans)
    

if __name__ == '__main__':
    main()

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

a=sorted(a,reverse=True)
if n==2:
    print(max(a))
    exit(0)

ans=a[0]
a=a[1:]
from math import floor

for i in range(n-2):
    index=floor(i/2)
    ans+=a[index]

print(ans)

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

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

a.sort(reverse=True)

ans=0

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

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

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

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

import math

ans = 0
for i in range(n):
    if i == 0:
        pass
    elif i == 1:
        ans += a[0]
    else:
        ans += a[math.floor(i/2)]

print(ans)

Solution (Python):
N = int(input())
A = list(map(int,input().split()))
l = len(A) - 1
A.sort(reverse=True)
counter = A[0]
num = 1
while l > 2:
    counter += A[num] * 2
    num += 1
    l -= 2
if l == 2:
    counter += A[num]
print(counter)

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

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

ans = 0
ans += a_list.pop(0)
cnt = 2
times_2 = 0
times_1 = 0
while 1:
  if cnt == n:
    break
  if n - cnt >= 2:
    cnt += 2
    times_2 += 1
  else:
    cnt += 1
    times_1 += 1
      
ind = 0
for i in range(times_2):
  ans += 2 * a_list[i]
  ind += 1
  
for i in range(times_1):
  ans += a_list[ind + i]

print(ans)

Solution (Python):
N=int(input())
ls=[int(s) for s in input().split()]
ls=sorted(ls,reverse=True)
ans=ls[0]
for i in range(2,N):
  ans+=ls[i//2]
print(ans)

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

a.sort(reverse=True)

score=0
index=1
count=0
for i in range(len(a)):
  if i==0:
    score+=0
  elif i==1:
    score+=a[0]
  else:
    score+=a[index]
    count+=1
    if count==2:
      index+=1
      count=0
print(score)

Solution (Python):
N = int(input())
A = sorted(map(int, input().split(" ")), reverse=True)
result = A[0]
current_comfortest = A[1]
for i in range(2, N):
    if i == 2:
        result += current_comfortest
        continue
    else:
        result += A[i // 2]
        continue
print(result)

Solution (Python):
from collections import deque

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

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

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