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



Sample input:
4
2 2 1 3


Sample output:
7

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
As = [int(s) for s in input().split()]

As.sort()
As.reverse()

ans = As[0]

idx = (N-2) // 2
ans += sum(As[1:1+idx]) * 2
if (N-2)%2 == 1:
    ans += As[1+idx]

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-

## Library

import sys
from fractions import gcd
import math
from math import ceil,floor
import collections
from collections import Counter
import itertools
import copy

## input

# N=int(input())
# A,B,C,D=map(int, input().split())
# S = input()
# yoko = list(map(int, input().split()))
# tate = [int(input()) for _ in range(N)]
# N, M = map(int,input().split()) 
#    P = [list(map(int,input().split())) for i in range(M)]
#    S = []
#    for _ in range(N):
#        S.append(list(input()))

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

A.sort(reverse=True)

if N == 2:
    print(A[0])
    sys.exit()

if N == 3:
    print(A[0]+A[1])
    sys.exit()

B = []
B.append(A[1])
B.append(A[2])
B.append(A[2])
ans = A[0]+A[1]

tmp = 0

for i in range(3,N):
    ans += B[tmp]
    tmp += 1
    B.append(A[i])
    B.append(A[i])

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
S = list(map(int, input().split()))
s = sorted(S, reverse=True)

if n %2 ==0:
  l = n/2
  m = s[0]
  s = s[1:int(l)]
  p = sum(s)
  o = p*2 + m 
  print(o)
else:
  l = (n+1)/2
  m = s[0]
  q = s[int(l)-1]
  s = s[1:int(l-1)]
  p = sum(s)
  o = p*2 + m +q
  print(o)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, 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)

Submission status: Accepted

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

a = sorted(a, reverse=True)
ans = a[0]

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

Submission status: Accepted

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

from collections import deque
ans = 0
q = deque([A[0]])
for a in A[1:]:
   ans += q.popleft()
   q.append(a)
   q.append(a)

print(ans)

Submission status: Accepted

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

