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: Time Limit Exceeded

Solution code:
N = int(input())
A = sorted(list(map(int,input().split())), reverse=True)#降順
kukan = []
ans = 0
kukan.append(A.pop(0))#降順リスト
for i in range(N-1):

    ans += kukan.pop(0)#区間の最大値を取り出す
    kukan.append(A[i])
    kukan.append(A[i])
print(ans)

Submission status: Time Limit Exceeded

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

import numpy as np

result = 0
args = np.argsort(A)
coms = []
for arg in reversed(args):
    com = A[arg]
    if len(coms)==0:
        coms.append(com)
    elif len(coms)==1:
        coms.append(com)
        result+=coms[0]
    else:
        current=-1
        insert_arg = None
        for tmp in range(len(coms)):
            min_ = min(coms[tmp-1], coms[tmp])
            if current<min_:
                insert_arg = tmp
                current=min_
            if current==min_:
                insert_arg = tmp
                break
        result+=current
        tmp = coms[insert_arg:]
        coms[:insert_arg-1].append(A[arg])
        coms += tmp
print(result)

Submission status: Time Limit Exceeded

Solution code:
from collections import deque


def main():
    input()  # n
    friendliness = [int(x) for x in input().split()]
    friendliness.sort()
    friendliness = deque(friendliness)
    edges = []
    popped1 = friendliness.pop()
    popped2 = friendliness.pop()
    edges.append((popped1, popped2))
    edges.append((popped2, popped1))
    ans = popped1
    while friendliness:
        popped = friendliness.pop()
        tmp_data = (-1, -1)  # max_comfort, edge_index
        for i, edge in enumerate(edges):
            x = min(edge)
            if x > tmp_data[0]:
                tmp_data = (x, i)
        ans += tmp_data[0]
        edge_tmp = edges[tmp_data[1]]
        edges[tmp_data[1]] = (0, 0)
        edges.append((edge_tmp[0], popped))
        edges.append((popped, edge_tmp[1]))
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
S = input().split()
A = []
for s in S:
    A.append(int(s))
A.sort(reverse=True)

result = 0

B = []
for i in range(N):
    flag = True
    if i == 0:
        B.append(A[i])
    elif i == 1:
        result += B[0]
        B.append(A[i])
    else:
        temp = []
        for j in range(len(B)-1):
            temp.append(min(B[j],B[j+1]))
        temp.append(min(B[0],B[-1]))
        select = max(temp)
        result+=select
        for j in range(len(B)-1):
            if select == min(B[j], B[j+1]):
                B.insert(j+1, select)
                flag = False
                break
        if flag:
            B.append(select)
        
print(result)

Submission status: Time Limit Exceeded

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), friendly[i])
        edge.insert(edge.index(max_edge) + 1, friendly[i])
        
print(confort)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
edge = {}
edge[0, 1] = min(A[0], A[1])
edge[1, 0] = min(A[0], A[1])
ans = A[0]
max_edge_list = [(0, 1), (1, 0)]
for i in range(2, N):
    x, y = max_edge_list[0]
    ans += edge[(x, y)]
    del edge[(x, y)]
    edge[x, i] = A[i]
    edge[i, y] = A[i]
    max_edge_list.append((x, i))
    max_edge_list.append((i, y))
    max_edge_list.remove((x, y))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

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

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

ans = 0
list = []
for i in range(N):
  if ( i==0 ) :
    list.append(A[0])
  else:
    max_index = list.index(max(list))
    ans += list[max_index]
    list[max_index] = A[i]
    list.insert(max_index, A[i])
  #print(list,ans)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    n = int(input().strip())
    A = list(map(int, input().strip().split()))
    A.sort(reverse=True)
    score = 0
    if not len(A) < 2:
        value_list = A[:2]
        min_max_list = [A[1], A[1]]
        score = A[0]
        for a in A[2:]:
            arg_max_index = min_max_list.index(max(min_max_list))
            score += min_max_list[arg_max_index]
            value_list.insert(arg_max_index + 1, a)
            min_max_list[arg_max_index] = a
            min_max_list.insert(arg_max_index, a)
    print(score)

Submission status: Time Limit Exceeded

Solution code:

a = int(input())
b = list(map(int,input().split()))
c = []

ans = 0    

