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:
#!/usr/bin/env python3
import sys
import collections


def II(): return int(sys.stdin.readline())


def MI(): return map(int, sys.stdin.readline().split())


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


def main():
    N = II()
    As = LI()
    As.sort()
    As.reverse()
    As = collections.deque(As)
    ans = []
    tmp = As.popleft()
    ans.append(tmp)

    for i in range(len(As)):
        tmp = As.popleft()
        ans.extend([tmp, tmp])

    print(sum(ans[:N-1]))
    # print(ans)


# oj t -c "pypy3 main.py"
# acc s main.py -- --guess-python-interpreter pypy
main()

Submission status: Accepted

Solution code:
from collections import deque

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

A.sort(reverse=True)

dp=deque([A[1]])
point=A[0]
cnt=0
for n in range(2,len(A)):
    if cnt==0:
        p=dp.popleft()
        cnt+=1
        dp.append(A[n])
        point+=p
    else:
        cnt=0
        dp.append(A[n])
        point+=p

print(point)

Submission status: Accepted

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

aa.sort(reverse=True)
# print(aa)
ans = 0
for i in range(1, N):
    ans += aa[i]

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])
#人１がすでに到着しているとしたら得られる値はa0に等しい
for n in range(1, N):
    tmp  = heapq.heappop(q)
    tmp *= -1
#優先度付きキューは最小値取り出すからー１かけて最大値取り出せるように
    ans += tmp  #各時点でのmin(ai,aj)の最大値を答えに足す
    heapq.heappush(q, -arr[n])
    heapq.heappush(q, -arr[n]) #新たに割り込める場所２個追加
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from heapq import *
n = int(input())
A = [*map(lambda x: -int(x), input().split())]
heapify(A)
ans = -heappop(A)
for c in range(0, n-2, 2): ans += -heappop(A) * (1<<(c+1 < n-2))
print(ans)

Submission status: Accepted

Solution code:
from collections import defaultdict as dd
from collections import deque
import bisect
import heapq

def ri():
    return int(input())

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


def solve():
    n = ri()
    A = rl()
    A.sort(reverse=True)

    ans = 0
    pos = 0
    for i in range(1, len(A)):
        # print (i, pos, A[pos])
        ans += A[pos]
        if i % 2 == 1:
            pos += 1
    print (ans)






mode = 's'

if mode == 'T':
    t = ri()
    for i in range(t):
        solve()
else:
    solve()

Submission status: Accepted

Solution code:
n =int(input())
a = [int(i) for i in input().split()]
a= sorted(a,reverse =True)
summ = a[0]
n-=2
i = 1
while(n>=2):
    summ+=2*a[i]
    i+=1
    n-=2
if n==1:
    summ+=a[i]
print(summ)

Submission status: Accepted

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

ret = 0
c_pointer = 0
comfort = []
tmp = A.pop()
comfort.append(tmp)

while len(A) > 0:
    tmp = A.pop()
    c_pointer += 1
    comfort.append(tmp)
    comfort.append(tmp)

print(sum(comfort[:c_pointer]))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from heapq import heapify, heappop,heappush
n=int(input())
a=list(map(int,input().split()))
a=[i*(-1) for i in a]
a.sort()

