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: Runtime Error

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

if len(A) <= 2:
    print(sum(A))
else:
    ans = A[0] + 2*sum(A[1:math.floor(n/2)]) + (N%2)*A[math.floor[N/2]]
    print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
a.sort
a.reverse()
b=0
c=0
if N % 2==1:
  b+=a[0]
  c+=1
  i=0
  while True:
    if c==N:
      print(b)
      break
    else:
      i+=1
      c+=2
      b+=2*a[i]
  
if N % 2==0:
  b+=a[0]
  c+=1
  i=0
  while True:
    i+=1
    c+=2
    b+=2*a[i]
    m=N-c
    if m==1:
      b+=a[i]
      print(b)
      break

Submission status: Runtime Error

Solution code:
N=int(input())
a=list(map(int,input().split()))
a=a.sort
b=0
c=0
if N % 2==1:
  b+=a[0]
  c+=1
  i=0
  while True:
    if c==N:
      print(b)
      break
    else:
      i+=1
      c+=2
      b+=2*a[i]
  
if N % 2==0:
  b+=a[0]
  c+=1
  i=0
  while True:
    i+=1
    c+=2
    b+=2*a[i]
    m=N-c
    if m==1:
      b+=a[i]
      print(b)
      break

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:

ans_list = list()
ans = 0


for num in range(n):
    flag = 0
    #print(ans)
    #print(a_list)
    person = a_list.pop(a_list.index(max(a_list)))
    if num == 0:
        ans_list.append(person)
    elif num == 1:
        ans_list.append(person)
        ans += ans_list[0]
    else:
        for ans_index in range(num):
            #print(ans_index)
            if person == ans_list[ans_index]:
                #print("pepepe")
                ans_list.insert(ans_index, person)
                ans += ans_list[ans_index]
                flag = 1
                break
                
            elif person < ans_list[ans_index] and ans_list[ans_index] >= ans_list[ans_index + 1]:
                #print("kekeke")
                ans += ans_list[ans_index+1]
                ans_list.insert(ans_index+1, person)
                #print(ans_list[ans_index])
                
                flag = 1
                break
                
        if flag == 0:
            #print("kokoko")
            ans += ans_list[len(ans_list)]
            ans_list.append(person)
    #print(ans_list)
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque

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

A.sort(reverse=True)
A = deque(A)
C = A.popleft()

for n in range(N-1):
    res += C
    C = A.popleft()
print(res)

Submission status: Runtime Error

Solution code:
N = int(input())

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

total = 0
if len(players) > 1:
    total = 0
    place = []
    for i, player in enumerate(players):
        if i == 0:
            place.append(player)
        elif i == 1:
            total += players[0]
            place.append(player)
        elif i == 2:
            total += players[1]
            place.append(player)
        else:
            shifted_place = [place[-1]].append(place[:-1])
            comfort_list = list(map(min, place, shifted_place))
            max_comfort = max(comfort_list)
            total += max_comfort
            max_comfort_index = comfort_list.index(max_comfort)
            place.insert(max_comfort_index, player)

print(total)

Submission status: Runtime Error

Solution code:
from collections import deque
N = int(input())
A = list(map(int, input().split()))
A.sort(reversre=True)
ans = 0
d = deque(A[0])
for x in A[1:]:
  ans += d.popleft()
  d.append(x)
  d.append(x)
print(ans)

Submission status: Runtime Error

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

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

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

Submission status: Runtime Error

Solution code:
n=int(input())
A=list(map(int,input().split()))
print(sum(a)-min(a))

Submission status: Runtime Error

Solution code:
def sum(a,N):
  if N == 1:
    return a[0]
  return a[N-1] +  sum(a,N-1)

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

Submission status: Runtime Error

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):
    ans += A[tmp]
    if i % 2 == 0:
        tmp += 1
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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:
  for i in range(1, int((N-1)/2)+1):
    tmp += A[i] * 2
  tmp -= A[int((N-3)/2)]

ans += tmp
print(ans)

Submission status: Runtime Error

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:
  for i in range(1, int((N-3)/2)+1):
    tmp += A[i] * 2
  tmp += A[int((N-3)/2)+1]

ans += tmp
print(ans)

Submission status: Runtime Error

Solution code:
n=int(input())
li=list(map(int,input().strip().split(" ")))
li.sort(reverse=True)
if n==2:
  print(li[0])
else:
  ans=li[0]
  req=n-2
  i=1
  while(req>0):
    if req==1:
      if i<n:
        ans+=li[i]
      req-=1
      i+=1
    else:
      if i<n:
        ans+=(li[i]*2)
      req-=2
      i+=1
    
