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:
from collections import Counter
import heapq
N = int(input())
A = sorted(list(map(int, input().split())))
max3 = heapq.nlargest(3, A)
d = Counter(A)
if d[max3[1]] == 1:
  ans = sum(A) + max3[1] - max3[2] - A[0]
else:
  ans = sum(A) - A[0]
print(ans)

Submission status: Runtime Error

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

cf = nums[0]

for i in range(2, n):
    nums_min = min(nums[i-1], nums[i-2])
    cf += nums_min

print(cf)

Submission status: Runtime Error

Solution code:
N=int(input())
a_list=list(map(int,input().split()))
a=a_list.count(max(a_list))
ans=sorted(set(a_list))[-1]*(2*a-1)
count=2*a-1
#print(ans,count)
for i in range(1,N):
    if count<N:
        A=sorted(set(a_list))[-i-1]
        B=a_list.count(A)
        #print(A,B)
        ans+=A*B*2
        count+=B*2
        #print(ans,count)
    else:
        ans+=(N-count-1)*A
        #print(i)
        break
    
print(ans)

Submission status: Runtime Error

Solution code:
#include <bits/stdc++.h>
using namespace std ;

int main()
{
  vector<int> ans ;
  int n,count,cnt ;
  count = 0 ;
  cnt = 1 ;
  cin >> n ;
  int A[n] ;
  for(int i = 0 ; i < n ; i++) cin >> A[i] ;
  sort(A, A + n, greater<int>()) ;
  ans.push_back(A[0]) ;
  for(int i = 1 ; i < n ; i++){
    if(A[i] == A[i-1]) count = count + A[i] ;
    else{
      ans.push_back(A[i]) ;
      cnt = cnt + 1 ;
    }
  }
  if(cnt % 2 == 1 && cnt != 1){
    cnt = cnt + 1 ;
    for(int i = 0 ; i < cnt/2 ; i++){
      if(i == 0 || i == cnt/2 - 1) count = count + ans[i] ;
      else count = count + ans[i] * 2 ;
    }
  }
  cout << count << endl ;
}

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(i) for i in input().split()]
A.sort(reverse = True)
B = A[:]
sumOfA = [0] * (N - 1)
for i in A[1:N]:
    sumOfA[i-1] = B[0]
    if B[0] != A[i]:
        B.pop(0)
else:
    print(sum(sumOfA))

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(i) for i in input().split()]

A.sort(reverse=True)

s = [A[0]]
s.append(A[1])

def make_conf_list(l):
    tmp = [ min(l[i],l[i+1]) for i in range(len(l)) if i < len(l) -1] 
    tmp.append(min(l[len(l) -1], l[0]))
    return tmp

ans = A[0]
tmp = make_conf_list(s)

for i in range(2,len(A)):
    idx = tmp.index(max(tmp))
    ans += max(tmp)

    tmp2 = min(A[i], s[idx])
    tmp3 = min(A[i], s[idx+1])
    s.insert(idx+1,A[i])
    tmp[idx] = tmp2
    tmp.insert(idx+1,tmp2)
    
print(ans)

Submission status: Runtime Error

Solution code:


def resolve():
    import sys
    input = sys.stdin.readline
    n = int(input().rstrip())
    a_list = [int(x) for x in input().rstrip().split(" ")]
    a_list.sort(reverse=True)
        
    from heapq import heappush, heappop

    hq = []
    joined = set()
    joined.add(0)
    heappush(hq, (-a_list[0], 0, 0))
    result = 0
            
    for i in range(1, n):
        next_one = a_list[i]
        comfort, left, right = heappop(hq)
        comfort = -comfort

        result += comfort

        heappush(hq, (-(a_list[left] if a_list[left] < a_list[next_one] else a_list[next_one]),left, next_one))
        heappush(hq, (-(a_list[right] if a_list[right] < a_list[next_one] else a_list[next_one]),next_one,right))
    
    print(result)


if __name__ == "__main__":
    resolve()


