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

Solution code:
# coding: utf-8
# Your code here!
N,K=map(int,input().split())
V=list(map(int,input().split()))

left=[0]
right=[0]

for n in range(N):
    left.append(V[n]+left[n])
for n in range(N):
    right.append(V[-n-1]+right[n])

#print(left)
#print(right)

dp=[[0]*(K+1) for _ in range(K+1)]

for l in range(min(K+1,N+1)):
    for r in range(min(K-l+1,N-l+1)):
        dp[l][r]=left[l]+right[r]
        temp=V[:l]+V[::-1][:r]#ここが不味い
        #print(temp)
        temp.sort()
        
        
        for i in range(min(K-l-r,N-l-r)):
            #ここでマイナス項を引きたい
            if len(temp)==0 or temp[0]>=0:
                break
            else:
                dp[l][r]-=temp.pop(0)
ans=0
for item in dp:
    for obj in item:
        ans=max(ans,obj)
#print(dp)
print(ans)

Submission status: Wrong Answer

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

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

ans = 0
for a in range(k+1):
    suba = vl[:a]
    for b in range(min(n-a, k+1-a)):
        subb = vl[-b:]
        if b == 0:
            subb = []
        sub = suba + subb
        ssub = sorted(sub)
        for _ in range(k-a-b):
            if len(ssub) == 0:
                break
            if ssub[0] < 0:
                ssub.pop(0)
            else:
                break
        if sum(ssub) > ans:
            ans = sum(ssub)

print(ans)

Submission status: Wrong Answer

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

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

ans = 0
for a in range(k+1):
    suba = vl[:a]
    for b in range(k+1-a):
        subb = vl[-b:]
        if b == 0:
            subb = []
        sub = suba + subb
        ssub = sorted(sub)
        for _ in range(k-a-b):
            if len(ssub) == 0:
                break
            if ssub[0] < 0:
                ssub.pop(0)
            else:
                break
        if sum(ssub) > ans:
            ans = sum(ssub)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
arr = list(map(int,input().split()))
_max = min(N,K)
val_arr = []
for R in range(_max+1):
  max_L = _max-R
  for L in range(max_L+1):
    hand = arr[:R]
    max_D = _max-R-L
    if L!=0:
      hand.extend(arr[-L:])
    hand = sorted(hand)
    
    
    n_num = len([h for h in hand if h < 0])
    D = min(max_D,n_num)
    value = sum(hand[D:])
    val_arr.append(value)
print(max(val_arr))

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left


def p_d():
    n, k = map(int, input().split())
    *v, = map(int, input().split())
    ans = 0
    for _l in range(k):
        for _r in range(k - _l):
            if _l + _r > n:
                break
            d = k - (_l + _r)
            s = v[:_l] + v[n - _r:]
            s.sort()
            d = min(len(s), d)
            ans = max(ans, sum(s[bisect_left(s, 0, 0, d):]))

    print(ans)
    
p_d()

Submission status: Wrong Answer

Solution code:
from collections import deque

N, K =map(int, input().split())
V=list(map(int, input().split()))
Vd = deque(V)
score = 0

for a in range(min(K,N)+1):
  for b in range(min(K-a+1,N-a+1)):
    print([a,b])
    hou = []
    Vd = deque(V)
    for _ in range(a):
      hou.append(Vd.popleft())
    for _ in range(b):
      hou.append(Vd.pop())
    hou.sort()
    for _ in range(K-a-b):
      if hou:
        if hou[0]>0:
          break
        hou.pop(0)
    score = max(score,sum(hou))
    
print(score)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,k=map(int,input().split())
    V=list(map(int,input().split()))

    from itertools import product
    import heapq
    score=0
    # l,r : 左、右から取り出す個数
    for l,r in product(range(k),repeat=2):
        if(l+r>k or l+r>n): continue
        A=[]
        for i in range(l):
            heapq.heappush(A,V[i])
        for i in range(n-r,n):
            heapq.heappush(A,V[i])
        for _ in range(k-l-r):
            if(A):
                a=heapq.heappop(A)
                if(a>0):
                    heapq.heappush(A,a)
        score=max(score,sum(A))
    print(score)