print(ans)

Submission status: Runtime Error

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: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
from collections import deque

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

A.sort(reverse=True)

deq = deque([A[0]])
ans = 0

for x in A[1:]:
    print(f"{deq=}, {x=}")
    ans += deq.popleft()
    deq.append(x)
    deq.append(x)

print(ans)

Submission status: Runtime Error

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(reverse=True)

        res = sum(A[:-1])

        print(res)

Submission status: Runtime Error

Solution code:
n = int(input())
a = sorted(list(map(int,input().split())))[::-1]
ans = 0
temp = a[0]
lim = 1
cnt = 1
nb = iter(set(a))
poy = next(nb)
nxt = next(nb)
for v in a[1:]:
  if lim==0:
    cnt = 2
    temp = nxt
    lim = cnt
    nxt = next(nb)
  ans += temp
  if temp==v:
    cnt += 1
    lim = cnt
  else:
    lim -= 1
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = sorted(list(map(int,input().split())))[::-1]
ans = 0
temp = a[0]
lim = 1
cnt = 1
nb = iter(set(a))
poy = next(nb)
nxt = next(nb)
for v in a[1:]:
  if lim==0:
    cnt = 2
    temp = nxt
    lim = cnt
    nxt = next(nb)
  ans += temp
  if temp==v:
    cnt += 1
    lim = cnt
  else:
    lim -= 1
  print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
#以下は上位者のコピー
n, k = map(int, input().split())
mod = 10 ** 9 + 7

from collections import deque

a = list(map(int, input().split()))
a.sort()
minus, zero, plus = 0, 0, 0
for i in range(n):
    if a[i] < 0:
        minus += 1
    elif a[i] == 0:
        zero += 1
    else:
        plus += 1

que = deque(a)

plus_flg = False
tmp = plus - k
if tmp >= 0:
    plus_flg = True
elif tmp*-2 > minus  :
    plus_flg = True

# plus可能
if plus_flg:
    ans = 1
    cnt = 0

    while cnt < k:
        if k - cnt >= 2:
            cnt += 2
            if que[0] * que[1] >= que[-1] * que[-2]:
                ans *= que.popleft()
                ans *= que.popleft()
                ans %= mod
            else:
                ans *= que.pop()
                ans *= que.pop()
                ans %= mod
        else:
            ans *= que.pop()
            ans %= mod
            cnt += 1

    print(ans)



# zero
elif zero > 0:
    print(0)

# minus
else:
    a = [abs(a[i]) for i in range(n)]
    a.sort()
    ans = 1
    for i in range(k):
        ans *= a[i]
        ans %= mod
    ans = -ans % mod
    print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def S(): return input().rstrip()
def LS(): return S().split()
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float("INF")

