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:
def solve():
    N = int(input())
    A = [int(i) for i in input().split()]
    sorted_A = sorted(A, reverse=True)
    cnt = N
    ptr = 0
    is_first = True
    ans = 0
    while cnt > 0:
        ans += sorted_A[ptr]
        if is_first:
            is_first = False
        else:
            ptr +=1
            is_first = True
        cnt -= 1
    print(ans - sorted_A[0])

if __name__ == "__main__":
    solve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
N = int(input())
A = list(map(int,input().split()))
Query = deque()
A.sort(reverse=True)
Query.append([A[0],A[1]])
Query.append([A[0],A[1]])
Answer = A[0]
for i in range(2,N):
    query = Query.popleft()
    Answer += min(query)
    Query.append([max(query),A[i]])
    Query.append([min(query),A[i]])
print(Answer)

Submission status: Accepted

Solution code:
input()
A=list(map(int,input().split()))
A.sort(reverse=True)
B=[]
ans=0
for i in A:
  if len(B)>=1:
    ans+=B[len(B)//2]
  B.append(i)
print(ans)

Submission status: Accepted

Solution code:
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
sys.setrecursionlimit(10**6)
# INF =  float("inf")
INF = 10**18
import bisect
import statistics
mod = 10**9+7
# mod = 998244353



N = int(input())

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

A = sorted(A, reverse=True)

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

print(ans)

Submission status: Accepted

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

A.sort(reverse=True)


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

print(ans)

Submission status: Accepted

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


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

Submission status: Accepted

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
import math
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
import sys
sys.setrecursionlimit(10000000)
#mod = 10**9 + 7
#mod = 9982443453
mod = 998244353
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
import math
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入g
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
#mod = 998244353
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
n = I()
A = sorted(readInts(), reverse=True)
ans = 0
dic = defaultdict(int)
dq = deque()
for i in range(n):
    if i == 0:
        dq.append(A[i])
    else:
        ans += dq.popleft()
        for j in range(2):
            dq.append(A[i])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
cnt = 0
for i, a in enumerate(A):
    if i == 0:
        ans += a
        cnt += 1
    else:
        if n-1 - cnt >= 2:
            ans += a*2
            cnt += 2
        else:
            ans += a
            cnt += 1
    if cnt == n-1:
        break
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
n = int(input())
A = sorted(list(map(int, input().split())))[::-1]

deq = deque([A[0]])
ans = 0
for x in A[1:]:
    ans += deq.popleft()
    deq.append(x)
    deq.append(x)
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())    
al = list(map(int, input().split()))    
al.sort(reverse=True)

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

print(ans)

Submission status: Accepted

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

A = sorted(A, reverse = True)

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

B = sorted(B, reverse = True)

ans = 0

for i in range(N - 1):
   ans +=  B[i]
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
c= sorted(list(map(int, input().split())), reverse=True)
l = len(c)
m = l//2
ans = c[0]

if l%2 == 0:
    for i in range(1,m):
        ans += 2*c[i]
else:
    for i in range(1,m):
        ans += 2*c[i]
    ans += c[m]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

sa = sorted(a,reverse=True)
ans = sa[0]
i = 2
for v in sa[1:]:
  if i >= n:
    break
  #print("i:{} ans:{}".format(i,ans))
  ans += v
  i += 1
  if i >= n:
    break
  #print("i:{} ans:{}".format(i,ans))
  ans += v
  i += 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = sorted(list(map(int, input().split())))
