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:
import heapq
n = int(input())
arr = list(map(int, input().split()))
arr = sorted(arr, reverse=True)
# print(arr)
ans = 0
q = []
# 人１人が既に到着しているとしたとき、得られる値はa0に等しい。
heapq.heappush(q, -arr[0])
# print(q)
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:
import sys
stdin = sys.stdin
sys.setrecursionlimit(10 ** 7)

def LI(): return list(map(int, stdin.readline().split()))
def LS(): return list(stdin.readline())

n = int(input())
a = sorted(LI())
ans = 0
for i in range(1, n):
    ans += a[n-1-(i//2)]
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:
    for i in range(1,int((N/2))):
        ans += A[i]*2
else:
    for i in range(1,int(N/2)):
        ans += A[i]*2
    ans += A[int(N/2)]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())

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

members.sort()
ans = 0
ans += members.pop()
for i in range(0, int((N-2)/2)):
    ans += members.pop()*2
if (N) % 2 == 1:
    ans += members.pop()

print(ans)

Submission status: Accepted

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


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

Submission status: Accepted

Solution code:
n = int(input())
a = list(map(int, input().split()))
aa = a + a
aa.sort()
aa.pop()
answer = 0
for i in range(n - 1):
  answer += aa.pop()
print(answer)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n = int(input())
    friendliness = [int(x) for x in input().split()]
    friendliness.sort(reverse=True)
    gg = (friendliness[i // 2] for i in range(1, n))
    print(sum(gg))


if __name__ == '__main__':
    main()

Submission status: Accepted

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import time
import math
st = time.perf_counter()
def inpl():
    return list(map(int, input().split()))
# ------------------------------

N = int(input())
A = inpl()

A.sort()
A.reverse()

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

print(ans)


# ------------------------------
ed = time.perf_counter()
print('time:', ed-st, file=sys.stderr)

Submission status: Accepted

Solution code:
N,*As=map(int,open(0).read().split());As.sort();print(As[-1]+sum(As[-2-i//2]for i in range(N-2)))

Submission status: Accepted

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

A.sort()
A.reverse()
ans = 0
ans += A[0]

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

ans += tmp
print(ans)

Submission status: Accepted

Solution code:
_MULTITEST = False
##
def solve():
    n = int(input())
    a = list(map(int, input().split()))

    a.sort()
    print(sum(
        a[n - (k//2) - 1] for k in range(1, n)
    ))

##
if __name__ == "__main__":
    t = (int(input()) if _MULTITEST else 1)
    for tt in range(t):
        solve()

Submission status: Accepted

Solution code:
import sys
import numpy as np

readline = sys.stdin.readline 

N = int(input())
A = np.array(readline().split(), np.int64)
A_sort = np.sort(A)[::-1] # 降順ソート

count = 0
limit = 1
t = 0
for i in range(N - 1):
    if i == 0:
        count += A_sort[0]
    else:
        count += A_sort[limit]
        t += 1
        if t % 2 == 0:
            limit += 1
        

print(count)

Submission status: Accepted

Solution code:
n = int(input())
a = sorted([int(i) for i in input().split()], reverse=True)
ans = 0
t = n - 1 # 何回心地よさを足すことができるか
for i, v in enumerate(a):
  lim = 1 if i == 0 else 2 # 初回だけ1回足せる、それ以外は2回
  for j in range(lim):
    if t > 0:
      ans += a[i]
      t -= 1
print(ans)

Submission status: Accepted

Solution code:
"""
Atcoder Beginner Contest 173
D - Chat in a circle

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

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

print(ans)

Submission status: Accepted

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

ans = a[0]
cnt = 1
num = 0
for _ in range(n - 2):
    ans += a[cnt]
    if num == 0:
        num += 1
    else:
        num = 0
        cnt += 1
print(ans)

Submission status: Accepted

Solution code:
N, *A = map(int, open(0).read().split())
A.sort()
A = [0] + A
print(sum(A[N - (k // 2)] for k in range(1, N)))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
n=int(input())
a=sorted(list(map(int,input().split())),reverse=True)
if n==2:
	print(a[0])
	exit(0)
ans=a[0]
d=deque()
d.append(a[1])
d.append(a[1])
for i in range(2,n):
	x=a[i]
	ans+=d.popleft()
	d.append(x)
	d.append(x)
print(ans)

Submission status: Accepted

Solution code:
import sys
INF = 1 << 60
MOD = 10**9 + 7 # 998244353
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
def resolve():
    n = int(input())
    A = list(map(int, input().split()))
    A += A
    A.sort()
    A.pop()
    ans = 0
    for _ in range(n - 1):
        ans += A.pop()
    print(ans)
resolve()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
heap = []
heapq.heappush(heap, -a[0])
for i in range(1, n):
    tmp = heapq.heappop(heap)
    tmp *= -1
    ans += tmp
    heapq.heappush(heap, -a[i])
    heapq.heappush(heap, -a[i])
print(ans)

Submission status: Accepted

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

A.sort()

ans = 0
can = 1
will = n-1
for i in range(n-1):
    ans += A[will]
    can -= 1
    if can == 0:
        can = 2
        will -= 1
print(ans)

Submission status: Accepted

Solution code:
n,*a = map(int,open(0).read().split())
a.sort(reverse=True)
print(a[0]+sum(a[1:n//2])+sum(a[1:n-n//2]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
ii = lambda : int(input())
mi = lambda : map(int,input().split())
li = lambda : list(map(int,input().split()))

from collections import deque

n = ii()
a = li()

a.sort(reverse = True)

l = deque()
l.append(a[1])
l.append(a[1])

ans = a[0]

if n != 2:
    for i in range(2,n):
        ans += l.popleft()
        l.append(a[i])
        l.append(a[i])

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque
n=int(input())
A=list(map(int,input().split()))
A.sort()
A.reverse()
conf=[A[0]]
ans=0
con=deque(conf)
A.pop(0)


for elem in A:
    ans+=con.popleft()
    
    for _ in range(2):
        con.append(elem)

print(ans)

Submission status: Accepted

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

A.sort()
res = 0

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

print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import heapq


def get_tuple(a, b):
    if a > b:
        a, b = b, a
    return -a, b


def main(a, N):
    a.sort(reverse=True)
    h = []
    if N == 1:
        comfort = 0
    else:
        comfort = a[0]
        heapq.heappush(h, get_tuple(a[0], a[1]))
        heapq.heappush(h, get_tuple(a[0], a[1]))
        for i in range(2, N):
            val = heapq.heappop(h)
            comfort -= val[0]
            heapq.heappush(h, get_tuple(a[i], -val[0]))
            heapq.heappush(h, get_tuple(a[i], val[1]))
    print(comfort)


if __name__ == '__main__':
    N = int(input())
    a = list(map(int, input().split()))
    main(a, N)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=1)
ans=a[0]
i=0
t=1
while t<n-1:
  i+=1
  for _ in range(2):
    t+=1
    ans+=a[i]
    if t>=n-1:break
print(ans)

Submission status: Accepted

Solution code:
N=int(input())
A=sorted([int(x) for x in input().split()],reverse=True)
print(2*sum(A[0:~-N//2+1])-A[0]-(A[~-N//2] if N%2 else 0))

Submission status: Accepted

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

Submission status: Accepted

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

A.sort(reverse = True)
ans = A[0]
count = 1
flg = False

if N == 2:
    print(ans)
    exit()
    
for i in range(1,N):
    for j in range(2):
        ans += A[i]
        count += 1
        if count >= N-1:
            flg = True
            break
    if flg == True:
            break
        
print(ans)

Submission status: Accepted

Solution code:
import collections

N = int(input())
Mylist = input().split()
Mylist = [int(s) for s in Mylist]
Mylist.sort()
ANS = 0
for i in range(1,N):
    ANS += Mylist[(N-1)-int(i/2)]

print(ANS)

Submission status: Accepted

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

A.sort(reverse = True)
ans = 0

from collections import deque
tmp = deque([A[0]])

for x in A[1:]:
    ans += tmp.popleft()
    tmp.append(x)
    tmp.append(x)
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin


def main():
    input = lambda: stdin.readline()[:-1]
    N = int(input())
    A = list(map(int, input().split()))

    A.sort(reverse=1)
    d, m = divmod(N, 2)
    n = [2] * (d + m)
    n[-1], n[0] = n[-1] - m, 1
    ans = 0
    for i, k in enumerate(n):
        ans += A[i] * k
    print(ans)


main()

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

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

A.sort(reverse=True)
ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
l=sorted(list(map(int,input().split())))
an=l[n-1]
x=l[n-2];cr=n-3;cc=0
for i in range(n-3,-1,-1):
  an+=x
  cc+=1
  if cc!=0 and cc%2==0:
    x=l[cr];cr-=1
  #print(an,x,cr)
print(an)

Submission status: Accepted

Solution code:
import sys
from collections import deque

input = sys.stdin.readline

N = int(input())
A = list(map(int, input().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:
import sys
import math
import collections
import heapq

def set_debug(debug_mode=False):
    if debug_mode:
        fin = open('input.txt', 'r')
        sys.stdin = fin


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


if __name__ == '__main__':
    # set_debug(True)

    # t = int(input())
    t = 1

    for ti in range(1, t + 1):
        n = int(input())

        A = int_input()

        A.sort()

        res = A[-1]
        for i in range(len(A)-2):
            res += A[-(i//2)-2]
        print(res)

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

A.sort()

ans = 0
can = 1 # lastの数が後何回足されうるか
will = n-1 # 次に足されるであろう数のindex
for i in range(n-1):
    ans += A[will]
    can -= 1
    if can == 0:
        # 足されきったら一つ小さい数へ
        can = 2 # 最大値以外は2回足されうる
        will -= 1
print(ans)

Submission status: Accepted

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

#print(A)

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

Submission status: Accepted

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

res = A[0]
num = (n - 1) // 2
for i in range(1, num + 1):
    res += A[i] * 2

if n % 2 == 1:
    res -= A[num]

print(res)

Submission status: Accepted

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

readline = sys.stdin.buffer.readline
def readstr():return readline().rstrip().decode()
def readstrs():return list(readline().decode().split())
def readint():return int(readline())
def readints():return list(map(int,readline().split()))
def printrows(x):print('\n'.join(map(str,x)))
def printline(x):print(' '.join(map(str,x)))

n = readint()
a = readints()

a.sort(reverse=1)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

input = sys.stdin.readline


def S():
    return input().rstrip()


def I():
    return int(input())


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


n = I()
al = list(MI())

al.sort()
al.reverse()

ans = 0
q = deque()
for a in al:
    if q:
        ans += q.popleft()
        q.append(a)
        q.append(a)
    else:
        q.append(a)

print(ans)

Submission status: Accepted

Solution code:
import collections

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

q = collections.deque()
a.sort(reverse=True)

ans = a[0]
q.append(a[1])
q.append(a[1])
for i in range(2,n):
    ans += q.popleft()
    q.append(a[i])
    q.append(a[i])

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import heapq

N = int(input())

arr = [int(x) for x in input().split()]
arr = sorted(arr,reverse = True)

ans = 0
q = []

heapq.heappush(q,-arr[0])

#print(q)

for i in range(1,N):
    tmp = heapq.heappop(q)
    #print(tmp)
    tmp *= -1
    ans += tmp
    #print(ans)
    heapq.heappush(q,-arr[i])
    heapq.heappush(q,-arr[i])
    #print(q)
print(ans)

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

Submission status: Accepted

Solution code:
from collections import deque

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

ans, cnt = 0, deque([a[0]])
for i in a[1:]:
    ans += cnt.popleft()
    cnt.extend([i,i])
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import heapq as heapy
n = int(input())
arr = [int(x) for x in input().split()]
arr.sort(reverse=True)
friendliness = 0
circle = [arr[0]]
best = [[-arr[0], (arr[0], arr[0])]]
heapy.heapify(best)
i = 1
while i != n:
    p = heapy.heappop(best)
    left = p[1][0]
    right = p[1][1]
    p1 = [-min(left, arr[i]), (left, arr[i])]
    p2 = [-min(right, arr[i]), (arr[i], right)]
    heapy.heappush(best, p1)
    heapy.heappush(best, p2)
    friendliness += -p[0]
    i += 1
print(friendliness)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int,input().split()))
A = sorted(A,reverse=True)
ans = 0
L = []
for i in range(N//2+1):
    if i == 0:
        L.append(A[i])
    elif i % 2 == 0:
        L.append(A[i])
        ans += min(L[0],L[-2])
    else:
        L.insert(0,A[i])
        ans += min(L[1],L[-1])
if N % 2 == 1:
    ans += sum(A[1:N//2+1])
else:
    ans += sum(A[1:N//2])
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.extend(A)
A.sort()
A.pop()
print(sum(A[-1:N-1:-1]))

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,*a=map(int,open(0).read().split());print(sum(sorted(a*2)[-n:-1]))

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
a=a+a
a.sort()
print(sum(a[-n:-1]))

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
a.sort()
a=a[::-1]
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()))
aa = a + a
ans = 0
aa.sort(reverse = True)
for i in range(1,n):
    ans += aa[i]
print(ans)

Submission status: Accepted

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

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

ctr = n-1
idx = 0
res = 0

while ctr > 0:
    if idx == 0:
        ctr -= 1
        res += a[idx]
    else:
        ctr -= 2
        if ctr == -1:
            res += a[idx]
        else:
            res += a[idx]*2
    idx += 1

print(res)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**7)
input = sys.stdin.readline

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

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

print(ans)

Submission status: Accepted

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


def readstr():
    return input().strip()


def readint():
    return int(input())


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


def readstrs():
    return input().split()


def main():
    N = readint()
    A = sorted(list(readnums()), reverse=True)

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

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline
n = int(input())
a = sorted(list(map(int, input().split())))
print(2*sum(a[n//2:])-a[-1]-(a[n//2] if n%2 else 0))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(300000)
from heapq import heappush, heappop

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def MI0(): return map(lambda s: int(s) - 1, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
def LMI0(): return list(map(lambda s: int(s) - 1, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


N = I()
A = LMI()
A.sort(reverse=True)

ans = 0
q = []
for i, a in enumerate(A):
    if i == 0:
        heappush(q, -a)
    else:
        m = -heappop(q)
        ans += m
        heappush(q, -a)
        heappush(q, -a)
print(ans)

Submission status: Accepted

Solution code:
n =int(input())
A=list(map(int,input().split()))
A = A+A
ans=0
A.sort()
A.pop()

for _ in range(n-1):
    ans+= A.pop()
print(ans)

Submission status: Accepted

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

num=0
n-=2

while n>0:
  if n==1:
    ans+=a[num]
    n-=1
    
  else:
    ans+=a[num]*2
    n-=2
    num+=1
    
print(ans)

Submission status: Accepted

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

ans = A.pop()
A.pop()
for _ in range(N - 2):
    ans += A.pop()

print(ans)

Submission status: Accepted

Solution code:
import heapq
n = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
ans = 0
tmp = []
heapq.heappush(tmp, -a[0])
for i in range(1, n):
    point = -heapq.heappop(tmp)
    ans += point
    heapq.heappush(tmp, -a[i])
    heapq.heappush(tmp, -a[i])
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin, stdout, setrecursionlimit
from collections import deque, defaultdict, Counter
from heapq import heappush, heappop
import math

rl = lambda: stdin.readline()
rll = lambda: stdin.readline().split()
rli = lambda: map(int, stdin.readline().split())
rlf = lambda: map(float, stdin.readline().split())

INF, NINF = float('inf'), float('-inf')

def main():
	n = int(rl())
	A = list(rli())
	A.sort(reverse = True)
	comfort = A[0]
	cnt = 0
	for i in range(2, n):
		if i % 2 == 0:
			cnt += 1
		comfort += A[cnt]
	print(comfort)	

if __name__ == "__main__":
	main()

Submission status: Accepted

Solution code:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush
from bisect import bisect_left,bisect_right 
import sys,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n = inp()
a = inpl()
a.sort(reverse = True)
res = 0
ind = 0
for i in range(1,n):
    res += a[ind]
    if i%2:
        ind += 1
print(res)

Submission status: Accepted

Solution code:
from sys import stdin
input = stdin.readline
X = int(input())
x_list = list(map(int, input().split()))
a = sorted(x_list,reverse = True)
ans = 0
for i in range(1,X):
  ans += a[i//2]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n=int(input())
l=list(map(int,input().split()))
d=[]
l.sort(reverse=True)
d.append(l[0])
for i in range(1,n):
    d.append(l[i])
    d.append(l[i])
max=0
for i in range(n-1):
    max+=d[i]
print(max)

Submission status: Accepted

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)  # 大きい順にソート
B = []  # 既に到着した人. 大きい順に追加される.
cnt = 0
for a in A:
    if len(B) != 0:
        j = len(B) // 2  # 中央値の位置を求める
        cnt += B[j]
    B.append(a)
print(cnt)

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(n-2):
    ans += a[i//2+1]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import heapq

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

Q = [[-A[1],-A[0]]]*2
heapq.heapify(Q)

ans = A[0]
for a in A[2:]:
    #print(Q)
    top = heapq.heappop(Q)
    ans += -top[0]

    heapq.heappush(Q, [-a,-top[0]])
    heapq.heappush(Q, [-a,-top[1]])
    #print(ans)
    
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
  N=int(input())
  A=list(map(int,input().split()))
  A.sort(reverse=True)
  ans=0
  for i in range(1,len(A)):
    ans+=A[i//2]
  print(ans)
  pass
if __name__ == '__main__':
  main()

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!

import copy
from collections import deque

N = int(input())
A = list(map(int, input().split()))
A.sort(reverse = True)
d = deque()
c = 0
count = 2
ans = 0

if N == 2:
    print(A[0])
elif N == 3:
    print(A[0] + A[1])
else:
    ans = A[0] + A[1]
    for i in range(3, N-1):
        if c == 0:
            ans += A[count]
            c += 1
        elif c == 1:
            ans += A[count]
            count += 1
            c = 0
        
    ans += A[1]
    print(ans)

Submission status: Accepted

Solution code:
import copy
from collections import deque

N = int(input())
A = list(map(int, input().split()))
A.sort(reverse = True)
d = deque()
c = 0
count = 2
ans = 0

for k in range(N-1):
    ans += A[ (k+1) // 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)*(-1)
    ans += tmp
    heapq.heappush(q, -arr[i])
    heapq.heappush(q, -arr[i])
print(ans)

Submission status: Accepted

Solution code:
n,*a=map(int,open(0).read().split())
a.sort(reverse=True)
p=[]
for i in range(n):
    p.append(a[i])
    p.append(a[i])
print(sum(p[:n])-p[0])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n = int(input())
lis = sorted(list(map(int, input().split())),reverse=True)
idx = 0
res = lis[0]
while True:
    if idx >= n-2:
        break
    res += lis[1+idx//2]
    idx += 1
print(res)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
import numpy as np
import heapq

N = int(input())
A =  np.sort([int(x) for x in input().split(" ")])[::-1]

q = [(-A[1], (0, 1)), (-A[1], (0, 1))]
confort = A[0]
heapq.heapify(q)

i = 2
while N > i:
    m = heapq.heappop(q)
    # print(f"{m[1][0]}と{m[1][1]}の間に割り込む. 気持ち良さ {-m[0]}")
    confort -= m[0]
    heapq.heappush(q, (-A[i], (i, m[1][0])))
    heapq.heappush(q, (-A[i], (i, m[1][1])))
    i += 1
print(confort)

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 += int(A[i//2])
print(ans)