b = sorted(b, reverse=True)
c.append(b.pop(0))
ans += c[0]
c.append(b.pop(0))
nmax = 0
maxindex = 0
if a > 2:
    ans += c[1]
    c.append(b.pop(0))
    if a > 3:
        for i in range(a - 3):
            nmax = 0
            maxindex = 0
            for j in range(len(c)):
                if j + 1 == len(c):
                    s = min(c[j], c[0])
                else:
                    s = min(c[j], c[j + 1])
                if nmax < s:
                    nmax = s
                    maxindex = j + 1
            ans += nmax
            c.insert(maxindex, b.pop(0))
print(str(ans))

Submission status: Time Limit Exceeded

Solution code:
import sys


def max_comfort(array):
    comfort = 0
    index = 0
    n = len(array)
    if n == 0:
        return 0, 0
    if n == 1:
        return array[0], 0
    for i in range(n):
        if i < n - 1:
            if comfort < min(array[i], array[i + 1]):
                comfort = min(array[i], array[i + 1])
                index = i + 1
        else:
            if comfort < min(array[i], array[0]):
                comfort = min(array[i], array[0])
                index = 0
    return comfort, index


def main():
    input = sys.stdin.buffer.readline
    n = int(input())
    a = list(map(int, input().split()))
    a.sort(reverse=True)
    round = []
    ans = 0
    for f in a:
        comfort, index = max_comfort(round)
        ans += comfort
        round.insert(index, f)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
data =  [int(x) for x in input().split()]
data.sort(reverse=True)

person_list = [data.pop(0),]

result = 0
for i,x in enumerate(data):
    max_value = [0,min(person_list[0],person_list[-1])] #[locate,value]
    for d in range(len(person_list) - 1):
        temp = min(person_list[d],person_list[d+1])
        if temp > max_value[1]:
            max_value[0] = d+1
            max_value[1] = temp
    result += max_value[1]
    person_list.insert(max_value[0],x)

print(result)

Submission status: Time Limit Exceeded

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

A.sort()

pot = [A.pop()]
com = 0
while A:
    com += pot.pop()
    new = A.pop()
    pot.append(new)
    pot.append(new)
    pot.sort()

print(com)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import numpy

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

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

ans = 0
list = []
for i in range(N):
  if ( i==0 ) :
    list.append(A[0])
  else:
    max_index = numpy.argmax(list)
    ans += list[max_index]
    list[max_index] = A[i]
    list.insert(max_index, A[i])
  #print(list,ans)

print(ans)

Submission status: Time Limit Exceeded

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

for i in range(N):
  if i == 0:
    list.append(S[i])
  elif i == 1 or i == 2:
    list.append(S[i])
    count += list[i-1]

  else:
    index = -1
    value = 0
    for j in range(len(list)):
      if j == len(list) -1:
        which_min = min(list[j],list[0])
      else:
      	which_min = min(list[j],list[j+1])
      if which_min > index:
        index = j
        value = which_min
    if not index == len(list) -1:
    	list.insert(index+1, S[i])
    else:
      list.insert(0,S[i])
    count +=value
print(count)

Submission status: Time Limit Exceeded

Solution code:
def arg_max(target_list):
    max_value = max(target_list)
    for num, i in enumerate(target_list):
        if i == max_value:
            return num
    return -1


if __name__ == '__main__':
    n = int(input().strip())
    A = list(map(int, input().strip().split()))
    A.sort(reverse=True)
    score = 0
    if not len(A) < 2:
        value_list = A[:2]
        min_max_list = [A[1], A[1]]
        score = A[0]
        for a in A[2:]:
            arg_max_index = arg_max(min_max_list)
            score += min_max_list[arg_max_index]
            value_list.insert(arg_max_index + 1, a)
            min_max_list[arg_max_index] = a
            min_max_list.insert(arg_max_index, a)
    print(score)

Submission status: Time Limit Exceeded

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

A.sort(reverse=True)
ans = 0

point = []
point.append(A.pop(0))
for i in A:
    ans += point.pop(0)
    point.append(i)
    point.append(i)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
l=[a[0]]
ans=0
for aa in a[1:]:
  ans+=max(l)
  l.remove(max(l))
  l.append(aa)
  l.append(aa)
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
from itertools import combinations
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
l=[]
l.append(a[0])
s=a[0]
for i in range(1,n-1):
    l.append(a[i])
    p=list(combinations(l,2))
    x=a[0]
    for j in range(len(p)):
        for k in range(2):
            x=min(x,p[j][k])
    s=s+x
print(s)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
liste = []
circle = []
comfort = 0
m = input()
liste = m.split(' ')
liste = [int(x) for x in liste]
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: Time Limit Exceeded