ans = a.pop()
if n % 2:
    for i in range((n - 2) // 2):
        ans += a.pop() * 2
    ans += a.pop()
    print(ans)
else:
    for i in range((n - 2) // 2):
        ans += a.pop() * 2
    print(ans)

Submission status: Accepted

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

cnt = 0
for i in range(len(A)):
  if i == 0:
    continue
  
  cnt += A[int(i/2)]

print(cnt)

Submission status: Accepted

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


def next_line():
    return input()


def next_int():
    return int(input())


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


def next_int_array_multi_lines(size):
    return [int(input()) for _ in range(size)]


def next_str_array(size):
    return [input() for _ in range(size)]


def main():
    n = next_int()
    ar = list(reversed(sorted(next_int_array_one_line())))
    # print(ar)
    res = ar[0]
    n -= 2
    use = 1
    while n > 0:
        if n >= 2:
            res += ar[use] * 2
            n -= 2
            use += 1
        else:
            res += ar[use]
            n -= 1
    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# import itertools
# import math
# import sys
# sys.setrecursionlimit(500*500)
# import numpy as np

N = int(input())
# S = input()
# n, *a = map(int, open(0))
# N, M = map(int, input().split())
A = list(map(int, input().split()))
# B = list(map(int, input().split()))
# tree = [[] for _ in range(N + 1)]
# B_C = [list(map(int,input().split())) for _ in range(M)]
# S = input()

A = sorted(A, reverse=True)
# all_cases = list(itertools.permutations(P))
# a = list(itertools.combinations_with_replacement(range(1, M + 1), N))
# itertools.product((0,1), repeat=n)

# A = np.array(A)
# cum_A = np.cumsum(A)
# cum_A = np.insert(cum_A, 0, 0)

# def dfs(tree, s):
#     for l in tree[s]:
#         if depth[l[0]] == -1:
#             depth[l[0]] = depth[s] + l[1]
#             dfs(tree, l[0])
# dfs(tree, 1)

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

print(tot)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys
import bisect as bi
import math
from collections import defaultdict as dd
import heapq
input=sys.stdin.readline
##import numpy as np
#sys.setrecursionlimit(10**7)
mo=10**9+7
def cin():
    return map(int,sin().split())
def ain():            
    return list(map(int,sin().split()))
def sin():
    return input()
def inin():
    return int(input())
##def power(x, y):
##    if(y == 0):return 1
##    temp = power(x, int(y / 2))%mo
##    if (y % 2 == 0):return (temp * temp)%mo 
##    else:
##        if(y > 0):return (x * temp * temp)%mo 
##        else:return ((temp * temp)//x )%mo
##
##for _ in range(inin()):
n=inin()
l=ain()
l=sorted(l,reverse=True)
if(n%2==0):
    if(n>=4):
        print(l[0]+0+2*sum(l[1:(n-2)//2+1]))
    else:
        print(l[0])
else:
    if(n>=5):
        print(l[0]+0+2*sum(l[1:(n-2)//2+1])+l[(n-2)//2+1])
    else:
        print(l[0]+l[1])
    


    
    
    
    
    
    
    
    
        
    
    
    
    
    
    
        
        
    
    
    
    


    





##
        
    
    
    
    
    
    
        
        
    
    
    
    


    





##def msb(n):n|=n>>1;n|=n>>2;n|=n>>4;n|=n>>8;n|=n>>16;n|=n>>32;n|=n>>64;return n-(n>>1) #2 ki power
##def pref(a,n,f):             
##    pre=[0]*n
##    if(f==0):         ##from beginning
##        pre[0]=a[0]
##        for i in range(1,n):
##            pre[i]=a[i]+pre[i-1]
##    else:              ##from end
##        pre[-1]=a[-1]
##        for i in range(n-2,-1,-1):
##            pre[i]=pre[i+1]+a[i]
##    return pre
##maxint=10**24 
##def kadane(a,size): 
##    max_so_far = -maxint - 1
##    max_ending_here = 0
##       
##    for i in range(0, size): 
##        max_ending_here = max_ending_here + a[i] 
##        if (max_so_far < max_ending_here): 
##            max_so_far = max_ending_here 
##  
##        if max_ending_here < 0: 
##            max_ending_here = 0   
##    return max_so_far

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
A=sorted(A,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()))
A=sorted(A)[::-1]
num=0
ans=0
cnt=1
i=0
while cnt<N:
    if i==0:
        ans+=A[i]
        i+=1
    else:
        if num==0:
            ans+=A[i]
            num+=1
        else:
            ans+=A[i]
            i+=1
            num=0
    cnt+=1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = sorted(map(int,input().split()),reverse = True)
answer = 0
i = 1
s = 0
answer = A[0]
if N == 2:
    anwer=A[0]
else:
    while True:
        s = (i+1)//2
        answer += A[s]
        i += 1
        if i == N-1:
            break
    
print(answer)

Submission status: Accepted

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

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

print(p)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import heapq

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

heapq.heappush(L, -A[0])

for i in range(1, N):
    max = heapq.heappop(L)
    max *= -1
    ans += max
    heapq.heappush(L, -A[i])
    heapq.heappush(L, -A[i])

print(ans)

Submission status: Accepted

Solution code:
import heapq

N=int(input())
A=sorted(list(map(int,input().split())), reverse=True)
ans=0
H=[-1*A[0]]
heapq.heapify(H)
for a in A[1:]:
  ans-=heapq.heappop(H)
  heapq.heappush(H,-1*a)
  heapq.heappush(H,-1*a)
print(ans)

Submission status: Accepted

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

def main():
    N = int( sys.stdin.readline() )
    A_list = list(map(int, sys.stdin.readline().split()))

    A_list.sort(reverse=True)
    arrived_idx = 1
    comfort = A_list[0]


    for i in range(1, len(A_list)):
        if ( len(A_list) - 1 - arrived_idx ) >= 2:
            comfort += (A_list[i] * 2)
            arrived_idx += 2
        elif ( len(A_list) - 1 - arrived_idx ) == 1:
            comfort += A_list[i]
            break
        else:  # ( len(A_list) - 1 - arrived_idx ) == 0:
            break


    print(comfort)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import heapq

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

heapq.heapify(a)
b = list(map(lambda x: x * (-1), a))
heapq.heapify(b)
ans -= heapq.heappop(b)

for i in range(n//2-1):
    ans -= 2 * heapq.heappop(b)
if n % 2 == 1:
    ans -= heapq.heappop(b)
print(ans)

Submission status: Accepted

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

A.sort(reverse=True)

B=[]
B.append(A[0])
for i in range(1,N):
    B.append(A[i])
    B.append(A[i])

ans=0
for i in range(N-1):
    ans += B[i]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

A.sort(reverse=True)

max_num, *p = A

node_num = N-1

q = [(2*i <= node_num) + (2*i+1 <= node_num) for i in range(1, node_num+1)]

# print(q)
# print(max_1, max_2, sum(p_i*q_i for p_i, q_i in zip(p, q)))
r = max_num + sum(p_i*q_i for p_i, q_i in zip(p, q))
print(r)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

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

main()

Submission status: Accepted

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

lis = sorted(lis, reverse=True)

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

print(ans)

Submission status: Accepted

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

lis = sorted(lis, reverse=True)

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

print(ans)

Submission status: Accepted

Solution code:

import sys
input = sys.stdin.readline

def inp():
    return(int(input()))
    
def inlt():
    return(list(map(int,input().split())))
    
n = inp()
f = inlt()
f = list(reversed(sorted(f)))
if n%2:
    print(2*sum(f[0:(n+1)//2])-f[0]-f[(n+1)//2-1])
else:
    print(2*sum(f[0:n//2])-f[0])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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


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]
cnt = 1
while cnt < n-1:
    ans += A[(cnt+1)//2]
    cnt += 1
print(ans)

Submission status: Accepted

Solution code:
import math
N = int(input())
A = list(map(int,input().split()))
A.sort(reverse = True)
score = A[0]
for i in range(1, math.ceil(N*0.5)):
    if N - i * 2 != 1:
        score += 2 * A[i]
    else:
        score += A[i]
print(score)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())

nums = []
nums = list(map(int, input().split()))
nums = sorted(nums, reverse=True)

cnt = 0

fir_num = nums[0]

del nums[0]

for i in range(n-2):
    fir_num += nums[int(i/2)]


print(fir_num)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def abc173d_chat_in_the_circle():
    n = int(input())
    a = sorted(map(int, input().split()), reverse=True)
    if n <= 1:
        print(0)
        return
    val = 0
    for i in range(0, n-1):
        idx = (i+1) // 2
        val += a[idx]
    print(val)

abc173d_chat_in_the_circle()

Submission status: Accepted

Solution code:
from collections import deque

def main():
    n = int(input())
    l = list(map(int, input().split()))

    comfort = 0
    l_sorted = sorted(l, reverse=True)
    q = deque([l_sorted[0]])
    for i in range(1, n):
        comfort += q[0]
        q.popleft()
        for _ in range(2):
            q.append(l_sorted[i])
    print(comfort)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

A.sort(reverse=True)
ans = 0
for i in range(N-1):
    ans += A[int((i+1)*0.5)]
    
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
nums = [int(i) for i in input().split()]


nums = sorted(nums, reverse=True)


a_sum = 0
for i in range(1,N):
    index = i//2
    a_sum += nums[index]

print(a_sum)

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
    n = ni()
    a = sorted(nl(), reverse=True)
    print(sum(a[:n//2]) + sum(a[1:(n+1)//2]))
    return

solve()

Submission status: Accepted

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

s = sorted(a, reverse=True)
print(sum(s[i//2] for i in range(1, n)))

Submission status: Accepted

Solution code:
from collections import deque
N = int(input())
A = sorted(map(int, input().split()), reverse=True)

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

Submission status: Accepted

Solution code:
n = int(input())
arr = list(map(int,input().split()))
arr.sort(reverse = True)
ans = arr[0]
odd = False
mult = n-2
if mult%2!=0:
    mult-=1
    odd = True
mult//=2
ans += sum(arr[1:1+mult])*2
if odd:
    ans+=arr[1+mult]
print(ans)
"""
Sample:
7
1 2 3 4 5 6 7


2537146
7+6+6+5+5+4
"""

Submission status: Accepted

Solution code:
import bisect
import statistics
n = int(input())
a = list((int(x) for x in input().split()))

a.sort()
incl = []
score = 0

for i in range(len(a)):
    ain = a.pop()
    incl.append(ain)
    if i==0: continue
    if (len(incl)%2 == 1):
        score += incl[ int(len(incl)/2) ]
    else:
        # smaller of middle two
        score += incl[ int(len(incl)/2)-1 ]

print(score)

Submission status: Accepted

Solution code:

N = int(input())
K = list(map(int,input().split()))
K = sorted(K,reverse=True)
"""
#print(K)
before_data = 0
before_data_before = 0
ans = 0 
for i in K :
    ans += before_data
    before_data_before = before_data
    before_data = i
"""
ans = 0 
for i,l in enumerate(K) :
    if (i == 0 ):
        ans += 0
    else :
        ans += K[i // 2]
print(ans)

Submission status: Accepted

Solution code:

"""
N= 4
K = [2, 2, 1, 3]
"""

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





K = sorted(K,reverse=True)
"""
#print(K)
before_data = 0
before_data_before = 0
ans = 0 
for i in K :
    ans += before_data
    before_data_before = before_data
    before_data = i
"""
ans = 0 
for i,l in enumerate(K) :
    if (i == 0 ):
        ans += 0
    
    else :
        ans += K[i // 2]

    


print(ans)

Submission status: Accepted

Solution code:
#D - Chat in a Circle
N = int(input())
A = list(map(int,input().split()))
#ソート
arr = []
arr = sorted(A,reverse = True)

Friend_min_max_wa = 0
i =0
j = 0
for i in range(N -1):
    if i % 2 != 0 : j = j + 1
    Friend_min_max_wa = Friend_min_max_wa + arr[j]

# 出力
print(Friend_min_max_wa)

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
'''
参考
https://yamakasa.net/atcoder-abc-173-d/
図
https://github.com/ymo999/AtCoder/blob/master/2020/20200705_ABC173/ABC173_D.pdf
'''
import math

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

ans = 0
for i in range(1, N):
    idx = math.floor(i/2)           # 心地よさの和を算出するためのindex
    ans += A[idx]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# 解説読んだ
# 大きい順に並べて、最初のが1回、それ以降は2回ずつ足されてN-1回で終了
# 構造まではなんとなく理解してたけど個数から頭が離れなかったなあ

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

A.sort()
ans=0

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
A = A + A
A.sort(reverse=True)
print(sum(A[1:N]))

Submission status: Accepted

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

L = len(A) // 2

if len(A) % 2 == 0:
    #偶数の時
    ans = sum(A[:L]) + sum(A[1:L])
else:
    #奇数の時
    ans = sum(A[:L]) + sum(A[1:L+1])

print(ans)

Submission status: Accepted

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

A.sort(reverse = True)

ans = A[0]

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

print(ans)

Submission status: Accepted

Solution code:
import math

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

s = sorted(a,reverse=True)

max = s[0]
for i in range(3,n+1):
  max += s[math.ceil(i/2)-1]
print(max)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


def sum_of_arithmetic_progression(s, d, n):
    return n * (2 * s + (n - 1) * d) // 2


def gcd(a, b):
    if b == 0:
        return a
    return gcd(b, a % b)


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


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

    ans = A[0]
    n = 1
    for i in range(1, N):
        if n == N - 1:
            break
        ans += A[i]
        n += 1
        if n == N - 1:
            break
        ans += A[i]
        n += 1
        if n == N - 1:
            break

    print(ans)


def main():
    solve()


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# import sys
# input = sys.stdin.readline
import itertools
import collections
from decimal import Decimal
from functools import reduce
import heapq

def main():
    n = int(input())
    a = input_list()
    aa = a + a
    aa.sort(reverse=True)
    ans = 0
    for i in range(1, n):
        ans += aa[i]
    print(ans)


def prime_factorize(n):
    a = []
    while n % 2 == 0:
        a.append(2)
        n //= 2
    f = 3
    while f * f <= n:
        if n % f == 0:
            a.append(f)
            n //= f
        else:
            f += 2
    if n != 1:
        a.append(n)
    return a


def bfs(H, W, black_cells, dist):
    d = 0
    while black_cells:
        h, w = black_cells.popleft()
        d = dist[h][w]
        for dy, dx in ((1, 0), (0, 1), (-1, 0), (0, -1)):
            new_h = h + dy
            new_w = w + dx
            if new_h < 0 or H <= new_h or new_w < 0 or W <= new_w:
                continue
            if dist[new_h][new_w] == -1:
                dist[new_h][new_w] = d + 1
                black_cells.append((new_h, new_w))
    return d


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


def input_list_str():
    return list(map(str, input().split()))


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
print(sum(sorted(a * 2)[::-1][1:n]))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split())) * 2
print(sum(sorted(a)[::-1][1:n]))

Submission status: Accepted

Solution code:
n=int(input())
lst=list(map(int,input().split()))
lst=sorted(lst)
if(n>2):
    sm=0
    sm+=lst[-1]
    k=0
    i=2
    num=2
    while(num<n):
        k+=1
        sm+=lst[-i]
        if(k==2):
            k=0
            i+=1
        num+=1
elif(n==2):
    sm=lst[-1]

print(sm)

Submission status: Accepted

Solution code:
import numpy as np
n=int(input())
A=np.array(input().split(),np.int64)
A.sort()
A=A[::-1]
ans=int(0)
for i in range(n-1):
    ans+=A[(i+1)//2]
print(ans)

Submission status: Accepted

Solution code:
import heapq

def solve():
    N = int(input())
    A = list(sorted(map(int, input().split()), reverse=True))
    C = [-A[0]]

    ans = 0
    for a in A[1:]:
        K = heapq.heappop(C)
        ans += -K
        heapq.heappush(C, -a)
        heapq.heappush(C, -a)

    print(ans)



solve()

Submission status: Accepted

Solution code:
## coding: UTF-8
#import random
import math

N = int(input())
A = list(map(int,input().split()))
#N = 9
#A = [1, 2, 3, 40, 50, 60, 100, 200, 300]


A = sorted(A, reverse = True)
#print(N, A)


score = 0
for i in range(1, N):
    index = math.floor(i/2)
    score += A[index]
    #print(i, index, A[index], score)

print(score)

Submission status: Accepted

Solution code:
N = int(input())
List = list(map(int,input().split()))
List += List
List = sorted(List)[::-1]
List = List[1:N]
print(sum(List))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
q, r = divmod(n, 2)
print(a[0] + sum(a[1:q]) * 2 + a[q] * r)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
A=list(map(int,input().split()))
A.sort(reverse=True)
cnt=2
ans=A[0]
i=1
while cnt<N:
	ans+=A[i]
	if (cnt+1)%2==0:
		i+=1
	cnt+=1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
ini = lambda : int(input())
inm = lambda : map(int,input().split())
inl = lambda : list(map(int,input().split()))
gcd = lambda x,y : gcd(y,x%y) if x%y else y

n = ini()
a = inl()
a = sorted(a,reverse = True)
c = 1
j = 0
ans = 0
for i in range(1,n):
        ans += a[j]
        c += 1
        if c == 2:
                c = 0
                j += 1
print(ans)

Submission status: Accepted

Solution code:
import queue

ans = 0

N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
q = queue.Queue()
q.put(0)
for a in range(len(A)):
    ans += q.get()
    q.put(A[a])
    if a != 0:
        q.put(A[a])

print(ans)