#solve
def solve():
    n = II()
    a = LI()
    a.sort()
    ans = 0
    a = a[::-1]
    for i in range(n):
        ans += ans[(i + 1) // 2]
    print(ans)
    return


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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
import math
import random # ランダムな配列の生成用
N=int(input())
A = list(map(int, input().strip().split()))

def quick_sort(arr):
    left = []
    right = []
    if len(arr) <= 1:
        return arr

    # データの状態に左右されないためにrandom.choice()を用いることもある。
    # ref = random.choice(arr)
    ref = arr[0]
    ref_count = 0

    for ele in arr:
        if ele < ref:
            left.append(ele)
        elif ele > ref:
            right.append(ele)
        else:
            ref_count += 1
    left = quick_sort(left)
    right = quick_sort(right)
    return left + [ref] * ref_count + right

A=quick_sort(A)
A.reverse()
count=2
ans=A[0]
A.pop(0)
while count<N:
    add_num=A.pop(0)
    print(add_num)
    ans+=add_num*2
    count+=2
    if count>N:
        ans-add_num





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

Submission status: Runtime Error

Solution code:
N=int(input())
A=list(map(int,input().split()))
A.sort(reverse=True)
def max_comfort(l,c,ans):
  if len(l)==0:
    return(ans)
  else:
    d=0
    mark=0
    for i in range(len(c)):
      if i==0:
        d=min(c[0],c[len(c)-1])
        mark=0
      else:
        if max(min(c[i],c[i-1]),d)==min(c[i],c[i-1]):
          mark=i
        d=max(min(c[i],c[i-1]),d)
    c.insert(mark,l[0])
    ans+=d
    l.pop(0)
    return max_comfort(l,c,ans)
    
c=[]
ans=0
a=max_comfort(A,c,ans)
print(a)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

ans = A[N]
cnt = 1
i = 1

while cnt <= N:
    ans += 2*A[N-i]
    i += 1
    cnt += 2

if N != cnt:
    ans -= A[N-i]

print(ans)

Submission status: Runtime Error

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

A.sort()
A.reverse()

list = []
ans = 0
list.append(A[0])
for i in range(len(A)):
        ans += list[i]
        list.append(A[i + 1])
        list.append(A[i + 1])

print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

A.sort()
A.reverse()

list = []
ans = 0
list.append(A[0])
for i in range(1, len(A)):
        ans += list[i]
        list.append(A[i])
        list.append(A[i])

print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
from sys import stdin
N = int(stdin.readline().rstrip())
ran = [int(x) for x in stdin.readline().rstrip().split()]
ran.sort(reverse=True)

gen = len(ran) - 2
goukei = ran[0]
if gen % 2 == 0:
  for i in range(int(gen/2)):
    goukei += 2 * ran[i + 1]
else:
  for i in rangen(int((gen-1)/2)):
    goukei += 2 * ran[i + 1]
  goukei += ran[i + 2]

print(goukei)

Submission status: Runtime Error

Solution code:
func chatInACircle(a []int) int {
	sort.Slice(a, func(i, j int) bool {
		return a[i] > a[j]
	})
	if len(a) == 2 {
		return a[0]
	}

	m := a[0]
	a = a[1:]

	for i := 0; i < len(a) - 1; i++ {
		m += a[i/2]
	}

	return m
}

Submission status: Runtime Error

Solution code:
for i in range(len(a)):
    b.append(0)

ans = 0
second = 0
for i in range(len(a)):
    c = []
    for co2 in b:
        c.append(copy.copy(co2))
    c.sort(reverse=True)
    for j in range(len(c)):
        if(c[0] > c[j]):
            second = c[j]
            break
    if(i == 1):
        second = c[0]
    b[i] = a[i]
    if(i == 0):
        for all in range(len(a)):
            b[all] = a[i]
    if(i != 1):
        if(b[i] < second):
            for k in range(b.index(second), len(b)-1):
                b[k+1] = b[k]
            b[b.index(second)] = 0
            b[b.index(second)-1] = a[i]
    ans += second
    #print(b, end=' ')
    #print(ans, end=' ')
    #print(second)

print(ans)

Submission status: Runtime Error

Solution code:
if __name__ == '__main__':
    n = int(input().strip())
    A = list(map(int, input().strip().split()))
    A.sort(reverse=True)
    score = 0
    if not len(A) < 2:
        min_max_list = [0] * n
        min_max_dict = {A[1]: 2}
        max_value = A[1]
        score = A[0]
        value_list = [A[1]]
        for num, a in enumerate(A[2:]):
            score += max_value
            min_max_dict[max_value] -= 1
            if a in min_max_dict:
                min_max_dict[a] += 2
            else:
                min_max_dict[a] = 2
                value_list.append(a)
            if min_max_dict[max_value] == 0:
                value_list.remove()
                max_value = value_list[0]
    print(score)

Submission status: Runtime Error

Solution code:
# 幅優先探索（行きがけ）
import collections
import sys
import copy
import re


def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())


def main():
    N = I()
    A = LI()
    A.sort()

    B = list(set(copy.copy(A)))
    maxnum = B.pop()
    cnt = [1]*(maxnum+1)
    chageCnt = 0
    ans = 0
    for i in range(1, N):
        a = A[i]
        if A[i-1] == A[i]:
            cnt[a] += 1
    
    for i in range(N-1):
        ans += maxnum
        chageCnt += 1
        if chageCnt == cnt[maxnum]:
            maxnum = B.pop()
            chageCnt = 0

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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

ans = 0
if N >= 4:
    for i in range(N-1):
        if i <3:
            ans += A[i]
        else:
            if A[i-1] != A[i-2]:
                ans += min[A[i-1],A[i-2]]
            else:
                ans += A[i-1]
else:
    if N == 3:
        ans = A[0]+A[1]
    else:
        ans = A[0]

print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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: Runtime Error

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

Submission status: Runtime Error

Solution code:
# 幅優先探索（行きがけ）
import collections
import sys
import copy
import re


def I(): return int(sys.stdin.readline().rstrip())
def LI(): return list(map(int, sys.stdin.readline().rstrip().split()))
def S(): return sys.stdin.readline().rstrip()
def LS(): return list(sys.stdin.readline().rstrip().split())