Solution code:
from collections import deque
N = int(input())
K = list(sorted(map(int, input().split()), reverse=True))

max1 = K[0]
max2 = K[1]
if N == 2:
    print(max1)
else:
    max3 = K[2]
    count = max1 + max2
    cands = [(max1, max2), (max2, max3)]
    for k in K[3:]:
        max_cand = (0, 0)
        max_i = -1
        for i, cand in enumerate(cands):
            if min(cand) > min(max_cand):
                max_cand = cand
                max_i = i
        hi, lo = max_cand
        count += lo
        cands.pop(max_i)
        cands.append((hi, k))
        cands.append((lo, k))
    print(count)

Submission status: Time Limit Exceeded

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

ali.sort(reverse=True)

# rings = [ali[0], ali[1]]
comforts = [ali[1], ali[1]]

answer = ali[0]

for i in ali[2:]:
    max_com = comforts.index(max(comforts))
    answer += comforts[max_com]

    comforts.pop(max_com)
    comforts.insert(max_com, i)
    comforts.insert(max_com, i) #新しく入った人の隣同士

print(answer)

Submission status: Time Limit Exceeded

Solution code:
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n=ni()
a=sorted(nl())[::-1]

# val next
l=[(-1,-1)]*(n+1)
l[0]=(a[0],0)
nx_idx=1

ans = 0

for i in range(1,n):
  aa = a[i]
  now = 0
  mx_idx=0
  mx=-1

  cnt=0
  while True:
    cnt+=1

    val = l[now][0]
    nxt = l[now][1]

    nxtv=0
    nxtv=l[nxt][0]
    
    n_val = min(val,nxtv)

    if mx < n_val:
      mx = n_val
      mx_idx = now

    if nxt == a[i]:
      break
    
    if cnt==nx_idx:
      break

    now = nxt

  ans+=mx

  val = l[mx_idx][0]
  nxt = l[mx_idx][1]

 
  l[nx_idx]=(a[i],l[mx_idx][1])
  l[mx_idx]=(mx,nx_idx)

  nx_idx+=1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())
a = list(map(int,input().split()))
a.sort(reverse=True)
b = []
b.extend([a[0]])
c = 0
for i in range(1,n):
  m = max(b)
  c = c + m
  b.extend([a[i],a[i]])
  b.remove(m)
print(c)

Submission status: Time Limit Exceeded

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

A = sorted(A, reverse = True)
data = [A[0], 10 ** 9 + 1]
score = 0

for i, a in enumerate(A):
    if i == 0: continue
    max_idx, max_point = 0, 0
    for j, _ in enumerate(data[:-1]):
        point = min(data[j], data[j+1])
        if point > max_point:
            max_idx = j
            max_point = point
    data = data[:j+1] + [a] + data[j+1:]
    score += max_point

print(score)

Submission status: Time Limit Exceeded

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

class wa:
    wal = []
    def append(self, index, comfort):
        self.wal.insert(index, comfort)
        if len(self.wal) == 1:
            return 0
        else:
            a = index - 1
            b = index + 1
            wal_len = len(self.wal)
            if a < 0:
                a += wal_len
            if b > wal_len - 1:
                b -= wal_len
            return min(self.wal[a], self.wal[b])
    def get(self, index):
        return self.wal[index]
    def get_comfort(self, index):
        wal_len = len(self.wal)
        if wal_len == 0:
            return 0
        else:
            a = index - 1
            b = index
            if a < 0:
                a += wal_len
            if b > wal_len - 1:
                b -= wal_len
            return min(self.wal[a], self.wal[b])
    def opt_append(self, comfort):
        wal_len = len(self.wal) + 1
        opt = 0
        opt_temp = -1
        opt_temp2 = 0 # 計算結果を一時的にスタック
        for i in range(wal_len):
            opt_temp2 = self.get_comfort(i)
            if opt_temp < opt_temp2:
                opt = i
                opt_temp2 = opt_temp
        return self.append(opt, comfort)

a.sort(reverse=True)
c = []

wa = wa()
for i in a:
    c.append(wa.opt_append(i))
print(sum(c))

Submission status: Time Limit Exceeded

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:
        m=len(c)
        ma=0
        mp=0
        mpl=0
        for j in range(m):
            if c[j-1]>=c[j]:
                ms=c[j]
                mp=j
            elif c[j]>c[j-1]:
                ms=c[j-1]
                mp=j-1

            if ma<=ms:
                ma=ms
                mpl=mp
        
        c.insert(mpl,q)
        hap+=ma

