Problem:


Score : 400 points

Problem Statement

Your friend gave you a dequeue D as a birthday present.

D is a horizontal cylinder that contains a row of N jewels.

The values of the jewels are V_1, V_2, ..., V_N from left to right. There may be jewels with negative values.

In the beginning, you have no jewel in your hands.

You can perform at most K operations on D, chosen from the following, at most K times (possibly zero):

Operation A: Take out the leftmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty.

Operation B: Take out the rightmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty.

Operation C: Choose a jewel in your hands and insert it to the left end of D. You cannot do this operation when you have no jewel in your hand.

Operation D: Choose a jewel in your hands and insert it to the right end of D. You cannot do this operation when you have no jewel in your hand.

Find the maximum possible sum of the values of jewels in your hands after the operations.

Constraints

All values in input are integers.

1 \leq N \leq 50

1 \leq K \leq 100

-10^7 \leq V_i \leq 10^7

Input

Input is given from Standard Input in the following format:

N K
V_1 V_2 ... V_N

Output

Print the maximum possible sum of the values of jewels in your hands after the operations.

Sample Input 1

6 4
-10 8 2 1 2 6

Sample Output 1

14

After the following sequence of operations, you have two jewels of values 8 and 6 in your hands for a total of 14, which is the maximum result.

Do operation A. You take out the jewel of value -10 from the left end of D.

Do operation B. You take out the jewel of value 6 from the right end of D.

Do operation A. You take out the jewel of value 8 from the left end of D.

Do operation D. You insert the jewel of value -10 to the right end of D.

Sample Input 2

6 4
-6 -100 50 -2 -5 -3

Sample Output 2

44

Sample Input 3

6 3
-6 -100 50 -2 -5 -3

Sample Output 3

0

It is optimal to do no operation.



Sample input:
6 4
-10 8 2 1 2 6


Sample output:
14

Submission status: Runtime Error

Solution code:
from heapq import heapify, heappop

N, K = map(int, input().split())
vl = list(map(int, input().split()))
vr = list(reversed(vl))
sum_L = [0]
for vv in vl:
    sum_L.append(sum_L[-1] + vv)

sum_R = [0]
for vv in vr:
    sum_R.append(sum_R[-1] + vv)

ans = 0
for pick in range(K, -1, -1):
    put = K - pick
    if pick < put: break
    for left in range(pick + 1):
        m_put = put
        right = pick - left
        t = sum_L[left] + sum_R[right]
        q = vl[:left] + vr[:right]
        heapify(q)
        while m_put > 0:
            t -= heappop(q)
            m_put -= 1
        ans = max(ans, t)
print(ans)

Submission status: Runtime Error

Solution code:
def Base_10_to_n(X, n):
    if (int(X/n)):
        return Base_10_to_n(int(X/n), n)+str(X%n)
    return str(X%n)
def do_ju(move):
    if move==0:
        if not v_list:
            return(0)
        else:
            ju=v_list.pop(0)
            have_ju.append(ju)
            return(1)
    elif move==1:
        if not v_list:
            return(0)
        else:
            ju=v_list.pop(-1)
            have_ju.append(ju)
            return(1)
    elif move==2:
        if not have_ju:
            return(0)
        else:
            ju=min(have_ju)
            have_ju.remove(ju)
            v_list.insert(0,ju)
            return(1)
    elif move==3:
        if not have_ju:
            return(0)
        else:
            ju=min(have_ju)
            have_ju.remove(ju)
            v_list.insert(0,ju)
            return(1)
          
          
N,K=map(int,input().split())
v_list_origin=list(map(int,input().split()))

max_ju_total=0

for pattern in range(4**K):
    have_ju=[]
    four_num=Base_10_to_n(pattern,4).zfill(4)
    v_list=v_list_origin.copy()
    #print(v_list)
    for keta in range(K):
        move=four_num[keta]
        #print(move)
        check=do_ju(int(move))
        #print(check)
        #print(v_list)
        if check==0:
            have_ju=[]
            break
    #print(four_num)
    #print(v_list)
    #print(have_ju)
    ans=sum(have_ju)
    if max_ju_total<ans:
        max_ju_total=ans