resolve()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = [int(i) for i in input().split()]

print(N, K)
print(V)

ans = -1
for i in range(min(N + 1, K + 1)): #i個ゲット
    for l in range(i + 1): #左からいくつのindexまでとるか
        a = V[0:l] + V[N - (i - l):N]
        a.sort()

        # K- i 個負のvalueを捨てれる
        t = 0
        s = sum(a)
        while i > t and K - i > t:
            if a[t] >= 0:
                break
            s -= a[t] #負の減算なので加算
            t += 1
        ans = max(ans, s)

print(ans)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
v=list(map(int,input().split()))
kaisuu = min(n,k)
res = 0
for i in range(kaisuu+1):
    for j in range(kaisuu-i+1):
        tres = v[:i]+v[n-j:]
        tres.sort(reverse=True)
        for l in range(kaisuu-i-j):
            if not tres: break
            if tres[-1] < 0:
                tres.pop()
            else:break
        res = max(res,sum(tres))

print(res)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
V = list(map(int,input().split()))
memo = [[0 for _ in range(N+1)] for _ in range(N+1)]
add = [[0 for _ in range(N+1)] for _ in range(N+1)]
add_count = [[max(K-i-j,0) for j in range(N+1)] for i in range(N+1)]
q = []
limit = min(N,K)
for i in range(1,N+1):
    if V[i-1] < 0:
        q.append([i,V[i-1]])
    for j in range(N+1):
        if i+j <= limit:
            memo[i][j] = memo[i-1][j] + V[i-1]
for j in range(1,N+1):
    for i in range(N+1):
        if i+j <= limit:
            memo[i][j] = memo[i][j-1] + V[N-j]

q.sort(key=lambda x:x[1])
for index,minus in q:
    for j in range(N+1):
        for i in range(index,N+1):
            if add_count[i][j] > 0:
                add[i][j] -= minus
                add_count[i][j] -= 1
    for i in range(index):
        for j in range(N-index+1,N+1):
            if add_count[i][j] > 0:
                add[i][j] -= minus
                add_count[i][j] -= 1
ans = 0
for i in range(N+1):
    for j in range(N+1):
        ans = max(ans,memo[i][j]+add[i][j])
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
V = list(map(int,input().split()))
memo = [[0 for _ in range(N+1)] for _ in range(N+1)]
add = [[0 for _ in range(N+1)] for _ in range(N+1)]
add_count = [[max(K-i-j,0) for j in range(N+1)] for i in range(N+1)]
q = []
for i in range(1,N+1):
    if V[i-1] < 0:
        q.append([i,V[i-1]])
    for j in range(N+1):
        if i+j <= K:
            memo[i][j] = memo[i-1][j] + V[i-1]
for j in range(1,N+1):
    for i in range(N+1):
        if i+j <= K:
            memo[i][j] = memo[i][j-1] + V[N-j]

q.sort(key=lambda x:x[1])
for index,minus in q:
    for j in range(N+1):
        for i in range(index,N+1):
            if add_count[i][j] > 0:
                add[i][j] -= minus
                add_count[i][j] -= 1
    for i in range(index):
        for j in range(N-index+1,N+1):
            if add_count[i][j] > 0:
                add[i][j] -= minus
                add_count[i][j] -= 1
ans = 0
for i in range(N+1):
    for j in range(N+1):
        ans = max(ans,memo[i][j]+add[i][j])
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
V = list(map(int,input().split()))
memo = [[0 for _ in range(N+1)] for _ in range(N+1)]
add = [[0 for _ in range(N+1)] for _ in range(N+1)]
add_count = [[max(K-i-j,0) for j in range(N+1)] for i in range(N+1)]
q = []
for i in range(1,N+1):
    if V[i-1] < 0:
        q.append([i,V[i-1]])
    for j in range(N+1):
        if i+j <= K:
            memo[i][j] = memo[i-1][j] + V[i-1]