print(hap)

Submission status: Time Limit Exceeded

Solution code:
n=int(input())
a=list(map(int,input().split()))
a.sort(reverse=True)
ans=a[0]
dp=[]
dp.append(a[0])
dp.append(a[1])
for i in range(2,n):
  check=0
  for j in range(i-1):
    check=max(check,min(dp[j],dp[j+1]))
  dp.insert(dp.index(check),a[i])
  ans=ans+check
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
edge = {}
edge[0, 1] = min(A[0], A[1])
edge[1, 0] = min(A[0], A[1])
ans = A[0]
max_edge_list = [(0, 1), (1, 0)]
#print(edge)
for i in range(2, N):
    max_kv = max(edge.items(), key=lambda x: x[1])
    x, y = max_edge_list[0]
    ans += edge[(x, y)]
    del edge[(x, y)]
    edge[x, i] = A[i]
    edge[i, y] = A[i]
    max_edge_list.append((x, i))
    max_edge_list.append((i, y))
    max_edge_list.remove((x, y))
    #print(edge)
print(ans)

Submission status: Time Limit Exceeded

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

a.sort(reverse=True)
dic = {a[0]:1}
ans = 0

for i in range(1,n):
    h = a[i]
    max_h = list(dic)[0]
    
    
    
    if h == max_h:
        dic[h] += 1
        ans += h
    
    else:
        if h in dic:
            dic[h] += 2
            dic[max_h] -= 1
            ans += max_h
        
        else:
            dic[h] = 2
            dic[max_h] -= 1
            ans += max_h
        
        if dic[max_h] == 0:
            del dic[max_h]
    
    

print(ans)

Submission status: Time Limit Exceeded

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)
    s.insert(idx+1,A[i])
    tmp = make_conf_list(s)

print(ans)

Submission status: Time Limit Exceeded

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

relax_all = 0

for i in range(int(all_people)):
    for t in range(int(all_people)):
        if relax[i] > relax[t]:
            tmp = relax[i]
            relax[i] = relax[t]
            relax[t] = tmp

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

print (relax_all)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

comfort = 0
players = []
for x in reversed(sorted(A)):
    if len(players) == 0:
        players.append(x)
    elif len(players) == 1:
        comfort += players[0]
        players.append(x)
    else:
        idx, _comfort = 0, -1
        for i in range(len(players)):
            _candidate = min(players[i], players[(i+1) % len(players)])
            if _candidate > _comfort:
                idx = i
                _comfort = _candidate
        players.insert(idx, x)
        comfort += _comfort
print(comfort)

Submission status: Time Limit Exceeded

Solution code:
ni = lambda: int(input())
nm = lambda: map(int, input().split())
nl = lambda: list(map(int, input().split()))

n=ni()
a=sorted(nl())[::-1]

# val next
l=[(-1,-1)]*(n+1)
l[0]=(a[0],0)
nx_idx=1

ans = 0

for i in range(1,n):
  aa = a[i]
  now = 0
  mx_idx=0
  mx=-1

  cnt=0
  while True:
    cnt+=1

    val = l[now][0]
    nxt = l[now][1]

    nxtv=0
    nxtv=l[nxt][0]
    
    n_val = min(val,nxtv)

    if mx < n_val:
      mx = n_val
      mx_idx = now
    
    if cnt==nx_idx:
      break

    now = nxt

  ans+=mx

  val = l[mx_idx][0]
  nxt = l[mx_idx][1]

 
  l[nx_idx]=(a[i],l[mx_idx][1])
  l[mx_idx]=(mx,nx_idx)

  nx_idx+=1
print(ans)

Submission status: Time Limit Exceeded

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

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

for i in range(1,N):
    max = 0
    l = len(circle)
    for j in range(l):
        if max < min(circle[j],circle[(j+1)%l]):
            ins = j
            max = min(circle[j],circle[(j+1)%l])
    circle.insert(ins,A[i])
    ans += max

print(ans)

Submission status: Time Limit Exceeded

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

A.sort()

circle = []

def add_circle(circle, n):
  if len(circle) == 0:
    circle.append(n)
    return 0
  elif len(circle) == 1:
    circle.append(n)
    return circle[0]
  comf_max = -float('inf')
  max_i = 0
  for i in range(len(circle)):
    if comf_max < min(circle[i], circle[(i + 1) % len(circle)]):
      comf_max = min(circle[i], circle[(i + 1) % len(circle)])
      max_i = i
  circle.insert(max_i, n)
  return comf_max