A.sort(reverse=True)
sum = 0
if N % 2 == 0:
    for i in range(1, N // 2, 1):
        sum = sum + A[i]
    answer = sum * 2 + A[0]
else:
    for i in range(1, N // 2, 1):
        sum = sum + A[i]
    answer = sum * 2 + A[0] + A[N // 2]

print(answer)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split(' ')))
A = sorted(A,reverse=True)
ans = A[0]
cnt = 1
i = 1
while cnt < N:
    for j in range(2):
        cnt += 1
        if cnt >= N:
            break
        ans += A[i]
    i += 1
print(ans)

Submission status: Accepted

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

ans = ls[0]

for i in range(2,n):
  ans += ls[i//2]
  
  
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import os
from sys import stdin, stdout


def solve(tc):
    n = int(stdin.readline().strip())
    seq = list(map(int, stdin.readline().split()))

    seq = sorted(seq, reverse=True)

    ans = seq[0]
    cur = 1
    shared = 0
    for i in range(2, n):
        ans += seq[cur]
        shared += 1
        if shared==2:
            cur += 1
            shared = 0
    print(ans)



tcs = 1
tc = 1
while tc <= tcs:
    solve(tc)
    tc += 1

Submission status: Accepted

Solution code:
import heapq
def main():
  N = int(input())
  A = list(map(int, input().split()))
  A.sort(reverse = True)
  member = [-1*A[0]]
  ans = 0
  for i in range(1, N):
    ans += -1*heapq.heappop(member)
    for _ in range(2):
      heapq.heappush(member, -1*A[i])
  print(ans)
  
  
    
if __name__ == "__main__":
  main()

Submission status: Accepted

Solution code:
import queue

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

A = sorted(A,reverse=True)

ans = 0
q = queue.Queue()
q.put(A[0])
for i in range(1,N):
    ans += q.get()

    q.put(A[i])
    q.put(A[i])

print(ans)

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def solve(A,N):
    A.sort(reverse = True)
    ans = A[0]
    if N % 2 == 0:
        pt = N // 2 - 1
        for i in range(1,pt+1):
            ans += (2 * A[i])
    else:
        pt = (N-1) // 2
        for i in range(1,pt):
            ans += (2 * A[i])
        ans += A[pt]
    return ans

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

    print(solve(A,N))

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
import sys

def input():
    return sys.stdin.readline()[:-1]

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

Submission status: Accepted

Solution code:
# from heapq import heapify, heappop, heappush
# from itertools import accumulate, permutations, combinations, combinations_with_replacement, groupby, product
# import math
# import numpy as np  # Pythonのみ！
# from operator import xor
# import re
# from scipy.sparse.csgraph import connected_components  # Pythonのみ！
# ↑cf.  https://note.nkmk.me/python-scipy-connected-components/
# from scipy.sparse import csr_matrix
# import statistics # Pythonのみ
# import string
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    n=int(input())
    A=sorted(list(map(int,input().split())),reverse=True)

        

    m=n
    m-=2
    p,q=divmod(m,2)
    if q==0:
        ans=2*sum(A[:p+1])
        ans-=A[0]
    else:
        ans=2*sum(A[:p+2])
        ans-=A[0]
        ans-=A[p+1]
    print(ans)





    
resolve()

Submission status: Accepted

Solution code:
#input = raw_input
n = int(input())
nums = sorted(map(int, input().split(" ")), reverse=True)

from collections import deque

for i in range(n):
    if i == 0:
        num0 = nums[i]
        out = 0
    elif i == 1:
        num1 = nums[i]
        out = num0
        q = deque([[num0, num1], [num0, num1]])
    else:
        in_e = q.popleft()
        #print(in_e)
        cur = nums[i]
        out += in_e[1]
        for nn in in_e: 
            e = [nn, cur]
            q.append(e)

print(out)

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    n = int(input())
    arr = list(map(int, input().split()))
    arr.sort(reverse=True)
    halfway = len(arr)//2 + 1
    otherhalf = n-halfway
    total = 0
    for i in range(1, halfway+2):
        if i == 1:
            total += arr[0]
        else:
            if i < halfway:
                total += min(arr[0], arr[i-1])
            if otherhalf >= 1:
                total += min(arr[0], arr[i-1])
                otherhalf -= 1
    print(total)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 
#mod = 998244353
from decimal import *
#import numpy as np
#decimal.getcontext().prec = 10

N = INT()
A = LIST()

A.sort(reverse = True)

p = [A[0]]

for i in range(1, N):
	p += [A[i]]*2

print(sum(p[:N-1]))

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n = int(input())
b = list(map(int,input().split()))

b.sort(reverse=True)
ret = 0
from collections import deque
stack_deque = deque([])
if n == 1:
    print(0)
    import sys
    sys.exit()
if n == 2:
    print(b[0])
    import sys
    sys.exit()

stack_deque.append(b[1])
stack_deque.append(b[1])
ret = b[0]
for bi in b[2::]:
    ret += stack_deque.popleft()
    stack_deque.append(bi)
    stack_deque.append(bi)
print(ret)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from functools import reduce
from fractions import gcd
import math
import bisect
import itertools
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline
INF = float("inf")


# 処理内容
def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort(reverse=True)

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque

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

if n == 2:
    print(max(A))
    exit()

A.sort()
B = A[::-1]
point = deque()
point.append(B[0])
ans = 0
for i in range(1, n):
    ans += point.popleft()
    point.append(B[i])
    point.append(B[i])
print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)
# input = sys.stdin.readline    ####
def int1(x): return int(x) - 1
def II(): return int(input())
def MI(): return map(int, input().split())
def MI1(): return map(int1, input().split())
def LI(): return list(map(int, input().split()))
def LI1(): return list(map(int1, input().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def MS(): return input().split()
def LS(): return list(input())
def LLS(rows_number): return [LS() for _ in range(rows_number)]
def printlist(lst, k=' '): print(k.join(list(map(str, lst))))
INF = float('inf')
# from math import ceil, floor, log2
from collections import deque
# from itertools import combinations as comb, combinations_with_replacement as comb_w, accumulate, product, permutations
# from heapq import heapify, heappop, heappush
# import numpy as np    # cumsum
# from bisect import bisect_left, bisect_right

def solve():
    N = II()
    A = LI()
    A = deque(sorted(A)[::-1])

    q = deque([A.popleft()])

    ans = 0
    while A:
        ans += q.popleft()
        a = A.popleft()
        q.extend([a, a])
    print(ans)

if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
N = int(input())

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

A.sort(reverse=True)

gain = 0

n = 1
while n <= N-1:
  if n % 2:
    gain += A[(n-1)//2]
    # print(f'{n}, added {A[(n-1)//2]}')
  else:
    gain += A[(n+1)//2]
    # print(f'{n}, added {A[(n+1)//2]}')
  n += 1

print(gain)

Submission status: Accepted

Solution code:
import queue
import sys
def input():
    return sys.stdin.readline().rstrip('\n')
    

def calc(N, A):
    A.sort(key=lambda x: -x)
    q = queue.Queue()
    q.put(A[0])
    ans = 0
    for a in A[1:]:
        ans += q.get()
        q.put(a)
        q.put(a)
    return ans


N = int(input())
A = [int(s) for s in input().split()]
print(calc(N, A))

Submission status: Accepted

Solution code:
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)

Submission status: Accepted

Solution code:
N = int(input())
F = list(map(int, input().split()))

F = sorted(F)
S = list()
for f in F:
    S.append(f)
    S.append(f)

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

Submission status: Accepted

Solution code:
import sys 
import heapq

N = int(sys.stdin.readline().strip())
A = list(map(int, sys.stdin.readline().strip().split()))

A.sort(reverse=True)
q = []
heapq.heappush(q, (-A[0], (A[0], A[0])))

ans = 0
for i in range(1, N):
    v, (left, right) = heapq.heappop(q)
    # print(-v, left, right)
    ans += -v
    heapq.heappush(q, (-1 * min(left, A[i]), (left, A[i])))
    heapq.heappush(q, (-1 * min(right, A[i]), (right, A[i])))

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list((map(int,input().split())))
a.sort(reverse=True)
p=1
ans=a[0]
i=1
while p<(n-1):
    ans+=(2*a[i])
    
    i+=1
    p+=2
if p!=(n-1):
    ans-=a[i-1]
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
n = int(input())
a = list(map(int,input().split()))
a.sort(reverse=True)
score = deque([a.pop(0)])

total = 0

for i in a:
    total += score.popleft()
    score.append(i)
    score.append(i)
print(total)

Submission status: Accepted

Solution code:
N = int(input())
A = sorted(map(int, input().split()), reverse=True)
if N % 2:
    print(sum(A[: N // 2]) + sum(A[1 : N // 2 + 1]))
else:
    print(sum(A[: N // 2 - 1]) + sum(A[1 : N // 2]) + A[N // 2 - 1])

Submission status: Accepted

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

array = [a[0]]

i = 1
while len(array) <= n:
  array.append(a[i])
  array.append(a[i])
  i += 1
  
print(sum(array[0:n-1]))

Submission status: Accepted

Solution code:
n = int(input())
As = [int(x) for x in input().split()]
As.sort(reverse=True)
mx = As[0]
i = 1
if n%2 == 0:
    for j in range(int((n-2)/2)):
        mx += As[i]*2
        i += 1
else:
    for j in range(int((n-2)/2)):
        mx += As[i]*2
        i += 1
    mx += As[i]
print(mx)

Submission status: Accepted

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

a.sort(reverse=True)

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

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

print(ans)

Submission status: Accepted

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

A.sort(reverse=True)

cnt = 0
if N % 2 == 0:
    cnt += A[0]
    for i in range(1, N//2):
        cnt += A[i] * 2
    
else:
    cnt += A[0]
    for i in range(1, N//2):
        cnt += A[i] * 2
    cnt += A[i+1]
print(cnt)

Submission status: Accepted

Solution code:
import sys, math,os
from io import BytesIO, IOBase
#data = BytesIO(os.read(0,os.fstat(0).st_size)).readline
# from bisect import bisect_left as bl, bisect_right as br, insort
from heapq import heapify, heappush, heappop
from collections import defaultdict as dd, deque, Counter
# from itertools import permutations,combinations
# from decimal import Decimal
def data(): return sys.stdin.readline().strip()
def mdata(): return list(map(int, data().split()))
def outl(var): sys.stdout.write(' '.join(map(str, var)) + '\n')
def out(var): sys.stdout.write(str(var) + '\n')
#sys.setrecursionlimit(100000 + 1)
INF = 10**9
mod = 998244353


n=int(data())
a=mdata()
a.sort(reverse=True)
ans=a[0]
ind=1
k=0
for i in range(2,n):
   ans+=a[ind]
   k+=1
   if k==2:
       k=0
       ind+=1
out(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
A.sort(reverse=True)
point = 0
for i in range(N):
    if i == 0:
        continue
    point += A[int(i/2)]
print(point)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)  # 大きい順にソート
amax = A[0]
half = N // 2
ans = sum(A[:half]) * 2 - amax
if N%2 == 1:
    ans += A[half]
print(ans)

Submission status: Accepted

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

A.sort(reverse=True)

Sum = 0

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

print(Sum)

Submission status: Accepted

Solution code:
import sys


if(__name__ == "__main__"):

    N = int( input().strip() )


    list_a = list( map(int, input().strip().split()) )
    list_a.sort(reverse=True)
    comfortables = list_a[0]
    N_twos = (N-2)//2
    if(N%2 !=0):
        comfortables+= list_a[N_twos+1]
    for i in range(1, N_twos+1):
        comfortables += list_a[i] * 2
    print(comfortables)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int ,input().split()))
sA = sorted(A, reverse=True)
if N%2==0:
    print(sA[0]+2*sum(sA[1:N//2]))
else:
    print(sA[0]+2*sum(sA[1:(N-1)//2])+sA[(N-1)//2])

Submission status: Accepted

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
import heapq
import math
from fractions import gcd
import random
import string
import copy
from itertools import combinations, permutations, product
from operator import mul
from functools import reduce
from bisect import bisect_left, bisect_right

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


#############
# Main Code #
#############

N = getN()
A = getList()
A.sort(reverse = True)
ans = A[0]
for i in range(N - 2):
    ans += A[1 + (i // 2)]
print(ans)

Submission status: Accepted

Solution code:
import sys
import collections
ri = lambda: int(sys.stdin.readline())
rl = lambda: list(map(int, sys.stdin.readline().split()))

n = ri()
li = sorted(rl(), reverse=True)
q = collections.deque([max(li)])
score = 0
for i in li[1:]:
    a = q.popleft()
    score += a
    q.append(i)
    q.append(i)
print(score)

Submission status: Accepted

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

A.sort()
ans = 0

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

Submission status: Accepted

Solution code:
#import sys
MOD = 10 ** 9 + 7
INFI = 10**10
#input = sys.stdin.readline
import math
from collections import deque
import itertools
import heapq
#import bisect
from fractions import Fraction
import copy
from functools import lru_cache
from collections import defaultdict
import pprint

#oo=list("ABCDEFGHIJKLMNOPQRSTUVWXYZ")
# ko=list("abcdefghijklmnopqrstuvwxyz")

def sosuhante(n):
    for k in range(2, int(math.sqrt(n))+1):
        if n% k ==0:
            return False
    return True
def cmb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2,r+1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p-1,r,p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def kingaku(a,b,n):
    keta=len(str(n))
    return a*n+b*keta

def my_index(l, x, default=False):
    if x in l:
        return l.index(x)
    else:
        return default

#    h,w,a,b = map(int, input().split())
#    c = [[0 for j in range(n)] for i in range(n)]

def ret(a):
    c=[None]*(len(a)-1)
    if len(a)==1:
        return a[0]
    elif len(a)==0:
        return 0
    for i in range(1,len(a)):
        c[i-1]=abs(a[i]-a[i-1])
    return ret(c)

def soinsubunkai(n):
    a = []
    i = 1
    while i*i <= n:
        if n % i == 0 and i!=1:
            a.append(i)
            n=n//i

        if n% i !=0 or i==1:
            i += 1
    nokori=[n]
    return a + nokori

def make_divisors(n):
    lower_divisors , upper_divisors = [], []
    i = 1
    while i*i <= n:
        if n % i == 0:
            lower_divisors.append(i)
            if i != n // i:
                upper_divisors.append(n//i)
        i += 1
    return lower_divisors + upper_divisors[::-1]

def main():
    n=int(input())
    a=list(map(int,input().split()))
    a=sorted(a,reverse=True)
    if n%2==0:
        a1=a[0:n//2]
        a2=a[n//2:]
    else:
        a1=a[0:n//2+1]
        a2=a[n//2+1:]
#    print(a,a1,a2)
    count=0
    if n>=3:
        for i in range(len(a1)):
            count+=a1[i]
        for i in range(1,len(a2)):
            count+=a1[i]
    else:
        count=a[0]
    print(count)

if __name__ == "__main__":

    main()

Submission status: Accepted

Solution code:
from sys import stdin
nii=lambda:map(int,stdin.readline().split())
lnii=lambda:list(map(int,stdin.readline().split()))

n=int(input())
a=lnii()
a.sort(reverse=True)

ans=a[0]

inx=1
i=2
while i<n:
  ans+=a[inx]

  if i%2==1:
    inx+=1

  i+=1

print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import collections
import decimal
import itertools
from collections import deque
from functools import reduce
import heapq
import copy
n = int(input())
#h, w, K = map(int, sys.stdin.readline().split())
#s = input()
a = list(map(int, sys.stdin.readline().split()))

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

print(p)

Submission status: Accepted

Solution code:
n=int(input())

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

t.sort(reverse=True)

ans = t[0]

i = 1

count = 1

flag = False

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

Submission status: Accepted

Solution code:
n,*l=map(int,open(0).read().split())
from heapq import *
l.sort()
hq=[-l.pop()]
heapify(hq)
a=0
for _ in range(n-1):
  m=heappop(hq)
  a-=m
  t=-l.pop()
  heappush(hq,t)
  heappush(hq,t)
print(a)

Submission status: Accepted

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

A.sort()
ans = 0
ans += A.pop()
i = 1
now =0
while i < N-1:
    now = A.pop()
    ans += now*2
    i += 2
if i > N-1:
    ans -= now
print(ans)

Submission status: Accepted

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

a.sort(reverse=True)

n -= 1

idx = 0
score = a[idx]
n -= 1
idx += 1

while n > 0:
    if n > 1:
        score += a[idx] * 2
        n -= 2
        idx += 1
    else:
        score += a[idx]
        n -= 1

print(score)

Submission status: Accepted

Solution code:
from collections import deque

n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
a = deque(a)
ans = a[0]
a.popleft()
n-=2
while n>0:
    if n>=2:
        ans += a[0]*2
        n-=2
        a.popleft()
    else:
        ans += a[0]
        n-=1
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

a.sort(reverse=True)
stack = list()
stack.append(a[0])

if n == 2:
    print(stack[0])
    exit(0)

for i in range((len(a) // 2) + 1):
    stack.append(a[i + 1])
    stack.append(a[i + 1])

print(sum(stack[:n-1]))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, A: "List[int]"):
    A = sorted(A, reverse=True)
    if N==2:
        print(A[0])
        return

    i = 0
    while(pow(2,i)<N): i+=1
    # print(A)

    rem = N-2**(i-1)
    if rem%2 == 1: od = 1
    else: od = 0
    # print(A[:2**(i-2)])
    ans = sum(A[:2**(i-2)])*2 - A[0]
    # print(ans)
    # print(A[2**(i-2):2**(i-2)+rem//2])
    ans+= sum(A[2**(i-2):2**(i-2)+rem//2])*2
    # print(ans)

    if od:
        ans+= A[2**(i-2)+rem//2]
    print(ans)

    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

a.sort(reverse=True)
ans+=int(a[0])
n-=2
where=1
kaisu=0
while n>0:
  ans+=int(a[where])
  kaisu+=1
  n-=1
  if kaisu==2:
    kaisu=0
    where+=1

print(str(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3


def compute_max(n, bs):
    cs = sorted(bs, reverse=True)
    if n == 1:
        return 0
    else:
        coefficients = [0 for _ in range(n)]
        coefficients[0] = 1
        d, m = divmod(n - 2, 2)
        for i in range(1, d + 1):
            coefficients[i] = 2
        coefficients[d + 1] = m
        res = sum(c * d for c, d in zip(cs, coefficients))
        return res


def main():
    n = int(input())
    bs = [int(b) for b in input().split()]
    res = compute_max(n, bs)
    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
l = [int(x) for x in input().split()]
l.sort(reverse = True)
ans = 0
for i in range(N-1):
    ans += l[(i+1)//2]
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys
import collections
from decimal import Decimal # 10進数で考慮できる

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

d = collections.deque()
def LI(): return list(map(int, sys.stdin.readline().split()))

N = int(input())
A = LI()

A.sort(reverse=1)
ans = 0
ans += A[0]

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
a = input().split()
a = list(map(int,a))
sa = sorted(a,reverse=True)
count = 0
count += sa[0]
f = 0
t = 1
for _ in range(N-2):
    if f == 2:
        t += 1
        count += sa[t]
        f = 0
    else:
        count += sa[t]
    f += 1
print(count)