for j in range(1,N+1):
    for i in range(N+1):
        if i+j <= K:
            memo[i][j] = memo[i][j-1] + V[N-j]

q.sort(key=lambda x:x[1])
for index,minus in q:
    for j in range(N+1):
        for i in range(index,N+1):
            if add_count[i][j] > 0:
                add[i][j] -= minus
                add_count[i][j] -= 1
    for i in range(index):
        for j in range(N-index+1,N+1):
            if add_count[i][j] > 0:
                add[i][j] -= minus
                add_count[i][j] -= 1
ans = 0
for i in range(N):
    for j in range(N):
        ans = max(ans,memo[i][j]+add[i][j])
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
V = list(map(int,input().split()))
memo = [[0 for _ in range(N+1)] for _ in range(N+1)]
add = [[0 for _ in range(N+1)] for _ in range(N+1)]
add_count = [[max(K-i-j,0) for j in range(N+1)] for i in range(N+1)]
q = []
for i in range(1,N+1):
    if V[i-1] < 0:
        q.append([i,V[i-1]])
    for j in range(N+1):
        if i+j <= K:
            memo[i][j] = memo[i-1][j] + V[i-1]
for j in range(1,N+1):
    for i in range(N+1):
        if i+j <= K:
            memo[i][j] = memo[i][j-1] + V[N-j]

q.sort(key=lambda x:x[1])
for index,minus in q:
    for j in range(N):
        for i in range(index,N):
            if add_count[i][j] > 0:
                add[i][j] -= minus
                add_count[i][j] -= 1
    for i in range(index):
        for j in range(N-index+1,N):
            if add_count[i][j] > 0:
                add[i][j] -= minus
                add_count[i][j] -= 1
ans = 0
for i in range(N):
    for j in range(N):
        ans = max(ans,memo[i][j]+add[i][j])
print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int,input().split())
V = list(map(int,input().split()))
memo = [[0 for _ in range(N+1)] for _ in range(N+1)]
add = [[0 for _ in range(N+1)] for _ in range(N+1)]
add_count = [[max(K-i-j,0) for j in range(N+1)] for i in range(N+1)]
q = []
for i in range(1,N+1):
    if V[i-1] < 0:
        q.append([i,V[i-1]])
    for j in range(N+1):
        if i+j <= K:
            memo[i][j] = memo[i-1][j] + V[i-1]
for j in range(1,N+1):
    for i in range(N+1):
        if i+j <= K:
            memo[i][j] = memo[i][j-1] + V[N-j]

q.sort(key=lambda x:x[1])
for index,minus in q:
    for j in range(N):
        for i in range(index,N):
            if add_count[i][j] > 0:
                add[i][j] -= minus
                add_count[i][j] -= 1
    for i in range(index):
        for j in range(N-index+1,N):
            if add_count[i][j] > 0:
                add[i][j] -= minus
                add_count[i][j] -= 1
ans = 0
for i in range(N):
    for j in range(N):
        ans = max(ans,memo[i][j]+add[i][j])

Submission status: Wrong Answer

Solution code:
from heapq import heapify, heappop, heappush
N, K = map(int, input().split())
V = list(map(int, input().split()))
answer = 0

for i in range(K + 1):
    for j in range(K - i + 1):
        heap = V[:i] + V[N - j:]
        heapify(heap)
        for _ in range(K - i - j):
            if heap:
                temp = heappop(heap)
            else:
                heappush(heap, 0)
                break
            if temp >= 0:
                heappush(heap, temp)
                break
        answer = max(answer, sum(heap))

print(answer)

Submission status: Wrong Answer

Solution code:
from heapq import heapify, heappop, heappush
N, K = map(int, input().split())
V = list(map(int, input().split()))
answer = 0

