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())
l = list(map(int,input().split()))
 
l.sort()
l.reverse()
 
ans = 0
ans += l[0]
 
for i in range(1,N-1):
  ans += l[(i+1)//2]
print(ans)

Submission status: Accepted

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

a.sort()
a.reverse()

ans = 0
ans += a[0]

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

def solve():
    N = II()
    A = LI()
    A = deque(sorted(A, reverse=True))

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


if __name__ == '__main__':
    solve()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
limit = 0
ans = 0
count = 0
lim = 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:
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    n=int(readline())
    a=list(map(int,readline().split()))

    a.sort(reverse=True)
    ans=0
    j=1
    cnt=0
    for i in range(n):
        if i==0:
            continue
        elif i==1:
            ans+=a[0]
        else:
            ans+=a[j]
            cnt+=1

        if cnt==2:
            cnt=0
            j+=1

    print(ans)

if __name__=="__main__":
    main()

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

list.sort(reverse=True)

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

Submission status: Accepted

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

Submission status: Accepted

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

# D - Chat in a Circle
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)

i = 0
ans = A[i]
count = 2

while True:	
	if count == N:
		break
	else:
		if count % 2 == 0:
			i += 1
		
		ans += A[i]
		count += 1

print(ans)

Submission status: Accepted

Solution code:
import sys

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

list_input.sort(reverse=True)

ans = 0
for i in range(1, n):  # 1-n-1まで調査
    ans += list_input[i//2]

print(ans)

Submission status: Accepted

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


Ai.sort()

ans=0

for i in range (1,N):
    if i%2==0:
        ans+=Ai[-1]
    else:
        ans+=Ai.pop()
print(ans)

Submission status: Accepted

Solution code:
import queue
N = int(input())
U = input().split()
l = [0 for i in range(N)]
for i in range(N):
    l[i]= int(U[i])

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

print(ans)

Submission status: Accepted

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

A.sort()

K=math.floor(N/2)

S=sum( A[K:N] ) *2

if(N%2==0):
  print(S-A[N-1])
else:
  print(S-A[N-1]-A[K])

Submission status: Accepted

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

    ans = 0
    count = N - 1

    for i in range(len(A)):
        lim = 0

        if i == 0:
            lim = 1
        else:
            lim = 2

        for j in range(lim):
            if count > 0:
                ans += A[i]
                count -= 1

    print(ans)

if __name__ == "__main__":
    main2()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int, input().split(' ')))
l.sort()
s = l.pop()
while n > 3:
	s += l.pop() * 2
	n -= 2
if n == 3:
	s += l.pop()
print(s)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
n = int(input())
que = [x*-1 for x in list(map(int,input().split()))]

