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:
N = int(input())
A = list(map(int,input().split()))
l = len(A)
A.sort()
print(A[-1] + A[-2] * (l-2))

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import heapq as hq

n = int(input())
a = list(map(lambda x: 0 - int(x), input().split()))
a.sort()

rst = 0
circle = []
hq.heappush(circle, a[0])
for i in a[1:]:
    k = hq.heappop(circle)
    rst += k
    if k < i:
        hq.heappush(circle, i)
    else:
        # k == i
        hq.heappush(circle, i)
        hq.heappush(circle, k)

print(-rst)

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:
import heapq
from collections import deque
import sys
input = sys.stdin.readline
# sys.setrecursionlimit(100000)


def main():
    _ = int(input().strip())
    A_l = [-int(i) for i in input().strip().split()]
    heapq.heapify(A_l)

    used = deque()
    ans = 0
    # 初回と２回目は固定行動
    m1 = -1 * heapq.heappop(A_l)
    used.append(m1)
    m2 = -1 * heapq.heappop(A_l)
    used.append(m2)
    used.append(m2)
    ans += used.popleft()

    while A_l:
        m = -1 * heapq.heappop(A_l)
        used.append(m)
        used.append(m)
        ans += used.popleft()
    return ans

Submission status: Wrong Answer

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort()
su=a[-1]
d=1
for i in range(n-2,-1,-1):
    su+=2*a[i]
    d+=2
    if d==n-1:
        break
    elif d>n-1:
        su-=a[i]
        break
print(su)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
from bisect import bisect_left
from bisect import bisect_right
from collections import defaultdict
from heapq import heappop, heappush
import itertools
import random
from collections import deque
from decimal import *
import queue

input = sys.stdin.readline

def inputInt(): return int(input())
def inputMap(): return map(int, input().split())
def inputList(): return list(map(int, input().split()))
def inputStr(): return input()[:-1]

inf = float('inf')
mod = 1000000007

#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-

def main():
	N = inputInt()
	A = inputList()

	A.sort()
	A = A[::-1]
	ans = 0
	maxs = 0
	cnt = 0
	fst = True

	ansans = []
	pgt = queue.Queue()
	for i,val in enumerate(A):
		if i == 0:
			pgt.put(val)
		else:
			tmp = pgt.get()
			ansans.append(tmp)

			pgt.put(val)

			if tmp == val:
				pgt.put(val)
			else:
				pgt.put(val)

	print(ansans)
	print(sum(ansans))


#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
#-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-
if __name__ == "__main__":
	main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n=int(input())
l=list(map(int,input().split()))
l.sort()
prev=l[0]
ans=0
for i in range(1,n):
  ans+=prev
  prev=min(prev,l[i])
print(ans)

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
kokoti=0
A.sort(reverse=True)
print(A)
for i in range(1,N-1):
    if(i==1):
        kokoti+=A[0]
        print(kokoti,A[0],1)
    elif(i==N-1):
        kokoti+=A[i-1]
        print(kokoti,A[i-1],2)
    if(A[i-1]<=A[N-i-2]):
        kokoti+=A[i-1]
        print(kokoti,A[i-1],3)
    elif(A[i-1]>=A[N-i-2]):
        kokoti+=A[N-i-2]
        print(kokoti,A[N-i-2],4)
print(kokoti)

Submission status: Wrong Answer

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:
        A=sorted(set(a_list))[-i]
        ans+=(N-count-1)*A
        #print(i)
        break
    
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())
l = list(map(int,input().split()))
ans = 0
l2 = sorted(l)
l2.pop(0)
print(l2)
for i in l2:
  ans += i
print(ans)

Submission status: Wrong Answer

Solution code:
n = int(input())

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

A.sort(reverse = True)

tot = A[0]
cnt = 2

for i in range(1,n):
    if n >= cnt+2:
        tot +=A[i]*2
        cnt += 2
        
if n >= cnt + 1:
    tot += A[i]
    cnt += 1
        
print(tot)

Submission status: Wrong Answer

Solution code:
def i():
	return int(input())
def i2():
	return map(int,input().split())
def s():
	return str(input())
def l():
	return list(input())
def intl():
	return list(int(k) for k in input().split())