# を理解する。そうすると、明らかに制限時間内に実行が終わらないソースコードを出さなくなる。ちなみに、競プロでは 108108 ～ 109109 回程度のループが回せるといわれている。

Submission status: Runtime Error

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

A.sort(reverse = True)
B = A[:]
sumOfA = [0] * (N - 1)
for i in A[1:N]:
    sumOfA[i-1] = B[0]
    if B[0] != A[i]:
        B.pop(0)
else:
    print(sum(sumOfA))

Submission status: Runtime Error

Solution code:
import sys
import collections
import math
import sympy

def main():

    input = sys.stdin.readline

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

    ans = A[0]

    count = 2

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

        if count >= N:
            break

        count += 1
        ans += A[i]

        if count >= N:
            break

    print(ans)

if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
N = int(input())
A_list = list(map(int,input().split()))
A_list.sort(reverse = True)
indx_list = []
for i in range(1,100000,2):
    indx_list.append(i)
insx_list = []
for j in range(0,18):
    insx_list.extend(indx_list[:2 ** j])
mx_cmfrt = 0
reached_list = [A_list[0],A_list[0]]
for j in range(1, len(A_list)):
    cmfrt = min(reached_list[j-1],reached_list[j])
    reached_list.insert(insx_list[j], A_list[j])
    mx_cmfrt += cmfrt
    cmfrt = 0
print(mx_cmfrt)

Submission status: Runtime Error

Solution code:
N = int(input())
w = list(map(int, input().split()))
#print(N,w)
wa = sorted(w, reverse = True)
#print(wa)
ans = []
re = wa[0] + wa[1]
for i in range(3):
  ans.append(wa.pop(0))
#print(ans,wa)
for i in range(len(wa)):
  mina = 0
  maxa = max(ans)+1
  for j in range(-1,len(ans)-1):
    if mina <= min(ans[j],ans[j+1]) and maxa > max(ans[j],ans[j+1]):
      x = j+1
      mina = min(ans[j],ans[j+1])
      maxa = max(ans[j],ans[j+1])  
  #print(ans,x,max,re)
  ans.insert(x,wa.pop(0))
  re += mina
print(re)

Submission status: Runtime Error

Solution code:
from collections import Counter 
n=int(input())
a=sorted(list(map(int,input().split())))
b=[]
c=[]
for i in list(Counter(a).values()):
  b.append(i)
for i in list(Counter(a).keys()):
  c.append(i)
ans=c.pop()*((b[-1])-1)
m=a[-1]
f=len(b)
for _ in range(f-2,-1,-1):
  if b[-1]>b[i]:
    b[-1]-=b[i]
    b.pop(i)
    c.pop(i)    
    ans+=b[i]*m
  else:
    ans+=m*b[-1]
    b[i]-=b[-1]
    m=c.pop()
    ans+=m*b[i]
    b.pop()
print(ans)

Submission status: Runtime Error

Solution code:
from collections import Counter 
n=int(input())
a=sorted(list(map(int,input().split())))
b=[]
c=[]
for i in list(Counter(a).values()):
  b.append(i)
for i in list(Counter(a).keys()):
  c.append(i)
ans=c.pop()*((b[-1])-1)
m=a[-1]
f=len(b)
for _ in range(f-2,-1,-1):
  i=-2
  if b[-1]>b[i]:
    b[-1]-=b[i]
    b.pop(i)
    c.pop(i)    
    ans+=b[i]*m
  else:
    ans+=m*b[-1]
    b[i]-=b[-1]
    m=c.pop()
    ans+=m*b[i]
    b.pop()
print(ans)

Submission status: Runtime Error

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

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

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

print(sum)

Submission status: Runtime Error

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

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

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

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

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

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



    print(ans)
    # comfort

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
n = int(input())
A = sorted(map(int, input().split()))
if n == 2:
    print(A[-1])
if n == 3:
    print(A[-1]+A[-2])