que.sort()
ans = 0
que = list(que)
for i in range(1,n):
  ans -= que[i//2]

print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
ans=0
for i in range(n-1):
    ans+=a[n-1-(i+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]
j = 1
for i in range(1, len(a) - 1):
    ans += a[j]
    if i % 2 == 0:
        j += 1

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

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

ans = 0
cnt = 0

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

    if cnt == N-1:
        break
print(ans)

Submission status: Accepted

Solution code:
import heapq
N = 4
ARR = [2, 2, 1, 3]

N = 7
ARR = [1, 1, 1, 1, 1, 1, 1]

N = int(input())

ARR = list(map(int,input().split()))
def calculate(n, arr):
    result = []

    arr = sorted(arr,reverse=True)

    heapq.heappush(result,-1 * arr[0])

    ans = 0
    for i in range(1,n):
        ans -= heapq.heappop(result)

        heapq.heappush(result,-1 * arr[i])
        heapq.heappush(result, -1 * arr[i])

    print(ans)

calculate(N, ARR)

Submission status: Accepted

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


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

    n = int(input())
    a = sorted(list(map(int, input().split())), reverse=True)
    ans = a[0]

    if n % 2 == 0:
        m = (n - 1) // 2
    else:
        m = (n - 2) // 2

    for i in range(m):
        ans += a[i + 1] * 2

    if n % 2 == 1:
        ans += a[m + 1]

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = 4
ARR = [2, 2, 1, 3]

N = 7
ARR = [1, 1, 1, 1, 1, 1, 1]

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

# 3 2 2 1
def calculate(n, arr):
    endIndex = 0
    ans = 0

    arr = sorted(arr, reverse=True)
    for i in range(n):

        if i == 0:
            ans += arr[i]
            endIndex += 1
            continue

        endIndex += 1
        if endIndex >= n:
            break
        ans += arr[i]

        endIndex += 1
        if endIndex >= n:
            break
        ans += arr[i]

    print(ans)


calculate(N, ARR)

Submission status: Accepted

Solution code:
# 貪欲に大きい順に入れていって，入れる場所はもっとも周りが大きくなる場所
# この方針は正しいただし，値の取り出しだけではなく挿入が起こるので，優先度付きキュー（heap）を使う必要があるっ
N = int(input())
A = list(map(int,input().split()))
A.sort(reverse=True)
import heapq
C = []
# heapは最小値しか取り出せないことに注意
heapq.heapify(C)
ans = 0
# 初期条件として2つの値を挿入する
heapq.heappush(C,-A[0])
for i in range(1,N):
    tmp = heapq.heappop(C)
    ans += -tmp
    heapq.heappush(C, -A[i])
    heapq.heappush(C, -A[i])
print(ans)

Submission status: Accepted

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

ans = A[0]

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
a = sorted(a, reverse=True)
if n == 2:
  print(a[0])
  exit()

idx = 1
s = a[0]

for i in range(2,n):
  s += a[idx]
  if i % 2 == 1:
    idx += 1
print(s)

Submission status: Accepted

Solution code:
N=int(input())
A=sorted(map(int,input().split()))[::-1]
from heapq import heappop,heappush
class dictmax():
    def __init__(self):
        self.d={}
        self.h=[]
    def add(self,k,v):
        if k not in self.d:
            heappush(self.h,-k)
            self.d[k]=0
        self.d[k]+=v
        if self.d[k]==0:
            del self.d[k]
    def max(self):
        while -self.h[0] not in self.d:
            heappop(self.h)
        return -self.h[0]#,self.d[-self.h[0]]
d=dictmax()
d.add(A[0],1)
r=0
for a in A[1:]:
    m=d.max()
    r+=m
    d.add(m,-1)
    d.add(a,2)
print(r)

Submission status: Accepted

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

a.sort(reverse=True)

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

Submission status: Accepted

Solution code:
import numpy as np

N,*A = map(int,open(0).read().split())
a = sorted(A,reverse=True)

arr = np.array(a)
bai = arr * 2
ruisekiwa = np.cumsum(bai[1:])
if N == 2:
    print(a[0])
    exit()
if N % 2 == 0:
    print(ruisekiwa[(N-1)//2 - 1] + a[0])
else:
    print(ruisekiwa[(N-1)//2 - 2] + a[(N-1)//2] + a[0])

Submission status: Accepted

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

ans += a[0]

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

if n % 2 != 0:
    ans += a[n//2]

print(ans)

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 x in A[1:]:
  ans += d.popleft()
  d.append(x)
  d.append(x)
print(ans)

Submission status: Accepted

Solution code:
import heapq
N = 4
ARR = [2, 2, 1, 3]


N = 7
ARR = [1, 1, 1, 1, 1, 1, 1]

N = int(input())
ARR = list(map(int,input().split()))
def calculate(n,arr):

    arr = sorted(arr,reverse=True)

    arr = [-1 * ar for ar in arr]


    mmk = []

    ans = 0
    for i in range(n):
        if i == 0:
            heapq.heappush(mmk,arr[i])
            continue

        ans -= heapq.heappop(mmk)

        heapq.heappush(mmk,arr[i])
        heapq.heappush(mmk,arr[i])

    print(ans)




calculate(N, ARR)

Submission status: Accepted

Solution code:
# self.author = Fuad Ashraful Mehmet ,CSE-UAP

from sys import stdin
input=stdin.readline

def HalfDead():
    b=sorted(a,reverse=True)
    ans=0
    for i in range(1,n):
        ans+=b[i//2]
    return ans
    
n=int(input())
a=list(map(int,input().split()))

print(HalfDead())

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
a = int(input())
b = list(map(int,input().split()))
ans = 0
c = 0
list.sort(b, reverse=True)
ans += b.pop(0)
b.extend(b)
list.sort(b, reverse=True)

ans += sum(b[0:a-2])
print(str(ans))

Submission status: Accepted

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

result = 0
spots = [a.pop()]

for i in range(n-1):
	x = a.pop()
	result += spots[i]
	spots.append(x)
	spots.append(x)
	
print(result)

Submission status: Accepted

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

for i in range(1, n):
    score += f[i//2]
print(score)

Submission status: Accepted

Solution code:
input()
l = sorted(map(int, input().split(' ')))

answer = 0
spots = [l.pop()]
x = len(l)

for i in range(x):
	n = l.pop()
	answer += spots[i]
	spots.append(n)
	spots.append(n)

print(answer)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import deque

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

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

a.sort(reverse=True)

d = deque([a[1]]*2)

ans = a[0]
for i in a[2:]:
    d.append(i)
    d.append(i)
    ans += d.popleft()

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

between=deque([a[0]])
score=0
for i in range(1,n):
  score+=between.popleft()
  between.extend([a[i],a[i]])
  
print(score)

Submission status: Accepted

Solution code:
n = int(input())
a = sorted(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:
## necessary imports
import sys
input = sys.stdin.readline
# from math import ceil, floor, factorial;

def ceil(x):
    if x != int(x):
        x = int(x) + 1;
    return x;
    
# swap_array function
def swaparr(arr, a,b):
    temp = arr[a];
    arr[a] = arr[b];
    arr[b] = temp

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

## nCr function efficient using Binomial Cofficient
def nCr(n, k): 
    if(k > n - k): 
        k = n - k 
    res = 1
    for i in range(k): 
        res = res * (n - i) 
        res = res / (i + 1) 
    return int(res) 

## upper bound function code -- such that e in a[:i] e < x;
def upper_bound(a, x, lo=0, hi = None):
    if hi == None:
        hi = len(a);
    while lo < hi:
        mid = (lo+hi)//2
        if a[mid] < x:
            lo = mid+1
        else:
            hi = mid
    return lo

## prime factorization
def primefs(n):
    ## if n == 1    ## calculating primes
    primes = {}
    while(n%2 == 0 and n > 0):
        primes[2] = primes.get(2, 0) + 1
        n = n//2
    for i in range(3, int(n**0.5)+2, 2):
        while(n%i == 0 and n > 0):
            primes[i] = primes.get(i, 0) + 1
            n = n//i
    if n > 2:
        primes[n] = primes.get(n, 0) + 1
    ## prime factoriazation of n is stored in dictionary
    ## primes and can be accesed. O(sqrt n)
    return primes

## MODULAR EXPONENTIATION FUNCTION
def power(x, y, p): 
    res = 1
    x = x % p  
    if (x == 0) : 
        return 0
    while (y > 0) : 
        if ((y & 1) == 1) : 
            res = (res * x) % p 
        y = y >> 1      
        x = (x * x) % p 
    return res 

## DISJOINT SET UNINON FUNCTIONS
def swap(a,b):
    temp = a
    a = b
    b = temp
    return a,b

# find function with path compression included (recursive)
# def find(x, link):
#     if link[x] == x:
#         return x
#     link[x] = find(link[x], link);
#     return link[x];

# find function with path compression (ITERATIVE)
def find(x, link):
    p = x;
    while( p != link[p]):
        p = link[p];
    
    while( x != p):
        nex = link[x];
        link[x] = p;
        x = nex;
    return p;


# the union function which makes union(x,y)
# of two nodes x and y
def union(x, y, link, size):
    x = find(x, link)
    y = find(y, link)
    if size[x] < size[y]:
        x,y = swap(x,y)
    if x != y:
        size[x] += size[y]
        link[y] = x

## returns an array of boolean if primes or not USING SIEVE OF ERATOSTHANES
def sieve(n): 
    prime = [True for i in range(n+1)] 
    p = 2
    while (p * p <= n): 
        if (prime[p] == True): 
            for i in range(p * p, n+1, p):
                prime[i] = False
        p += 1
    return prime

#### PRIME FACTORIZATION IN O(log n) using Sieve ####
MAXN = int(1e5 + 5)
def spf_sieve():
    spf[1] = 1;
    for i in range(2, MAXN):
        spf[i] = i;
    for i in range(4, MAXN, 2):
        spf[i] = 2;
    for i in range(3, ceil(MAXN ** 0.5), 2):
        if spf[i] == i:
            for j in range(i*i, MAXN, i):
                if spf[j] == j:
                    spf[j] = i;
    ## function for storing smallest prime factors (spf) in the array

################## un-comment below 2 lines when using factorization #################
# spf = [0 for i in range(MAXN)]
# spf_sieve();
def factoriazation(x):
    ret = {};
    while x != 1:
        ret[spf[x]] = ret.get(spf[x], 0) + 1;
        x = x//spf[x]
    return ret
    ## this function is useful for multiple queries only, o/w use
    ## primefs function above. complexity O(log n)

## taking integer array input
def int_array():
    return list(map(int, input().strip().split()))
## taking string array input
def str_array():
    return input().strip().split();

#defining a couple constants
MOD = int(1e9)+7;
CMOD = 998244353;
INF = float('inf'); NINF = -float('inf');

################### ---------------- TEMPLATE ENDS HERE ---------------- ###################

# h, w, k = int_array(); a = [];
# for _ in range(h):
#     a.append(list(input().strip()));

n = int(input()); a = sorted(int_array(), reverse = 1);
ans = a[0]; i = 1; n -= 2;
while(n):
    if n == 1:
        ans += a[i];
        n -= 1; continue;
    ans += 2 * a[i];
    i += 1; n -= 2;
print(ans);

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

def calculate(n, arr):
    arr = sorted(arr, reverse=True)
    ans = 0

    ans += arr[0]
    endIndex = 1
    for i in range(1, n - 1):

        if endIndex < (n-1):
            endIndex += 1
            ans += arr[i]
        else:
            break

        if endIndex < (n-1):
            endIndex += 1
            ans += arr[i]
        else:
            break

    print(ans)


calculate(N, ARR)

Submission status: Accepted

Solution code:
conf = 0
num_char = int(input())
char = list(map(int, input().split()))
char.sort(reverse=True)
count = 0
conf_int = 0
for i in range(num_char):
  if count == 0:
    count = 1
  elif count == 1:
    count = 2
    conf = char[conf_int]
    count_int = 0
    conf_int = 1
  else:
    if count_int == 0 :
      conf += char[conf_int]
      count_int = 1
    else:
      conf += char[conf_int]
      conf_int += 1
      count_int = 0
print(conf)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    N = int(readline())
    A = [int(i) for i in readline().split()]
    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:
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n=ni()
a=sorted(nl())[::-1]
 
ans=0

cur=0
now=1
for i in range(n-1):
  ans+=a[cur]
  if now==1:
    now=0
    cur+=1
  elif now==0:
    now=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]) #人1がすでに到着しているとしたとき、得られる値はa0に等しい
for i in range(1,n):
 tmp=heapq.heappop(q) #Pythonの優先度付きキューは最小値を取り出すので-を掛けて最大値を取り出せるようにしている
 tmp*=-1
 ans+=tmp #各時点でのmin(ai,aj)の最大値を答えに足し合わせる
 heapq.heappush(q,-arr[i]) #新たに割り込める場所として追加された人iと人kの間・人kと人jの間で得られる値akを優先度付きキューに追加する
 heapq.heappush(q,-arr[i])
print(ans)

Submission status: Accepted

Solution code:
import sys
from collections import deque
 
read = sys.stdin.buffer.read
 
N, *A = map(int, read().split())
A.sort(reverse=True)
 
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()))
b = sorted(a,reverse=True)

ans = 0

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

Submission status: Accepted

Solution code:

def resolver(N, A):
    A.sort(reverse=True)
    ans = 0
    for i, _ in enumerate(A):
        if i == 0:
            continue
        elif i == 1:
            ans += A[0]
        else:
            j = (i // 2) 
            ans += A[j]
    
    return ans



def _resolver(N, A):
    A.sort(reverse=True)
    circle = []
    ans = 0

    for a in A:
        if len(circle) == 0:
            circle.append(a)
        elif len(circle) == 1:
            ans += circle[0]
            circle.append(a)
        else:
            idx = max(range(len(circle)), key=lambda i: min(circle[i-1], circle[i]))
            ans += min(circle[idx-1], circle[idx])
            circle.insert(idx, a)

    return ans

def main():

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
AA = []
for i in range(N):
    if i == 0:
        AA.append(A[i])
    else:
        AA.append(A[i])
        AA.append(A[i])
AA.sort(reverse=True)
ans = 0
for i in range(N-1):
    ans += AA[i]
print(ans)

Submission status: Accepted

Solution code:
import heapq

N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
h = [-A[0]]
ans = 0
for i in range(1,N):
    ans -= heapq.heappop(h)
    heapq.heappush(h, -A[i])
    heapq.heappush(h, -A[i])
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()));
a.sort()
com = 0;
for k in range(n-1):
    kk = n - (k+1) // 2 - 1
    com += a[kk]
print(com);

Submission status: Accepted

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

import heapq
a.sort(reverse=True)
q = []
ans = 0
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)

cnt = 1
ans = a[0]

if n == 2:
    print(ans)
    exit()

for i in range(1, n):
    cnt += 1
    ans += a[i]
    if cnt == n - 1:
        break
    else:
        cnt += 1
        ans += a[i]
    if cnt == n - 1:
        break

print(ans)

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)
    c = a[0]
    n -= 2
    for x in a[1:]:
        if n == 0:
            break
        if n == 1:
            c += x
            n -= 1
        else:
            c += x * 2
            n -= 2
    print(c)
    return

solve()

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 = int(input())
alist = list(map(int, input().split()))
alist.sort(reverse=True)
sumscore=alist[0]
for n in range(1,int(N/2)):
  sumscore = sumscore + alist[n]*2
if N%2==1:
  sumscore += alist[n+1]

print(sumscore)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n=int(input())
alist=list(map(int,input().split()))
blist=sorted(alist,reverse=True)
total=blist[0]
for i in range(1,int(n/2)):
  total+=blist[i]*2
if n%2:
  total+=blist[int(n/2)]
print(total)

Submission status: Accepted

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

A.sort(reverse=True)

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
A.sort(reverse=True)
C = A[0]
j=0
for i in range(1,N-1):
  if i%2 == 0:
    C = C+A[j]
  elif i%2 != 0:
    j=j+1
    C = C+A[j]
print(C)

Submission status: Accepted

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

Submission status: Accepted

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

A.sort(reverse=True)

ans=A[0]

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

if N%2 != 0:
    ans += A[N//2]
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
num=int(input())
s=[int(i) for i in input().split()]
s.sort(reverse=True)
array=[]
for i in range(num-1):
    if i==0:
        array.append(s[i])
    else:
        array.append(s[i])
        array.append(s[i])
point=0
for i in range(num-1):
    if i==0:
        point+=array[i]
    else:
        point+=array[i]
print(point)

Submission status: Accepted

Solution code:
num=int(input())
s=[int(i) for i in input().split()]
s.sort(reverse=True)
array=[]
for (i,j) in enumerate(s):
    if i==0:
        array.append(j)
    else:
        array.append(j)
        array.append(j)
point=0
for i in range(num-1):
    if i==0:
        point+=array[i]
    else:
        point+=array[i]
print(point)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin python3
# -*- coding: utf-8 -*-

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

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import heapq


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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def INT():
    return int(input())

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

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

N = INT()
A = LI()
A = A + sorted(A, reverse = True)[1 : ]
A.sort(reverse = True)
ans = 0

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

Submission status: Accepted

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

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math

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

a_lst = sorted(a_lst, reverse=-1)
ans = 0
for i in range(1, n):
  ans += a_lst[math.floor(i / 2)]
print(ans)

Submission status: Accepted

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