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()
a.reverse()

























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

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

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

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

print(ans)

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

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

sorted_A = sorted(A, reverse=True)

if N % 2 == 0:
    print(sorted_A[0] + 2*sum(sorted_A[1:N//2]))
elif N == 3:
    print(sorted_A[0] + sorted_A[N // 2])
else:
    print(sorted_A[0] + 2*sum(sorted_A[1:N//2]) + sorted_A[N//2])

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

A.sort(reverse=True)

happiness = A.pop(0)
tree = [0] * N
tree[0] = A[0]

index = 0
appendIndex = 1
while(len(A)>appendIndex):
  happiness += tree[index]
  tree[appendIndex] = A[appendIndex]
  appendIndex += 1
  if(len(A)==appendIndex):
    break
  happiness += tree[index]
  tree[appendIndex] = A[appendIndex]
  appendIndex += 1
  index += 1

print(happiness)

Solution (Python):
import heapq

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

place=[[-A[0],1]]
ans=0
for i in range(1,N):
    ans-=place[0][0]
    if place[0][1]==1:
        heapq.heappop(place)
    else:
        place[0][1]-=1
    heapq.heappush(place,[-A[i],2])
print(ans)

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

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    N = int(input())
    A = sorted([int(x) for x in input().strip().split()], reverse=True)
    ans = A[0]
    for n in range(2, N):
        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)

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

Solution (Python):
from collections import deque, defaultdict

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

A.sort(reverse=True)

d = defaultdict(int)
d[A[0]] += 1
x = deque([A[0]])
ans = 0
for a in A[1:]:
    ans += x[0]
    d[x[0]] -= 1
    if d[x[0]] == 0:
        x.popleft()
    if d[a] == 0:
        x.append(a)
    d[a] += 2
print(ans)

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

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

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

score_list = [0, A[0]]
for i in range(1, len(A)):
    score_list.append(A[i])
    score_list.append(A[i])
    
print(sum(score_list[:n]))

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(n//2):
    ans=ans+a[i]*2
  print(ans-a[0])
else:
  for i in range(n//2):
    ans=ans+a[i]*2
  print(ans-a[0]+a[n//2])

Solution (Python):
from sys import stdin
import heapq as hq
input = stdin.readline

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

A.sort(reverse=True)
ans = 0

ls = []
hq.heapify(ls)

hq.heappush(ls, [-A[0], -A[0]])

for i in range(1, N):
    c = A[i]
    a, b = hq.heappop(ls)
    ans += abs(max(a, b))
    hq.heappush(ls, [-min(abs(A[i]), abs(a)), -max(abs(A[i]), abs(a))])
    hq.heappush(ls, [-min(abs(A[i]), abs(b)), -max(abs(A[i]), abs(b))])

print(ans)

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

for i in range(1,N,1):
  
  
  ans += d.popleft()
  d.extend([A[i],A[i]])
print(ans)

Solution (Python):



def input_int():
    return int(input())



def int1(x):
    return int(x) - 1



def input_to_int_map():
    return map(int, input().split())



def input_to_int_tuple():
    return tuple(map(int, input().split()))



def input_to_int_tuple_minus1():
    return tuple(map(int1, input().split()))


def main():
    n = input_int()
    a_list = input_to_int_tuple()

    sorted_list = sorted(a_list, reverse=True)
    temp_cnt = n - 2
    ans = sorted_list[0]

    plus = False
    if temp_cnt % 2 == 0:
        temp_cnt //= 2
    else:
        temp_cnt //= 2
        plus = True

    if temp_cnt >= 1:
        ans += sum(sorted_list[1:temp_cnt + 1]) * 2

    if plus:
        ans += sorted_list[temp_cnt + 1]

    print(ans)



if __name__ == "__main__":
    main()

Solution (Python):
import numpy as np

n = int(input())
a = input().split()
npa=np.array(a, dtype=int)

npa1=np.sort(npa)[::-1]

ninzuu=n-1
if (ninzuu % 2 != 0):
  ninzuu2=int((ninzuu-1)/2)
  atai=npa1[0]+sum(npa1[1:ninzuu2+1])*2
else:
  ninzuu2=int((ninzuu)/2)
  atai=npa1[0]+sum(npa1[1:ninzuu2])*2+npa1[ninzuu2]

print(atai)

Solution (Python):








import queue


def solve():
    N = int(input())
    As = list(map(int, input().split()))
    As.sort(reverse=True)

    score = 0

    q = queue.Queue()
    q.put(As[0])

    for a in As[1:]:
        score += q.get()
        q.put(a)
        q.put(a)

    return score


def main():
    print(solve())


if __name__ == "__main__":
    main()

Solution (Python):




import sys
from io import StringIO
import unittest
import heapq
import copy

def yn(b):
    print("Yes" if b==1 else "No")
    return

def resolve():
    readline=sys.stdin.readline

    
    n=int(readline())
    arr=list(map(int, readline().rstrip().split()))
    arr.sort(reverse=True)
    c=[]
    heapq.heapify(c)
    ans=0
    for i in range(n):
        if i==0:
            heapq.heappush(c,(-1)*arr[i])
        else:
            ans+=heapq.heappop(c)*(-1)
            heapq.heappush(c,(-1)*arr[i])
            heapq.heappush(c,(-1)*arr[i])
    print(ans)
    
    
    

    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

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

a.sort(reverse=True)
n -= 2
r = a[0]
i = 1
while n-2 >= 0:
    r += a[i]*2
    i += 1
    n -= 2
if n==1:
    r += a[i]
print(r)

Solution (Python):
from collections import Counter
N = int(input())
A = list(map(int, input().split()))
sortA = sorted(A, reverse=True)
C = Counter(sortA)
for k in C.keys():
    C[k] = C[k] * 2    
sort_ai = sorted(C.keys(), reverse=True)
ii = 0
ans = 0
for i  in range(N):
    tmp = sort_ai[ii]
    ans += tmp
    C[tmp] -= 1
    if C[tmp] == 0:
        ii += 1
print(ans-sort_ai[0])

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

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

A = sorted(A, reverse=True)

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

print(score)

Solution (Python):
n = int(input())
la = [int(w) for w in input().split()]
la.sort(reverse=True)

ans = la[0]
cnt = n - 2
for a in la[1:]:
    if cnt >= 2:
        cnt -= 2
        ans += a * 2
    elif cnt == 1:
        cnt -= 1
        ans += a
    else:
        break
print(ans)

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

A.sort(reverse=True)

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

Solution (Python):





import sys
import heapq

def main():
    n = int(input())    
    nums = list(map(int, sys.stdin.readline().split()))

    nums.sort()
    ans = 0

    
    temp = nums.pop()
    ans += temp
    n -= 2

    for i in range(n // 2):
        ans += nums[-i - 1] * 2

    if n % 2:
        ans += nums[-(n // 2) - 1]
    
    
    print(ans)

    return

if __name__ == "__main__":
    main()

Solution (Python):
import math
N = int (input())
x = list(map(int, input().split()))
sum = 0
red = sorted(x)[::-1]
count = math.floor(N/2)
for i in range(count):
    sum += red[i] * 2
sum -= red[0]
desire = N - count*2
sum += desire * red[count]
print(sum)

Solution (Python):
import heapq

N = int(input())
A = [-int(x) for x in input().split()]
A.sort()
pq = []
heapq.heapify(pq)
heapq.heappush(pq, A[0])
ans = 0
for a in A[1:]:
    ans += heapq.heappop(pq)
    heapq.heappush(pq, a)
    heapq.heappush(pq, a)
print(-ans)

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

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

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

print(ans)

Solution (Python):
import sys
input = lambda: sys.stdin.readline().rstrip("\r\n")

n = int(input())
a = [int(x) for x in input().split()]
a.sort()
ans = a.pop()
a = a[::-1]

if n % 2 == 0:
    n -= 1

    n -= 1
    ans += 2*sum(a[:n//2])

else:
    n -= 1

    n -= 1
    ans += 2*sum(a[:n//2]) + a[n//2]
print(ans)

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

Solution (Python):
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
ans = A[0]
i = 2
j = 1
while(True):
    if i == N:
        break
    ans += A[j]
    i += 1
    if i == N:
        break
    ans += A[j]
    i += 1
    j += 1

print(ans)

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

res = 0
for i in range(N):
    res += A[i//2]
print(res-A[0])

Solution (Python):
from sys import stdin
N = int(input())
A = [int(x) for x in stdin.readline().rstrip().split()]
A.sort(reverse=True)
ans = A[0]
cnt = 2
for i in range(1,N):
    ans += (A[i]*2)
    cnt += 2
    if cnt >= N:
        if cnt > N:
            ans -= A[i]
        break
if N == 2:
    ans = max(A)
print(ans)

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

res = A_list[0]
cnt = 2
for i in range(1, N):
    if N - cnt >= 2:
        res += 2 * A_list[i] 
        cnt += 2
    elif N - cnt == 1:
        res += A_list[i]
        break
    else:
        break
print(res)

Solution (Python):
N = int(input())
A_list = [int(n) for n in input().split()]
A_list.sort(reverse = True)
ans = 0

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

print(ans)

Solution (Python):
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**6)

n=int(input().rstrip())
a=sorted([int(i) for i in input().split()],reverse=1)
nokori=n-2
i=1
ans=a[0]
while nokori>1:
    ans+=a[i]*2
    nokori-=2
    i+=1
if n%2:ans+=a[i]
print(ans)

Solution (Python):
import heapq

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

first = A.pop()
Q = [(-first,first,first)]

ans = 0
for i in range(N-1):
    a = A.pop()
    b,c,d = heapq.heappop(Q)
    ans -= b
    heapq.heappush(Q,(-a,c,a))
    heapq.heappush(Q,(-a,d,a))

print(ans)

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

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

Solution (Python):
from sys import stdin, stdout

int_in = lambda: int(stdin.readline())
arr_in = lambda: [int(x) for x in stdin.readline().split()]
mat_in = lambda rows: [arr_in() for _ in range(rows)]
str_in = lambda: stdin.readline().strip()
out = lambda o: stdout.write("{}\n".format(o))
arr_out = lambda o: out(" ".join(map(str, o)))
bool_out = lambda o: out("YES" if o else "NO")
tests = lambda: range(1, int_in() + 1)
case_out = lambda i, o: out("Case 


def solve(n, a):
    a.sort(reverse=True)
    comfort = 0
    for i in range(1, len(a)):
        comfort += a[i // 2]

    return comfort


if __name__ == "__main__":
    n = int_in()
    a = arr_in()
    out(solve(n, a))

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

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

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

print(ans)

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

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

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

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

A.sort()
A = A[::-1]

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

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

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

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

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

i = 0
indexer = 0
flag = 0
count = 0
while i < len(listA):
    if i == 0:
        count+= 0
        i+=1
    elif i == 1:
        count+=listA[indexer]
        indexer+=1
        i+=1
    else:
        count+=listA[indexer]
        if flag:
            indexer+=1
            flag = 0
        else:
            flag = 1
        i+=1
print(count)

Solution (Python):
from collections import defaultdict as dd
import heapq
N = int(input())
a = list(map(int,input().split()))
res = 0
a = sorted(a, reverse=True)
dic=dd(list)
ls = [-a[0]]
heapq.heapify(ls)
for val in a[1:]:
    tmp = -heapq.heappop(ls)
    res += tmp
    heapq.heappush(ls,-val)
    heapq.heappush(ls,-val)
print(res)

Solution (Python):


import os
import sys
from io import BytesIO, IOBase

"""
    for _ in range(int(input())):
    n,m=map(int,input().split())
    n=int(input())
    a = [int(x) for x in input().split()]
"""

def main():
    n = int(input())
    a = [int(x) for x in input().split()]
    a.sort(reverse=True)
    
    ans=0
    k=n
    ans-=a[0]
    for x in a:
        if k==0:
            break
        if k==1:
            ans+=x
            break
        ans+=2*x
        k-=2
    print(ans)



BUFSIZE = 8192


class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")



if __name__ == "__main__":
    main()

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

if n%2==0:
    k = n//2
    print(A[0]+2*sum(A[1:k]))
else:
    print(A[0]+2*sum(A[1:n//2])+A[n//2])

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

from collections import deque
q = deque([])

ans = 0
i = 0
for i in range(N):
    a = A[i]
    if i == 0:
        q.append(a)
    else:
        q.append(a)
        q.append(a)    
        s = q.popleft()
        ans += s

print(ans)

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

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

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

ans = 0
hq = [(-A[0], A[0], A[0])]
for a in A[1:]:
    s, n, m = heapq.heappop(hq)
    ans += -s
    heapq.heappush(hq, (-min(a, n), n, a))
    heapq.heappush(hq, (-min(a, m), m, a))
print(ans)

Solution (Python):
import heapq

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

A_sorted = sorted(A,reverse = True)

circle = []
heapq.heappush(circle, -A_sorted[1])
heapq.heappush(circle, -A_sorted[1])
answer = A_sorted[0]

for i in range(2,N):
    target = A_sorted[i]
    popped = heapq.heappop(circle)
    answer -= popped
    heapq.heappush(circle, -target)
    heapq.heappush(circle, -target)

print(answer)

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

Solution (Python):
N = int(input())
a = list(map(int,input().split()))
a.sort(reverse=True)
A = [a[0]]
for i in range(1,N):
    A.append(a[i])
    A.append(a[i])
print(sum(A[:N-1]))

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

ans = l[0]
num = 1

if len(l) == 2:
  print(ans)
else:
  for i in range(1, n):
    ans += l[i]
    num += 1
    if num == n-1:
      break
    ans += l[i]
    num += 1
    if num == n-1:
      break
  print(ans)

Solution (Python):


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

n1 = 1 + ((N - 2) // 2)
n2 = N - n1

print(sum(A[:n1]) + sum(A[1:n2]))

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

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

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


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

if n>2:
    if n%2==0:
        for i in range(1,n//2):
            ans+=2*A[i]
    else:
        for i in range(1,n//2):
            ans+=2*A[i]
        ans+=A[n//2]

print(ans)

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


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


def main():
    n = int(input())
    a = map(int, input().rstrip().split())
    ans = 0
    candidates = collections.deque()
    for v in sorted(a, reverse=True):
        if not candidates:
            candidates.appendleft(v)
            continue
        if candidates[-1] > v:
            v2 = candidates.pop()
            ans += v2
            candidates.appendleft(v)
            candidates.appendleft(v)
        else:
            candidates.appendleft(v)
            ans += v
    print(ans)


if __name__ == '__main__':
    main()

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

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

ans = A[0]
Q = [(-A[1],-A[0]),(-A[1],-A[0])] 
heapify(Q)
for i in range(2,N):
  nxt = A[i]
  s,t = heappop(Q)
  s *= -1; t*= -1 
  
  ans += s
  heappush(Q,(-nxt,-s))
  heappush(Q,(-nxt,-t))
  
print(ans)

Solution (Python):
def main():
    n = int(input())
    s = list(map(int, input().split()))
    s.sort(reverse=True)
    res = 0
    for i in range(1, n):
        res += s[i // 2]
    print(res)
if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():
    N = in_n()
    A = sorted(in_nl(), reverse=True)

    ans = A[0]
    num = 1
    i = 1
    while True:
        if num < N - 1:
            ans += A[i]
            num += 1
        if num < N - 1:
            ans += A[i]
            num += 1
        if num < N - 1:
            i += 1
        else:
            break

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import math
N = int(input())
A = list(sorted(map(int, input().split()))[::-1])
ans = 0

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

print(ans)

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


import collections
cnt = collections.Counter(a)
for i, ci in cnt.items():
    cnt[i] = ci * 2

cnt[max(a)] -= 1
n -= 1


keys = list(cnt.keys())
keys.sort(reverse=True)
for i in keys:
    if n == 0:
        break
    ans += i * min(n, cnt[i])
    n -= min(n, cnt[i])
    

print(ans)

Solution (Python):

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

Solution (Python):
N = int(input())
A = sorted(list(map(int, input().split())), reverse=True)
B = [A[0]]
for a in A[1:]:
    B += [a, a]
print(sum(B[:len(A[:-1])]))

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

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

sum_idx = -(-N // 2)-1
if N % 2 == 0:
    print(sum(A[0:sum_idx+1]) + sum(A[1:sum_idx+1]))
else:
    print(sum(A[0:sum_idx+1]) + sum(A[1:sum_idx]))

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

As.sort(reverse=True)
rlt = As[0]

if N == 2:
  print(rlt)
  exit()

n = N // 2
if N % 2 == 1:
  for i in range(1,n):
    rlt += 2*As[i]
  rlt += As[n]
else:
  for i in range(1,n):
    rlt += 2*As[i]
  
print(rlt)

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

Solution (Python):
import sys
import math
import heapq
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, m=DVSR): return pow(x, m - 2, m)
def DIV(x, y, m=DVSR): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def FLIST(n):
    res = [1]
    for i in range(1, n+1): res.append(res[i-1]*i%DVSR)
    return res

N=II()
AS=LI()
AS.sort(reverse=True)


i, j = AS[0], AS[1]
HQ=[]
heapq.heappush(HQ, (-i, -j))
heapq.heappush(HQ, (-i, -j))

res = AS[0]
for n in range(2, len(AS)):
    k = AS[n]
    i, j = heapq.heappop(HQ)
    i *= -1
    j *= -1
    res += min(i, j)
    heapq.heappush(HQ, (-k, -i))
    heapq.heappush(HQ, (-k, -j))
    
    

print(res)

Solution (Python):
n = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
result = 0
count = 1
while count != n:
    result += A[count // 2]
    count += 1

print(result)

Solution (Python):















mod = int(1e9+7)

INF = 1<<50

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

def main():
    n = int(input())
    a = readInt()
    a.sort(reverse=True)
    m = {}
    order = []
    for i in range(n):
        if a[i] in m.keys():
            m[a[i]] += 1
        else:
            m[a[i]] = 1
            order.append(a[i])
    count = m[a[0]]
    num = 0
    ans = 0
    for i in range(1,n):
        if i>count*2-1:
            num += 1
            count += m[order[num]]
        ans += order[num]
    print(ans)
    return

if __name__=='__main__':
    main()

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

alist.sort(reverse=True)
answer=0
hq=[]
heapq.heappush(hq,-alist[0])
for a in alist[1:]:
  r=heapq.heappop(hq)
  answer+=-r
  heapq.heappush(hq,-a)
  heapq.heappush(hq,-a)
  
print(answer)

Solution (Python):
def main():
    n = int(input())
    a=[int(i) for i in input().split()]
    a.sort(reverse = True)

    
    res = a[0]
    tmp = a[1]
    k = 1
    cnt = 0
    for i in range(2,n):
        res += tmp
        cnt+=1
        if(cnt == 2):
            k+=1
            tmp = a[k]
            cnt = 0
    print(res)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
import time
import math
from collections import deque
import heapq
import itertools
from decimal import Decimal
import bisect
from operator import itemgetter
MAX_INT = int(10e18)
MIN_INT = -MAX_INT
mod = 1000000000+7
sys.setrecursionlimit(1000000)
def IL(): return list(map(int,input().split()))
def SL(): return input().split()
def I(): return int(sys.stdin.readline())
def S(): return input()

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

ans = a[0]
i = 1
cnt = 2
while True:
  if cnt == N:
    break
  ans += a[i]
  cnt += 1
  if cnt == N:
    break
  ans += a[i]
  cnt += 1
  i += 1
print(ans)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

if N == 2:
    print(A[0])
    exit()
elif N == 3:
    print(A[0] + A[1])
    exit()

ans = A[0]
N -= 2

for i in range(1, N):
    if N == 0:
        break
    a = A[i]
    c = min(2, N)
    ans += a * c
    N -= c

print(ans)


"""
if N == 2:
    print(A[0])
    exit()
elif N == 3:
    print(A[0] + A[1])
    exit()

ans = sum(A[:-2]) + A[1]
print(ans)
"""

Solution (Python):
import numpy as np
N = int(input())
A = np.array(list(map(int, input().split())))
A = np.sort(A)[::-1]

div, mod = divmod(N, 2)

print(2 * np.sum(A[:div]) + (A[div] if mod==1 else 0) - A[0])

Solution (Python):
N = int(input())
A = [int(x) for x in input().split()]
A.sort(reverse = True)
ans = A[0]
cnt = 2
for i in range(1, N):
    for j in range(2):
        cnt += 1
        if cnt > N: break
        ans += A[i]

print(ans)

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

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

Solution (Python):
n = input()
t = int(n)
num = t-2
list = input().rstrip().split(" ")
target = []
for i in list:
    target.append(int(i))
target.sort()
target.reverse()
ans = target[0]
i = 1
k = 0
while num > 0:
    ans += target[i]
    k += 1
    num -=1
    if k == 2:
        i += 1
        k = 0
print(ans)

Solution (Python):
n = int(input())
a = [int(x) for x in input().split()]
a.sort(reverse=True)

k = 1
index = 0
ans = 0
for i in range(n-1):
  k -= 1
  ans += a[index]
  if k == 0:
    index += 1
    k = 2

print(ans)