n = i()
a = intl()
a.sort(reverse = True)

ans = 0
memo = a[0]
cnta = 0
cntb = 0

for i in range(1,n):
    if a[i-1] == a[i]:
        cntb += 1
    else:
        cnta = cntb
        cntb = 0
    if cnta >= cntb:
        ans += memo
    elif cnta < cntb:
        memo = a[i]
        ans += memo

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq

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

comfort = A[0]
edges = [A[1], A[1]]

for ai in A[2:]:
    m = edges.pop(-1)
    comfort += m
    heapq.heappush(edges, ai)
    heapq.heappush(edges, ai)
print(comfort)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = [int (x) for x in input().split()]
comfort = 0
B = 0
#数字がでかい順に到着させる
B = sorted(A)
comfort = B[-1]

for x in reversed(range(len(sorted(B)))):
    if x == 1:
        break

    #print(x)
    #print(str(B[x]) + ' ' +str(B[x-1]))
    if B[x] >= B[x-1]:
        print(str(B[x]) + ' ' + str(B[x - 1]))
        comfort += B[x-1]
    elif B[x] <= B[x-1]:
        print(str(B[x]) + ' ' + str(B[x - 1]))
        comfort += B[x]

print(comfort)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

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

Submission status: Wrong Answer

Solution code:
from sys import stdin


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

    A.sort(reverse=1)
    players = [A[0], float('inf'), float('inf'), A[0]]
    ans = 0
    for a in A[1:]:
        if players[1] > players[-2]:
            ans += min(players[0], players[1], players[2])
            players[1] = a
        else:
            ans += min(players[-1], players[-2], players[-3])
            players[-2] = a
    print(ans)


main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

    A.sort(reverse=True)
    ans = sum(A) - min(A)

    if (A[0] > 1 and A[0] == A[1]):
        ans += A[0] - A[-2]

    print(ans)

resolve()

Submission status: Wrong Answer

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

a.sort(reverse=True)

score=0

for i in range(len(a)):
  if i==0:
    score+=0
  elif i==1:
    score+=a[0]
  else:
    score+=a[1]
print(score)

Submission status: Wrong Answer

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_counter = collections.Counter(As)
    As_set = list(set(As))
    As_set.sort()
    As_set.reverse()

    As_set = collections.deque(As_set)

    ans = [As_set[0] for i in range(As_counter[As_set[0]])]
    joined_num = As_counter[As_set[0]]
    As_set.popleft()
    while len(As_set) > 0:
        target = As_set.popleft()

        target_num = As_counter[target]

        if (joined_num >= target_num):
            tmp = [target for i in range(2 * target_num)]
            ans.extend(tmp)
        else:
            tmp = [target for i in range(
                2 * joined_num + (target_num - joined_num))]
            ans.extend(tmp)

        joined_num += target_num

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


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

Submission status: Wrong Answer

Solution code:
all_people = input()
relax = list(map(int, input().split()))

relax_all = 0

relax.sort(reverse=True)

for i in range(int(all_people) - 1):
    relax_all += relax[i]

print (relax_all)

Submission status: Wrong Answer

Solution code:
lis = []
a = []
n = int(input())
a = input().split()
a_  = sorted(a,reverse=True)
a_ = list(a_)
for i in a_:
    if len(lis)==0:
        lis.append(int(i))
    elif len(lis)==1:
        lis.append(lis[0])
    else:
        lis_ = sorted(lis,reverse=True)
        lis.append(lis[1])
print(sum(int(j) for j in lis))

Submission status: Wrong Answer

Solution code:
n = int(input())
liste = []
m = input()
liste = m.split(' ')
liste = [int(x) for x in liste]
liste.remove(min(liste))
print(sum(liste))

Submission status: Wrong Answer

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

ans = 0
ans += A[-1]
ans += sum(2*A[N//2:-1])
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int,input().split()))
A.sort(reverse=True)
cnt = 0
S = [A[0]]
for i in range(1,N) :
    cnt += S[round(i/2)]
    S.append(A[i])
print(cnt)

Submission status: Wrong Answer

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

cnt = 0