else:
    tmp = n/3
    count = 0
    while True:
        count +=1
        tmp//=2
        if tmp == 0:
            break
    if count == 1:
        if n == 4:
            print(A[-1]+2*A[-2])
        elif n == 5:
            print(A[-1] + 2*A[-2] + A[-3])
        else:
            print(A[-1] + 2*A[-2] + A[-3]*2)
    else:
        ans = 0
        mo = 0
        las = n-3*2**(count-1)
        while True:
            if las <=2:
                ans += A[-(3*2**(count-2)+mo)]*las
                break
            else:
                ans += A[-(3*2**(count-2)+mo)]*2
                mo +=1
                las -=2
        for i in range(1,3*2**(count-2)+1):
            ans += 2*A[-i]
        print(ans-A[-1])

Submission status: Runtime Error

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

Submission status: Runtime Error

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

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

Submission status: Runtime Error

Solution code:
n = int(input())
 
arr = list(map(int,input().split()))
arr.sort()
print(reduce(lambda x,y:x+y,arr[1:]))

Submission status: Runtime Error

Solution code:
n = int(input())

arr = list(map(int,input().split()))
arr.sort()
print(reduce(return a+b,arr[1:]))

Submission status: Runtime Error

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

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

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

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

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

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



    print(ans)
    # comfort

if __name__ == '__main__':
    main()

Submission status: 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:
num = int(input())
a_i = list(map(int, input().split()))
list.sort(a_i, reverse=True)
if num <= 3:
    print(sum(a_i[:num - 1]))
