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

friend.sort(reverse = True)

ans = friend[0]
i = 1
ind = 1
while i < n - 1:
    for j in range(2):
        ans += friend[ind]
        i += 1
        if i == n-1: break
    ind += 1
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# coding: utf-8

def solve(*args: str) -> str:
    n = int(args[0])
    A = sorted(map(int, args[1].split()), reverse=True)

    return str(sum([A[i//2] for i in range(1, n)]))


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
from collections import deque


def solve(*args: str) -> str:
    n = int(args[0])
    A = sorted(map(int, args[1].split()), reverse=True)

    Q = deque()

    ret = 0
    for i in range(-(-n//2)):
        if 0 < i:
            ret += min(Q[-1], Q[0])
        if i % 2:
            Q.appendleft(A[i])
        else:
            Q.append(A[i])

    for i in range(n-len(Q)):
        ret += min(Q[i], Q[(i+1) % len(Q)])

    return str(ret)


if __name__ == "__main__":
    print(solve(*(open(0).read().splitlines())))

Submission status: Accepted

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

Submission status: Accepted

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

A.sort()
A.reverse()
ans = 0
if N%2 == 0 :
  ans += A[0]
  for i in range(int(N/2) - 1) :
    ans += A[i+1] * 2
if N%2 == 1 :
  ans += A[0]
  ans += A[int(N/2)]
  for i in range(int(N/2) - 1) :
    ans += A[i+1] * 2
    
print(ans)

Submission status: Accepted

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

a = sorted(a)
a = list(reversed(a))

ans = 0
index = 0

for i in range(n):
    if i == 0:
        continue
    elif i == 1:
        ans += a[0]
        index += 1
    elif i%2 == 0:
        ans += a[index]
    else:
        ans += a[index]
        index += 1

print(ans)

Submission status: Accepted

Solution code:
import sys
# import re
import math
import collections
# import decimal
import bisect
import itertools
import fractions
# import functools
import copy
# import heapq
import decimal
# import statistics
import queue

# import numpy as np

sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7
# MOD = 998244353

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))

# ===CODE===

eps = 10 ** (-9)


def main():
    n = ni()
    a = na()

    a.sort(reverse=True)

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

n=int(input())
b=input().split()
a=[0 for i in range(n+1)]
for i in range(1,n+1):
  a[i]=int(b[i-1])

a[0]=10**10

m=int((n-2)/2)

a=sorted(a,reverse=True)

comfort=a[1]
if(n==2):
  print(comfort)
else:
  
  for i in range(2,m+2):
    comfort+=(2*a[i])

  if(n%2 ==0):
    print(comfort)
  else:
    print(comfort+a[m+2])

Submission status: Accepted

Solution code:
import sys 

def A():
  n = int(sys.stdin.readline().rstrip())
  charge = (n+999)//1000 * 1000 - n
  print(charge)


from collections import Counter
def B():
  n, *s = sys.stdin.read().split() 
  c = Counter(s)
  for v in 'AC, WA, TLE, RE'.split(', '):
    print(f'{v} x {c[v]}')


def C():
  h, w, k = map(int, sys.stdin.readline().split())
  c = [sys.stdin.readline().rstrip() for _ in range(h)]
  tot = 0
  for i in range(1<<h):
    for j in range(1<<w):
      cnt = 0
      for y in range(h):
        for x in range(w):
          if i>>y & 1 or j>>x & 1:
            continue 
          cnt += c[y][x] ==  '#'
      tot += cnt == k
  print(tot)


def D():
  n, *a = map(int, sys.stdin.read().split())
  a.sort(reverse=True)
  res = a[0] + sum(a[1:1+(n-2)//2])*2 + a[1+(n-2)//2]*(n & 1)
  print(res)


def E():
  pass


def F():
  pass

if __name__ == "__main__":
  # A() 
  # B()
  # C()
  D()
  E()
  F()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

import bisect
import copy
import collections
import heapq
import itertools
import math
import numpy as np
from operator import itemgetter
import scipy.misc
import sys

"""
#### 入力例 ####
N, X = map(int, input().split())

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

P = [0]*N
Y = [0]*N
for n in range(N):
    P[n], Y[n] = map(int, input().split())
"""


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

def main():
    A.sort(reverse=True)
    dq = collections.deque(A)
    now = dq.popleft()
    hq = []
    heapq.heapify(hq)
    heapq.heappush(hq, (-now, now))    
    ans = 0
    for n in range(1,N):
        ans += heapq.heappop(hq)[1]
        now = dq.popleft()
        heapq.heappush(hq, (-now, now))
        heapq.heappush(hq, (-now, now))
    print(ans)
    return
    

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:

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

A = sorted(A)
A = A[::-1]
sumkoko = A.pop(0)
sumkoko += sum(A[0:(N-2)//2]) * 2
if ((N-2)%2) == 1:
    sumkoko += A[(N-2)//2]

print(sumkoko)

Submission status: Accepted

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

res = 0
res += a[0]
for i in range(2, n):
  res += a[i//2]
  
print(res)

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import time,random,resource

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1,0),(0,1),(1,0),(0,-1)]
ddn = [(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
def LI_(): return [int(x)-1 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 I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)
def JA(a, sep): return sep.join(map(str, a))
def JAA(a, s, t): return s.join(t.join(map(str, b)) for b in a)


def main():
    n = I()
    a = sorted(LI(), reverse=True)

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

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


print(main())

Submission status: Accepted

Solution code:
n = int(input())

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

a = sorted(a, reverse = True)
final = 0

if n % 2 == 0:
    ak = a[0:int(n/2)]
    for i in ak[1::]:
        final += i
    final *= 2
    final += ak[0]
else:
    ak = a[0:(int(n/2) + 1)]
    for i in ak[1::]:
        final += i
    final *= 2 
    final += ak[0]
    final -= ak[len(ak) - 1]


print(final)

Submission status: Accepted

Solution code:
def main():
    import sys
    readline = sys.stdin.buffer.readline
    sys.setrecursionlimit(10 ** 7)

    n = int(readline())
    a = list(map(int, readline().split()))
    a.sort(reverse=True)
    idx = 1
    ans = a[0]
    for aa in a[1:]:
        if idx + 2 >= n:
            if idx + 1 < n:
                ans += aa
            break
        idx += 2
        ans += aa * 2
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n = int(input())
a = list(map(int, input().split()))
a.sort()
ans = a[-1]
cnt = 1
while cnt < n - 1:
    ans += a[-1 * (1 + (1 + cnt) // 2)]
    cnt += 1

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(reversed(sorted([int(i) for i in input().split()])))
ans = a[0]
n -= 2
i = 1
while n > 0:
    if n > 1:
        ans += a[i] * 2
        n -= 2
        i += 1
    else:
        ans += a[i]
        n -= 1
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

ans = A[0]
N -= 2

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

if N & 1:
    ans += A[N // 2 + 1]

print(ans)

Submission status: Accepted

Solution code:
import heapq
n = int(input())

arr = list(map(int, input().split()))
arr = sorted(arr, reverse=True)
ans = 0
q = []
heapq.heappush(q, -arr[0])
for i in range(1, n):
    tmp = heapq.heappop(q)
    tmp *= -1
    ans += tmp
    heapq.heappush(q, -arr[i])
    heapq.heappush(q, -arr[i])
print(ans)

Submission status: Accepted

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

# Aを降順に並び替える
A = sorted(A)[::-1]

# A0 + A1 + A1 + A2 + A2 + ....
sum = 0
for i in range(len(A) - 1):
    if i % 2 == 1:
        sum += A[int((i+1)/2)]
    elif i % 2 == 0:
        sum += A[int(i/2)]
print(sum)

Submission status: Accepted

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

a.sort(reverse=True)

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

a.sort(reverse=True)

score = - a[0]
for i in range(n):
    score += a[i // 2]
print(score)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

SUM=0
A=sorted(A)
COUNT=N-1

if COUNT==0:
    print(SUM)
    exit()

SUM+=A[-1]
del A[-1]
COUNT-=1

if COUNT==0:
    print(SUM)
    exit()

while True:
    for i in range(2):
        SUM+=A[-1]
        COUNT-=1
        if COUNT==0:
            print(SUM)
            exit()
    del A[-1]

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())
A_list = list(map(int, input().split()))
A_np = np.array(A_list)
A_sort = np.sort(A_np)[::-1]
comfort = 0
for i in range(0,N-1):
  if i == 0:
    comfort = comfort + A_sort[i]
  else:
    comfort = comfort + A_sort[int((i+1)/2)]
print(comfort)

Submission status: Accepted

Solution code:
n=int(input())
a=[int(i) for i in input().split()]
a.sort()
ans=0
ans+=max(a)
a.pop()
n-=2
while n:
#    print("#",ans)
    if n>=2:
        ans+=a.pop()*2
        n-=2
    else:
        ans+=a.pop()
        n-=1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

A = list(map(int, input().split()))
A.sort()
if N % 2:
    start = N//2
    end = N-1
    print(sum(A[start:end]) * 2 + A[end] - A[start])
else:
    start = N//2
    end = N-1
    print(sum(A[start:end]) * 2 + A[end])

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N = I()
    A = LI()

    A.sort(reverse=True)

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

    print(ans)

if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
def main():
    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)
if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/python3
import sys
input = lambda: sys.stdin.readline().strip()
n = int(input())
a = sorted((int(x) for x in input().split()), reverse=True)
print(sum(a[1 + i // 2] for i in range(n - 2)) + a[0])

Submission status: Accepted

Solution code:
import heapq

N = int(input())
An = [int(e) for e in input().split()]

def quick_sort(arr):
    left = []
    right = []
    if len(arr) <= 1:
        return arr

    ref = sum([e for e in arr]) / len(arr)

    for e in arr:
        if e < ref:
            left.append(e)
        else:
            right.append(e)

    if len(left) == 0:
        return right

    left = quick_sort(left)
    right = quick_sort(right)

    return left + right

p = quick_sort(An)
v0 = p.pop()

q = [-v0]
heapq.heapify(q)

s = 0

while p:
    v = heapq.heappop(q)
    newv = p.pop()
    heapq.heappush(q, -newv)
    heapq.heappush(q, -newv)
    s -= v

print(s)

Submission status: Accepted

Solution code:
n= int(input())
n -= 1
arr= sorted(list(map(int,input().split())))[::-1]
ans = arr[0]
i = 1
while n > 1:

    if n > 2:
        ans += arr[i]*2
        i += 1
        n -= 2
    elif n ==2:
        ans += arr[i]
        break
                
print(ans)

Submission status: Accepted

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

value = 0

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

print(value)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

    a_sort = sorted(a, reverse=True)

    ans = a_sort[0]

    if (n-2) % 2 == 0:
        for i in range(int((n-2)/2)):
            ans += a_sort[i+1]*2

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

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N = int(input())
A = sorted(list(map(int, input().split())), reverse=True) # 降順にソート
count = 0
score = 0
i = 0
while count < N-1:
  if i == 0:
    score += A[0]
    count += 1
    i += 1
    continue
  score += A[i]
  count += 1
  if count < N-1:
    score += A[i]
    count += 1
  i += 1
print(score)

Submission status: Accepted

Solution code:
import heapq

def solve(n, a):
    a = sorted(a, reverse=True)
    ans = a[0]
    que = [(-a[1], -a[0]), (-a[1], -a[0])]
    heapq.heapify(que)
    for i in range(2, n):
        x, y = heapq.heappop(que)
        ans += min(-x, -y)
        heapq.heappush(que, (-a[i], x))
        heapq.heappush(que, (-a[i], y))
    return ans

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
#import time
#from collections import deque, Counter, defaultdict
#from fractions import gcd
import bisect
#import heapq
#import math
import itertools
import numpy as np
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
inf = 10**18
MOD = 1000000007
ri = lambda : int(input())
rs = lambda : input().strip()
rl = lambda : list(map(int, input().split()))


n = ri()
a = rl()
a=a+a
a.sort()

a.pop()
ans=0
for i in range(n-1):
    ans+=a[-i-1]
print(ans)

Submission status: Accepted

Solution code:
import sys
#import time
#from collections import deque, Counter, defaultdict
#from fractions import gcd
import bisect
#import heapq
#import math
import itertools
input = sys.stdin.readline
sys.setrecursionlimit(10**8)
inf = 10**18
MOD = 1000000007
ri = lambda : int(input())
rs = lambda : input().strip()
rl = lambda : list(map(int, input().split()))


n = ri()
a = rl()
a=a+a
a.sort()

a.pop()
ans=0
for i in range(n-1):
    ans+=a[-i-1]
print(ans)

Submission status: Accepted

Solution code:
import sys
import math
import itertools
import bisect
from copy import copy
from collections import deque,Counter
from decimal import Decimal
import functools
def s(): return input()
def k(): return int(input())
def S(): return input().split()
def I(): return map(int,input().split())
def X(): return list(input())
def L(): return list(input().split())
def l(): return list(map(int,input().split()))
def lcm(a,b): return a*b//math.gcd(a,b)
sys.setrecursionlimit(10 ** 9)
mod = 10**9+7
cnt = 1
ans = 0
inf = float("inf")

n = k()
a = l()

a.sort(reverse = True)

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.sort(reverse=True)
P=A[0]
for i in range(2,N):
  P+=A[i//2]
print(P)

Submission status: Accepted

Solution code:
import sys
#DEBUG=True
DEBUG=False
if DEBUG:
    f=open("202007_1st/D_input.txt")
else:
    f=sys.stdin
N=int(f.readline().strip())
A=list(map(int,f.readline().split()))
A.sort(reverse=True)
ans=0
for _ in range(1,N):
    ans+=A[int(_/2)]
print(ans)
f.close()

Submission status: Accepted

Solution code:
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,len(a)):
  d.append(a[i])
  d.append(a[i])
  ans+=d.popleft()
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = sorted([int(x) for x in input().split(" ")])[::-1]

N -= 2
S = A[0]

i = 1
while 1:
    if N == 0:
        break 
    if N == 1:
        S += A[i]
        break
    S += 2 * A[i]
    N -= 2
    i += 1
print(S)

Submission status: Accepted

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

# a_list = []
# a_dic = {}
# for a_i in reversed( a[ : -2 ] ):
#     try:
#         a_dic[ a_i ] += 1
#     except KeyError:
#         a_dic[ a_i ] = 1
#         a_list.append( a_i )

# ans = a_list[ 0 ]
# for a_i in a_list[1:]:
#     print( ans )
#     ans += ( a_dic[ a_i ] + 1 ) * a_i

# print( ans )

# 以下カンニング
n = int( input() )
a = list( map( int, input().split() ) )
a.sort()

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

print( ans )

Submission status: Accepted

Solution code:
N = int(input())
A = [int(hoge) for hoge in input().split()]
A.sort(reverse=True)
Twoused = (N - 2)//2
Twosum = 2*sum([A[i] for i in range(1,Twoused+1)])
if N%2:
    ans = Twosum + A[0] + A[Twoused+1]
else:
    ans = Twosum + A[0]
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())
l = list(map(int, input().split()))
l = l + l
l = sorted(l, reverse = 1)

print(sum(l[1:n]))

Submission status: Accepted

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


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

Submission status: Accepted

Solution code:
count = int(input())
people=input().split(" ")
people1=[]
 
for i in range(count):
    people1.append(int(people[i]))
 
people1.sort(reverse=True)
#print(people1)

ans=0
k=1
for i in range(count-1):
    if i == 0:
        ans = people1[0]
    elif i % 2 != 0:
        ans = ans+people1[k]
    else:
        ans = ans + people1[k]
        k=k+1
    i=i+1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = sorted(list(map(int,input().split())),key=lambda x: -x)
import heapq
Q = [-A.pop(0)]
ans = 0
for a in A:
    tmp = -heapq.heappop(Q)
    ans += tmp
    heapq.heappush(Q,-a)
    heapq.heappush(Q,-a)
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
a.sort(reverse=True)
c = []
for i in range(n):
    c.append(i)
    c.append(i)
ans = 0
for i in range(n):
    ans += a[c[i]]

print(ans-a[0])

Submission status: Accepted

Solution code:
n,*l=map(int,open(0).read().split())
l.sort(reverse=1)
print(sum(l[i//2] for i in range(1,n)))

Submission status: Accepted

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

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(1,n-1):
        if i%2!=0:
            ans += a[(i+1)//2]
        else:
            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)
ans=0
li=[A[0]]
for i in range(1,N):
  ans+=li[i-1]
  li.append(A[i])
  li.append(A[i])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import bisect
import copy
import heapq
import sys
import itertools
import math
from functools import lru_cache
from collections import deque
input = sys.stdin.readline
sys.setrecursionlimit(1000000)
mod = 10 ** 9 + 7 

def read_values(): return map(int, input().split())
def read_index(): return map(lambda x: int(x) - 1, input().split())
def read_list(): return list(read_values())
def read_lists(N): return [read_list() for n in range(N)]


def main():
    N = int(input())
    A = read_list()
    A.sort(reverse=True)
    
    res = 0
    for n in range(N):
        if n == 0:
            res += 0
        else:
            res += A[n // 2]

    print(res)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())
print(sum(sorted([int(x) for x in input().split()] * 2, reverse = True)[1:n]))

Submission status: Accepted

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

ans = A[0] + 2*sum(A[1:math.floor(n/2)]) + (n%2)*A[math.floor(n/2)]
print(ans)

Submission status: Accepted

Solution code:
from collections import deque

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

aa = deque(sorted(a, reverse=True))
ans = aa.popleft()
for i in range(n-2):
    if i % 2 == 0:
        comf = aa.popleft()
    ans += comf
    #print(ans)
print(ans)

Submission status: Accepted

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


def main():
    N = int(input())
    A = list(map(int,input().split()))
    A.sort(reverse = True)
    '''
    niko = (len(A) - 2) // 2
    conft = A[0] + sum(A[1:niko + 1]) * 2
    if (len(A) - 2) % 2 == 1:
        conft += A[niko + 2]
    print(conft)
    '''
    ans=0
    for i in range(1, N): #sum[i=2 to n](a(i+1)//2)を求める
        ans += A[i//2]
    print(ans)

    
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:

import sys, io, os, re
from bisect import bisect, bisect_left, bisect_right, insort, insort_left, insort_right
from pprint import pprint
from math import sin, cos, pi, radians, sqrt, floor, ceil
from copy import copy, deepcopy
from collections import deque
from fractions import gcd
from functools import reduce
from itertools import groupby
from heapq import heapify, heappush, heappop

# sys.setrecursionlimit(5000)

n1 = lambda: int(sys.stdin.readline().strip())
nn = lambda: list(map(int, sys.stdin.readline().strip().split()))
f1 = lambda: float(sys.stdin.readline().strip())
fn = lambda: list(map(float, sys.stdin.readline().strip().split()))
s1 = lambda: sys.stdin.readline().strip()
sn = lambda: list(sys.stdin.readline().strip().split())
nl = lambda n: [n1() for _ in range(n)]
fl = lambda n: [f1() for _ in range(n)]
sl = lambda n: [s1() for _ in range(n)]
nm = lambda n: [nn() for _ in range(n)]
fm = lambda n: [fn() for _ in range(n)]
sm = lambda n: [sn() for _ in range(n)]

def array1(n, d=0): return [d] * n
def array2(n, m, d=0): return [[d] * m for x in range(n)]
def array3(n, m, l, d=0): return [[[d] * l for y in xrange(m)] for x in xrange(n)]
def linc(A, d=1): return map(lambda x: x + d, A)
def ldec(A, d=1): return map(lambda x: x - d, A)

N = n1()
A = nn()

A.sort(reverse=True)

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import heapq
N = int(input())
A = list(map(int, input().split()))
A = sorted(A, reverse=True)
ans = 0
q = []
heapq.heappush(q,-A[0])
for i in range(1,N):
    tmp = heapq.heappop(q)
    tmp*=-1
    ans+=tmp
    heapq.heappush(q,-A[i])
    heapq.heappush(q,-A[i])
print(ans)

Submission status: Accepted

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

a.sort(reverse=True)
p = -(-n//2)
print(sum(a[:p])*2 - a[0] - (0 if n % 2 == 0 else a[p-1]))

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
n = int(input())
x = list(map(int,input().split()))
x.sort(reverse=True)
count = 0
for i in range(n-1):
    count += x[int((i+1)/2)]
print(count)

Submission status: Accepted

Solution code:
import sys
def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int,sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())
N = I()
A = LI()

A = sorted(A, reverse=True)
ans = A[0]
A = A[1:]
i = 0

while i < N-2:
	ans += A[i//2]
	i += 1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#atcoder template
def main():
    import sys
    imput = sys.stdin.readline
    #文字列入力の時は上記はerrorとなる。
    #ここにコード
    #input
    n = int(input())
    A = list(map(int, input().split()))

    #output
    #Aは予め降順に並べておく。
    #n = 2の時
        #s = A[0]
    #n = 3の時
        #まずA[0]を入れる。次にA[1]が入ってA[0]が点になる。次にA[2]が入ってA[1]が点になる。
        #s = A[0] + A[1]
    #n = 4の時
        #A[0]とA[1]の間にA[3]を入れる。
        #s = A[0] + A[1] + A[1]
    #n = 5の時
    A = sorted(A, reverse = True)
    answer = 0
    for i in range(n-1):
        answer += A[(i+1)//2]
    print(answer)    

    #N = 1のときなどcorner caseを確認！
if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n = int(input())

a_list = list(map(int,input().split()))
for i in range(n):
  a_list[i] *= -1
import heapq  # heapqライブラリのimport
heapq.heapify(a_list)
n = n-1
result = 0
target = heapq.heappop(a_list)
result += -(target)
n = n-1

while True:
  if n == 0:
    break
  target = heapq.heappop(a_list)
  result += -(target)
  n = n-1
  if n == 0:
    break
  result += -(target)
  n = n-1
  if n == 0:
    break
print(result)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
arr = sorted(list(map(int,input().split())),reverse = True)
vals = [arr[0]]
import math
for i in range(1,math.ceil(n//2) + 1):
  vals.append(arr[i])
  vals.append(arr[i])
print(sum(vals[:n-1]))