Problem:


Score: 400 points

Problem Statement

Quickly after finishing the tutorial of the online game ATChat, you have decided to visit a particular place with N-1 players who happen to be there. These N players, including you, are numbered 1 through N, and the friendliness of Player i is A_i.

The N players will arrive at the place one by one in some order. To make sure nobody gets lost, you have set the following rule: players who have already arrived there should form a circle, and a player who has just arrived there should cut into the circle somewhere.

When each player, except the first one to arrive, arrives at the place, the player gets comfort equal to the smaller of the friendliness of the clockwise adjacent player and that of the counter-clockwise adjacent player. The first player to arrive there gets the comfort of 0.

What is the maximum total comfort the N players can get by optimally choosing the order of arrivals and the positions in the circle to cut into?

Constraints

All values in input are integers.

2 \leq N \leq 2 \times 10^5

1 \leq A_i \leq 10^9

Input

Input is given from Standard Input in the following format:

N
A_1 A_2 \dots A_N

Output

Print the maximum total comfort the N players can get.

Sample Input 1

4
2 2 1 3

Sample Output 1

7

By arriving at the place in the order Player 4, 2, 1, 3, and cutting into the circle as shown in the figure, they can get the total comfort of 7.

They cannot get the total comfort greater than 7, so the answer is 7.

Sample Input 2

7
1 1 1 1 1 1 1

Sample Output 2

6



Sample input:
4
2 2 1 3


Sample output:
7

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
a.sort()
cnt=0
ans=-1*a[-1]
c = 1
while cnt!=n:
    cnt+=1
    if c==1:
        k=a.pop()
    ans+=k
    c*=-1
 
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

A.sort(reverse=True)

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

Submission status: Accepted

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

A = sorted(A, reverse=True)

result = A[0]
cnt =1
position = 1
flag = 0
while cnt<N-1:
    result += A[position]
    cnt += 1
    if cnt == N-1:
        break
    result += A[position]
    cnt +=1
    position += 1

print(result)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# D

n = int(input())
a_l = [int(x) for x in input().split()]

a_l.sort(reverse=True)
ans = -a_l[0]
now = a_l[0]
count = 1
for i in range(n):
    if i % 2 == 0:
        ans += now
    else:
        ans += now
        now = a_l[count]
        count += 1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
if n==2:
    exit(print(max(a)))
b=[a[0],a[1]]
ans=a[0]
a=a[1:]
from math import floor
for i in range(n-2):
    num=floor(i/2)
    ans+=a[num]
print(ans)

Submission status: Accepted

Solution code:
import heapq

class ErasableHeapq:
    def __init__(self):
        self.p = list()
        self.q = list()
        self.len = 0

    def insert(self, x):
        heapq.heappush(self.p, x)
        self.len += 1
        return

    def erase(self, x):
        heapq.heappush(self.q, x)
        self.len -= 1
        return

    def minimum(self):
        while self.q and self.p[0] == self.q[0]:
            heapq.heappop(self.p)
            heapq.heappop(self.q)
        return self.p[0]

    def __len__(self):
        return self.len

    def empty(self):
        return self.len == 0

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

    # 大きい順
    A.sort()
    sA = A[::-1]

    ans = sA[0]

    eq = ErasableHeapq()
    eq.insert((-sA[1], -sA[0]))
    eq.insert((-sA[1], -sA[0]))

    for a in sA[2:]:
        best_pair = eq.minimum()
        eq.erase(best_pair)
        # print(a, best_pair)
        ans += -best_pair[0]
        eq.insert((-a, best_pair[0]))
        eq.insert((-a, best_pair[1]))

    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)
if n==2:
    print(max(a))
    exit(0)

ans=a[0]
a=a[1:]
from math import floor