res = 0

while A:
  i = A.pop()
  res += add_circle(circle, i)

print(res)

Submission status: Time Limit Exceeded

Solution code:
INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))

def do():
    n=INT()
    a=LIST()
    a=sorted(a,reverse=True)
    ct=0
    cir=[]
    for i in range(n):
        if i==0:
            cir.append(a[i])
        else:
            t=max(cir)
            ct+=t
            cir[cir.index(t)]=a[i]
            cir.append(a[i])
        
    print(ct)
    
if __name__ == '__main__':
    do()

Submission status: Time Limit Exceeded

Solution code:
N=int(input())

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

B=sorted(A,reverse=True)

ans=B[0]

C=[B[0],B[1]]

ans=B[0]
for i in range(2,N):
    jmax=1
    scoremax=min(C[0],C[1])
    for j in range(1,len(C)-1):
        score = min(C[j],C[j+1])
        if scoremax < score:
            scoremax = score
            jmax=j
    ans += scoremax
    C=C[0:jmax]+[B[i]]+C[jmax:]
    # print(C)

print(ans)

Submission status: Time Limit Exceeded

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

A.sort(reverse=True)
ans = 0
N -= 2
ans += A[0]
A.pop(0)

if N == 0:
    print(ans)
    exit()
elif N == 1:
    ans += A[0]
    print(ans)
    exit()
elif N >= 2:
    q = N // 2
    r = N % 2
    for i in range(1, q + 1):
        ans += 2 * A[0]
        A.pop(0)
    if r == 1:
        ans += A[0]
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
A_list = sorted(list(map(int, input().split())), reverse=True)

edge_list = [A_list[0]]
score = 0
for i, a in enumerate(A_list[1:]):
    maxidx = np.argmax(edge_list)
    if edge_list[maxidx] == 1:
        break
    score += edge_list[maxidx]
    #print(edge_list, maxidx, score)
    edge_list[maxidx] = a
    edge_list.insert(maxidx, a)
    #print(edge_list)
else:
    print(score)
    exit()
score += N - i - 1
print(score)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N = int(input())
A_list = sorted(list(map(int, input().split())), reverse=True)

ar_list = [A_list[0]]
edge_list = [A_list[0]]
score = 0
for a in A_list[1:]:
    maxidx = np.argmax(edge_list)
    score += edge_list[maxidx]
    #print(edge_list, maxidx, score)
    edge_list[maxidx] = a
    edge_list.insert(maxidx, a)
    #print(edge_list)

print(score)

Submission status: Time Limit Exceeded

Solution code:
n = int(input())

lst = list(map(int, input().split()))
lst = sorted(lst, reverse=True)
class Queue:
  def __init__(self):
    self.queue_lst = []
    self.first_flag = True
    self.count = 0
  
  def add(self, friend_val):
    if self.first_flag:
      self.queue_lst.append(friend_val)
      self.first_flag = False
    else:
      self.queue_lst.append(friend_val)
      self.queue_lst.append(friend_val)
      self.count += self.queue_lst.pop(0)
  
queue = Queue()

for i in lst:
  queue.add(i)



print(queue.count)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())

s = input().split()
A = []
for i in range(N):
  A.append(int(s[i]))
  
result = 0
for i in range(N-1):
  if i == 0 or i%2 == 1:
    m = max(A)
    result += m
    A.remove(m)
  else:
    result += m
    
print(result)

Submission status: Time Limit Exceeded

Solution code:
# vim: fileencoding=utf-8


def main():
    n = int(input())
    li = list(map(int, input().split()))
    res = 0
    li.sort(reverse=True)
    tmp = [li[0]]
    for e in li[1:]:
        res += tmp[0]
        tmp = tmp[1:]
        tmp.append(e)
        tmp.append(e)
    print(res)


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
A.sort(reverse=True)
ring = [A[0], A[1]]
ans = A[0]
for i in range(2, N):
    max_tmp = 0
    num_tmp = 0
    for j in range(len(ring) - 1):
        if max_tmp < min(ring[j], ring[j + 1]):
            max_tmp = min(ring[j], ring[j + 1])
            num_tmp = j + 1
    if max_tmp < min(ring[0], ring[-1]):
        max_tmp = min(ring[0], ring[-1])
        num_tmp = -1
    if num_tmp == -1:
        ring.append(A[i])
    else:
        ring.insert(num_tmp, A[i])
    ans += max_tmp