for i in range(K + 1):
    for j in range(K - i + 1):
        heap = V[:i] + V[N - j:]
        heapify(heap)
        for _ in range(K - i - j):
            if heap:
                temp = heappop(heap)
            else:
                break
            if temp >= 0:
                heappush(heap, temp)
                break
        answer = max(answer, sum(heap))

print(answer)

Submission status: Wrong Answer

Solution code:
import sys
n, k = [int(i) for i in sys.stdin.readline().split()]
v_ls = [int(i) for i in sys.stdin.readline().split()]
inv_v_ls = v_ls[::-1]
sum_a_b = min(n, k) + 1
best = 0
for a in range(sum_a_b):
    for b in range(sum_a_b - a):
        ls = v_ls[:a] + inv_v_ls[:b]
        ls.sort()
        for c in range(k - a - b):
            if c >= len(ls):
                break
            score = sum(ls[c:])
            if score > best:
                best = score
            if ls[c] > 0:
                break
print(best)

Submission status: Wrong Answer

Solution code:
import sys
n, k = [int(i) for i in sys.stdin.readline().split()]
v_ls = [int(i) for i in sys.stdin.readline().split()]
inv_v_ls = v_ls[::-1]
sum_a_b = min(n, k) + 1
best = 0
for a in range(sum_a_b):
    for b in range(sum_a_b - a):
        ls = v_ls[:a] + inv_v_ls[:b]
        ls.sort()
        for c in range(sum_a_b - a - b):
            if c >= len(ls):
                break
            score = sum(ls[c:])
            if score > best:
                best = score
            if ls[c] > 0:
                break
print(best)

Submission status: Wrong Answer

Solution code:
import sys
n, k = [int(i) for i in sys.stdin.readline().split()]
v_ls = [int(i) for i in sys.stdin.readline().split()]
inv_v_ls = v_ls[::-1]
sum_a_b = min(n, k) + 1
best = 0
for a in range(sum_a_b):
    for b in range(sum_a_b - a):
        ls = v_ls[:a] + inv_v_ls[:b]
        ls.sort()
        for c in range(sum_a_b - a - b):
            if c >= len(ls):
                break
            score = sum(ls[c:])
            print(score)
            if score > best:
                best = score
            if ls[c] > 0:
                break
print(best)

Submission status: Wrong Answer

Solution code:
import sys
n, k = [int(i) for i in sys.stdin.readline().split()]
v_ls = [int(i) for i in sys.stdin.readline().split()]
inv_v_ls = v_ls[::-1]
sum_a_b = min(n, k) + 1
best = 0
for a in range(sum_a_b):
    for b in range(sum_a_b - a):
        ls = v_ls[:a] + inv_v_ls[:b]
        ls.sort()
        for c in range(sum_a_b - a - b):
            if c >= len(ls):
                break
            score = sum(ls[c:])
            if score > best:
                best = score
            if v_ls[c] > 0:
                break
print(best)

Submission status: Wrong Answer

Solution code:
import math
N, K = map(int,input().split())
V = list(map(int,input().split()))
V_rev = list(reversed(V))
ans = 0
tmp = -1 * float("inf")
L = 0
R = 0