def main():
    N = I()
    A = LI()
    A.sort()

    B = list(set(copy.copy(A)))
    maxnum = B.pop()
    cnt = [0]*(N)
    chageCnt = 0
    ans = 0
    for i in range(1, N):
        a = A[i]
        if A[i-1] == A[i]:
            cnt[a] += 1

    for i in range(N-1):
        ans += maxnum
        chageCnt += 1
        if chageCnt == cnt[maxnum-1]:
            maxnum = B.pop()
            chageCnt = 0

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N , K = map(int, input().split())
A =list(map(int, input().split()))
mod = 10**9 + 7
count =0
for k in range(len(A)):
    if A[k] == 0:
        count +=1
        A[k] = float("inf")
if N-count < K:
    print(0)
    exit()
A.sort()
A = A[:N-count]
A.sort(reverse=True)
#print(A)
ans = 1
plus = 0
minus = -1
countp = 0
countm = 0
if A[0] >0:
    for k in range(K):
        if abs(A[plus]) >= abs(A[minus]):
            ans = ans*A[plus]%mod
            plus+=1
        else:
            ans = ans*A[minus]%mod
            if A[minus] <0:
                lasm = 0+minus
            minus-=1
            
    if minus%2 == 1:
        print(ans%mod)
    else:
        print(ans//A[lasm]*max(A[plus], A[minus])%mod)
else:
    for k in range(K):
        ans = ans*A[k]%mod
    print(ans%mod)

Submission status: Runtime Error

Solution code:
total = 0

for _ in range(int(input()))
	list1 = list(map(int, input().split()))
	for ele in range(0, len(list1)):
		total = total + list1[ele]
	print(total-min(list1))

Submission status: Runtime Error

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

ans = 0

for i in range(N-1):
    if i <3:
        ans += A[i]
    else:
        if A[i-1] != A[i-2]:
            ans += min[A[i-1],A[i-2]]
        else:
            ans += A[i-1]

print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
N = int(input())
A = sorted(map(int, input().split()))
q = deque()
a = a.pop()
b = a.pop()
ans = a + b
q.append((a,b))
q.append((a,b))
while A:
    c = A.pop()
    a,b = q.popleft()
    ans += min(a,b)
    q.append((a,c))
    q.append((c,b))
print(ans)

Submission status: Runtime Error

Solution code:
test = int(input())
x=test
nums=[int(test) for test in input().split()]

ans = 0
nums.sort()
nums.reverse()
copy=nums.copy()
ans +=lol[0]
lol.remove(max(lol))
test=test-2

if(test%2==0):
    for i in range(int(test/2)):
        ans+=(2*lol[i])
    print(ans)
else:
    for i in range(int((test-1)/2)):
        ans+=(2*lol[i])
    ans+=(copy[int(x/2)])
    print(ans)

Submission status: Runtime Error

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

def li():
    return [int(x) for x in input().split()]

N = int(input())
A = li()

A.sort(reverse=True)

B = [A[i] for i in range(N) if i < N//2]

# 9 8 7 6 5
# 9 1 8 2 7 3 6 4 5
# 9 6 8 7
# 9+8+7+6+5+6+7+8

# 8 7 6 5
# 8 1 7 2 6 3 5 4
# 8+7+6+5+5+6+7

sm = B[0] + sum(B[i] * 2 for i in range(N/2) if 0 < i)
print(sm)

Submission status: Runtime Error

Solution code:

def abc173_d():
    N = int(input())
    a =  list(map(int,input().split()))


    a = sorted(a,reverse=True)

    Enkan = [a[0],a[1]]





    gokei = []
    def saiki(i,suma,Enkan):
        
        if i == N:
            gokei.append(sum(suma))
            return
        else:
            for k in range(len(Enkan)):

                if k != len(Enkan)-1:
                    suma.append(min(Enkan[k],Enkan[k+1]))
                    Enkan.insert(k,a[i])
                    saiki(i+1,suma,Enkan)
                    del Enkan[k]
                    del suma[-1]
                else:
                    suma.append(min(Enkan[-1],Enkan[0]))
                    Enkan.insert(0,a[i])
                    saiki(i+1,suma,Enkan)
                    del Enkan[0]
                    del suma[-1]

    saiki(2,[3],[a[0],a[1]])
    print(max(gokei))



abc173_d()

Submission status: Runtime Error

Solution code:
from collections import Counter

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

a_count = Counter(a)
a_key = list(a_count.keys()).sort(reverse=True)
a_room = {}

ans = 0
a_count[a_key[0]] -= 1
a_room[a_key[0]] = 1
if a_count[a_key[0]] == 0:
    del a_key[0]
for i in range(n):
    trykey = a_key[0]
    a_count[trykey] -= 1
    if a_count[a_key[0]] == 0:
        del a_count[a_key[0]]
    ans += list(a_room.keys())[0]
    a_room[list(a_room.keys())[0]] -= 1
    if trykey == list(a_room.keys())[0]:
        a_room[trykey] += 2
    else:
        if a_room.get(trykey):
            a_room[trykey] += 2
        else:
            a_room[trykey] = 2

print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a = list(map(int, input().split()))
a = sorted(a)[::-1]
l = [0] * (n)
s = 0
e = n - 1

for i in range(n):
    if  i % 2 != 0:
        l[s] = a[i]
        s += 1
    else:
        l[e] = a[i]
        e -=1

cnt = 0
for i in range(1, n-1):
    cnt += min(l[i-1] - l[i], l[i+1]-l[i])
print(cnt - (l[n-1] - l[n-2])

Submission status: Runtime Error

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

ans = 0

for i in range(N-1):
    if i <2:
        ans += A[i]
    else:
        if A[i-1] != A[i-2]:
            ans += min[A[i-1],A[i-2]]
        else:
            ans += A[i-1]

print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

for i in range(N-1):
    if i <2:
        ans += A[i]
    else:
        if A[i-1] != A[i-2]:
            ans += min[A[i-1],A[i-2]]
        else:
            ans += A[i-1]

print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N = input()
S = [input().split() for _ in range(int(N))]

max=S[0];

semimax=S[1];

val=max;

for i in range(int(N-2)):
	val+=semimax
	if S[i+1]>max:
		max=S[i+1]
        if S[i+1]>semimax:
			semimax=S[i+1]

print(val)

Submission status: Runtime Error

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

l=[]
n=A.pop()
n2=A.pop()
sum=n
comfort={n2:[[n,n2],[n2,n]]}

while A:
  max=sorted(comfort.keys())[-1]
  n=comfort[max].pop()
  sum+=max
  n2=A.pop()
  if comfort.get(min(n[0],n2))==None:comfort[min(n[0],n2)]=[]
  if comfort.get(min(n[1],n2))==None:comfort[min(n[1],n2)]=[]
  comfort[min(n[0],n2)].append([n[0],n2])
  comfort[min(n[1],n2)].append([n[1],n2])
print(sum)

Submission status: Runtime Error

Solution code:
N = int(input())
data = input().split()
circle = []
maxn = 0
pointer = -1
kokotiyosa = 0
for num in range (N):

  if maxn < int(data[num]):
    maxn = int(data[num])
    pointer = num
data.pop(pointer)
circle += [maxn]
cc = 1
def searchmax(data):
  global  kokotiyosa,circle,cc
  maxn = 0
  pointer = -1
  for num in range (len(data)):
    if maxn < int(data[num]):
    	maxn = int(data[num])
    	pointer = num
  data.pop(pointer)
  maxkokoti = 0
  switch = 0
  if(cc <= 2):
    minn = 1000000000000000
    for i in range(len(circle)):
    	if(circle[i] < minn):
          minn = circle[i]
          maxkokoti = circle[i]
    circle += [maxn]
    switch = 1
    kokotiyosa += maxkokoti
  if(cc >=3) and (switch == 0):
    inn = 0
    for i in range(len(circle)):
      l = circle[i]
      if(i<len(circle)-1):
        r = circle[i+1]
      if(i==len(circle)-1):
        r = circle[0]
      if(l < r) and (maxkokoti < l):
        maxkokoti = l
        inn = i+1
      if(l >= r) and (maxkokoti < r):
        maxkokoti = r
        inn = i+1
    circle.insert(inn,maxn)
    kokotiyosa += maxkokoti
  if(len(circle)>=int(N)):
    print(kokotiyosa)
    exit()
  cc += 1
  searchmax(data)
  
searchmax(data)

Submission status: Runtime Error

Solution code:
n,k=input().split()
n=int(n)
k=int(k)
a=input().split()
for i in range(n):
  a[i]=int(a[i])
  
for j in range(n):
  for l in range(n-j):
    if a[j]<a[j+l]:
      x=a[j]
      a[j]=a[j+l]
      a[j+l]=x
      
f=1;
e=1;
for q in range(k):
  f=f*a[q]
  e=e*a[n-q-1]
  
r=0
if e>f:
  r=e
if f>=e:
  r=f
r=r%1000000007
print(r)

Submission status: Runtime Error

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

A.sort()
ans = A[-1]
count = 1
tmp_count = 1
j = -2
for i in range(N):
    ans += A[j]
    count += 1
    tmp_count +=1
    if count == 2:
        count = 0
        j -= 1
    if tmp_count == N-1:
        break
        
print(ans)