print(ans)

Submission status: Time Limit Exceeded

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

A.sort(reverse=True)
ans = 0
N -= 2
ans += A[0]
A.pop(0)

while True:
    if N == 0:
        print(ans)
        exit()
    elif N == 1:
        ans += A[0]
        print(ans)
        exit()
    elif N >= 2:
        ans += 2 * A[0]
        N -= 2
        A.pop(0)

Submission status: Time Limit Exceeded

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


def min_point(data):
    lgt = len(data)
    m=0
    ins=0
    for i in range(lgt):
        if i ==0:
            buf = min(data[i],data[lgt-1])
        else:
            buf = min(data[i],data[i-1])
        if buf > m:
            m=buf
            ins=i
    return [m,ins]

a.sort(reverse=True)
cnt=0
lst=[]
for i in range(n):
    if i == 0:
        lst.append(a[i])
    elif len(lst) == 1:
        cnt += lst[0]
        lst.append(a[i])
    else:
        mp = min_point(lst)
        cnt += mp[0]
        lst.insert(mp[1], a[i])

print(cnt)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

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

A.sort(reverse=True)
row = [A[0], A[1]]
min_list =[A[1], A[1]]
ans = A[0]
for i in range(2, N):
    index = np.argmax(min_list)
    ans += min_list[index]
    if index == len(min_list)-1:
        l, r = row[index], row[0]
    else:
        l, r = row[index], row[index+1]
    row.insert(index+1, A[i])
    min_list[index] = A[i]
    min_list.insert(index+1, A[i])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == '__main__':
    n = int(input())
    a = [int(i) for i in input().split()]
    a.sort(reverse=True)
    ans = 0
    dict = {}
    for i in a:
        if len(dict) > 0:
            ans+=max(dict)
            if dict[max(dict)] ==1:
                dict.pop(max(dict))
            else:
                dict[max(dict)]-=1
            if i in dict:
                dict[i]+=2
            else:
                dict[i]=2


        else:
            dict[i]=1


    print(ans)

Submission status: Time Limit Exceeded

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

list.sort(inList, reverse=True)
circleList = []
point = 0

def best_point(circleList):
    out,bigger,index = 0,0,0
    for i in range(len(circleList)):
        lesser = min([circleList[i-1], circleList[i]])
        if lesser > out:
            out = lesser
            index = i
    return out, index

for i in inList:
    if i == 0:
        circleList.append(i)
    else:
        tmpPoint,index = best_point(circleList)
        circleList.insert(index, i)
        point += tmpPoint
print(point)

Submission status: Time Limit Exceeded

Solution code:
from itertools import permutations
_ = input()
li = sorted(list(map(int, input().split())))
score = 0
status = []

tmp_1 = li.pop()
status.append(tmp_1)
tmp_2 = li.pop()
score+=tmp_1
status.append(tmp_2)

for _ in range(len(li)):
  tmp = li.pop()
  max_status = []
  max_score = score
  for i in range(len(status)):
    if min(status[i-1], status[i])+score > max_score:
      left = status.copy()
      right = status.copy()
      max_score = min(status[i-1], status[i])+score
      max_status = (left[:i] + [tmp] + right[i:]).copy()
      
  score = max_score
  status = max_status.copy()

print(score)

Submission status: Time Limit Exceeded

Solution code:
N = int(input())
A = list(map(int, input().split()))
# print(N)
# print(A)
A.sort(reverse=True)
ans = 0
N -= 2
ans += A[0]
A.pop(0)
# print("test" + str(ans))
if N == 0:
    print(ans)
    exit()

while N > 0:
    if N == 0:
        print(ans)
        exit()
    elif N == 1:
        ans += A[0]
        print(ans)
        exit()
    elif N >= 2:
        ans += 2 * A[0]
        N -= 2
        A.pop(0)
        if N == 0:
            print(ans)
            exit()

Submission status: Time Limit Exceeded

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

#N = 12
#A = [12,9,9,6,6,5,5,4,3,2,1,1]

ret = 0
array = []
array.append(A.pop())
while len(A) > 0:
    #print('array', array)
    comfort = [0 for i in range(len(array))]
    for i in range(len(comfort)):
        comfort[i] = min(array[i-1], array[i])
    #print('comfort', comfort)
    val = max(comfort)
    ret += val
    #print('ret', ret)
    array.insert(comfort.index(val), A.pop())

print(ret)