for A in range(min(N+1,K+1)):
  for B in range(min(N,K) - A):
    if A == 0 and B == 0:continue
    tmp = -1 * float("inf")
    if A == 0:
      tmp = sum(V_rev[:B])
    elif B == 0:
      tmp = sum(V[:A])
    else:
      tmp = sum(V[:A])+sum(V_rev[:B])
    if A == 0:
      push_list = V_rev[:B]
    elif B == 0:
      push_list = V[:A]
    else:
      push_list = V[:A] + V_rev[:B]
    push_list = sorted(push_list)
    #print(tmp,push_list,A,B)
    for i in range(min(len(push_list),K-(A+B))):
      if push_list[i] < 0:
        tmp += abs(push_list[i])
        #print("tmp",tmp)
    ans = max(ans,tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
import math
N, K = map(int,input().split())
V = list(map(int,input().split()))
V_rev = list(reversed(V))
ans = 0
tmp = -1 * float("inf")
L = 0
R = 0
for k in range(1,K+1):
  for take in range(math.floor(k/2),k+1):
    push = k - take
    for l in range(take+1):
      tmp = -1 * float("inf")
      r = take - l
      if l == 0:
        tmp = sum(V_rev[:r])
      elif r == 0:
        tmp = sum(V[:l])
      else:
        tmp = sum(V[:l])+sum(V_rev[:r])
      if l == 0:
        push_list = V_rev[:r]
      elif r == 0:
        push_list = V[:l]
      else:
        push_list = V[:l] + V_rev[:r]
      push_list = sorted(push_list)
      #print(tmp,push_list,push)
      for i in range(min(push,len(push_list))):
        if push_list[i] < 0:
          tmp += abs(push_list[i])
      #print("tmp",tmp)
      ans = max(ans,tmp)
print(ans)

Submission status: Wrong Answer

Solution code:
import math
N, K = map(int,input().split())
V = list(map(int,input().split()))
V_rev = list(reversed(V))
ans = 0
tmp = 0
L = 0
R = 0
for k in range(1,K+1):
  for take in range(math.floor(k/2),k+1):
    push = K - take
    tmp = 0
    for l in range(take+1):
      r = take - l
      if l == 0:
        tmp = sum(V_rev[:r])
      elif r == 0:
        tmp = sum(V[:l])
      else:
        tmp = sum(V[:l])+sum(V_rev[:r])
      if l == 0:
        push_list = V_rev[:r]
      elif r == 0:
        push_list = V[:l]
      else:
        push_list = V[:l] + V_rev[:r]
      push_list = sorted(push_list)
      #print(push_list)
      ans = max(ans,tmp - sum(push_list[:push]))
print(ans)

Submission status: Wrong Answer

Solution code:
import math
N, K = map(int,input().split())
V = list(map(int,input().split()))
V_rev = list(reversed(V))
ans = 0
tmp = 0
L = 0
R = 0
for take in range(math.floor(K/2),K+1):
  push = K - take
  tmp = 0
  for l in range(take+1):
    r = take - l
    if l == 0:
      tmp = sum(V_rev[:r])
    elif r == 0:
      tmp = sum(V[:l])
    else:
      tmp = sum(V[:l])+sum(V_rev[:r])
    if l == 0:
      push_list = V_rev[:r]
    elif r == 0:
      push_list = V[:l]
    else:
      push_list = V[:l] + V_rev[:r]
    push_list = sorted(push_list)
    #print(push_list)
    ans = max(ans,tmp - sum(push_list[:push]))
print(ans)

Submission status: Wrong Answer

Solution code:

import sys
n, k = [int(i) for i in sys.stdin.readline().split()]
v_ls = [int(i) for i in sys.stdin.readline().split()]
right_memo_ls = [[0 for i in range(k)]for j in range(k)]
left_memo_ls = [[0 for i in range(k)]for j in range(k)]
right_memo_ls[0][0] = 0
left_memo_ls[0][0] = 0
INF = 1e10
right_cost2val = {i : -INF for i in range(1+k)}
left_cost2val = {i : -INF for i in range(1+k)}
right_cost2val[0] = 0
left_cost2val[0] = 0
for i in range(min(n,k)):
    for j in range(0, min(k - i, i + 2)):
        if j == 0:
            right_memo_ls[i][j] = right_memo_ls[max(0,i-1)][j] + v_ls[i]
            left_memo_ls[i][j] = left_memo_ls[max(0,i-1)][j] + v_ls[- i - 1]
        elif j == i+1:
            right_memo_ls[i][j] = 0
            left_memo_ls[i][j] = 0
        else:
            right_memo_ls[i][j] = max(right_memo_ls[max(0, i-1)][max(0, j-1)], right_memo_ls[max(0, i-1)][j] + v_ls[i])
            left_memo_ls[i][j] = max(left_memo_ls[max(0, i-1)][max(0, j-1)], left_memo_ls[max(0, i-1)][j] + v_ls[-i-1])
        right_cost2val[i + j + 1] = max(right_cost2val[i + j + 1], right_memo_ls[i][j])
        left_cost2val[i + j + 1] = max(left_cost2val[i + j + 1], left_memo_ls[i][j])


best = -INF
for i in range(k+1):
    for j in range(k+1-i):
        score = right_cost2val[i] + left_cost2val[j]
        if best < score:
            best = score
print(best)

Submission status: Wrong Answer

Solution code:
from sys import stdin
n, k = map(int, stdin.readline().rstrip().split())
gems = list(map(int, stdin.readline().rstrip().split()))
total = -99999999999
for i in range(min(n, k)+1):
    for j in range(min(n, k)+1):
        if i + j <= min(n, k):
            tmp = gems[:i] + gems[n-j:]
            tmp.sort()
            total = max(total, sum(tmp))
            for l in range(min(k-i-j+1, len(tmp))):
                if tmp[l] < 0:
                    total -= tmp[l]
                else:
                    break

print(total)

Submission status: Wrong Answer

Solution code:
from sys import stdin
n, k = map(int, stdin.readline().rstrip().split())
gems = list(map(int, stdin.readline().rstrip().split()))
total = -99999999999
for i in range(min(n, k)+1):
    for j in range(min(n, k)+1):
        if i + j <= min(n, k):
            tmp = gems[:i] + gems[n-j:]
            tmp.sort()
            total = max(total, sum(tmp))
            for l in range(k-i-j+1):
                del tmp[:l]
                total = max(total, sum(tmp))

print(total)

Submission status: Wrong Answer

Solution code:
import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
mod = 10**9 + 7
inf = float('inf')
ansC = []

N, K = LI()
V = LI()
dp = [[[0]]*(K+1) for _ in range(K+1)]
right = [0]*N
left = [0]*N
#right[1] = V[-1]
#left[1] = V[0]

for i in range(min(N,K+1)):
    for j in range(min(N+1,K-i+1)):
        if i<N-j:
            dp[i][j] = [V[:i]+V[N-j:]+[0]]
        else:
            dp[i][j] = [V+[0]]
        dp[i][j][0].sort()

#print(dp)

for i in range(min(N,K+1)):
    for j in range(min(N,K-i+1)):
        if i+j!=0:
            t = max(K - (i + j),0)
            for l in range(t):
                if dp[i][j]!=[]:
                    m = dp[i][j][0][0]
#                    print(m)
                    if m < 0:
                        del dp[i][j][0][0]
#            print(i,j)
#            print(dp[i][j])
            if dp[i][j][0]:
#                print(dp[i][j][0])
                ansC.append(sum(dp[i][j][0][::]))

#print(ansC)
if ansC:
    print(max(ansC))
else:
    print("0")

Submission status: Wrong Answer

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

ans = 0
for left in range(K + 1):
    for right in range(K + 1):
        if left >= N - right or left + right > K:
            continue

        G = V[:left] + V[N - right:]
        G.sort()
        dis = 0
        for i, g in enumerate(G):
            if i >= K - (left + right) or g >= 0:
                break
            dis += g

        ans = max(ans, sum(G) - dis)

print(ans)

Submission status: Wrong Answer

Solution code:
N,K = map(int, input().split())
V = [int(v) for v in input().split()]


def f(L, i, n):
    if i == 0 or len(L) == 0:
        return 0
    if L[n] >= 0:
        t = L[n]
        if n >= 0:
            n += 1
        else:
            n -= 1
        return t+f(L,i-1,n)
    else:
        t = L[n]
        if n >= 0:
            n += 1
        else:
            n -= 1
        if i >= 2:
            return max(f(L, i-2, n), t+f(L, i-1, n))
        else:
            return t+f(L, i-1, n)
    

t = min(K+1, N)
L1 = [0]*(t)
L2 = [0]*(t)
cnt1 = 0
cnt2 = 0
for j in range(t):
    if V[j] < 0:
        cnt1 += 1
    if cnt1 < j:
        L1[j] = f(V, j, 0)
    if V[t-j-1] < 0:
        cnt2 += 1
    if cnt2 < j:
        L2[t-j-1] = f(V, t-j-1, -1)
    
V_sort = sorted(V)
s = sum(V)
ans = 0
for i in range(1, K+1):
    if i >= N:
        c = s
        j = 0
        while j < N:
            if V_sort[j] < 0:
                c -= V_sort[j]
            else:
                j = N
            j += 1
        ans = max(ans, c)
    else:
        c = 0
        for j in range(i+1):
            c = max(c, L1[j]+L2[i-j])
        ans = max(ans, c)
        
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
n,k=map(int,input().split())
v=[int(x) for x in input().split()]

print(n,k)
print(v)

p = list()
m = min(n,k)
t = 0

for h in range(1,m+1):
  for i in range(0,h+1):
    a = b = 0
    p.clear()
    for j in range(0,i):
      if v[j]<0:
        p.append(v[j])
      a += v[j]
    for j in range(1,h-i+1):
      if v[-j]<0:
        p.append(v[-j])
      b += v[-j]
    p.sort(reverse=True)
    n = a+b
    for i in range(min(k-h,len(p))):
      n -= p.pop()
    t = max(t,n)
print(t if t > 0 else 0)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 9)

