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: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    a = LI()
    a.sort(reverse=True)
    r = a[0]
    l = a[0]
    ans = 0
    for i in range(1,n-1):
        if l < r:
            ans += l
            r = a[i]
        else:
            ans += r
            l = a[i]
    ans += min(l,r)
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
import math

n=int(input())
seq=list(map(int,input().split()))
print(sum(seq)-1)

Submission status: Wrong Answer

Solution code:
#: Author - Soumya Saurav
import sys,io,os,time
from collections import defaultdict
from collections import OrderedDict
from collections import deque
from itertools import combinations
from itertools import permutations
import bisect,math,heapq
alphabet = "abcdefghijklmnopqrstuvwxyz"

input = sys.stdin.readline

########################################
n = int(input())
arr = list(map(int , input().split()))
s = sum(arr) - min(arr)
print(s)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
import math
import heapq
from collections import defaultdict, deque
from decimal import *
def r():
    return int(input())
def rm():
    return map(int,input().split())
def rl():
    return list(map(int,input().split()))

n=r()
a=rl()
a.sort(reverse=True)
print(sum(a[:-1]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n = int(input())
li = list(map(int,input().split()))
lia = sorted(li)
print(sum(lia)-lia[0])

Submission status: Wrong Answer

Solution code:
#!usr/bin/env python3
from collections import defaultdict, deque
from heapq import heappush, heappop
from itertools import permutations, accumulate
import sys
import math
import bisect
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def I(): return int(sys.stdin.readline())
def LS():return [list(x) for x in sys.stdin.readline().split()]
def S():
    res = list(sys.stdin.readline())
    if res[-1] == "\n":
        return res[:-1]
    return res
def IR(n):
    return [I() for i in range(n)]
def LIR(n):
    return [LI() for i in range(n)]
def SR(n):
    return [S() for i in range(n)]
def LSR(n):
    return [LS() for i in range(n)]

sys.setrecursionlimit(1000000)
mod = 1000000007

def solve():
    n = I()
    a = LI()
    a.sort(reverse=True)
    r = a[0]
    l = a[0]
    ans = 0
    for i in range(1,n-1):
        if l < r:
            ans += l
            l = a[i]
        else:
            ans += r
            r = a[i]
    ans += min(l,r)
    print(ans)
    return

#Solve
if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

print( sum( A[1:] ) )

Submission status: Wrong Answer

Solution code:
n=int(input())
l=sorted(list(map(int,input().split())),reverse=1)
ans=0
ri=l[0]
lf=l[0]

ans=0
for i in range(1,n):
    
    ans+=min(ri,lf)
    if i%2:ri=l[i]
    else:lf=l[i]

print(ans)

Submission status: Wrong Answer

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

A = sorted(A,reverse=True)
print(sum(A[:-1]))

Submission status: Wrong Answer

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

print(sum(arr)-min(arr))

Submission status: Wrong Answer

Solution code:
N=int(input())
print(N-1)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
n, *a = map(int, open(0).read().split())
print(sum(a) - min(a))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, gcd
from itertools import accumulate, permutations, combinations, product, groupby, combinations_with_replacement
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N = INT()
A = LIST()
print(sum(A)-min(A))

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(500005)
stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().strip()

n = ni()
a = na()
a.sort(reverse=True)
print(sum(a[0:-1]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
input=lambda: sys.stdin.readline().rstrip()
n=int(input())
A=[int(i) for i in input().split()]
print(sum(A)-min(A))

Submission status: Wrong Answer

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

print(sum(A[1:]))

Submission status: Wrong Answer

Solution code:
from math import ceil
N = int(input())
A = [int(i) for i in input().split()]
A.sort(reverse=True)
c = 0
for i in range((N+1)//2):
    c += A[i]*2
print(c - A[0] - A[(N+1)//2]*(N&1))

Submission status: Wrong Answer

Solution code:
N = int(input())
L = list(map(int, input().split()))
print(sum(L) - min(L))

Submission status: Wrong Answer

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

print(sum(sorted(A)[1:]))

Submission status: Wrong Answer

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

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

def main():
    mod=10**9+7
    N=I()
    A=LI()
    A.sort(reverse=True)
    ans=sum(A)-A[-1]-A[-2]+A[1]
    print(ans)

main()

Submission status: Wrong Answer

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

A=sorted(A, reverse=True)
print(sum(A[:-1]))

Submission status: Wrong Answer

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

friendly.sort()

ans = sum(friendly[1:])

print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

N=int(input())
A=list(map(int,input().split()))
print(sum(A)-min(A))

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
min1 = a[0]
min2 = 10 ** 9
total = a[0]
#for i in a[1:]:
#    if min1 >= min2:
#        min1 = i
#    else:
#        min2 = i
#    total += min(min1, min2)

print(sum(a[:N - 1]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
print(sum(a) - min(a))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
l.sort()
s=sum(l)
s-=l[0]
print(s)

Submission status: Wrong Answer

Solution code:
N,*A=map(int, open(0).read().split())
A.sort()
print(sum(A[1:]))

Submission status: Wrong Answer

Solution code:
import sys
import math
from bisect import bisect_left
def II():
	return int(sys.stdin.readline())

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

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

def SI():
	return sys.stdin.readline().strip()
n = II()
a = LI()
a.sort(reverse=True)
print(sum(a[:-1]))

Submission status: Wrong Answer

Solution code:
def main():
    n = int(input())
    inlis = list(map(int, input().split()))
    inlis.sort()
    print(sum(inlis)-inlis[0])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

A.sort()
ans=A[-1]+(N-2)*A[-2]
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
a = list(map(int, input().split()))
a.sort(reverse=True)
min1 = a[0]
min2 = 10 ** 9
total = a[0]
for i in a[1:]:
    if min1 >= min2:
        min1 = i
    else:
        min2 = i
    total += min(min1, min2)

print(total - a[-1])

Submission status: Wrong Answer

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]
print(ans)

Submission status: Wrong Answer

Solution code:
N=int(input())
A=[int(s) for s in input().split()]
A=sorted(A,reverse=True)
print(A[0]+A[1]*(N-2))

Submission status: Wrong Answer

Solution code:
N=int(input())
A=sorted(map(int,input().split()))
print(sum(A[1:]))

Submission status: Time Limit Exceeded

Solution code:
import math , sys 
from collections import deque

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

conf=0
#d=deque()
i=0
while len(Cs)>0:
    tar=Cs.pop(0)
    #print(tar,Cs)
    if i==0:
        conf+=tar
        #d.append(tar)
        i+=1
    elif i==1:
        #conf+=d[0]
        #d.append(tar)
        Ds=deque([tar,tar])
        i+=1
    else:
        #print(Ds,d)
        c=max(Ds)
        conf+=c
        ind=Ds.index(c)
        #d.insert(ind,tar)
        Ds.insert(ind,tar)
        Ds[ind+1]=tar
        #print(Ds,d)
print(conf)

Submission status: Time Limit Exceeded

Solution code:
import math , sys 
from collections import deque

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

conf=0
d=deque()
i=0
while len(Cs)>0:
    tar=Cs.pop(0)
    #print(tar,Cs)
    if i==0:
        conf+=tar
        d.append(tar)
        i+=1
    elif i==1:
        #conf+=d[0]
        #d.append(tar)
        Ds=deque([tar,tar])
        i+=1
    else:
        #print(Ds,d)
        c=max(Ds)
        conf+=c
        ind=Ds.index(c)
        #d.insert(ind,tar)
        Ds.insert(ind,tar)
        Ds[ind+1]=tar
        #print(Ds,d)
print(conf)

Submission status: Time Limit Exceeded

Solution code:
def f(n):
    global tmp
    ans = 0
    memo = -1
    for i in range(1,len(tmp)):
        if ans<min(tmp[i-1],tmp[i]):
            #print('min',tmp[i-1],tmp[i])
            ans=min(tmp[i-1],tmp[i])
            memo = i
    tmp.insert(i,n)
    #print(i,n,ans)
    return ans

n = int(input())
tmp_a = list(map(int,input().split()))
a = sorted(tmp_a,reverse=True)
#print(a)
tmp = []
tmp.append(a[0])
tmp.append(a[0])
kotae = 0
for i in range(1,len(a)):
    kotae += f(a[i])
    #print(tmp)
print(kotae)

Submission status: Time Limit Exceeded

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

a.sort(reverse=True)

arrival = 1
ans = 0

while arrival < N:
    score = a.pop(0)
    if arrival == 1:
        ans += score
        arrival += 1
    else:
        ans += score*min(2, N - arrival)
        arrival += 2

print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main() :
  def minp(list, len) :
    point = 0
    val = 10000000000
    for i in range((len-1)) :
      a = list[i]
      b = list[i+1]
      num = min(a, b)
      if num >= point and val >= a + b:
        point = num
        res = i + 1
        val = a + b
    return res, point
  
  

  n = int(input().strip())
  people = [int(i)for i in input().strip().split(' ')]
  people.sort(reverse=True)
  ring = [people[0]]*2 #python は .eachがないためfor i in で代用
  people.pop(0)
  result = 0
  ringlen = 2
  for person in people :
    res, point = minp(ring, ringlen)
    ring.insert(res, person)
    result += point
    ringlen += 1
  print(result)
    
main()

Submission status: Time Limit Exceeded

Solution code:
def main() :
  def minp(list, len) :
    point = 0
    val = (10**9)*2
    for i in range((len-1)) :
      num = min(list[i], list[i+1])
      if num >= point and val >= list[i] + list[i+1]:
        point = num
        res = i + 1
        val = list[i] + list[i+1]
    return res, point
  
  

  n = int(input().strip())
  people = [int(i)for i in input().strip().split(' ')]
  people.sort(reverse=True)
  ring = [people[0]]*2 #python は .eachがないためfor i in で代用
  people.pop(0)
  result = 0
  for person in people :
    res, point = minp(ring, len(ring))
    ring.insert(res, person)
    result += point
  print(result)
    
main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = sorted([int(x) for x in input().split()], reverse=True)


ans = 0
for i in range(N):
    if i == 0:
        B = [A[0]]
        continue
    index, mx = 0, 0
    for j in range(len(B)):
        tmp = min(B[j-1], B[j])
        if mx < tmp:
            mx = tmp
            index = j
    B.insert(index, A[i])
    ans += mx
print(ans)

Submission status: Time Limit Exceeded

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

sa = sorted(a,reverse=True)
ans = 0
ls = [sa[0]]
for v in sa[1:]:
  i = ls.index(max(ls))
  ans+= ls[i]
  new_min = min(ls[i], v)
  ls = ls[:i] + [new_min, new_min] + ls[i+1:]
  #print(ls)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
members = [int(i) for i in input().split()]


sol = []
members = list(reversed(sorted(members)))


sol.append(members.pop(0))
score = sol[0]
sol.append(members.pop(0))


while members != []:
    max_d = [0, -1]
    for i in range(len(sol)-1):
        tmp_min_i = 0
        if sol[i] < sol[i+1]:
            tmp_min_i = i
        else:
            tmp_min_i = i+1
        if sol[tmp_min_i] > max_d[1]:
            max_d[0] = tmp_min_i
            max_d[1] = sol[tmp_min_i]
    score += max_d[1]
    sol.insert(max_d[0], members.pop(0))

print(score)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
a = list(map(int, input().split()))
a.sort(reverse = True)
p=0
ppl = [a[0]]*2
best_index = 1
best_point = a[0]
for i in a[1:]:
    p += best_point
    ppl = ppl[:best_index]+[i]+ppl[best_index:]
    #print(ppl)
    point_list = [min(ppl[i],ppl[i+1]) for i in range(0,len(ppl)-1)]
    #print(point_list)
    best_point = max(point_list)
    for j,q  in enumerate(point_list):
        if q == best_point:
            best_index = j+1
            continue
            
print(p)

Submission status: Time Limit Exceeded

Solution code:
from numba import njit
N = int(input())
a = list(map(int, input().split()))

@njit
def fnc(N,a):
    a.sort(reverse = True)
    p=0
    ppl = [a[0]]*2
    best_index = 1
    best_point = a[0]
    for i in a[1:]:
        p += best_point
        ppl = ppl[:best_index]+[i]+ppl[best_index:]
        #print(ppl)
        point_list = [min(ppl[i],ppl[i+1]) for i in range(0,len(ppl)-1)]
        #print(point_list)
        best_point = max(point_list)
        for j,q  in enumerate(point_list):
            if q == best_point:
                best_index = j+1
                continue
    return p     

print(fnc(N,a))

Submission status: Time Limit Exceeded

Solution code:
import collections

N=int(input())
A=list(map(int,input().split()))
A=sorted(A,reverse=True)
c=collections.Counter(A)
x=2*c[A[0]]-1
if x>N-1:
    print(sum(A)-A[0])
    exit()
ans=A[0]*(2*c[A[0]]-1)
while x<N-1:
    A=A[c[A[0]]:]
    x+=2*c[A[0]]
    ans+=A[0]*2*c[A[0]]
y=x-N+1
ans-=y*A[0]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

N=int(input())
A=list(map(int,input().split()))
A=sorted(A,reverse=True)
c=collections.Counter(A)
x=2*c[A[0]]-1
if x>N-1:
    print(sum(A)-A[0])
    exit()
ans=A[0]*(2*c[A[0]]-1)
A=A[c[A[0]]:]
while x<N-1:
    x+=2*c[A[0]]
    ans+=A[0]*2*c[A[0]]
    if x>N-1:
        break
    A=A[c[A[0]]:]
y=x-N+1
ans-=y*A[0]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import collections

N=int(input())
A=list(map(int,input().split()))
A=sorted(A,reverse=True)
c=collections.Counter(A)
x=2*c[A[0]]-1
if x>N-1:
    print(sum(A)-A[0])
    exit()
ans=A[0]*(2*c[A[0]]-1)
A=A[c[A[0]]:]
while x<N-1:
    x+=2*c[A[0]]
    ans+=A[0]*2*c[A[0]]
    if x<N-1:
        A=A[c[A[0]]:]
    else:
        y=x-N+1
        ans-=y*A[0]
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

nums = []
nums = list(map(int, input().split()))
nums = sorted(nums, reverse=True)

cnt = 0

fir_num = nums[0]

del nums[0]

for i in range(n-2):
    fir_num += nums[0]
    cnt += 1
    if cnt == 2:
        del nums[0]
        cnt = 0

print(fir_num)

Submission status: Time Limit Exceeded

Solution code:
import bisect
import statistics
n = int(input())
a = list((int(x) for x in input().split()))

a.sort()
incl = []
score = 0

for i in range(len(a)):
    ain = a.pop()
    incl.append(ain)
    if i==0: continue
    if (len(incl)%2 == 1):
        score += statistics.median(incl)
    else:
        # smaller of middle two
        score += incl[ int(len(incl)/2)-1 ]

print(score)

Submission status: Time Limit Exceeded

Solution code:
#D - Chat in a Circle
N = int(input())
A = list(map(int,input().split()))
#ソート
#B = []
arr = A.copy()
left = 0
right = N -1
#B = sorted(A,reverse = True)

def quicksort(arr,left,right):
    index = partition(arr,left,right)
    if left < index- 1 :
        quicksort(arr,left,index - 1)
    if index < right:
        quicksort(arr,index,right)

def partition(arr,left,right):
    pivot = arr[int((left + right) / 2)]
    while left <= right:
        while arr[left]  > pivot:
            left  += 1
        while arr[right] < pivot:
            right -= 1
        if left <= right:
             arr[left],arr[right] = arr[right],arr[left]
             left += 1
             right -= 1
    return left

quicksort(arr,left,right)

Chat = []
Friend_min = 0
Friend_min_max = 0
Friend_min_max_wa = 0
max_no = 0
i =0
Chat.append(arr[0])
for i in range(N):
    for j in range(i):
        if (Chat[j] > Chat[j+1]):
            Friend_min = Chat[j+1]
        else:
            Friend_min = Chat[j]
        if Friend_min >= Friend_min_max:
            Friend_min_max = Friend_min
            max_no = j + 1
        Friend_min = 0
    Chat.insert(max_no,arr[i])
    Friend_min_max_wa = Friend_min_max_wa + Friend_min_max
    Friend_min_max = 0
    max_no = 0
#    print(Chat)    
# 出力
print(Friend_min_max_wa)

Submission status: Time Limit Exceeded

Solution code:
#D - Chat in a Circle
N = int(input())
A = list(map(int,input().split()))
#ソート
B = []
B = sorted(A,reverse = True)
Chat = []
Friend_min = 0
Friend_min_max = 0
Friend_min_max_wa = 0
max_no = 0
i =0
Chat.append(B[0])
for i in range(N):
    for j in range(i):
        if (Chat[j] > Chat[j+1]):
            Friend_min = Chat[j+1]
        else:
            Friend_min = Chat[j]
        if Friend_min >= Friend_min_max:
            Friend_min_max = Friend_min
            max_no = j + 1
        Friend_min = 0
    Chat.insert(max_no,B[i])
    Friend_min_max_wa = Friend_min_max_wa + Friend_min_max
    Friend_min_max = 0
    max_no = 0
#    print(Chat)    
# 出力
print(Friend_min_max_wa)

Submission status: Time Limit Exceeded

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

for i in range(n):
    for j in range(n - i - 1):
        if a[j] > a[j + 1]:
            a[j],a[j+1] = a[j+1],a[j]

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = input()
xs = [ int(x) for x in input().split() ]
xs.sort(reverse=True)
best, buf = 0, [xs[0]]
for x in xs[1:]:
  y, i = max([ (min(buf[i-1], y), i) for i,y in enumerate(buf) ])
  best += y
  buf.insert(i, x)
print(best)

Submission status: Time Limit Exceeded

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

for i in range(n-1):
    m = a.pop()
    now = 0
    ind = 0
    for j in range(len(l)):
        b = min(l[j], l[(j+1)%len(l)])
        if b > now:
            now = b
            ind = j
    l.insert(ind+1, m)
    ans += now
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
ans = A[0]
S = [A[0], A[1]]
for i in range(2, len(A)):
    m = min(A[0], A[-1])
    cnt = -1
    for j in range(len(A)-1):
        if min(A[j], A[j+1]) > m:
            m = min(A[j], A[j+1])
            cnt = j
    S = S[:cnt] + [A[i]] + S[cnt:]
    ans += m
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = sorted(list(map(int, input().split())), reverse=True)
sum_com = a[0]
comfort = [a[1], a[1]]

for ai in a[2:]:
    sum_com += comfort[0]
    if comfort[0] == ai:
        comfort.append(ai)
    else:
        comfort.pop(0)
        comfort += [ai, ai]

print(sum_com)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split(" ")))
C = []
P = 0
for a in reversed(sorted(A)):
  #print("len(C):",len(C),"a:",a,"P:",P)
  if len(C) == 0:
    C.append(a)
    P += 0
  elif len(C) == 1:
    C.append(a)
    P += max(C)
  else:
    place = 0
    point = 0
    for i in range(len(C)):
      if min(C[i-1],C[i])>point:
        place = i
        point = min(C[i-1],C[i])
    else:
      C.insert(place,a)
      P += min(C[place-1],C[place+1])
else:
  print(P)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int,input().split()))
C = []
P = 0
for a in reversed(sorted(A)):
  #print("len(C):",len(C),"a:",a,"P:",P)
  if len(C) == 0:
    C.append(a)
    P += 0
  elif len(C) == 1:
    C.append(a)
    P += max(C)
  else:
    place = 0
    point = 0
    for i in range(len(C)):
      if min(C[i-1],C[i])>point:
        place = i
        point = min(C[i-1],C[i])
    else:
      C.insert(place,a)
      P += min(C[place-1],C[place+1])
else:
  print(P)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = sorted(list(map(int, input().split())),reverse=True)
score = [A[0]]
s = 0

for a in A[1:N]:
    s += score[0]
    score = score[1:] + [a,a]

print(s)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = sorted(list(map(int, input().split())),reverse=True)
score = [A[0]]
s = 0

for i in range(1,N):
    s += score[0]
    score = score[1:] + [A[i],A[i]]

print(s)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = sorted(list(map(int, input().split())),reverse=True)
ring = [A[0]]
score = [A[0]] #ringのi-1とiの間に割り込んだ時に入るフレンドリーさ
s = 0

for i in range(1,N):
    s += max(score)
    j = score.index(max(score))
    ring.insert(j,A[i])
    score.insert(j,A[i])
    score[j+1] = A[i]
    # print("ring")
    # print(ring)
    # print(score)
    # print(s)

print(s)

Submission status: Time Limit Exceeded

Solution code:
num=int(input().rstrip())
m=input().rstrip().split()
main=[]
for i in m:
    main.append(int(i))
sample=sorted(main,reverse=True)
new=[]
plusold=0
result=0
for i in range(num):
    if len(new)==0:
        new.append(sample[i])
    elif len(new)<=2:
        new.append(sample[i])
        result+=sample[i-1]
    else:
        plus=new[len(new)-1]
        point=len(new)
        for j in range(len(new)-1):
            if plus<new[j] and plus<new[j+1]:
                plus=min([new[j],new[j+1]])
                point=j+1
                if plus==plusold:
                    break
        result+=plus
        plusold=plus
        if point==len(new):
            new.append(sample[i])
        else:
            new.insert(point,sample[i])
print(result)

Submission status: Time Limit Exceeded

Solution code:
num=int(input().rstrip())
m=input().rstrip().split()
main=[]
for i in m:
    main.append(int(i))
sample=sorted(main,reverse=True)
new=[]
result=0
for i in range(num):
    if len(new)==0:
        new.append(sample[i])
    elif len(new)<=2:
        new.append(sample[i])
        result+=sample[i-1]
    else:
        plus=new[len(new)-1]
        point=len(new)
        for j in range(len(new)-1):
            if plus<new[j] and plus<new[j+1]:
                plus=min([new[j],new[j+1]])
                point=j+1
        result+=plus
        if point==len(new):
            new.append(sample[i])
        else:
            new.insert(point,sample[i])
print(result)

Submission status: Time Limit Exceeded

Solution code:
num=int(input().rstrip())
m=input().rstrip().split()
main=[]
for i in m:
    main.append(int(i))
sample=sorted(main,reverse=True)
new=[]
result=0
for i in range(num):
    if len(new)==0:
        new.append(sample[i])
    elif len(new)<=2:
        new.append(sample[i])
        result+=sample[i-1]
    else:
        plus=new[len(new)-1]
        point=len(new)
        for j in range(len(new)-1):
            if plus<new[j] and plus<new[j+1]:
                plus=min([new[j],new[j+1]])
                point=j+1
        result+=plus
        if point==len(new):
            new.append(sample[i])
        elif point==len(new)-1:
            new.append(new[len(new)-1])
            new[len(new)-1]=sample[i]
        else:
            new.append(new[len(new)-1])
            for k in range(len(new)-1,point,-1):
                new[k]=new[k-1]
            new[point]=sample[i]
print(result)

Submission status: Time Limit Exceeded

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

tmp = a.pop()
tmp_a.append(tmp)
tmp_a.append(tmp)

ans = 0
#print(n-1,a)
#print(n-1,tmp_a)
for i in range(n-2,-1,-1):
    ans_max = 0
    for j in range(0,len(tmp_a)-1):
        if ans_max < min(tmp_a[j],tmp_a[j+1]):
            ans_j = j
            ans_max = min(tmp_a[j],tmp_a[j+1])
    ans += ans_max
    tmp = a.pop()
    tmp_a.insert(ans_j+1,tmp)
    #print(i,a)
    #print(i,tmp_a)

print(ans)

Submission status: Time Limit Exceeded

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

cnt=0
ans=[]
ans.append(A[0])
ans.append(A[1])
cnt+=ans[0]
for i in range(2,N):
    k=ans.index(A[int((i-1)/2)])
    if k==len(ans)-1:
        kb=k-1
        kn=0
    else:
        kb=k-1
        kn=k+1

    if ans[kb]>=ans[kn]:
        cnt+=min(ans[k],ans[kb])
        ans.insert(kb,A[i])
    elif ans[kb]<ans[kn]:
        cnt+=min(ans[k],ans[kn])
        ans.insert(kn,A[i])

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

A = []
a, *an = map(int, input().split())
A.append(a)
A += an
A.sort()

Node = []
score = 0
Node.append(A.pop())

#print(A)

for n in range(N - 1):
    score += Node[0]
    num = A.pop()
    Node = Node[1:]
    Node.append(num)
    Node.append(num)

#print(Node)
print(score)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

A = []
a, *an = map(int, input().split())
A.append(a)
A += an
A.sort()

Node = []
score = 0
Node.append(A.pop())

#print(A)

for n in range(N - 1):
    score += Node.pop(0)
    num = A.pop()
    Node.append(num)
    Node.append(num)

#print(Node)
print(score)

Submission status: Time Limit Exceeded

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

circle = []
score = [0]
ans = 0


def check():
    global score
    score = []
    l = len(circle)
    if l == 0:
        score = [0]
        return
    for i in range(l):
        if i == l - 1:
            score.append(min(circle[i], circle[0]))
            continue
        score.append(min(circle[i], circle[i + 1]))


A.sort(reverse=True)

for a in A:
    max_score = max(score)
    circle.insert(score.index(max_score), a)
    ans += max_score
    check()

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N=int(input())
A=list(map(int,input().split()))
L=[(i,A[i]) for i in range(N)]
B=sorted(L,key=lambda x: x[1],reverse=True)
S=0
i=0
for j in range(1,N):
    if j%2==1:
        S+=B[0][1]
        del B[0]
    else:
        S+=B[0][1]
print(S)

Submission status: Time Limit Exceeded

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

    def feelGood(table: list):
        pos = value = 0
        if len(table) == 0:
            return pos, value
        circle = table + [table[0]]
        for i in range(len(circle)-1):
            v = min(circle[i], circle[i+1])
            if (v > value):
                pos = i
                value = v
        return pos, value

    A.sort(reverse=True)
    table = []
    total = 0

    for i in A:
        pos, value = feelGood(table)
        table.insert(pos, i)
        total += value
    print(total)

if __name__ == "__main__":
    resolve()