print(max_ju_total)

Submission status: Runtime Error

Solution code:
from sys import stdin
import math
import fractions
from collections import deque
import itertools

INF = 10 ** 10

N, K = [int(x) for x in stdin.readline().rstrip().split()]
V = list(map(int,stdin.readline().rstrip().split()))

getV_num = min(N, K)

ans = 0

for i in range(getV_num//2+1, getV_num+1):
    toru = i
    sute = K-i

    #print(toru)
    #print(sute)
    
    for j in range(toru+1):
        leftn = j
        rightn = (toru - j)

        data = []


        for k in range(leftn):
            data.append(V[k])

        for k in range(rightn):
            data.append(V[N-1-k])

        data.sort()
        for _ in range(sute):
            if data[0] < 0:
                del data[0]
            else:
                break

        ans = max(ans, sum(data))

print(ans)

Submission status: Runtime Error

Solution code:
from heapq import heapify, heappop

N, K = map(int, input().split())
v = list(map(int, input().split()))

slice_L = [v[:i] for i in range(N + 1)]
sum_L = [0]
for vv in v:
    sum_L.append(sum_L[-1] + vv)

v.reverse()
slice_R = [v[:i] for i in range(N + 1)]
sum_R = [0]
for vv in v:
    sum_R.append(sum_R[-1] + vv)

ans = 0
for pick in range(K, 0, -1):
    put = K - pick
    if pick < put: break
    for left in range(pick + 1):
        m_put = put
        right = pick - left
        t = sum_L[left] + sum_R[right]
        q = slice_L[left] + slice_R[right]
        heapify(q)
        while m_put > 0:
            t -= heappop(q)
            m_put -= 1
        ans = max(ans, t)
print(ans)

Submission status: Runtime Error

Solution code:
from sys import stdin
import math
import fractions
from collections import deque
import itertools

INF = 10 ** 10

N, K = [int(x) for x in stdin.readline().rstrip().split()]
V = list(map(int,stdin.readline().rstrip().split()))

getV_num = min(N, K)

ans = 0

for i in range(getV_num//2+1, getV_num+1):
    toru = i
    sute = K-i

    #print(toru)
    #print(sute)
    
    for j in range(toru+1):
        leftn = j
        rightn = (toru - j)*-1

        #print(leftn, rightn)

        if rightn == 0:
            data = V[:leftn]
        elif leftn == 0:
            data = V[rightn:]
        else:
            data = V[:leftn] + V[rightn:]
        #print(data)

        for _ in range(sute):
            data.sort()
            if data[0] < 0:
                del data[0]

        #print(data)

        ans = max(ans, sum(data))

print(ans)

Submission status: Runtime Error

Solution code:
from heapq import heapify, heappop

N, K = map(int, input().split())
v = list(map(int, input().split()))

slice_L = [v[:i] for i in range(N + 1)]
sum_L = [0]
for vv in v:
    sum_L.append(sum_L[-1] + vv)

slice_R = [v[N - 1 - i:] for i in range(-1, N)]
sum_R = [0]
for vv in v[::-1]:
    sum_R.append(sum_R[-1] + vv)

ans = 0
for put, pick in enumerate(range(K, 0, -1), 0):
    if pick < put: break
    for left in range(pick + 1):
        m_put = put
        right = pick - left
        t = sum_L[left] + sum_R[right]
        q = slice_L[left] + slice_R[right]
        heapify(q)
        while m_put > 0 and q:
            t -= heappop(q)
            m_put -= 1
        if m_put > 0:
            continue
        ans = max(ans, t)
print(ans)

Submission status: Runtime Error

Solution code:
from sys import stdin
import math
import fractions
from collections import deque
import itertools

INF = 10 ** 10

N, K = [int(x) for x in stdin.readline().rstrip().split()]
V = [list(map(int,stdin.readline().rstrip().split()))]

getV_num = min(N, K)

for i in range(getV_num//2+1, getV_num+1):
    toru = i
    sute = K-i

    #print(toru)
    #print(sute)
    
    for j in range(toru+1):
        leftn = j
        rightn = (toru - j)*-1

        #print(leftn, rightn)

        if rightn == 0:
            data = V[:leftn]
        elif leftn == 0:
            data = V[rightn:]
        else:
            data = V[:leftn] + V[rightn:]
        #print(data)

        for _ in range(sute):
            data.sort()
            if data[0] < 0:
                del data[0]

        #print(data)

        ans = max(ans, sum(data))

print(ans)

Submission status: Runtime Error

Solution code:
from heapq import heapify, heappop

N, K = map(int, input().split())
v = list(map(int, input().split()))

slice_L = [v[:i] for i in range(N + 1)]
sum_L = [0]
for vv in v:
    sum_L.append(sum_L[-1] + vv)

slice_R = [v[N - 1 - i:] for i in range(-1, N)]
sum_R = [0]
for vv in v[::-1]:
    sum_R.append(sum_R[-1] + vv)

ans = 0
for put, pick in enumerate(range(K, 0, -1), 0):
    for left in range(pick + 1):
        m_put = put
        right = pick - left
        t = sum_L[left] + sum_R[right]
        q = slice_L[left] + slice_R[right]
        heapify(q)
        while m_put > 0 and q:
            t -= heappop(q)
            m_put -= 1
        if m_put > 0:
            continue
        ans = max(ans, t)
print(ans)

Submission status: Runtime Error

Solution code:
from sys import stdin

def main():
     def max_n_main(oku,kosu_s,kosu_e):
          if len(v)>oku:
               ans = max_n_sub(oku,kosu_s,kosu_e,v[:oku])
               ans = max(ans,max_n_sub(oku,kosu_s,kosu_e,v[len(v)-oku+1:]))
               for i in range(1,oku):
                    tmpv = v[:i]+v[len(v)-oku+i:]
                    ans = max(ans,max_n_sub(oku,kosu_s,kosu_e,tmpv))
               return ans
          elif len(v)==oku:
               return max(0,max_n_sub(oku,kosu_s,kosu_e,v.sort()))
          else:
               return 0
               
     def max_n_sub(oku,kosu_s,kosu_e,tmpv):#奥行きokuから個数kosu_sから個数kosu_eの間の個数をとる時価値が最大な値を返す
          tmpv.sort()
          ko = 0
          ans = 0
          for i in range(len(tmpv)):
               if ko < kosu_s:
                    ans += tmpv[len(tmpv)-i-1]
                    ko += 1
               else:
                    if ko == kosu_e:
                         break
                    elif tmpv[len(tmpv)-i-1]>0:
                         ans += tmpv[len(tmpv)-i-1]
                         ko += 1
                    else:
                         break
          return ans
          
     n,k= [int(x) for x in stdin.readline().split()]
     v = [int(x) for x in stdin.readline().split()]
     ans = 0
     for ki in range(1,k):
          ans = max(ans,max_n_main(ki,max(1,2*ki-k),ki))
     print(ans)
     return 0
     
if __name__ == '__main__':
     main()

Submission status: Runtime Error

Solution code:
from heapq import heapify, heappop

N, K = map(int, input().split())
v = list(map(int, input().split()))

slice_L = [v[:i] for i in range(N + 1)]
sum_L = [0]
for vv in v:
    sum_L.append(sum_L[-1] + vv)

slice_R = [v[N - 1 - i:] for i in range(-1, N)]
sum_R = [0]
for vv in v[::-1]:
    sum_R.append(sum_R[-1] + vv)

ans = 0
for put, pick in enumerate(tuple(range(K, 0, -1)), 0):
    for left in range(pick + 1):
        m_put = put
        right = pick - left
        t = sum(slice_L[left]) + sum(slice_R[right])
        q = slice_L[left] + slice_R[right]
        heapify(q)
        while m_put > 0 and q:
            t -= heappop(q)
            m_put -= 1
        if m_put > 0:
            continue
        ans = max(ans, t)
print(ans)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
V=list(map(int,input().split()))
ans=[0]
if N==1:
    print(V[0]*(K%2))
else:    
    for i in range(K+1):
    #print(i)
        for j in range(i+1):
            if N<=i:
                temp=sorted(V)
            elif j-i!=0:
                temp=sorted(V[:j]+V[j-i:])
            else:
                temp=sorted(V[:j])
            t=0
            for k in range(i):
                if temp[k]>=0:
                    t=k+1
                    break
            #print(temp,t,j,j-i)
        #if t==0:
        #    print(":",0)
            if t>K-i:
                #print("1",temp[K-i:])
                #print("1",sum(temp[K-i:]))
                ans.append(sum(temp[K-i:]))
            elif (K-1-t)%2==1:
                if t>=2:
                  #  print("2",max(sum(temp[t-2:]),sum(temp[t:])))
                    ans.append(max(sum(temp[t-2:]),sum(temp[t:])))
                else:
                 #   print("3",sum(temp[t:]))
                    ans.append(sum(temp[t:]))
            else:
                #print("4",sum(temp[t-1:]))
                ans.append(sum(temp[t-1:]))
    print(max(ans))

Submission status: Runtime Error

Solution code:
# coding: utf-8

import sys
input = sys.stdin.readline

# N = int(input())
N, K = map(int, input().split())
D = [int(x) for x in input().split()]
# S = input()

ans = 0
for l in range(N + 1):
    if l > K:
        break

    for r in range(N - l + 1):
        if l + r > K:
            break

        value_list = D[0 : l] + D[N - r : N]
        sort(value_list)
        
        for i in range(K - (l + r) + 1):
            ans = max(ans, sum(value_list[i:]))
        
print(ans)

Submission status: Runtime Error

Solution code:
import bisect

N,K=map(int,input().split())
V=list(map(int,input().split()))

ans=0
for i in range(1,K+1):
    if i<=N:
        for j in range(i+1):
            right=V[:i-j]
            left=V[N-j:]
            right.extend(left)
            right.sort()
            index = bisect.bisect_left(right, 0)
            if index + i <= K:
                if index<len(right):
                    ans = max(ans, sum(right[index:]))
            else:
                les = K - i
                if 0<= les < len(right):
                    ans = max(ans, sum(right[les:]))
    else:
        rignt=V
        rignt.sort()
        index = bisect.bisect_left(right, 0)
        if index + i <= K:
            if index < len(right):
                ans = max(ans, sum(right[index:]))
        else:
            les = K - i
            if 0<= les < len(right):
                ans = max(ans, sum(tmp[les:]))



print(ans)

Submission status: Runtime Error

Solution code:
def f(s, v, k):
    if k == 0:
        return sum(s)
    v1 = v[::]
    s1 = s[::]
    v2 = v[::]
    s2 = s[::]
    v3 = v[::]
    s3 = s[::]
    s3.append(v3.pop(0))
    v4 = v[::]
    s4 = s[::]
    s4.append(v4.pop(-1))
    a = -1 * (10 ** 7)
    b = -1 * (10 ** 7)
    if len(s) > 0:
        ms = min(s)
        v1.insert(0, ms)
        s1.pop(s1.index(ms))
        v2.append(ms)
        s2.pop(s2.index(ms))
        a = f(s1, v1, k - 1)
        b = f(s2, v2, k - 1)
    c = f(s3, v3, k - 1)
    d = f(s4, v4, k - 1)
    return max(a, b, c, d)


def main():
    N, K = map(int, input().split())
    V = list(map(int, input().split()))
    ans = max(f([V[0]], V[1:], K - 1), f([V[-1]], V[:-1], K - 1))
    if ans > 0:
        print(ans)
    else:
        print(0)


main()

Submission status: Runtime Error

Solution code:
from heapq import heapify, heappop, heappush

N, K = map(int, input().split())
v = list(map(int, input().split()))

slice_L = [list(sorted(v[:i])) for i in range(N + 1)]
sum_L = [sum(slice_L[i]) for i in range(N + 1)]

slice_R = [list(sorted(v[N - 1 - i:])) for i in range(-1, N)]
sum_R = [sum(slice_R[i]) for i in range(N + 1)]

ans = 0
for put, pick in enumerate(tuple(range(K, 0, -1)), 0):
    for left in range(pick + 1):
        m_put = put
        right = pick - left
        t = sum(slice_L[left]) + sum(slice_R[right])

        q = slice_L[left] + slice_R[right]
        heapify(q)
        while m_put > 0 and q:
            t -= heappop(q)
            m_put -= 1
        if m_put > 0:
            continue
        ans = max(ans, t)
print(ans)

Submission status: Runtime Error

Solution code:
N,K=map(int,input().split())
V=list(map(int,input().split()))
ans=[0]
for i in range(K+1):
    #print(i)
    for j in range(i+1):
        if N<=i:
            temp=sorted(V)
        elif j-i!=0:
            temp=sorted(V[:j]+V[j-i:])
        else:
            temp=sorted(V[:j])
        t=0
        for k in range(i):
            if temp[k]>=0:
                t=k+1
                break
        #print(temp,t)
        #if t==0:
        #    print(":",0)
        if t>K-i:
            #print(":",temp[K-i:])
            ans.append(sum(temp[K-i:]))
        elif (K-1-t)%2==1:
            if t>=2:
                #print(":",max(sum(temp[t-2:]),sum(temp[t:])))
                ans.append(max(sum(temp[t-2:]),sum(temp[t:])))
            else:
                #print(":",sum(temp[t:]))
                ans.append(sum(temp[t:]))
        else:
            #print(":",sum(temp[t-1:]))
            ans.append(sum(temp[t-1:]))
print(max(ans))

Submission status: Runtime Error

Solution code:
N,K=input().split()

N = int(N)
K = int(K)

chance = K
v = list(int(i) for i in input().split())
j=0
count = 0

print(v)
i = 0
while (K > 0):
  if(v[i] != None):
    if(v[i] > 0):
      count = count + v[i]
    v[i] = None
    if(v[N-1] != None):
      if(v[N-1] > 0):
        count = count + v[N-1]
      v[N-1] = None
  i+=1
  N-=1
  K-=1

print(count)

Submission status: Runtime Error

Solution code:
a,k=map(int,input().split())
L=list(map(int,input().split()))
ans=0
 
for i in range(min(k,a)+1):
  for j in range(min(k,a)-i+1):
    S=L[:i]+L[a-j:]
    S.sort()
    x=0
    while x < k-(j+i):
      if S[x]>=0:
        break
      x+=1
    ans=max(ans,sum(S)-sum(S[:x]))
    
print(ans)

Submission status: Runtime Error

Solution code:
N,K = map(int,input().split())
D = list(map(int,input().split()))
L, R = [0]*(K),[0]*K
for i in range(K+1):
    tmp = D[:i]
    tmp.sort()
    X = sum(tmp)
    for j in range(min(K-i,i)):
        if tmp[0] < 0:
            X-=tmp.pop(0)
        else:
            L[i+j] = max(L[i+j],X)
            break
        L[i+j] = max(L[i+j],X)

for i in range(K+1):
    tmp = D[N-i:]
    tmp.sort()
    X = sum(tmp)
    for j in range(min(K-i,i)):
        if tmp[0] < 0:
            X-=tmp.pop(0)
        else:
            R[i+j] = max(R[i+j],X)
            break
        R[i+j] = max(R[i+j],X)

ans = []
for i in range(K):
    ans.append(L[i]+R[-i-1])
print(max(ans))

Submission status: Runtime Error

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

for i in range(min(k,n)+1):
  for j in range(min(k,n)-i+1):
    S=[0]+L[:i]+L[a-j:]
    S.sort()
    x=0
    while x < k-(j+i):
      if S[x]>=0:
        break
      x+=1
    ans=max(ans,sum(S)-sum(S[:x]))
    
print(ans)

Submission status: Runtime Error

Solution code:
import math, string, itertools, fractions, heapq, collections, re, array, bisect, copy, functools, random
import sys
from collections import deque, defaultdict, Counter; from heapq import heappush, heappop
from itertools import permutations, combinations, product, accumulate, groupby
from bisect import bisect_left, bisect_right, insort_left, insort_right
from operator import itemgetter as ig
sys.setrecursionlimit(10 ** 7)
inf = 10 ** 20; INF = float("INF"); ans = 0; tmp = 0; ansli = []; tmpli = []; candili = []
eps = 1.0 / 10 ** 10; mod = 10 ** 9 + 7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]; ddn = dd + [(-1, 1), (1, 1), (1, -1), (-1, -1)]; ddn9 = ddn + [(0, 0)]
"""for dx, dy in dd:
        nx = j + dx; ny = i + dy
            if 0 <= nx < w and 0 <= ny < h:"""
def wi(): return list(map(int, sys.stdin.readline().split()))
def wip(): return [int(x) - 1 for x in sys.stdin.readline().split()]#WideIntPoint
def ws(): return sys.stdin.readline().split()
def i(): return int(sys.stdin.readline())
def s(): return input()
def hi(n): return [i() for _ in range(n)]
def hs(n): return [s() for _ in range(n)]#HeightString
def mi(n): return [wi() for _ in range(n)]#MatrixInt
def mip(n): return [wip() for _ in range(n)]
def ms(n): return [ws() for _ in range(n)]

n, k = wi()
v = wi()
v = deque(v)
pq = []
po = 0
cnt = 0
for i in range(k + 1):
    if i != 0:
        heappush(pq, v[i - 1])
    for j in range(k + 1 - i):
        if j != 0:
            heappush(pq, v[-j])
        while k - i - j - cnt > 0:
            if pq != []:
                po = 0
                po = heappop(pq)
                if po < 0:
                    cnt += 1
                    tmpli.append(po)
                else:
                    heappush(pq, po)
                    cnt = 0
                    break
            else:
                break
        cnt = 0
        ans = max(ans, sum(pq))
        for bb in tmpli:
            heappush(pq, bb)
        tmpli = []
    pq = []
    for g in range(i + 1):
        if g != 0:
            heappush(pq, v[g - 1])

print(ans)

Submission status: Runtime Error

Solution code:
import copy
import heapq
from collections import deque
from functools import lru_cache

N, K = [int(i) for i in input().split()]
V = deque([int(i) for i in input().split()])

maximum = -1
for k in range(K):
    for offset in range(k):
        acc = 0
        heap = []
        for j in range(k):
            if V[j-offset] < 0:
                heapq.heappush(heap, V[j-offset])
            acc += V[j-offset]
            for m in range(K-k+1):
                if len(heap) > 0:
                    acc -= heapq.heappop(heap)
        maximum = max(maximum, acc)

print(maximum)

Submission status: Runtime Error

Solution code:
import bisect

N,K=map(int,input().split())
V=list(map(int,input().split()))

ans=0
for i in range(1,K+1):
    if i<N:
        for j in range(i+1):
            right=V[:i-j]
            left=V[N-j:]
            right.extend(left)
            right.sort()
            index = bisect.bisect_left(right, 0)
            if index + i <= K:
                if index<len(right):
                    ans = max(ans, sum(right[index:]))
            else:
                les = K - i
                if 0<= les < len(right):
                    ans = max(ans, sum(right[les:]))
    else:
        rignt=V
        rignt.sort()
        index = bisect.bisect_left(right, 0)
        if index + i <= K:
            if index < len(right):
                ans = max(ans, sum(right[index:]))
        else:
            les = K - i
            if 0 <= les < len(right):
                ans = max(ans, sum(tmp[les:]))



print(ans)

Submission status: Runtime Error

Solution code:
a,b = map(int,input().split())
A = list(map(int,input().split()))
c = 0
d = 0
tmp1 = 0
tmp2 = 0
count1 = 0
count2 = 0
M = 0
m = 0
while True:
    tmp1 = c
    c = A[0+count1]
    count1= count1 + 1
    if c >= tmp1:
        M = c
    if (count1 + count2) >= b:
        break
    tmp2 = d
    d = A[-1-count2]
    count2 = count2 + 1
    if d >= tmp2:
        m = d
    if (count1 + count2) >= b:
        break
print(M+m)

Submission status: Runtime Error

Solution code:
import math

n,k = list(map(int, input().split()))
val = list(map(int, input().split()))

val2 = [val[n-i-1] for i in range(n)]
#print(val2)

valcase = []
valcase.append([[],[]])

for ii in range(k):
    i=ii+1
    valcase.append([[],[]])
    for j in range(min(i,n)):
        valtemp=val[j]
        if(valtemp>=0):
            valcase[i][1].append(valtemp)
        else:
            valcase[i][0].append(valtemp)
    list.sort(valcase[i][0])
    list.sort(valcase[i][1],reverse=True)
#print(valcase)

left = [[] for i in range(k+1)]

for i in range(k):
    j=i+1
    left[j] = [[] for ii in range((j+1)//2)]
    
#print(left)

for ii in range(k):
    i=ii+1
    if(i==1):
        left[i][0]=val[0]
        continue
    for j in range((i+1)//2):
        if(j==0):
            left[i][0] = left[i-1][0] + val[i-1]
        else:
            if(len(valcase[i-j-1][0])<j):
                left[i][j] = left[i-1][j-1]
            else:
                left[i][j] = left[i-1][j-1] - valcase[i][0][j-1]

#print("left")
#print(left)
left2 = left

val=val2

valcase = []
valcase.append([[],[]])

for ii in range(k):
    i=ii+1
    valcase.append([[],[]])
    for j in range(min(i,n)):
        valtemp=val[j]
        if(valtemp>=0):
            valcase[i][1].append(valtemp)
        else:
            valcase[i][0].append(valtemp)
    list.sort(valcase[i][0])
    list.sort(valcase[i][1],reverse=True)
#print(valcase)

left = [[] for i in range(k+1)]

for i in range(k):
    j=i+1
    left[j] = [[] for ii in range((j+1)//2)]
    
#print(left)

for ii in range(k):
    i=ii+1
    if(i==1):
        left[i][0]=val[0]
        continue
    for j in range((i+1)//2):
        if(j==0):
            left[i][0] = left[i-1][0] + val[i-1]
        else:
            if(len(valcase[i-j-1][0])<j):
                left[i][j] = left[i-1][j-1]
            else:
                left[i][j] = left[i-1][j-1] - valcase[i][0][j-1]

#print("right")
#print(left)

right=left
left=left2
left[0].append(0)
right[0].append(0)

maxim = 0

for i in range(k+1):
    for j in range(k-i+1):
    
        temp=max(left[i])+max(right[j])
        if(maxim<temp):
            maxim=temp
            
print(maxim)

Submission status: Runtime Error

Solution code:
import sys
from collections import deque
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsm = lambda: map(str, sys.stdin.readline().split())

n, k = nm()
v = nl()
d = deque(v)
ans = 0
for m in range(k + 1):
    lis = d.copy()
    neg = []
    summ = 0
    for i in range(m):
        lis = d.copy()
        neg = []
        summ = 0
        if len(lis) != 0:
            l = lis.popleft()
            summ += l
            if l < 0:
                neg.append(l)
            for j in range(m - i):
                if len(lis) != 0:
                    l = lis.pop()
                    summ += l
                    if l < 0:
                        neg.append(l)
            neg.sort()
            for o in range(k - m):
                if o < len(neg):
                    summ -= neg[o]
                else:
                    break
    ans = max(ans, summ)
print(ans)

Submission status: Runtime Error

Solution code:
from copy import copy

n,k = (int(num) for num in input().split())
v = [int(num) for num in input().split()]

def p(k,v):
    ret = 0
    for i in range(k+1):
        vt = copy(v)
        vt = vt[:i]
        vt.sort(reverse=True)
        t = 0 
        #print(i,len(vt))
        for j in range(i):
            t += vt[j]
            if j+1 >= i*2-k and t > ret:
                ret = t
    return ret

def length(v):
    ret = 0
    for num in v:
        if num < 0:
            ret += 2
        else:
            ret += 1
    return ret
            
vr = copy(v)
vr.reverse()
ans = 0

k = min(k,length(v)-1)
for i in range(k+1):
    t = p(i,v) + p(k-i,vr)
    #print(i,p(i,v),p(k-i,vr),t)
    if t > ans:
        ans = t
print(ans)

Submission status: Runtime Error

Solution code:
n,m=map(int,input().split())
D=list(map(int,input().split(" ")))
answer=-99999999999
 
for i in range(m+1):
    take=i
    trash=m-i
    for j in range(take+1):
        if j>=n:
            goukei=D
        else:
            left=j
            right=j-take
            leftjewel=D[:left]
            rightjewel=D[right:]
            if right==0:
                rightjewel=[]
            goukei=leftjewel+rightjewel
        goukei.sort()
        for i in range(trash):
            if goukei!=[]:
                if goukei[0]<0:
                    del goukei[0]
        if sum(goukei)>answer:
            answer=sum(goukei)
print(answer)
n,m=map(int,input().split())
D=list(map(int,input().split(" ")))
answer=-99999999999

for i in range(m+1):
    take=i
    trash=m-i
    for j in range(take+1):
        if j>=n:
            goukei=D
        else:
            left=j
            right=j-take
            leftjewel=D[:left]
            rightjewel=D[right:]
            if right==0:
                rightjewel=[]
            if left-right>n:
              	rightjewel=D[n-right+1:]
            goukei=leftjewel+rightjewel
        goukei.sort()
        for i in range(trash):
            if goukei!=[]:
                if goukei[0]<0:
                    del goukei[0]
        if sum(goukei)>answer:
            answer=sum(goukei)
print(answer)
提出情報
提出日時

Submission status: Runtime Error

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

slice_L = [list(sorted(v[:i])) for i in range(N + 1)]
sum_L = [sum(slice_L[i]) for i in range(N + 1)]

slice_R = [list(sorted(v[N - 1 - i:])) for i in range(-1, N)]
sum_R = [sum(slice_R[i]) for i in range(N + 1)]
# print(slice_L)
# print(slice_R)

ans = 0
for put, pick in enumerate(tuple(range(K, 0, -1)), 0):
    for left in range(pick + 1):
        ff = False
        m_put = put
        right = pick - left
        t = sum(slice_L[left]) + sum(slice_R[right])
        sl_i = 0
        sr_i = 0
        # print(m_put)
        while m_put > 0:
            # print(right, sr_i, slice_R[right])
            if sl_i == left:
                if sr_i == right:
                    ff = True
                    break
                t -= slice_R[right][sr_i]
                sr_i += 1
            elif sr_i == right:
                t -= slice_L[left][sl_i]
                sl_i += 1
            elif slice_L[left][sl_i] < slice_R[right][sr_i]:
                t -= slice_L[left][sl_i]
                sl_i += 1
            elif slice_L[left][sl_i] > slice_R[right][sr_i]:
                # print('test')
                t -= slice_R[right][sr_i]
                sr_i += 1
            else:
                if sl_i + 1 == left:
                    if sr_i == right:
                        ff = True
                        break
                    t -= slice_R[right][sr_i]
                    sr_i += 1
                elif sr_i + 1 == right:
                    t -= slice_L[left][sl_i]
                    sl_i += 1
                else:
                    flg = False
                    k = 0
                    while slice_L[left][sl_i + k] == slice_R[right][sr_i + k]:
                        k += 1
                        if sl_i + k == left:
                            t -= slice_R[right][sr_i]
                            sr_i += 1
                            flg = True
                            break
                        elif sr_i + k == right:
                            t -= slice_L[left][sl_i]
                            sl_i += 1
                            flg = True
                            break
                    if not flg:
                        if sl_i + k == left and sr_i + k == right:
                            t -= slice_L[left][sl_i]
                            sl_i += 1
                        elif slice_L[left][sl_i + k] < slice_R[right][sr_i + k]:
                            t -= slice_L[left][sl_i]
                            sl_i += 1
                        elif slice_L[left][sl_i + k] > slice_R[right][sr_i + k]:
                            t -= slice_R[right][sr_i]
                            sr_i += 1
            m_put -= 1
        # print(pick, left, right, t, slice_R[right], slice_L[left])
        if ff: continue
        ans = max(ans, t)
print(ans)