N, K = map(int, input().split())
V = list(map(int, input().split()))
V2 = V * 2
answer = 0
for i in range(1, K+1): # iは宝石を取り出す個数
    for j in range(0, min(i, K-i)+1): # jは何個の宝石を手離すか
        for k in range(max(0, N-i), N+1): # V2の中で何番目から取り出すか
            use = V2[k:k+i]
            use.sort()
            s = sum(use[j:])
            if s > answer:
                answer = s

print(answer)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
V2 = V * 2
answer = 0
for i in range(1, K+1): # iは宝石を取り出す個数
    for j in range(0, min(i, K-i)+1): # jは何個の宝石を手離すか
        for k in range(max(0, N-i), N+1): # V2の中で何番目から取り出すか
            use = V2[k:k+i]
            use.sort()
            s = sum(use[j:])
            if s > answer:
                answer = s

print(answer)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
V2 = V * 2
answer = 0
for i in range(1, K+1): # iは宝石を取り出す個数
    for j in range(0, min(i, K-i)+1): # jは何個の宝石を手離すか
        for k in range(N-i, N+1): # V2の中で何番目から取り出すか
            use = V2[k:k+i]
            use.sort()
            s = sum(use[j:])
            if s > answer:
                answer = s

print(answer)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
input = lambda: sys.stdin.readline()[:-1]
sys.setrecursionlimit(10**8)