ans=0
q=[]
heappush(q,a[0])
for i in range(1,n):
    tmp = heappop(q)
    ans += tmp*(-1)
    heappush(q,a[i])
    heappush(q,a[i])
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(1,N):
    ans += A[i//2]
print(ans)

Submission status: Accepted

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

new=a+a

new=sorted(new, reverse=True)

ans=0

for i in range(1, n):
    ans+=new[i]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

pq = []
res = 0
heapq.heapify(pq)

for i in range(n):
  if len(pq) == 0:
    heapq.heappush(pq, -a[i])
  else:
    score = -heapq.heappop(pq)
    res += score
    for _ in range(2):
      heapq.heappush(pq, -a[i])

print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
from heapq import heappush, heappop

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 19
MOD = 10 ** 9 + 7
EPS = 10 ** -10

N = INT()
A = LIST()

A.sort(reverse=1)
que = [-A[0]]
ans = 0
for i in range(1, N):
    cur = -heappop(que)
    ans += cur
    heappush(que, -A[i])
    heappush(que, -A[i])
print(ans)

Submission status: Accepted

Solution code:
import sys
import queue

input_methods=['clipboard','file','key']
using_method=1
input_method=input_methods[using_method]

tin=lambda : map(int, input().split())
lin=lambda : list(tin())
mod=1000000007

#+++++

def main():
	n = int(input())
	#b , c = tin()
	#s = input()
	al = lin()
	al.sort(reverse = True)
	q = queue.PriorityQueue()
	q.put((-al[1], al[1]))
	q.put((-al[1], al[1]))
	score = al[0]
	for v in al[2:]:
		o, f = q.get()
		score += f
		q.put((-v, v))
		q.put((-v, v))
	return score
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import math
N = int(input())
Nlist = list(map(int, input().split()))
Nlist.sort(reverse=True)
x = math.ceil(N/2)
ans = sum(Nlist[0:x])*2-Nlist[0]
if N % 2 != 0:
    ans -= Nlist[x-1]
print(ans)

Submission status: Accepted

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

A = sorted(A)

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

print(conf)

Submission status: Accepted

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

N = int(readline().strip())
A = list(map(int, readline().split()))

A.sort(reverse=True)

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

Submission status: Accepted

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

ans = 0
for i in range(1, N):
    ans += aa[i]
print(ans)

Submission status: Accepted

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

cnt = a.pop()
tmp = a.pop()
l = 0
for i in range(n - 2):
    if l == 2:
        l = 0
        tmp = a.pop()
    cnt += tmp
    l += 1
print(cnt)

Submission status: Accepted

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

D()

Submission status: Accepted

Solution code:
N = int(input())
A = [int(_) for _ in input().split()]
ans = 0
AA = A + A
AA.sort(reverse=True)

# sumする
for i in range(1, N):
    ans += AA[i]

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

Submission status: Accepted

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

a.sort(reverse=True)

q = [-a[0]]
heapq.heapify(q)
ans = 0
for i in range(1,n):
    t = heapq.heappop(q)
    ans += -t
    heapq.heappush(q,-a[i])
    heapq.heappush(q,-a[i])
    
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3

from pprint import pprint
from bisect import bisect_left
import sys

sys.setrecursionlimit(10 ** 6)
INF = float('inf')


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

# 到着する順番は降順が最適
# 足せる回数は合計 N-1 回
# 最初の数字は 1 回、他は高々 2 回足される

ans = 0
times = N - 1

for i in range(N):
    limit = 2
    if i == 0:
        limit = 1
    for _ in range(limit):
        if times > 0:
            ans += A[i]
            times -= 1

print(ans)

Submission status: Accepted

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


def main(input, print):
    n = int(input())
    a = list(map(int, input().split()))
    a.sort(reverse=True)
    ans = 0
    cnt = 0
    for i in range(n):
        lim = 1 if i == 0 else 2
        for j in range(lim):
            if cnt < n-1:
                ans += a[i]
                cnt += 1
    print(ans)


if __name__ == '__main__':
    main(sys.stdin.readline, print)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import deque


def main():
    input()  # n
    array = [int(x) for x in input().split()]
    array.sort()
    coming = deque(array)

    edges = deque()
    noob = coming.pop()
    edges.append(noob)
    ans = 0
    while coming:
        ans += edges.popleft()
        noob = coming.pop()
        edges.append(noob)
        edges.append(noob)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque


def main():
    input()  # n
    coming = [int(x) for x in input().split()]
    coming.sort()
    coming = deque(coming)

    edges = deque()
    new = coming.pop()
    edges.append(new)
    ans = 0
    while coming:
        ans += edges.popleft()
        new = coming.pop()
        edges.append(new)
        edges.append(new)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque


def main():
    input()  # n
    array = [int(x) for x in input().split()]
    array.sort()
    coming = deque(array)

    edges = deque()
    new = coming.pop()
    edges.append(new)
    ans = 0
    while coming:
        ans += edges.popleft()
        new = coming.pop()
        edges.append(new)
        edges.append(new)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque


def main():
    input()  # n
    friendliness = [int(x) for x in input().split()]
    friendliness.sort()
    friendliness = deque(friendliness)

    edges = deque()
    popped = friendliness.pop()
    edges.append(popped)
    ans = 0
    while friendliness:
        ans += edges.popleft()
        popped = friendliness.pop()
        edges.append(popped)
        edges.append(popped)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque


def main():
    input()
    (edges := deque()).append(
        (friendliness := deque(sorted(int(x) for x in input().split()))).pop())
    ans = 0
    while friendliness:
        ans += edges.popleft()
        edges.append(popped := friendliness.pop())
        edges.append(popped)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from collections import deque


def main():
    input()  # n
    friendliness = [int(x) for x in input().split()]
    friendliness.sort(reverse=True)
    edges = deque()
    edges.append(friendliness[0])
    ans = 0
    for fri in friendliness[1:]:
        ans += edges.popleft()
        edges.append(fri)
        edges.append(fri)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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


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

  max_ = A[0]
  max_iter = N-2
  iter_ = 0
  for i in range(1, N):
    for _ in range(2):
      if iter_ >= max_iter:
        print(max_)
        return
      max_ += A[i]
      iter_ += 1


if(__name__ == '__main__'):
  main()

Submission status: Accepted

Solution code:
from math import ceil

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

ans = 0

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


print(ans)

Submission status: Accepted

Solution code:
import heapq

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

ans = 0
ans += a[0]

h = []
heapq.heapify(h)
heapq.heappush(h, -a[1])
heapq.heappush(h, -a[1])

for i in range(2, n):
    b = heapq.heappop(h)
    ans += (-b)
    heapq.heappush(h, -a[i])
    heapq.heappush(h, -a[i])

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

def resolve():
    N = int(input())
    A = sorted(map(int, input().split()), reverse=True)
    ans = 0
    t = N-1
    for i in range(N):
        lim = 2
        if i == 0:
            lim = 1
        for l in range(lim):
            if t > 0:
                ans += A[i]
                t -= 1
    print(ans)

if __name__ == "__main__":
    resolve()

Submission status: Accepted

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

A.sort(reverse=True)
wa=A[0]
i=0
amari=len(A)%2

while(1):

    if(amari==0):

        if(len(A)-1==1+2*i):
            print(wa)
            break
        wa=A[i+1]*2+wa


    elif(amari==1):
        if(len(A)-2==1+2*i):
            print(A[i+1]+wa)
            break
        wa=A[i+1]*2+wa

    i=i+1

Submission status: Accepted

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


resolve()

Submission status: Accepted

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

count = 0
result = 0

for i in A:
    count += 1
    if count == 1:
        result += i
    elif count < len(A):
        result += i * 2
        count += 1
    elif count == len(A):
        result += i
    else:
        break 

    
print(result)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:

l = int(input())

a = map(int, input().split())
a = sorted(a, reverse=True)
#print(a)
if l%2==0:
	print(a[0] + sum(a[1:l//2])*2)
else:
	print(a[0] + sum(a[1:(l-1)//2])*2 + a[(l-1)//2])

Submission status: Accepted

Solution code:
n = int(input())

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

dic = {}
lis = []
for i in a:
    if i in dic:
        dic[i] = dic[i] + 1
    else:
        dic[i] = 1
        lis.append(i)

lis.sort()
lis.reverse()

ans = 0
n -= 1
ma = lis[0]
for i in lis:
    if i == ma:
        k = 2 * dic[i] - 1
    else:
        k = 2 * dic[i]
    if n - k >= 0:
        ans += i * k
    else:
        ans += i * n
        break
    n -= k

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

print(ans)

Submission status: Accepted

Solution code:
import sys
import heapq

sys.setrecursionlimit(10 ** 8)
ini = lambda: int(sys.stdin.readline())
inm = lambda: map(int, sys.stdin.readline().split())
inl = lambda: list(inm())
ins = lambda: sys.stdin.readline().rstrip()
debug = lambda *a, **kw: print("\033[33m", *a, "\033[0m", **dict(file=sys.stderr, **kw))

N = ini()
A = inl()


def solve():
    A.sort(reverse=True)
    hp = [(-A[1], -A[0]), (-A[1], -A[0])]
    score = A[0]
    for i in range(2, N):
        asmall, abig = heapq.heappop(hp)
        score += -asmall
        heapq.heappush(hp, (-A[i], asmall))
        heapq.heappush(hp, (-A[i], abig))
    return score


print(solve())

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())
arr = [int(x) for x in input().split()]
arr.sort()
arr = arr[::-1]
res, ans = [arr[0]], 0
for i in range(1, n - 1):
  res.append(arr[i])
  res.append(arr[i])
for i in range(n - 1):
  ans += res[i]
print(ans)

Submission status: Accepted

Solution code:
N = int(input())
A = [int(x) for x in input().split()]
A.sort(reverse=True)
if N == 2:
    print(A[0])
else:
    tocome = N-2
    curval = A[0]
    i = 1
    while tocome > 1:
        curval += A[i]*2
        tocome -= 2
        i += 1
    if tocome == 1:
        curval += A[i]
    print(curval)

Submission status: Accepted

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

a.sort(reverse=True)

point = a[0]
cnt = 0
k = 1
for i in range(1, n-1):
    point += a[k]
    cnt += 1
    if cnt == 2:
        cnt = 0
        k += 1

print(point)

Submission status: Accepted

Solution code:
import os
import sys
from atexit import register
from io import BytesIO
sys.stdin = BytesIO(os.read(0, os.fstat(0).st_size))
sys.stdout = BytesIO()
register(lambda: os.write(1, sys.stdout.getvalue()))
input = lambda: sys.stdin.readline().rstrip('\r\n')
raw_input = lambda: sys.stdin.readline().rstrip('\r\n')

n = int(input())
seq = sorted((int(x) for x in input().split()), reverse=True)
ans = seq[0]

i = 1
cnt = 2
cnt_in = 0
while cnt < n:
    ans += seq[i]
    cnt_in += 1
    if cnt_in == 2:
        cnt_in = 0
        i += 1
    cnt += 1

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin,stdout
def INPUT():return list(int(i) for i in stdin.readline().split())
import math
def inp():return stdin.readline()
def out(x):return stdout.write(x)
import math as M
#####################################
n=int(input())
A=INPUT()
A.sort(reverse=True)
B=[0]*n
distinct=list(set(A))
distinct.sort(reverse=True)
#print(distinct)
ans=0
D={}
for ele in A:
    if ele in D:
        D[ele]+=1
    else:
        D[ele]=1
#print(D)
count=0
i=0
p=0
while(i<n):
    ele=distinct[p]
    count=D[ele]*2
    while(i<n and count>0):
        B[i]=ele
        count-=1
        i+=1
    p+=1


#print(B)
print(sum(B)-B[0])

Submission status: Accepted

Solution code:
# D - Chat in a Circle
def main():
    N, *A = map(int, open(0).read().split())
    A.sort(reverse=True)
    res = A[0] + 2 * sum(A[1 : N // 2]) + (A[N // 2] if N % 2 else 0)
    print(res)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

ans += A[0]

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

print(ans)

Submission status: Accepted

Solution code:
# import numpy as np
# import math
# import copy
# from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(10000)


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

    As = sorted(A)

    res = 0

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

    print(res)



main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#------------------------template--------------------------#
import os
import sys
from math import *
from collections import *
from fractions import *
from bisect import *
from heapq import*
from io import BytesIO, IOBase
def vsInput():
    sys.stdin = open('input.txt', 'r')
    sys.stdout = open('output.txt', 'w')
BUFSIZE = 8192
class FastIO(IOBase):
    newlines = 0
    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None
    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()
    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()
    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)
class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")
sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
def value():return tuple(map(int,input().split()))
def array():return [int(i) for i in input().split()]
def Int():return int(input())
def Str():return input()
def arrayS():return [i for i in input().split()]

#-------------------------code---------------------------#
#vsInput()

n=Int()
a=array()
a.sort(reverse=True)

series=[a[0]]
have=1
need=n-1

for i in range(1,n):
    if(need>=have+2):
        series.append(a[i]*2)
        have+=2
    elif(need>have):
        have+=1
        series.append(a[i])
    else:
        break

print(sum(series))

Submission status: Accepted

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

A.sort(reverse=True)
q = deque()
q.append(A[0])

for i, a in enumerate(A):
    if i != 0:
        q.append(a)
        q.append(a)

for i in range(N-1):
    ans += q.popleft()

print(ans)

Submission status: Accepted

Solution code:
import heapq

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

Submission status: Accepted

Solution code:
from collections import deque
input()
A = sorted([*map(int, input().split())], reverse=True)
q = deque([A[0]])
ans = 0
for a in A[1:]:
    ans += q.popleft()
    q.append(a); q.append(a)
print(ans)

Submission status: Accepted

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
ans=a[0]
point=1
k=1
while point<n-1:
    ans+=2*a[k]
    k+=1
    point+=2
if point>n-1:
    ans-=a[k-1]

print(ans)

Submission status: Accepted

Solution code:
n = int(input())
arr = list(map(int , input().split()))
arr.sort(reverse = True)
ptr = 1
s = arr[0]
n-=2
while n:
    s+=arr[ptr]
    n-=1
    if n == 0: break
    s+=arr[ptr]
    n-=1
    ptr+=1
print(s)

Submission status: Accepted

Solution code:
import sys;input=lambda:sys.stdin.readline()
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=1)
b=[]
for i in range(n):
  b.append(a[i])
  if i:
    b.append(a[i])
print(sum(b[:n-1]))

Submission status: Accepted

Solution code:
from collections import deque
input()
A = [*map(int, input().split())]
A.sort(reverse=True)
q = deque([A[0]])
P = []
for a in A[1:]:
    p = q.popleft()
    P.append(p)
    q.append(a); q.append(a)
print(sum(P))

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)

ans = a[0]

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

Submission status: Accepted

Solution code:
from collections import deque
n = int(input())
A = [*map(int, input().split())]
# print(n)
# print(A)

A.sort(reverse=True)

# print(A)

q = deque([A[0]])
B = []
for a in A[1:]:
    x = q.popleft()
    B.append(x)
    q.append(a)
    q.append(a)
# print(q)
# print(B)
print(sum(B))

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

Submission status: Accepted

Solution code:
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf') ;mod = 10**9+7
mans = inf ;ans = 0 ;count = 0 ;pro = 1

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

Submission status: Accepted

Solution code:
def solve():
    N = int(input())
    A = list(map(int, input().split()))
    A = sorted(A * 2, reverse=True)
    print(sum(A[1:N]))


if __name__ == "__main__":
    solve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
M = int(input())
li = list(map(int, input().split()))
li = sorted(li * 2, reverse=True)
print(sum(li[1:M]))

Submission status: Accepted

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

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

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

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

if N == 1:
    print(0)
else:
    cur = N-2
    temp = max(A)
    for i in range(1,len(A)):
        if cur == 0:
            break
        temp += A[i]
        cur -= 1
        if cur == 0:
            break  
        temp += A[i]
        cur -= 1
    print(temp)

Submission status: Accepted

Solution code:
import heapq

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

hp=[]

ans=0
heapq.heappush(hp,-A[0])

for a in A[1:]:
    ans+=heapq.heappop(hp)
    heapq.heappush(hp,-a)
    heapq.heappush(hp,-a)
print(-ans)

Submission status: Accepted

Solution code:
n = int(input())
arr = sorted([int(x) for x in input().split(" ")], reverse=True)
S = arr[0]
iter = 1
c = False
for i in range(n - 2):
    S += arr[iter]
    if c == False:
        c = True
        continue
    if c == True:
        iter += 1
        c = False
        continue
        
print(S)

Submission status: Accepted

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

ans = 0
for i, A_i in enumerate(A):
  if i == 0:
    ans += A_i
    continue
  elif 2 * i >= N:
    break
  elif 2 * i == N - 1:
    ans += A_i
    break
  else:
    ans += 2 * A_i

print(ans)

Submission status: Accepted

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

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

print(conf_max)

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(N):
    if not i == 0:
        ans += B[i // 2]
        
print(ans)

Submission status: Accepted

Solution code:
import math


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

    A.sort(reverse=True)

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

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n=int(input())
inp = list(map(int,input().split()))
inp.sort(reverse=True)
ans=inp[0]
x=len(inp)-1
flag=0
i=1
while x>1:
    if flag==0 or flag==1:
        ans+=inp[i]
        flag+=1
    if flag==2:
        flag=0
        i+=1
    x-=1
print (ans)