for i in range(n-2):
    index=floor(i/2)
    ans+=a[index]

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l = list(map(int,input().split()))
l_sort = sorted(l,reverse=True)
l_len = len(l)
if(l_len%2==0):
    ans = sum(l_sort[:(l_len//2)-1])+sum(l_sort[1:(l_len//2)])+l_sort[(l_len//2)-1]
else:
    ans = sum(l_sort[:(l_len//2)])+sum(l_sort[1:(l_len//2)+1])

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int,input().split()))
a.sort()
cnt=0
ans=-1*a[-1]
c = 1
while cnt!=n:
    cnt+=1
    if c==1:
        k=a.pop()
    ans+=k
    c*=-1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

sum=l[-1]
for i in range(1,n//2):
  sum+=2*l[-1-i]
if n%2!=0:
  sum+=l[n//2]
print(sum)

Submission status: Accepted

Solution code:
import numpy as np

N = int(input())
a = list(map(int,input().split()))
A = np.sort(np.array(a+a))

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

Submission status: Accepted

Solution code:
import numpy as np

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

Submission status: Accepted

Solution code:
n = int(input())
a = sorted(list(map(int, input().split())),reverse=True)
i = 0
cnt = 1
ans = a[0]
a.pop(0)
while i < n-1 or cnt < n - 1:
    for j in range(2):
        if cnt == n - 1:
            break
        cnt += 1
        ans += a[i]
        #print(cnt,ans)
    i += 1
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

A.sort()
A.reverse()

ans = 0

if len(A) == 2:
  print(A[0])
  exit()
count = 0
for i in range(len(A)):
  if i == 0:
    ans += A[i]
    count += 1
  else:
    ans += A[i]
    count += 1
    if count == len(A)-1:
      break
    count += 1
    ans += A[i]
    if count == len(A)-1:
      break

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**6)

n = int(input())
A = list(map(int, input().split()))
A = [-a for a in A]

A.sort()

from heapq import *

ans = -A[0]

q = A[:2][:]
heapify(q)
q[0] = q[-1]

for a in A[2:]:
    ans += -q[0]
    t1 = heappop(q)
    t1 = max(t1, a)
    heappush(q, t1)
    heappush(q, a)
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
import sys

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

ali.sort(reverse=True)

comforts = deque([ali[1], ali[1]])
answer = ali[0]
if len(ali) < 3:
    print(answer)
    sys.exit()

for i in ali[2:]:
    answer += comforts.pop()
    comforts.appendleft(i)
    comforts.appendleft(i)

print(answer)

Submission status: Accepted

Solution code:
import sys
import os
import itertools
import numpy as np
from collections import deque

def main():
    if os.getenv("ENV")=='LOCAL':
        sys.stdin = open(os.path.dirname(__file__)+'/input.txt', 'r')

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

    A.sort(reverse=True)
    # print(A)
    # s=sum(A)-min(A)
    A_set=list(set(A))
    A_set.sort(reverse=True)
    set_p=0
    # print(A_set)

    ans=0
    # print(A_set)
    # print([0]*len(A_set))
    arc={k: 0 for k in A_set}
    # print(type(arc))
    # print(A[0])
    arc[A_set[0]]=arc[A_set[0]]+1
    max=A_set[0]    #取得できる最大の心地よさ
    for a in A[1:]:
        # print(arc)
        arc[a]+=2

        ans+=max
        arc[max]-=1
        if arc[max]==0:
            set_p+=1
            max=A_set[set_p]


    print(ans)
    # comfort

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort()
Sum = 0
for i in range(len(A)):
    if i == 0:
        pass
    elif i == 1:
        Sum += A.pop()
    else:
        if i % 2 == 0:
            temp = A.pop()
            Sum += temp
        else:
            Sum += temp
print(Sum)

Submission status: Accepted

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

import math

ans = 0
for i in range(n):
    if i == 0:
        pass
    elif i == 1:
        ans += a[0]
    else:
        ans += a[math.floor(i/2)]

print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
l = len(A) - 1
A.sort(reverse=True)
counter = A[0]
num = 1
while l > 2:
    counter += A[num] * 2
    num += 1
    l -= 2
if l == 2:
    counter += A[num]
print(counter)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
ans += a_list.pop(0)
cnt = 2
times_2 = 0
times_1 = 0
while 1:
  if cnt == n:
    break
  if n - cnt >= 2:
    cnt += 2
    times_2 += 1
  else:
    cnt += 1
    times_1 += 1
      
ind = 0
for i in range(times_2):
  ans += 2 * a_list[i]
  ind += 1
  
for i in range(times_1):
  ans += a_list[ind + i]

print(ans)

Submission status: Accepted

Solution code:
N=int(input())
ls=[int(s) for s in input().split()]
ls=sorted(ls,reverse=True)
ans=ls[0]
for i in range(2,N):
  ans+=ls[i//2]
print(ans)

Submission status: Accepted

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

a.sort(reverse=True)

score=0
index=1
count=0
for i in range(len(a)):
  if i==0:
    score+=0
  elif i==1:
    score+=a[0]
  else:
    score+=a[index]
    count+=1
    if count==2:
      index+=1
      count=0
print(score)

Submission status: Accepted

Solution code:
N = int(input())
A = sorted(map(int, input().split(" ")), reverse=True)
result = A[0]
current_comfortest = A[1]
for i in range(2, N):
    if i == 2:
        result += current_comfortest
        continue
    else:
        result += A[i // 2]
        continue
print(result)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import heapq

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

comfort = A[0]
edges = [-A[1], -A[1]]

for ai in A[2:]:
    m = heapq.heappop(edges) * -1
    comfort += m
    heapq.heappush(edges, -ai)
    heapq.heappush(edges, -ai)
print(comfort)

Submission status: Accepted

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

lst = sorted(lst, reverse=True)

print(sum(lst[0:n//2]) + sum(lst[1:(n - n//2)]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
if n==2:
    print(a[-1])
else:
    su=a[-1]
    d=1
    for i in range(n-2,-1,-1):
        su+=2*a[i]
        d+=2
        if d==n-1:
            break
        elif d>n-1:
            su-=a[i]
            break
    print(su)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
edge = {}
edge[0, 1] = min(A[0], A[1])
edge[1, 0] = min(A[0], A[1])
ans = A[0]
max_edge_list = [(0, 1), (1, 0)]
idx = 0
for i in range(2, N):
    x, y = max_edge_list[idx]
    ans += edge[(x, y)]
    #del edge[(x, y)]
    edge[x, i] = A[i]
    edge[i, y] = A[i]
    max_edge_list.append((x, i))
    max_edge_list.append((i, y))
    #max_edge_list.pop(0)
    idx += 1
print(ans)

Submission status: Accepted

Solution code:
n = int(input())
A = sorted(list(map(int, input().split())), reverse=True)
a = A.pop()
Res = [a]

ans = 0
flg = True
pointer = 0
for i in range(n-1):
    ans += A[pointer]
    if flg == True:
        pointer += 1
        flg = False
    else:
        flg = True
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
len_a = len(a)
i = len_a // 2
a.sort()
a.reverse()
if len_a % 2 == 0:
    result = a[0] + 2*(sum(a[1:i]))
else:
    result = a[0] + 2*(sum(a[1:i]))+a[i]

print(result)

Submission status: Accepted

Solution code:
from collections import deque

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


A.sort(reverse=True)

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

    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

def main(n,a):
    #print(a)
    a.sort(reverse=True)
    #print(a)
    sum=a[0]
    j=0
    k=1
    for i in range(2,n):
        sum+=a[k]
        #print(a[k])
        j+=1
        if j==2:
            j=0
            k+=1
    return sum

if __name__=='__main__':
    n,a=readinput()
    ans=main(n,a)
    print(ans)

Submission status: Accepted

Solution code:
import math

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


num = math.ceil(N/2)

a = Asrt[:num]

if N % 2 == 0:#guusuu
    print(sum(a) * 2 - a[0])
else:#kisuu
    print(sum(a) * 2 - a[0] - a[-1])

Submission status: Accepted

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

sum = A[0]
index = 1

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

print(sum)

Submission status: Accepted

Solution code:
n = int(input())
li = list(map(int,input().split()))
li.sort(reverse=True)
ans = [li[0]] + li[1:]*2
ans.sort(reverse=True)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
B = sorted(A)[::-1]

idx0 = 0
idx1 = N // 2

idx2 = 1
idx3 = (N + 1) // 2

print(sum(B[idx0:idx1]) + sum(B[idx2:idx3]))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N = int(input())

s = input().split()
A = []
for i in range(N):
  A.append(int(s[i]))
  
result = 0
A.sort(reverse=True)
result += A[0]
A.pop(0)
if N % 2 == 0:
  result += sum(A[:(int(N/2)-1)])*2
else:
  result += (sum(A[:(int((N-1)/2)-1)])*2 + A[int((N-1)/2)-1])
    
print(result)

Submission status: Accepted

Solution code:
# D - Chat in a Circle

N = int(input())
A = list(int(a) for a in input().split())

A.sort(reverse=True)

n = N-1
ans = A[0]
n -= 1
i = 1
while n > 0:
    if n == 1:
        ans += A[i]
        n -= 1
    else:
        ans += A[i] * 2
        n -= 2
    i += 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(1, N):
    index = i // 2
    ans += A[index]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
players = list(map(int, input().strip().split(" ")))

players.sort(reverse=True)
total = 0
for i in range(1,N):
    j = int(i/2)
    total += players[j]
print(total)

Submission status: Accepted

Solution code:
import sys
import numpy as np

readline = sys.stdin.readline
read = sys.stdin.read

n = int(input())
a = np.array(readline().split(), dtype=np.int64)
a.sort()
a = a[::-1]
m = -(-n//2)
ans = np.sum(a[:m])*2 - a[0] if n % 2 == 0 else np.sum(a[:m])*2 - a[0] - a[m-1]
print(ans)

Submission status: Accepted

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

#print(A)

wait = deque()
wait.append(A[0])
ans = 0
for a in A[1:]:
    #print(wait)
    ans += wait.popleft()
    wait.extend([a, a])
print(ans)

Submission status: Accepted

Solution code:
from collections import deque
n=int(input())
a=list(map(int,input().split()))
a.sort()
a=a[::-1]
ans=0
x=deque([a[0]])
nn=n//2
for i in range(1,nn+1):
  ans+=min(x[0],x[-1])
  x.append(a[i])
  x.appendleft(a[i])
x=ans-a[0]
if n%2==0:
  ans+=x
  print(ans)
else:
  ans+=x+a[nn]
  print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    import sys
    N=int(sys.stdin.readline())
    A=list(map(int,sys.stdin.readline().split()))
    if N==2:
        print(A[0])
        return

    
    A.sort(reverse=True)
    score=A[0]
    count=1
    
    
    for a in A[1:]:
        score+=a
        count+=1
        if count==N-1:
            break

        score+=a
        count+=1
        if count==N-1:
            break
    
    print(score)

main()

Submission status: Accepted

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

a.sort()
a.reverse()

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

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

Submission status: Accepted

Solution code:
n = int(input())

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

A.sort(reverse = True)

tot = A[0]
cnt = 2
j = 0
for i in range(1,n):
    if n >= cnt+2:
        tot +=A[i]*2
        cnt += 2
        j = i
        
if n >= cnt + 1:
    tot += A[j+1]
    cnt += 1
        
print(tot)

Submission status: Accepted

Solution code:
n = int(input())
s = [int(i) for i in input().split()]
s.sort(reverse=True)
ans = s[0]

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import heapq

N = int(input())
A = sorted(list(map(int, input().split())),reverse=True)
B = [0]
heapq.heapify(B)
conf = 0
ans = 0
for i, a in enumerate(A):
    conf = heapq.heappop(B)
    if i == 0:
        heapq.heappush(B, -a)
    else:
        for _ in range(2):
            heapq.heappush(B, -a)
    ans += (-1) * conf
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import heapq as hq

n = int(input())
a = list(map(lambda x: 0 - int(x), input().split()))
a.sort()

rst = 0
circle = []
hq.heappush(circle, a[0])
for i in a[1:]:
    k = hq.heappop(circle)
    rst += k
    if k < i:
        hq.heappush(circle, i)
        hq.heappush(circle, i)
    else:
        # k == i
        hq.heappush(circle, i)
        hq.heappush(circle, k)

print(-rst)

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
result = a[0]
num = 2
temp = 0
for i in range(1, n):
    temp = i
    num += 2 
    result += a[i] * 2
    if(num >= n):
        break
print(result - a[temp] * (num- n))

Submission status: Accepted

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

A.sort(reverse=True)
ans = 0
if N >= 2:
    ans += A[0]

if N >= 3:
    idx = 1
    for i in range(N-2):
        ans += A[idx]
        if i % 2 == 1:
            idx += 1

print(ans)

Submission status: Accepted

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

A.sort(reverse=True)

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

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

if N%2 == 1:
    A_total += A[i+1]

print(A_total)

Submission status: Accepted

Solution code:
from sys import stdin
import sys
import itertools
import array
import textwrap
import math

N = int(input())
A = list(map(int, input().split()))
A.sort()
ans = A[-1]
c = len(A)-2
temp = len(A)-2
d = 0
for i in range(c):
    ans += A[temp]
    d += 1
    if d == 2:
        d = 0
        temp = temp-1
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
from collections import deque
a=list(map(int,input().split()))
a.sort()
a.reverse()
c=0
d=0
e=0
b=deque(a)
c+=b.popleft()
f=(n-2)//2
for i in range(f):
  e=b.popleft()
  c+=2*e
if n%2==1:
  e=b.popleft()
  c+=e
print(c)

Submission status: Accepted

Solution code:
n = int(input())
l =list(map(int, input().split())) 
l.sort()
list_a = []
list_m =[0]*n*2
for i in l[::-1]:
  list_a.append(i)
count =list_a[0]
for i in range(0,n):
  list_m[2*(i-1)] = list_a[i]
  list_m[2*(i-1)+1] = list_a[i]
  
for i in range(n-2):
  count+=list_m[i]

print(count)

Submission status: Accepted

Solution code:
from heapq import heapify, heappop, heappush


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

A.sort(reverse=True)

heap = []
heapify(heap)

val = 0
heappush(heap, -A[1])
val += A[0]

for i, a in enumerate(A[2:]):
    n = heappop(heap)
    val += -n

    if i == 0:
        heappush(heap, n)

    heappush(heap, -a)
    heappush(heap, -a)


print(val)

Submission status: Accepted

Solution code:
import sys

n = int(input().strip())
a = [int(x) for x in input().strip().split(' ')]
a = sorted(a, reverse=True)

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

sum = a[0] + a[1]
for i in range(n-3):
    if i % 2 == 0:
        sum += a[int(i/2+1)]
    else:
        sum += a[int(i/2+2)]

print(sum)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

out = 0
A.sort(reverse=True)
for i in range(n):
    if (i + 1) * 2 <= n:
        out += A[i] * 2
    elif (i + 1) * 2 - 1 <= n:
        out += A[i]

print(out - A[0])

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
a=sorted(a,reverse=True)
space=[a[0]]
t=0
coco=0
for i in range(1,n):
    coco += space[t]
    t += 1
    space.append(a[i])
    space.append(a[i])
print(coco)

Submission status: Accepted

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

A.sort(reverse = True)

ans = A[0]

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

print(ans)

Submission status: Accepted

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

ans = 0
ans += A[-1]
if N % 2 == 0:
  ans += sum(2*A[N//2:-1])
  # ans += sum(2*A[-(-N//2):-1])
else:
  # import pdb;pdb.set_trace()
  ans += A[N//2] + sum(2*A[-(-N//2):-1])
print(ans)

Submission status: Accepted

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


A.sort(reverse=True)
confort = A[0]

if (n % 2 == 0):
    for i in range(1, int(n/2)):
        confort += 2*A[i]
else:
    for i in range(1, int((n+1)/2)):
        confort +=2*A[i]
    confort -= A[int((n+1)/2-1)]


print(confort)

Submission status: Accepted

Solution code:
import sys

def resolve():
    inp = sys.stdin.readlines()
    inp = list(map(lambda x: x.strip(), inp))
    N = int(inp[0])
    A = list(map(int, inp[1].split(' ')))
    A.sort(reverse=True)
    num = 0
    for i in range(N-1):
        num += A[(i+1)//2]

    print(num)
    
resolve()

Submission status: Accepted

Solution code:
import math
import collections
import bisect
from collections import deque
from copy import copy, deepcopy


def main():
    N = int(input())
    A = list(map(int, input().split()))
    sortedA = list(reversed(sorted(A)))
    ans = 0
    ans += sortedA[0]
    deq = deque()
    deq.append([sortedA[0], sortedA[1]])
    deq.append([sortedA[1], sortedA[0]])

    for i in range(2, N):
        tmp = deq.popleft()
        ans += min(tmp)
        deq.append([tmp[0], sortedA[i]])
        deq.append([sortedA[i], tmp[1]])

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

A.sort()
from collections import deque

pot = deque([A.pop()])
com = 0
while A:
    com += pot.pop()
    new = A.pop()
    pot.appendleft(new)
    pot.appendleft(new)

print(com)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# import sys
# sys.setrecursionlimit(10 ** 6)
# import bisect
# from collections import deque
# from decorator import stop_watch
#
#
# @stop_watch
def solve(N, As):
    import math
    As = sorted(As, reverse=True)
    ans = As[0]
    for i in range(2, N):
        ans += As[math.ceil((i + 1) / 2) - 1]
    print(ans)



if __name__ == '__main__':
    # S = input()
    N = int(input())
    # N, M = map(int, input().split())
    As = [int(i) for i in input().split()]
    # Bs = [int(i) for i in input().split()]
    solve(N, As)

Submission status: Accepted

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

Submission status: Accepted

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

a.sort(reverse = True)

ans = 0
ans += a[0]
i = 1
j = 1
while i < n-1 :
    if i+2 <= n-1 :
        ans += 2*a[j]
        j += 1
        i += 2
    else :
        ans += a[j]
        j += 1
        i += 1

print(ans)

Submission status: Accepted

Solution code:
import sys

import math
import itertools
import random
def next(index, lis):
    for i in range(index, 0):
        if (i != 0):
            return i

n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
ans = a[0]
cnt = 1
tmp=1
while (True):
    if (cnt == n-1):
        break
    ans += a[tmp]
    cnt+=1
    if (cnt == n-1):
        break
    ans += a[tmp]
    cnt+=1
    tmp+=1
print(ans)