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:
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[insx_list[j-1]-1],reached_list[insx_list[j-1]])
    reached_list.insert(insx_list[j-1], A_list[j])
    mx_cmfrt += cmfrt
print(mx_cmfrt)

Submission status: Runtime Error

Solution code:
import math

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


num = math.ceil(N/2)

a = Asrt([:num])

if N % 2 == 0:#guusuu
    print(sum(a) * 2 - a[0])
else:#kisuu
    print(sum(a) * 2 - a[0] - a[-1])

Submission status: Runtime Error

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

if n <= 2:
  a_list.pop(-1)
  print(sum(a_list))
else:
  ans = 0
  ans += a_list.pop(0)
  cnt = 2
  while 1:
    if cnt == n:
      break
    if n - cnt >= 2:
      ans += a_list.pop(0) * 2
      cnt += 2
    else:
      ans += a_list.pop(0) * 1
      cnt += 1
      
print(ans)

Submission status: Runtime Error

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

A.sort(reverse=True)
T = A[0] + A[1]
J = [A[0],A[1],A[2]]
del A[0:3]

S = len(J)
def Sonyu(S):
    j = 0
    k = 1
    global T, J
    for i in range(len(S)):
        if len(J) > k:
            T += J[k]
            J.insert(k, S[j])
        else:
            T += J[-1]
            J.append(S[j])
        j += 1
        k += 2

syoki = 3
for i in range(N):
    if syoki < len(A):
        Sonyu(A[0:syoki])
        del A[0:syoki]
        syoki += 3
    else:
        Sonyu(A)
        break
print(T)

Submission status: Runtime Error

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

A = sorted(A, reverse=True)
sum = A[0]
temp=[]
temp.append(A[0])
temp.append(A[2])
for i in range(2,len(A)):
  temp.append(A[i])
  sum += temp[len(temp)-2]
print(sum)

Submission status: Runtime Error

Solution code:
import sys

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

    A.sort(reversed=True)

    res=0
    res+=A[0]
    res+=A[1]*(N-2)
    print(res)

if __name__=="__main__":
    main()

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 = int(input())
A = list(map(int, input().split()))
A = sorted(A)
A = reversed(A)
B = set(A)
B = list(B)
B = B[::-1]
C = N-1
ans = 0
if N%2 == 0:
  ans += B[0]
  for i in range(N//2-1):
    ans += 2*B[i+1]
else:
  ans += B[0]
  ans += B[N//2]
  for i in range(N//2-1):
    ans += 2*B[i+1]
print(ans)

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(data)==0):
    print(kokotiyosa)
    exit()
  cc += 1
  searchmax(data)
  
searchmax(data)

Submission status: Runtime Error

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

Solution code:
from collections import deque

N = int(input())
A = [int(e) for e in input().split()]

A.sort(reverse=True)

if N == 2:
    print(A[1])
else:
    ans = A[0]
    friendly_deque = deque()
    friendly_deque.append(A[1])
    friendly_deque.append(A[1])
    
    #print(ans,friendly_deque)
    
    for i in range(2,N):
        add = friendly_deque.popleft()
        ans += add
        friendly_deque.append(A[i])
        friendly_deque.append(A[i])
        #print(ans,friendly_deque)
print(ans)

Submission status: Runtime Error

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]
        buf += 1
        if buf == 2:
            pos += 1
            buf = 0
    else:
        score += A_list[i-1]

print(score)

Submission status: Runtime Error

Solution code:
for i in range(int(input())):
	a = list(map(int, input().split()))
	res = -1
	for i in a:
		res+=i
	print(res)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

length = int(sys.stdin.readline())

array = np.array([int(i) for i in sys.stdin.readline().split()], dtype=np.int32)

array[::-1].sort()

def max_score(added, to_add):
  if len(to_add) == 1:
    return max(min(added[i-1], added[i]) for i in range(len(added)))
  else:
    to_add_now = to_add[0]
    to_add_later = to_add[1:]
    
    top_score = 0
    for i in range(len(added)):
      this_score = min(added[i-1], added[i])
      new_added = added.copy()
      new_added.insert(i, to_add_now)
      child_score = max_score(new_added, to_add_later)
      
      score = this_score + child_score
      if score > top_score:
        top_score = score
    return top_score

array = list(array)
print(max_score(array[0:1], array[1:]))

Submission status: Runtime Error

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