n,k=map(int,input().split())
v1=list(map(int,input().split()))
v2=v1[::-1]
r=min(n,k)
ans=0
for i in range(r+1):
    for j in range(i+1):
        tmp=v1[:j]+v2[:i-j]
        tmp.sort()
        for k in range(min(k-i,i)):
            if tmp[k]<0:
                tmp[k]=0
        ans=max(ans,sum(tmp))

print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
input = lambda: sys.stdin.readline()[:-1]
sys.setrecursionlimit(10**8)

n,k=map(int,input().split())
v1=list(map(int,input().split()))
v2=v1[::-1]
r=min(n,k)
ans=0
for i in range(r+1):
    for j in range(i+1):
        tmp=v1[:j]+v2[:i-j]
        tmp.sort()
        for k in range(min(r-i,i)):
            if tmp[k]<0:
                tmp[k]=0
        ans=max(ans,sum(tmp))

print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n,k = map(int, input().split())
vl = list(map(int, input().split()))

res = -1

for l in range(min(n+1,k+1)):
    for r in range(min(n+1,k+1)):
        if l+r>n:
            break

        w = k-(l+r)
        sl = []
        if l>0:
            sl = vl[:l]
        if r>0:
            sl += vl[-r:]
        sl = sorted(sl)
        x = w
        for i in range(min(w,len(sl))):
            if sl[i]>0:
                x = i
                break
        sl = sl[x:]
        s = sum(sl)
        res = max(res, s)

