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 = [int(i) for i in input().split()]
a.sort(reverse=True)
b = [a[0]]
for i in range(1, n):
    b.append(a[i])
    b.append(a[i])
print(sum(b[:n - 1]))

Submission status: Accepted

Solution code:
n = int(input())
a = [int(i) for i in input().split()]
a.sort(reverse=True)
i = 1
ans = a[0]
c = 1
while c < n - 1:
    k = min(2, n - 1 - c)
    c += k
    ans += a[i] * k
    i += 1
print(ans)

Submission status: Accepted

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


a_sort = sorted(a, reverse=True)

a_sum = a_sort[0]

if (N-2) % 2 == 0:
    for i in range(int((N-2)/2)):
        a_sum += 2*a_sort[i+1]
    print(a_sum)
else:
    for i in range((N-2)//2):
        a_sum += 2*a_sort[i+1]
    print(a_sum+a_sort[(N-2)//2 + 1])

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
ans = 0
ans = A[0] + sum(A[1:N//2])*2
if N % 2 == 1:
    ans += A[N//2]
# print(A[1:N//2])
# スコアを獲得できるのはN-1回
print(ans)

Submission status: Accepted

Solution code:
import collections

n=int(input())
a= list(map(int, input().split()))
aa=list(set(a))
sortaa=sorted(aa,reverse=True)
c = collections.Counter(a)
comf=[]
for i in range(len(aa)):
    if i == 0:
        comf.extend([sortaa[i] for ii in range(2*c[sortaa[i]]-1)])
    else:
        comf.extend([sortaa[i] for ii in range(2*c[sortaa[i]])])

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

Submission status: Accepted

Solution code:
n = int(input())
a = sorted(map(int,input().split()))[::-1]
ans = 2 * sum(a[:(n-1)//2+1]) - 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)
ans=a[0]
x=1
for i in range(n-2):
  ans+=a[x]
  if i%2==1:
    x+=1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
arr = sorted(list(map(int, input().split())),reverse=True)
arr2 = [arr[0]]

for i in range(1,N,1):
    arr2.append(arr[i])
    arr2.append(arr[i])


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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math

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


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

Submission status: Accepted

Solution code:
import math

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

K = int(math.log2(N-1))

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

Submission status: Accepted

Solution code:
N=int(input())
A=list(int(x) for x in input().split())
A.sort()
sum = 0
for i in range(N-1):
  k=i+1
  sum+=A[N-int(k/2)-1]
print(sum)

Submission status: Accepted

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

a.sort(reverse=True)
ans = a[0]
now = 1
cnt = 0
while n > 0:
    if cnt == 0:
        ans += a[now]
        cnt += 1
        n -= 1
    else:
        ans += a[now]
        cnt = 0
        n -= 1
        now += 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(sum(A[:int(N/2)])*2 - A[0])
else:
    print(sum(A[:N//2+1])*2 - A[0] - A[N//2])

Submission status: Accepted

Solution code:
def resolve():
    '''
    code here
    '''
    N = int(input())
    As = [int(item) for item in input().split()]
    As.sort()

    res = 0

    for i in range(1, N):
        res += As[N-1 - i//2]        

    print(res)

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
# coding: utf-8
N=int(input())
A=list(map(int,input().split()))

A.sort()
ans=max(A)

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int,input().split()))
li.sort()
lis = li
for i in range(n-1):
    lis.append(li[i])
lis.sort()
print(sum(lis[-(n-1):]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
s2nn = lambda s: [int(c) for c in s.split(' ')]
ss2nn = lambda ss: [int(s) for s in ss]
ss2nnn = lambda ss: [s2nn(s) for s in ss]
i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())
ii2ss = lambda n: [sys.stdin.readline().rstrip() for _ in range(n)]
ii2sss = lambda n: [list(sys.stdin.readline().rstrip()) for _ in range(n)]
ii2nn = lambda n: ss2nn(ii2ss(n))
ii2nnn = lambda n: ss2nnn(ii2ss(n))
from collections import deque  # 双方向キュー
from collections import defaultdict  # 初期化済み辞書
from heapq import heapify, heappush, heappop, heappushpop  # プライオリティキュー
from bisect import bisect_left, bisect_right  # 二分探索
sys.setrecursionlimit(int(1e+6))
MOD = int(1e+9) + 7
#import numpy as np  # 1.8.2
#import scipy  # 0.13.3

def main():
    N = i2n()
    A = i2nn()
    A.sort()
    A.reverse()
    # 5 4 3 2 1 1
    # 0: 5
    # 5: 5 4
    # 9: 5 3 4
    # 13: 5 3 4 2
    # 16: 5 1 3 4 2
    # 19: 5 1 3 1 4 2
    n = 0
    for i in range(1, N):
        n += A[i // 2]
    print(n)

main()

Submission status: Accepted

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

A += A[1:]
A = sorted(A, reverse=True)

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

Submission status: Accepted

Solution code:
import sys
import heapq

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
printV = lambda x: print(*x, sep="\n")
printH = lambda x: print(" ".join(map(str,x)))
def IS(): return sys.stdin.readline()[:-1]
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LI1(): return list(map(int1, sys.stdin.readline().split()))
def LII(rows_number): return [II() for _ in range(rows_number)]
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def LLI1(rows_number): return [LI1() for _ in range(rows_number)]
mo = 10**9+7

def main():
    N = II()
    A = LI()
    A.sort(reverse=True)
    ans = 0
    hq=[]
    heapq.heapify(hq)
    heapq.heappush(hq,A[0]*-1)
    for i,a in enumerate(A[1:]):
        ans+=heapq.heappop(hq)*-1
        heapq.heappush(hq,a*-1)
        heapq.heappush(hq,a*-1)
    print(ans)

    

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def solve(ls, debug=0):
    n = len(ls)
    ls = sorted(ls)
    result = 0
    # Pick once largest
    if n >= 2:
        result += ls[-1]
    # Pick twice for each next larger one
    for i in range(2, n):
        result += ls[-1 - i // 2]
    return result


def main(istr, ostr):
    n = int(istr.readline())
    ls = list(map(int, istr.readline().strip().split()))
    print(solve(ls), file=ostr)


if __name__ == "__main__":
    import sys

    main(sys.stdin, sys.stdout)

Submission status: Accepted

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

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
N = int(input())

A = []
a, *an = map(int, input().split())
A.append(a)
A += an
A.sort()

Node = []
score = 0
Node.append(A.pop())

#print(A)
start = 0

for n in range(N - 1):
    score += Node[start]
    num = A.pop()
    start += 1
    #Node = Node[1:]
    Node.append(num)
    Node.append(num)

#print(Node)
print(score)

Submission status: Accepted

Solution code:
from collections import deque

N = int(input())
A = sorted(list(map(int, input().split())), reverse=True)
s,d = 0,deque()

d.append(A[0])
for i in range(1,N):
	s += d.popleft()
	d.append(A[i])
	d.append(A[i])
print(s)

Submission status: Accepted

Solution code:
import math
n = int(input())
lst = list(map(int, input().split()))
sortlst = sorted(lst, reverse = True)
#print(sortlst)
ans = 0
for i in range(n - 1):
	point =sortlst[math.ceil(i / 2)]
#	print(point)
	ans += point

print(ans)

Submission status: Accepted

Solution code:
import math

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

ans = friendliness[0]

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = input()
A = A.split()
A = [int(i) for i in A]
A = sorted(A, reverse = True)
#print(A)
ans = 0

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

ans -= A[0]
#print(ans)
if n&1:
  ans += A[int(n/2)]


print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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()))
B=sorted(A,reverse=True)
S=0
i=0
for j in range(1,N):
    if j%2==1:
        S+=B[i]
        i+=1
    else:
        S+=B[i]
print(S)

Submission status: Accepted

Solution code:
import numpy as np
N = int(input())

A = np.array([int(i) for i in input().split()])
A_sort = np.sort(A)

#print(A_sort)
score = 0
score += A_sort[-1]
for i in range(0, (N-2)//2):
    score += A_sort[-2-i] * 2
    
if (N-2) % 2 == 1:
    score += A_sort[-3-i]
    
print(score)

Submission status: Accepted

Solution code:
import math

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

    A.sort()

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

if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
N = int(input())
table = sorted(list(map(int, input().split())), reverse=True)
ans = table[0]
rst = N - 2

for ti in table[1:]:
    if rst == 0:
        break
    if rst == 1:
        ans = ans + ti
        break
    ans = ans + 2 * ti
    rst = rst - 2

print(ans)

Submission status: Accepted

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

    if amari==1:
        ans += A[i+1]*amari

    print(ans)

main()

Submission status: Accepted

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

if n % 2 == 0:
    total = 0
    for i in A[::-1][:n//2]:
        total += i*2
    total = total - A[-1]
    print(total)
else:
    total = 0
    for i in A[::-1][:n//2+1]:
        total += i*2
    total = total - A[-1]
    total = total - A[-(n//2+1)]
    print(total)

Submission status: Accepted

Solution code:
import math

n = int(input())
players = [int(x) for x in input().split()]
players.sort(reverse = True)
comfortness = 0
for i in range(n-1):
    comfortness += players[math.ceil(i/2)]

print(comfortness)

Submission status: Accepted

Solution code:
from collections import deque
n = int(input())
a_ls = list(map(int, input().split()))
a_ls.sort(reverse = True)
queue = deque()
queue.append(a_ls[0])
for i in range(1,n):
    queue.append(a_ls[i])
    queue.append(a_ls[i])
ans = 0
for _ in range(n-1):
    ans += queue.popleft()
print(ans)

Submission status: Accepted

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

A.sort(reverse=True)
Aq=deque(A)

ans=Aq.popleft()
a=0
for i in range(1,N):
    if i%2==0:
        a=Aq.popleft()
    ans+=a

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==1:
  n=int((N-1)/2)
  ans+=A[n]
  for i in range(1,n):
    ans+=2*A[i]
elif N==2:
  ans+=0
elif N%2==0 and N>2:
  n=int((N-2)/2)
  for i in range(1,n+1):
    ans+=2*A[i]
print(ans)

Submission status: Accepted

Solution code:
import sys
import collections
import bisect
import math
readline = sys.stdin.readline


def main():
    n = int(readline().rstrip())
    A = sorted(list(map(int, readline().rstrip().split())))
    a = sorted(A[0:n-1] * 2, reverse=True)
    print(max(A) + sum(a[0:n-2]))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

a.sort(reverse=True)

ans = a[0]

mid = int(len(a) / 2)
for i in range(1, mid):
    ans = ans + a[i] * 2
if len(a) % 2 == 1:
    ans = ans + a[mid]

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
arr = list(map(int, input().split()))
arr.sort(reverse=True)
ans = arr[0]
i = 1
j = 1
while i < n-1:
    ans += arr[j]
    #print (arr[j])
    if i % 2 == 0:
        j += 1
    i += 1
print (ans)

Submission status: Accepted

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

# import copy


# 引数が一つだけのやつ
n = int(input())
# 引数が複数あるやつ（全部数字）
target_1 = [int(x) for x in input().split()]

# 引数が複数あるやつ（そうじゃないの）
# target = [int(x) for x in input().split()]

# number = int(input())
# 複数行を受け取って処理するやつ
# target = [list(input()) for i in range(H)]

# 関数
def count_sharp(target):
    return sum([x.count("#") for x in target])


def bin_code(value, keta):
    return list(("{:0" + str(keta) + "b}").format(value))


# 初期
target_1.sort()
point = 0
for idx, ins in enumerate(target_1):
    if idx != len(target_1) - 1:
        point += target_1[-1 - math.ceil(idx / 2)]

# 出力
print(point)

Submission status: Accepted

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

A.sort(reverse=True)
ans = 0
tmp = 0
j = 0
for i in range(len(A)-1):
    if i == 0:
        ans += A[0]
        j += 1
    elif i % 2 == 0:
        ans += tmp
    else:
        tmp = A[j]
        ans+=tmp
        j += 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.sort(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 math
N = int(input())
A = list(map(int, input().split()))
ans = 0
A = sorted(A, reverse = True)
for i in range(N-1):
    ans += A[math.ceil(i/2)]
print(ans)

Submission status: Accepted

Solution code:
n  = int(input())
ai = list(reversed(sorted(map(int, input().split()))))

added   = 2
mins    = [ ai[1], ai[1] ]
comfort = ai[0]

def double(xs):
    for x in xs:
        yield x
        yield x

while added < len(ai):
    pick = mins[ : len(ai) - added ]
    comfort += sum(pick)
    mins = list(double(ai[ added : added + len(pick) ]))
    added += len(pick)

print(comfort)

# 7 6 5 4 3 2 1 0

# (0) 7
# (_ + 7 * 1) 7 6
# (_ + 6 * 2) 7 5 6 4
# (_ + 5 * 2 + 4 * 2) 7 3 5 2 6 1 4 0

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#/usr/bin/python

def solve():
    n = int(input())
    a = sorted(map(int, input().split()), reverse=True)
    a2 = [0] * len(a) * 2
    for i in range(len(a2)):
        a2[i] = a[i//2]
        if i >= n:
            break
    a2[0] = 0
    return sum(a2[:n])

print(solve())

Submission status: Accepted

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

    A.sort(reverse=True)

    res=A[0]

    counter=1

    while counter<=N-2:
        index = (counter + 1) // 2
        res+=A[index]
        counter+=1

    print(res)

if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
AS=sorted(A,reverse=True)
B=sorted(AS+AS[1:],reverse=True)
print(sum(B[:n-1]))

Submission status: Accepted

Solution code:
import math
N=int(input())
A=[int(x) for x in input().split()]
ans=0
A=sorted(A)
ans+=A[N-1]
for i in range(1,N-1):
    ans+=A[N-1-math.ceil(i/2)]
print(ans)

Submission status: Accepted

Solution code:
#coding:utf-8
N = int(input())
A = [int(i) for i in input().split()]

A.sort(reverse = True)

k = N-2
index = 1
ans = A[0]

while k > 0:
    if k > 1:
        k -= 2
        ans += A[index] * 2
    elif k == 1:
        k -= 1
        ans += A[index]
    index += 1

print("{}".format(ans))

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
ans=a[0]
n-=2
for i in a[1:]:
  if n == 0:
    break
  if n == 1:
    ans += i
    break
  ans += i*2
  n -= 2
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(N-1):
  ans += A[(i+1)//2]

print(ans)

Submission status: Accepted

Solution code:
import heapq


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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
A=list(map(int,input().split()))
A=sorted(A,reverse=True)
#print(A)
i=1
s=A[0]
while i<n-1:
    i+=1
    s+=A[i//2]
print(s)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from collections import deque

N = int(sys.stdin.readline())
A = [int(i) for i in sys.stdin.readline().split(' ')]

A.sort(reverse=True)
max_value = A.pop(0)
diff = deque([max_value])
ans = 0
for i in A:
    ans = ans + diff.popleft()
    diff.append(i)
    diff.append(i)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=input()
a=[int(i) for i in raw_input().split()]
a.sort()
s=a[n-1]
for i in range(n/2,n-1):
  s+=a[i]*2
if(n%2==1):
  s-=a[n/2]
print s

Submission status: Accepted

Solution code:
def main():
    N=int(input())
    A=sorted([int(_) for _ in input().split()])[::-1]
    ans = A[0]
    if N==2:
        print(ans)
    else:
        for i in range(1, N//2):
            ans += A[i]*2
        if N%2:
            ans += A[N//2]
        print(ans)
main()

Submission status: Accepted

Solution code:
import numpy as np
# def main():
#     N=input()
#     N=int(N)
#     A=input().split(" ")
#     A=np.array([int(i) for i in A]).astype(np.uint64)
#     # A=np.random.randint(0,100000,200000)
#     l=[]
#     A=np.sort(A)
#     A=np.flip(A).astype(np.uint64)
#     sum=0
#     for i,num in enumerate(A):
#         if i%2==0:
#             l=l+[num]
#             if i!=0:
#                 sum+=min(l[-2],l[0])
#         else:
#             l=[num]+l
#             sum+=min(l[1],l[-1])
#     print(int(sum))

# def main2():
#     N=input()
#     N=int(N)
#     A=input().split(" ")
#     A=np.array([int(i) for i in A]).astype(np.uint64)
#     # A=np.random.randint(0,100000,200000)
#     A=np.sort(A)
#     A=np.flip(A).astype(np.uint64)
#     sum=0
#     for i,num in enumerate(A):
#         if i==0:
#             l=A[0]
#             r=A[0]
#             continue
#         sum+=min(l,r)
#         if l<r:
#             r=num
#         else:
#             l=num
#     print(int(sum))

def main3():
    N=input()
    N=int(N)
    A=input().split(" ")
    A=np.array([int(i) for i in A]).astype(np.uint64)
    l=[]
    A=np.sort(A)
    A=np.flip(A).astype(np.uint64)
    sum=0
    for i,num in enumerate(A):
        if i==0:
            l.append(num)
            continue
        l1=np.array(l,dtype=np.uint64)
        l2=np.roll(l1,1)
        l1=l1.reshape(-1,1)
        l2=l2.reshape(-1,1)
        l1=np.concatenate([l1,l2],1)
        l2=l1.min(1)
        arg=l2.argmax()
        l=l[:arg+1]+[num]+l[arg+1:]
        sum+=l2[arg]

    print(int(sum))


def correct():
    N=input()
    N=int(N)
    A=input().split(" ")
    A=np.array([int(i) for i in A]).astype(np.uint64)
    l=[]
    A=np.sort(A).astype(np.uint64)
    sum=0
    for i in range(N-1):
        sum+=A[N-(i+1)//2-1]
    print(int(sum))

if __name__ == "__main__":
    # main3()
    correct()

Submission status: Accepted

Solution code:
import sys

n = int(sys.stdin.readline())
lst = list(map(int, sys.stdin.readline().split()))

lst.sort(reverse = True)

ans = lst[0]
cnt = 0
idx = 1

for _ in range(1, n-1):
    if cnt <= 1:
        ans += lst[idx]
        cnt += 1
    else:
        idx += 1
        cnt = 1
        ans += lst[idx]


print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

A = sorted(A, reverse=True)
print(A[0] + sum(A[1:N//2]) * 2 + A[N//2] * (N%2))

Submission status: Accepted

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

A.sort(reverse = True)

ans = A[0]

idx = 1
cnt = 0
for i in range(2,N):
    ans += A[idx]
    cnt += 1
    if cnt == 2:
        idx += 1
        cnt = 0

print(ans)

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 = list(map(int, input().split()))
a = sorted(a)[::-1]
ans = 2 * sum(a[1:n // 2]) + a[0]
if n % 2 == 1:
    ans += a[n // 2]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
import sys


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


sys.setrecursionlimit(20000000)

MOD = 10 ** 9 + 7
INF = float("inf")


def main():
    N = int(input())
    A = list(map(int, input().split()))
    A.sort(reverse=True)
    answer = 0
    for i in range(1, N):

        answer += A[math.floor(i / 2)]

    print(answer)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N=int(input())
a=list(map(int, input().split()))
 
a=sorted(a)[::-1] 
ans =a[0]

if len(a)%2 !=0:
    for i in range(1,int((len(a))/2)+1):
        for j in range(2):
            ans +=a[i]
    print(ans-a[int((len(a))/2)])
else:
    for i in range(1,(len(a))//2):
        for j in range(2):
            ans +=a[i]
    print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
a.sort(reverse=True)
count=1
ans = a[0]
i = 1
while count < n-1:
    ans += a[i]*2
    count += 2
    i += 1
    #print(count,ans,i,a[i]*2)
else:
    if count != n-1:
        #print(a)
        #print(ans,count, i)
        ans -= a[i-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 = [int(x) for x in input().split()]

list.sort(a,reverse=True)

ans=a[0]

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

print(ans)

Submission status: Accepted

Solution code:
import time
startTimeProblem=time.time()

import fileinput, sys, itertools, functools
from math import *
from bisect import *
from heapq import *
from collections import *

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

class InputHelper:
    def __init__(self):
        self.myinput = fileinput.input()

    def isLocal(self):
        return not fileinput.isstdin()

    def int(self):
        return int(self.myinput.readline().rstrip())

    def ints(self):
        return [int(_) for _ in self.myinput.readline().rstrip().split()]

    def str(self):
        return self.myinput.readline().rstrip()

    def strs(self):
        return [_ for _ in self.myinput.readline().rstrip().split()]

class OutputHelper:
    def int(self, a):
        print(a)    

    def ints(self, a):  
        print(" ".join([str(_) for _ in a]))
    
    def intsNL(self, a):
        for _ in a:
            print(_)
    
    def str(self, s):
        print(s)

    def strs(self, s):
        print(" ".join([_ for _ in s]))

    def strsNL(self, s):
        for st in s:
            print(st)

class ListNode:
    def __init__(self, val):
        self.val = val
        self.next = None
        self.prev = None

In = InputHelper()
Out = OutputHelper()

######################################

N = In.int()
A = In.ints()

A.sort(reverse=True)

l = 0
ans = 0
tick = True
for i in range(len(A)-1):
    ans+=A[l]
    
    if i==0:
        l+=1
    else:        
        if tick:
            tick=not tick
        else:
            l+=1
            tick=not tick

Out.int(ans)
            

######################################

if len(sys.argv)>2 and sys.argv[2]=="TIMEIT":
    fin = (time.time()-startTimeProblem)*1000
    print("{:.2f}".format(fin) + "ms")

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
N = int(input())
x = list(map(int,input().split()))

list.sort(x, reverse=True)
l = len(x)
if N == 1:
    print(0)
elif N == 2:
    print(x[0])
elif N%2==0:
    ans = 0
    for i in range(math.floor(N/2)):
        ans += 2 * x[i]
    print(ans - x[0])
else:
    ans = 0
    for i in range(math.floor(N/2)+1):
        ans += 2 * x[i]
    print(ans - x[0] - x[math.floor(N/2)])

Submission status: Accepted

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

i = 0   # Aを足した回数（最後にはi=N）
ans = A[0]
while 2 * i < N - 2:
    if N - 2 - 2 * i == 1:
        ans += A[i + 1]
    else:
        ans += 2 * A[i + 1]
    i += 1


print(ans)

Submission status: Accepted

Solution code:
N=int(input())

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

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

print(point)

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(2,n):
    ans+=A[i//2]
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:
from collections import deque

def main():
    n = int(input())
    a = [int(x) for x in input().split()]
    ans = 0
    a.sort(reverse=True)
    q = deque([])
    q.append(a[0])
    for i in range(1, n):
        ans += q.popleft()
        # ここを現状最大を2回加えることがポイント（両脇分）
        q.append(a[i])
        q.append(a[i])
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

AN.sort()
ans = 0
for n in range(1,N):
  ans += AN[N-1-(n//2)]
print(ans)

Submission status: Accepted

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

if __name__ == "__main__":
    main()