else:
    ans = sum(a_i[:2])
    for x in range(3, num):
        if x == 3: ans += a_i[1]
        else: ans += a_i[x // 2]
print(ans)

Submission status: Runtime Error

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

A=sorted(A, reverse=True)

circle=[A.pop(0),A.pop(0),A.pop(0)]
score=circle[0]+circle[1]

for a in A:
    frs=0
    index=0
    for i in range(len(circle)):
        _frs=min(circle[i],circle[(i+1)%len(circle)])
        if _frs>frs:
            index=i
            frs=_frs
    score+=frs
    circle.insert(index+1,a)

print(score)

Submission status: Runtime Error

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

A=sorted(A, reverse=True)

circle=[A.pop(0),A.pop(0),A.pop(0)]
score=circle[0]+circle[1]

for a in A:
    frs=0
    index=0
    for i in range(len(circle)):
        _frs=min(circle[i],circle[(i+1)%len(circle)])
        if _frs>frs:
            index=i
            frs=_frs
    score+=frs
    circle=circle.insert(index+1,a)

print(score)

Submission status: Runtime Error

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

A=sorted(A, reverse=True)

circle=[A.pop(0),A.pop(0),A.pop(0)]
score=circle[0]+circle[1]

for a in A:
    frs=0
    index=0
    for i in range(len(circle)):
        _frs=min(circle[i],circle[(i+1)%len(circle)])
        if _frs>frs:
            index=i
            frs=_frs
    score+=frs
    circle=circle[:index+1]+[a]+circle[index+1:]

print(score)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

read = sys.stdin.read

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

Submission status: Runtime Error

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

def cmax(list,x):
    if x==2:
        return list[0]
    else:
        if n%2==0:
            p=n//2
        else:
            p=n//2+1
        
        return cmax(list, x-1)+list[p]
        
        
print(cmax(c2,n))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = int(input())
p_list = list(map(int, input()split(" ")))
print(p_list)

Submission status: Runtime Error

Solution code:
# Problem D - Chat in a Circle

# input
N = int(input())
a_nums = list(map(int, input().split()))

# initialization
a_nums = sorted(a_nums, reverse=True) # sort
a_max = max(a_nums)
a_count_list = [0]*(a_max + 1)
a_count_list[a_nums[0]] += 1
point = 0
max_i = a_nums[0]

# count
for i in range(1, N):
    a = a_nums[i]
    while max_i>0 and a_count_list[max_i]==0:
        max_i -= 1
    a_count_list[max_i] -= 1
    point += max_i # add point
    a_count_list[a] += 2

# output
print(point)

Submission status: Runtime Error

Solution code:
N = int(input())
w = list(map(int, input().split()))
#print(N,w)
wa = sorted(w, reverse = True)
#print(wa)
ans = []
re = wa[0] + wa[1]
for i in range(3):
  ans.append(wa.pop(0))
#print(ans,wa)
for i in range(len(wa)):
  max = 0
  for j in range(-1,len(ans)-1):
    if max < min(ans[j],ans[j+1]):
      x = j+1
      max = min(ans[j],ans[j+1])
  #print(ans,x,max,re)
  ans.insert(x,wa.pop(0))
  re += max
print(re)

Submission status: Runtime Error

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

A.sort(reverse = True)
B = A[:]

sumOfA = []
for i in A[1:N]:
    sumOfA.append(B[0])
    if B[0] != A[i]:
        B.pop(0)
else:
    print(sum(sumOfA))

Submission status: Runtime Error

Solution code:
N = int(input())
A = [int(N) for N in input().split()]
A1 = sorted(A)
if N%2!=0:
  x = sum(A1)-A1[0]
  print(x)
else:
  y = sum(A1)-A1[0]-A1[1]+A1[2]
  print(y)

Submission status: Runtime Error

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

ans = A[0]
cnt = 1
n = N - 2

while(1):
    
    ans += A[cnt]
    n -= 1
    if n == 0:  break
        
    ans += A[cnt]
    n -= 1
    if n == 0:  break
        
    cnt += 1
    
print(ans)

Submission status: Runtime Error

Solution code:
n = int(input())
a_li = list(map(int,input().split()))
a_li.sort(reverse=1)
if n == 2:
    print(a_li[0])
if n == 3:
    print(a_li[0]+a_li[1])

m = a_li[0]
l = a_li[1]
r = a_li[2]
ans = 0
ans += a_li[0]+a_li[1]
for i in range(3,n):
    if l < r:
        ans += l
        l = a_li[i]
    else:
        ans += r
        r = a_li[i]
print(ans)

Submission status: Runtime Error

Solution code:
from math import floor,ceil,sqrt
import fractions
from collections import deque
import itertools
from collections import deque
import collections
from heapq import *
import json
l=[]
N=int(input())
A=list(map(int,input().split()))
c=collections.Counter(A)
values=list(c.values())	#aのCollectionのvalue値のリスト(n_1こ、n_2こ…)
key=list(c.keys())	#先のvalue値に相当する要素のリスト(要素1,要素2,…)
for i in range(len(key)):
    l.append([key[i],values[i]])#lは[要素i,n_i]の情報を詰めたmatrix
l.sort(key=lambda x:x[0],reverse=True)
ans=0
num=N-1
for i in range(len(l)):
    if i==0:
        ans=ans+l[i][0]*min(2*l[i][1]-1,num)
        num=num-min(2*l[i][1]-1,num)
    else:
        ans=ans+l[i][0]*min(2*l[i][1],num)
        num=num-xmin(2*l[i][1],num)
print(ans)

Submission status: Runtime Error

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

place=[A_list.pop(0),A_list.pop(1)]
comfort=place[0]
edge=[place[1],place[1]]
for i in range(len(A_list)):
    now=A_list.pop(0)
    ins=edge.index(max(edge))
    comfort+=max(edge)
    place.insert(ins,now)
    
    edge[ins]=min(place[ins],now)
    edge.insert(ins+1,min(place[ins+1],now))
print(comfort)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
A.sort(reverse = True)
B = A[:]
sumOfA = []
for i in A[1:]:
    sumOfA.append(B[0])
    if B[0] != A[i]:
        B.pop(0)
else:
    print(sum(sumOfA))

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N = int(input())
A = []
for i in range(N):
    A.append(int(input()))
print(sum(A)-min(A))

Submission status: Runtime Error

Solution code:
n = int(input())
friendliness = [int(s) for s in input().split()]

res = friendliness[0] + 2 * sum(friendliness[1:n//2+1]) + (n%2) * friendliness[n//2+1]

Submission status: Runtime Error

Solution code:
# cd ~/Desktop/python_lesson
# python script.py
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
b=a[0]
a=a[1:n-1]
print(a)
for i in range(0,n-3):
    b+=min(a[i],a[i+1])
b+=a[n-3]
print(b)

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int,input().split()))
A.sort(reverse = True)
B = A[:]
sumOfA = []
for i in A[1:]:
    sumOfA.append(B[0])
    if B[0] != A[i]:
        B.pop(0)
print(sum(sumOfA))

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
import heapq
N=int(input())
A=list(map(int,input().split()))
A.sort(reverse=True)
count=0
Q=[]
heapq.heapify(Q) 
heapq.heappush(Q, (-A[0],-A[0],0))
for i in range(1,N):
    B=heapq.heappop(Q)
    count+=-B[0]
    heapq.heappush(Q, (-A[1],-A[i],B[1]))
    heapq.heappush(Q, (-A[2],-A[i],B[2]))
print(count)

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
#include <iostream>

using namespace std;

int main() {
	int N;
	cin >> N;

	long long min = LLONG_MAX;
	long long sum = 0;

	for (int i = 0; i < N; i++) {
		long long a;
		cin >> a;
		sum += a;
		min = min > a ? a : min;
	}

	cout << sum - min << endl;

	return 0;
}

Submission status: Runtime Error

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


def solve(N: int, A: "List[int]"):
    A = sorted(A, reverse=True)

    i = 0
    while(pow(2,i)<N): i+=1
    # print(A)

    rem = N-2**(i-1)
    if rem%2 == 1: od = 1
    else: od = 0
    # print(A[:2**(i-2)])
    ans = sum(A[:2**(i-2)])*2 - A[0]
    # print(ans)
    # print(A[2**(i-2):2**(i-2)+rem//2])
    ans+= sum(A[2**(i-2):2**(i-2)+rem//2])*2
    # print(ans)

    if od:
        ans+= A[2**(i-2)+rem//2]
    print(ans)

    return


# Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    A = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, A)

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import random
import copy

N = int(input())
A = list(map(int,input().split()))
list.sort(A, reverse = True)
B = copy.copy(A)
sumOfA = []
for i in A[1:]:
    sumOfA.append(B[0])
    if B[0] != A[i]:
        B.pop(0)
print(sum(sumOfA))

Submission status: Runtime Error

Solution code:
n = int(input())
liste = []
circle = []
comfort = 0
for i in range(n):
  person = int(input())
  liste.append(person)
circle.append(max(liste))
liste.remove(max(liste))
while len(liste) > 0:
  if circle[0] > max(liste):    
    circle.insert(0, max(liste))
    liste.remove(max(liste))
    comfort += min(circle[-1], circle[1])
  else:    
    circle.append(max(liste))
    liste.remove(max(liste))
    comfort += min(circle[0], circle[-2])
print(comfort)

Submission status: Runtime Error

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

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
n = int(input())
lst = list(map(int,input().split()))
 
lst.sort()
in_list = lst[n-1]
sum = 0
lst = lst[:-1]
for i in range(n-1):
  sum += in_list
  if lst[n-i-1] < in_list:
    in_list = lst[len(lst)-1]
  
print(sum)

Submission status: Runtime Error

Solution code:
n = int(input())
liste = []
circle = []
comfort = 0
for i in range(n):
  person = int(input())
  liste.append(person)
circle.append(max(liste))
liste.remove(max(liste))
while len(liste) > 0:
    if circle[0] > max(liste):    
        circle.insert(0, max(liste))
        liste.remove(max(liste))
        comfort += min(circle[-1], circle[1])
    else:    
        circle.append(max(liste))
        liste.remove(max(liste))
        comfort += min(circle[0], circle[-2])
print(comfort)