print(res)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n,k = map(int, input().split())
vl = list(map(int, input().split()))

res = -1

for l in range(min(n+1,k+1)):
    for r in range(min(n+1,k+1)):
        if l+r>k:
            break

        w = k-(l+r)
        sl = []
        if l>0:
            sl = vl[:l]
        if r>0:
            sl += vl[-r:]
        sl = sorted(sl)
        x = w
        for i in range(min(w,len(sl))):
            if sl[i]>0:
                x = i
                break
        sl = sl[x:]
        s = sum(sl)
        res = max(res, s)

print(res)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n,k = map(int, input().split())
vl = list(map(int, input().split()))

res = -1

for l in range(k+1):
    for r in range(k+1):
        if l+r>k:
            break

        w = k-(l+r)
        sl = []
        if l>0:
            sl = vl[:l]
        if r>0:
            sl += vl[-r:]
        sl = sorted(sl)
        x = w
        for i in range(w):
            if i<len(sl) and sl[i]>0:
                x = i
                break
        sl = sl[x:]
        s = sum(sl)
        res = max(res, s)

print(res)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
n,k = map(int, input().split())
vl = list(map(int, input().split()))

res = -1

for l in range(k+1):
    for r in range(k+1):
        if l+r>k:
            break

        w = k-(l+r)
        sl = []
        if l>0:
            sl = vl[:l]
        if r>0:
            sl += vl[-r:]
        sl = sorted(sl)
        sl = sl[w:]
        s = sum(sl)
        res = max(res, s)

print(res)

Submission status: Wrong Answer

Solution code:
def main():
    N, K = map(int, input().split())
    V = list(map(int, input().split()))
    ans = 0
    for l in range(K+1):
        for r in range(K+1-l):
            if l + r > K:
                continue
            D = K - l - r
            S = []
            now = 0
            for i in range(min(l,N)):
                now += V[i]
                S.append(V[i])
            for j in range(N-1, max(N-1-r,-1), -1):
                now += V[j]
                S.append(V[j])

            sort_S = sorted(S)
            for d in range(min(D,len(S))):
                if sort_S[d] < 0:
                    now += abs(sort_S[d])

            ans = max(ans, now)

    print(ans)
            

main()

Submission status: Wrong Answer

Solution code:
from collections import deque
from copy import deepcopy
N, K = map(int, input().split())
#V = list(map(int, input().split()))
V = deque(map(int, input().split()))
ans = 0
for l in range(K):
    for r in range(0 , K - l):
        work_V = deepcopy(V)
        if l + r > N:
            continue
        d = K - (r + l)
        now = 0
        hold = []
        for i in range(l):
            #hold.append(V[i])
            hold.append(work_V.popleft())
        for i in range(1 , r + 1):
            #hold.append(V[-i])
            hold.append(work_V.pop())
        hold.sort()
        now = sum(hold)
        for i in range(d):
            if i >= len(hold):
                break
            if hold[i] > 0:
                break
            else:
                now -= hold[i]
        ans = max(ans , now)
print(ans)

Submission status: Wrong Answer

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

ans = 0
for l in range(K):
    for r in range(0 , K - l):
        if l + r > N:
            continue
        d = K - (r + l)
        now = 0
        hold = []
        for i in range(l):
            hold.append(V[i])
        for i in range(1 , r + 1):
            hold.append(V[-i])
        hold.sort()
        now = sum(hold)
        for i in range(d):
            if i >= len(hold):
                break
            if hold[i] > 0:
                break
            else:
                now -= hold[i]
        ans = max(ans , now)
print(ans)