tmp = a[0]
for i in range(n-1):
    if i == 0:
        cnt += a[0]
    else:
        if a[i] == tmp:
            cnt += a[i]
        else:
            cnt += min(tmp, a[i])

        tmp = min(tmp, a[i])
        # print(tmp, cnt)
print(cnt)

Submission status: Wrong Answer

Solution code:
n=int(input())
l=[]
s=0
num=map(int,input().split())
for k in num:
    s=s+k
    l.append(k)
s=s-l[n//2]
print(s)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

print(A.sum()-A.min())

Submission status: Wrong Answer

Solution code:
#D
N=int(input())
A=list(map(int,input().split()))
A=sorted(A,reverse=True)
    
Cnt=N-2
ans=A[0]
b=A[0]
for i in range(1,N):
    if Cnt==0:
        break
    if b==A[i] and Cnt>0:
        ans+=A[i]
        Cnt-=1
    elif Cnt>1:
        ans+=A[i]*2
        Cnt-=2
    elif Cnt==1:
        ans+=A[i]
        Cnt-=1
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
A = list(map(int,input().split( )))
A.sort()
A = A[::-1]
a = A[:3].copy()
ans = a[0] + a[1]
for plus in A[3:]:
    b = [min(a[i-1],a[i]) for i in range(len(a))]
    flag = np.argmax(b)
    ans += b[flag]
    a.insert(flag,plus)
print(ans)

Submission status: Wrong Answer

Solution code:
n=int(input())
frnds = list(map(int,input().split()))
frnds.sort(reverse=True)
cf = 0
mn=0
for i in range(n):
    if i == 0:
        mn = frnds[i]
        continue
    elif i==1:
        cf += mn
        mn = min(frnds[i],mn)
    else:
        cf += mn
        mn = min(frnds[i],mn)
print(cf)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun May  3 16:22:00 2020

@author: neb0123
"""

def solution(A,n):
	if len(A) <= 3:
		A = sorted(A)
		return 2*A[0] + A[1]

	A = sorted(A, reverse=True)
	B = A[::2]
	C = A[1::2]

	cnt = 0

	for i in range(1,len(B)-1):
		cnt += min(B[i-1],B[i+1])
	for i in range(1,len(C)-1):
		cnt += min(C[i-1],C[i+1])
	cnt += min(B[1],C[0])
	cnt += min(B[-2],C[-1])
	cnt += min(B[0],C[1])
	cnt += min(B[-1],C[-2])
	return cnt

if __name__ == "__main__":
	n = int(input())
	A = list(map(int,input().split(' ')))
	print(solution(A,n))

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 = list(map(int, input().split())) 

A_list.sort(reverse=True)
#print(A_list)

score = 0
pos = 2
for i in range(1, N):
    if i > 3:
        score += A_list[i-pos]
        pos += 1
    else:
        score += A_list[i-1]

print(score)

Submission status: Wrong Answer

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

# from math import gcd
from fractions import gcd
from collections import Counter, deque, defaultdict
from heapq import heappush, heappop, heappushpop, heapify, heapreplace, merge
from bisect import bisect_left, bisect_right, bisect, insort_left, insort_right, insort
from itertools import accumulate, product, permutations, combinations, combinations_with_replacement

N = int(input())
A = [int(_) for _ in input().split()]
A = sorted(A, reverse=True)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

for i in range(3,N):
  ans += max(A[i],A[i-1],A[i-2])
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import heapq


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


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


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

Submission status: Wrong Answer

Solution code:
n=int(input())

a = input().split()


int_a= [int(s) for s in a]
sum=sum(int_a)

print(sum-min(int_a))

Submission status: Wrong Answer

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

A.sort(reverse=True)

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

print(ans)

Submission status: Wrong Answer

Solution code:
# import sys
# input = sys.stdin.readline
# import re
import math
import itertools

def main():
    n = int(input())
    a = input_list()
    aa = sorted(a, reverse=True)
    print(sum(aa) - aa[-1])

def combinations_count(n, r):
    return math.factorial(n) // (math.factorial(n - r) * math.factorial(r))

def count(mazes, hc, wc):
    hc = list(hc)
    wc = list(wc)
    r = 0
    for hi, row in enumerate(mazes):
        if hi in hc:
            continue
        for wi, m in enumerate(row):
            if wi in wc:
                continue
            if m == '#':
                r += 1
    return r

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

def input_list_str():
    return list(map(str, input().split()))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n = int(input())
x = input()
lst = [int(i) for i in x.split(' ')]
lst.sort(reverse=True)
lst = lst[:-1]
print(sum(lst))

Submission status: Wrong Answer

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

print(sum(a)-min(a))

Submission status: Wrong Answer

Solution code:
t=int(input())
arr1=list(map(int,input().strip().split()))[:t]
y=min(arr1)
summ=0
for x in arr1:
  summ=summ+x
z=summ-y
print(z)

Submission status: Wrong Answer

Solution code:
n = int(input())
s = [int(i) for i in input().split()]
s = sorted(s, reverse=True)

ans = 0 + max(s) + s[1] * (n-2)
print(ans)

Submission status: Wrong Answer

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

a.sort(reverse=True)

b=[]
score=0

for i in range(len(a)):
  b.append(a[i])
  b.sort(reverse=True)
  if i==0:
    score+=0
  elif i==1:
    score+=b[0]
  else:
    score+=b[1]
print(score)

Submission status: Wrong Answer

Solution code:
N = int(input())

s = input().split()
A = []
for i in range(N):
  A.append(int(s[i]))
  
result = 0
A.sort(reverse=True)
result += A[0]
A.pop(0)
if N % 2 == 0:
  result += sum(A[:(int(N/2)-1)])*2
else:
  result += sum(A[:(int((N-1)/2)-1)])*2 + A[int((N-1)/2)]
    
print(result)

Submission status: Wrong Answer

Solution code:
from collections import deque
N=int(input())
A=list(map(int,input().split()))
#多分下から2番目の値を取り続ける
A.sort(reverse=True)
#0 0
#1 並んでる数値で上から2番目に大きい数値
#2 並んでる数値で上から2番目に大きい数値
#3 並んでる数値で2番目に大きい数値
q=deque()
ans=0
for i in range(N):
    if i ==0:
        q.append(A[i])
    if i ==1:
        q.append(A[i])
        ans+=q[0]
    elif i>1:
        q.append(A[i])#新しい値を足す
        a=q.popleft()#でかい値を消す
        ans+=q[0]
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
A = sorted(A, reverse=True)
lst1 = []
lst2 = []
kei = A[0]
for i in range(N):
    if int(i % 2) == 0:
        lst1.append(A[i])
        if len(lst1) >= 2:
            kei += lst1[i - len(lst1) - 1]
        elif len(lst1) == 3:
            kei += lst1.pop(0)
    elif int(i % 2) == 1:
        lst2.append(A[i])
        if len(lst2) >= 2:
            kei += lst2[i - len(lst2) - 1]
        elif len(lst2) == 3:
            kei += lst2.pop(0)
print(kei)

Submission status: Wrong Answer

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,25):
    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: Wrong Answer

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


def solve(N: int, A: "List[int]"):
    a = list(reversed(sorted(A)))
    print(sum(a)-a[-1])
    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: Wrong Answer

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

list2=[]

c=0
a1 = 0
a2 = 0

for i in range(n):
    if i ==0:
        a1 = alist[i]
        c+=0
#        print("[{}]:{} gets 0".format(i,alist[i]))
    elif i==1:
        a2 = alist[i]
        c+=alist[0]
#        print("[{}]:{} gets {}".format(i,alist[i],a1))
    else:
        if a1 > a2:
            # a1の方が好感度高ければ、そちらにつける
#            print("[{}]:{} gets {} ({}/{})".format(i,alist[i],a2,a1,a2))
            c+=a2
            a1 = alist[i]
        else:
            # a2の方が好感度高ければ、そちらにつける
#            print("[{}]:{} gets {} ({}/{})".format(i,alist[i],a1,a1,a2))
            c+=a1
            a2 = alist[i]
print(c)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

confort = 0

edge = []

for i in range(n):
    if i == 0:
        continue
    elif i == 1:
        confort += friendly[0]
        edge.append(friendly[1])
        edge.append(friendly[1])
    else:
        max_edge = max(edge)
        confort += max_edge
        edge.insert(edge.index(max_edge) + 1, friendly[i])
        
print(confort)

Submission status: Wrong Answer

Solution code:
n=int(input())

a = input().split()

a.sort()
int_a= [int(s) for s in a]
sum=sum(int_a)

print(sum-int_a[0])

Submission status: Wrong Answer

Solution code:
import collections
n = int(input())
a = list(map(int, input().split()))
a = sorted(a, reverse=True)
c = collections.Counter(a)
ans = a[0]*(c[a[0]]-1)
length = 0
residue = n
for i in c.keys():
  length += c[i]
  n -= c[i]
  if length >= n:
    ans += i*residue
    print(ans)
    exit()
  else:
    ans += i*c[i]
  residue -= c[i]*2
print(ans)

Submission status: Wrong Answer

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

A_cnt = collections.Counter(A)
A_key = list(A_cnt.keys())[::-1]
A_val = list(A_cnt.values())[::-1]
total = 0
for i in range(len(A_val)):
    a_val = A_val[i]
    j = 0
    while a_val>0:
        if A_val[j]==0:
            j +=1
        else:
            if a_val > A_val[j]:
                total += (A_val[j])*A_key[j]
                a_val = a_val - A_val[j]
                A_val[j] = 0
            else:
                total += (a_val)*A_key[j]
                A_val[j] = A_val[j]-a_val+1
                a_val = 0
            j +=1

print(total-A_key[0])

Submission status: Wrong Answer

Solution code:
from collections import deque
n=int(input())
a=sorted([int(i) for i in input().split()])[::-1]

if n==2:
    print(a[0])
else:
    b=deque([a[1],a[2]])
    ans=a[0]+a[1]
    minlist=[min(b)]
    j=1
    for i in range(n-3):
        if j==0:
            j+=1
            ans+=b[0]
            b+=[a[i+2]]
        else:
            j=0
            ans+=b[0]
            b.popleft()
            b+=[a[i+2]]
    print(ans)

Submission status: Wrong Answer

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

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

a.sort(reverse=True)

left = 0
right = 0
center = 0

s = 0

for i in range(n):
    if i == 0:
        center = a[i]
    elif i == 1:
        s += center
        right = a[i]
    elif i == 2:
        s += min(center, right)
        left = a[i]
    else:
        if min(left, center) > min(center, right):
            s += min(left, center)
            left = a[i]
        else:
            s += min(center, right) 
            right = a[i]

print(s)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
if N == 2:
    print(A[0])
    exit()
ans = 0
second_half = N // 2
first_half = N - second_half
for i in range(first_half - 1):
    ans += A[i]
for i in range(second_half):
    ans += A[i + 1]
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque
N=int(input())
A=list(map(int,input().split()))
#多分下から2番目の値を取り続ける
A.sort(reverse=True)
#0 0
#1 並んでる数値で上から2番目に大きい数値
#2 並んでる数値で上から2番目に大きい数値
#3 並んでる数値で2番目に大きい数値
q=deque()
ans=0
for i in range(N):
    if i ==0:
        q.append(A[i])
    if i ==1:
        q.append(A[i])
        ans+=q[0]
        print(q[0])
    elif i>1:
        q.append(A[i])#新しい値を足す
        a=q.popleft()#でかい値を消す
        ans+=q[0]
print(ans)

Submission status: Wrong Answer

Solution code:
N = int(input())

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

A.sort(reverse = True)

# print(A)
score = A[0]

i = 2

while i < N:
    score += min(A[i-1],A[i-2])
    i += 1


print(score)

Submission status: Wrong Answer

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

ans = 0
A.sort(reverse=True)
#print(A)
for i in range(1,N):
    if i == 1:
        ans += A.pop(0)
        continue
    if i % 2 != 0:
        ans += A[0]
    else:
        ans += A.pop(0)
#print(A)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

def resolve():
    inp = sys.stdin.readlines()
    inp = list(map(lambda x: x.strip(), inp))
    N = int(inp[0])
    A = list(map(int, inp[1].split(' ')))
    A.sort(reverse=True)
    num = 0
    for i in range(N-1):
        num += A[i+1//2]

    print(num)

resolve()

Submission status: Wrong Answer

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

sort_a = sorted(a, reverse=True)

comfort = 0
insert_flag = -1

list = []
list.append(sort_a[0])

for cnt in range(1, n):
    for cnt_list in range(len(list)):
        insert_flag = 0
        if list[cnt_list] == sort_a[cnt]:
            list.insert(0, sort_a[cnt])
            insert_flag = 1
            comfort += list[-1]
            break
    if insert_flag == 0:
        list.append(sort_a[cnt])
        comfort += list[-2]

print(comfort)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import heapq as hq
def resolve():
    N = int(input())
    A = list(map(int,input().split()))
    A.sort(reverse= True)
    if N == 2:
        print(A[0])
        return
    if N == 3:
        print(A[0]+A[1])

    score = A[0]+A[1]
    lst = []
    hq.heappush(lst,A[1])
    hq.heappush(lst,A[2])
    hq.heappush(lst,A[2])
    for i in A[3:]:
        mn = hq.heappop(lst)
        score+= mn
        hq.heappush(lst,i)
        hq.heappush(lst,i)
    print(score)
resolve()

Submission status: Wrong Answer

Solution code:
n=int(input(''))
a=list(map(int,input('').split(' ')))
b=[]
for i in range(n):
    q=a.pop(0)
    if len(b)==0:
        b.append(q)
    else:
        ok=1
        for j in range(len(b)):
            if q>=b[j]:
                b.insert(j,q)
                ok=0
                break

        if ok==1:
            b.append(q)
c=[]
hap=0
for i in range(n):
    q=b.pop(0)
    if len(c)==0:
        c.append(q)
        hap+=0
    elif len(c)==1:
        c.append(q)
        hap+=c[0]
    else:
        c.insert(1,q)
        hap+=c[2]

print(hap)

Submission status: Wrong Answer

Solution code:
import math
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 = list(map(int, input().split()))
a.sort(reverse=True)
result = 0
num = 1
temp = 0
for i in range(n):
    temp = i
    num += 2**i 
    result += a[i] * 2**i
    if(num >= n):
        break
print(result - a[temp] * (num- n))

Submission status: Wrong Answer

Solution code:
import numpy as np
N = int(input())
A = list(map(int,input().split( )))
A.sort()
A = A[::-1]
a = A[:3].copy()
ans = a[0] + a[1]
for plus in A[3:]:
    b = [min(a[i-1],a[i]) for i in range(len(a))]
    flag = np.argmax(b)
    ans += max(b)
    a.insert(flag,plus)
    flag = np.argmax(a)
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
def main1():
    N=int(input())
    A=list(map(int,input().split()))
    A.sort(reverse=True)
    cn=1
    ans=A[0]
    tmp=0
    print(-(-N//2))
    if N%2==0:
        while cn<-(-N//2):
            ans+=A[cn]*2
            cn=cn+1
        print(ans)
    else:
        while cn<-(-N//2)-1:
            ans+=A[cn]*2
            cn=cn+1
        print(ans+A[cn])
main1()

Submission status: Wrong Answer

Solution code:
a = int(input())
b = list(map(int, input().split(' ')))
b = sorted(b, reverse=True)
r = b[0]
l = b[0]

point = b[0]
b.pop(0)

for i in range(a-2):
  if r >= l:
    r = b[i]
  elif l >= r:
    l = b[i]
  plus = min(r, l)
  point = point + plus

print(point)

Submission status: Wrong Answer

Solution code:
from collections import deque

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

confort = 0

edge = deque()

for i in range(n):
    if i == 0:
        continue
    elif i == 1:
        confort += friendly[0]
        edge.append(friendly[1])
        edge.append(friendly[1])
    else:
        max_edge = max(edge)
        confort += max_edge
        edge.insert(edge.index(max_edge) + 1, friendly[i])
        
print(confort)

Submission status: Wrong Answer

Solution code:
def judge():
    ans = True
    return ans

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

print(b[-1] + (n-2)* b[-2])

Submission status: Wrong Answer

Solution code:
import sys

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

Solution code:
from collections import deque

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

ans = a_list[0]
l = deque([a_list[0], a_list[1]])
for i in range(2, N):
    if i % 2 == 0:
        ans += l[-1]
        l.appendleft(a_list[i])
       
    else:
        ans += l[0]
        l.append(a_list[i])
print(ans)