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:
import bisect


def f(a: list, n: int) -> list:
    i = bisect.bisect(a, 0)
    return a[min(i, n):]


assert f([1, 2, 3], 1) == [1, 2, 3]
assert f([-1, 2, 3], 1) == [2, 3]
assert f([-2, -1, 3], 1) == [-1, 3]
assert f([-2, 0, 3], 1) == [0, 3]
assert f([-2, -1, 3], 3) == [3]


def main():
    # pass
    N, K = map(int, input().split())
    V = [int(x) for x in input().split()]
    Vr = list(reversed(V))

    ans = -float("inf")
    M = min(N, K)
    for l in range(0, M+1):
        for r in range(0, M+1):
            n = max(0, K - r - l)  # 戻すことができる最大の回数

            # dequeが空の場合は飛ばす
            if N <= l + r:
                continue

            # l, r個ずつ取り出す
            a1 = V[:l]
            a2 = Vr[:r]
            a = a1 + a2
            a = sorted(a)

            ans = max(ans, sum(f(a, n)))
            # i = bisect.bisect(a, 0)
            #
            # b = a[min(i, n):]
            # score = sum(b)
            # ans = max(ans, score)

            # print(f"{l} {r} {n} {a1}+{a2}={a}")
            # print(i, a[:i], b)
            # print(f"score={score}")

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

V = list(map(int, input().split(' ')))

dp = [[[-1000000] * (K+1) for _ in range(N)] for _ in range(N)]
dp[0][N-1][0] = 0
for i in range(N) :
    for j in range(N-1, i-1, -1) :
        for k in range(1, K+1) :
            dp[i][j][k] = max(dp[i][j][k], dp[i][j][k-1])
            if 0 <= i-1 < N:
                dp[i][j][k] = max(dp[i][j][k], dp[i-1][j][k-1] + V[i-1])
            if 0 <= j+1 < N :
                dp[i][j][k] = max(dp[i][j][k], dp[i][j+1][k-1] + V[j+1])
            if k >= 2 :
                if 0 <= i-1 < N :
                    dp[i][j][k] = max(dp[i][j][k], dp[i-1][j][k-2])
                if 0 <= j+1 < N :
                    dp[i][j][k] = max(dp[i][j][k], dp[i][j+1][k-2])

ans = 0
for i in range(N) :
    for j in range(N) :
        for k in range(K+1) :
            ans = max(ans, dp[i][j][k])
            #print(i, j, k, dp[i][j][k])

print(ans)

Submission status: Wrong Answer

Solution code:
n,m=map(int,input().split())
arr=list(map(int,input().split()))
ans=0
for k in range(m):
  for i in range(m+1-k):
    j=m-k-i
    if i==m-k:
      tarr=arr[:i]
    elif i+j<=m:
       tarr=arr[:i]+arr[-j:]
    else:
      tarr=arr[:]
    tarr=sorted(tarr,reverse=True)
    for l in range(min(len(tarr),k)):
      if tarr[-(l+1)]<0:
        tarr[-(l+1)]=0
    tsum=sum(tarr)
    ans=max(ans,tsum)
print(ans)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-



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

def get_sum(a,b):
  # a個取って、b個捨てる
  ret = 0
  for i in range(a+1):
    v = sorted(Vn[:i]+Vn[N-a+i:])
    ret = max(ret, sum(v[b:]))
  return ret

def main():
  ans = 0
  for n in range(min(N+1,K)):
    ans = max(ans, get_sum(n, max(K-n,0)))
  return ans

if __name__ == "__main__":
  print(main())

Submission status: Wrong Answer

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

# 1手で左を取る
# 2手で左を捨てる

# 1手で右を取る
# 2手で右を捨てる

def search(L, R, k):
    if L == R or k <= 0:
        return 0
    return max([
        search(L + 1, R, k - 1) + V[L],
        search(L + 1, R, k - 2),
        search(L, R - 1, k - 1) + V[R],
        search(L, R - 1, k - 2)
    ])

print(search(0, N-1, K))

Submission status: Wrong Answer

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

for i in range(k+1):
  for j in range(k-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[x:]))
print(ans)

Submission status: Wrong Answer

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

max_score = 0
for i in range(N):
    for j in range(N):
        if not (i+j>K or i+j>N):
    
            H=V[:i]+V[N-j:]
            H.sort()
            suteru=[]            
            for k in range(min(len(H),K-i-j)):
                if H[k]>0:
                    break
                else:
                    suteru.append(H[k])
            max_score=max(sum(H)-sum(suteru),max_score)
       
print(max_score)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = [int(x) for x in input().split()]
L = [[] for i in range(K + 1)]
R = [[] for i in range(K + 1)]
for i in range(1, K + 1):
  L[i] = sorted(V[:i])
  R[i] = sorted(V[-i:], reverse=True)
ans = 0
for l in range(K + 1):
  for r in range(min(N, K) - l):
    LR = L[l] + R[r]
    k = K - l - r
    while k > 0:
      if len(LR) and LR[0] <= LR[-1] and LR[0] < 0:
        LR.pop(0)
      elif len(LR) and LR[-1] <= LR[0] and LR[-1] < 0:
        LR.pop(len(LR) - 1)
      k -= 1
    ans = max(sum(LR), ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
n,k=map(int,input().split())
V=[int(i) for i in input().split()]
M=sum(v for v in V if v>=0)
A=[[0,0]]
for i in range(1,n):
  VS=V[:i]
  cost=i
  A.append([cost,sum(VS)])
  heapq.heapify(VS)
  for _ in range(i):
    cost+=1
    heapq.heappop(VS)
    A.append([cost,sum(VS)])
B=[[0,0]]
V=V[::-1]
for i in range(1,n):
  VS=V[:i]
  cost=i
  B.append([cost,sum(VS)])
  heapq.heapify(VS)
  for _ in range(i):
    cost+=1
    heapq.heappop(VS)
    B.append([cost,sum(VS)])
ans=-float("inf")
for a in A:
  for b in B:
    if a[0]+b[0]<=k:
      ans=max(ans,a[1]+b[1])
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

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



v = np.array(v)
cand = v[:k]

def calc(cand, res):
#     print("inp", cand)
#     if len(cand) <= 1:
#         return np.sum(cand)
    cand = list(cand)
    for i in range(len(cand)):
        if np.min(cand) >= 0:
            break
        if np.argmin(cand) != len(cand):
            if -np.min(cand)  > cand[-1]:
                cand.pop(np.argmin(cand))
                cand = cand[:-1]
        if len(cand) == 0:
            break
            
    if len(cand) > 0:
        while True:
            
            if cand[-1] < 0:
                cand = cand[:-1]
            else:
                break
            if len(cand) ==0:
                break
    if res > 0:
        s = np.sort(cand)
#         print(s)
        anss = [-1000]
        for kk in range(0, res+1):
            tmp = max([max(anss), sum(s[kk:])])
            anss.append(tmp)
#         anss = [sum(s[kk:]) for kk in range(0,res + 1)]
        anss = np.array(anss[1:])
    else:
        anss = sum(cand)
        
#     print("out", anss)
    return anss

inds = np.arange(k + 1)
max_score = 0
for ind in inds:
    ind1 = np.arange(np.min([ind, n]))
    ind2 = np.arange(np.min([k, n]) - ind)
    a1 = v[ind1]
    a2 = v[::-1][ind2]
    res = 0
    if k > n:
        a2 = a2[:n - ind]
        res = k - n
    score1 = calc(a1, res)
    score2 = calc(a2, res)
    try:
        if len(score2) > 1:
            score2 = score2[::-1]
    except:
        pass
    score = score1 + score2
    score = np.max(score)
#     print("----", res)
#     print(score)
    if max_score < score:
        max_score = score
print(max_score)

Submission status: Wrong Answer

Solution code:
import bisect


def f(a: list, n: int) -> list:
    i = bisect.bisect(a, 0)
    return a[min(i, n):]


assert f([1, 2, 3], 1) == [1, 2, 3]
assert f([-1, 2, 3], 1) == [2, 3]
assert f([-2, -1, 3], 1) == [-1, 3]
assert f([-2, 0, 3], 1) == [0, 3]
assert f([-2, -1, 3], 3) == [3]


def main():
    # pass
    N, K = map(int, input().split())
    V = [int(x) for x in input().split()]
    Vr = list(reversed(V))

    ans = -float("inf")
    M = min(N, K)
    for l in range(0, M+1):
        for r in range(0, M+1):
            n = M - r - l  # 戻すことができる最大の回数

            if n < 0:
                continue

            # l, r個ずつ取り出す
            a1 = V[:l]
            a2 = Vr[:r]
            a = a1 + a2
            a = sorted(a)

            ans = max(ans, sum(f(a, n)))
            # i = bisect.bisect(a, 0)
            #
            # b = a[min(i, n):]
            # score = sum(b)
            # ans = max(ans, score)

            # print(f"{l} {r} {n} {a1}+{a2}={a}")
            # print(i, a[:i], b)
            # print(f"score={score}")

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n,k=map(int, input().split())
v=list(map(int, input().split()))
ans=0
for i in range(k+1):
    for j in range(k-i+1):
        if j==0:
            g=v[:i]
        else:
            g=v[:i]+v[-j:]
        g.sort()
        mai=len([t for t in g if t<0])
        if k-i-j>=mai:
            ans=max(ans, sum(g[mai:]))
        else:
            ans=max(ans, sum(g[k-i-j:]))
print(ans)

Submission status: Wrong Answer

Solution code:
import bisect

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

ans = 0

for a in range(1,K+1) :
    for i in range(a+1) :
        G = []
        G.extend(V[:i])
        G.extend(V[len(V)-(a-i):])
        G.append(0)
        G = sorted(G)
        #bi = bisect.bisect(G,0)
        #G = G[min(bi-1,K-a):]
        
        cnt = 0
        calc = 0
        for i in range(len(G)) :
            if G[i] < 0 :
                if cnt < K-a :
                    cnt += 1
                else :
                    calc += G[i]
            else :
                calc += G[i]
                
        
        ans = max(ans,calc)
print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
v = list(map(int, input().split()))
ret = 0
for l in range(k + 1):
    for r in range(k - l + 1):
        d = k - l - r
        g = sorted(v[:l] + v[n - r:])
        for i in range(min(d, len(g))):
            g[i] = max(0, g[i])
        ret = max(ret, sum(g))
print(ret)

Submission status: Wrong Answer

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

get, rel = 0, 0
ans = 0
for a in range(K + 1):  # a回行う
    for i in range(a):
        rel = i
        get = a - i
        if rel == get:
            break
        right, left = 0, 0
        for j in range(get):
            point = 0
            r, l = [], []
            right = j
            left = get - right
            l = V[:left]
            r = V[N - right:]
            al = r + l
            al.sort()
            point = sum(al[rel:])
            ans = max(ans, point)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
import numpy as np
# n = int(input())
# a, b = map(int, input().split())
# int_list = list(map(int, input().split()))
# l = list(input().split())

def solve():
    n, k = map(int, input().split())
    v_list = list(map(int, input().split()))

    temoti_max = 0
    for i in range(k+1):
        for j in range(k-i):
            #print(v_list)
            temoti_list = []
            temoti_list +=  v_list[:i]
            #print(i, j)
            #print(temoti_list)
            temoti_list += v_list[n-j:]
            #print(temoti_list)
            temoti_list.sort()
            #print(temoti_list)

            temoti = sum(temoti_list)
            for iret in range(min(len(temoti_list), k-i-j)):
                if temoti_list[iret] < 0:
                    temoti -= temoti_list[iret]

            if temoti_max <= temoti:
                temoti_max = temoti

    print(temoti_max)

solve()

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = [int(x) for x in input().split()]
L = [[] for i in range(K + 1)]
R = [[] for i in range(K + 1)]
for i in range(1, K + 1):
  L[i] = sorted(V[:i])
  R[i] = sorted(V[-i:], reverse=True)
ans = 0
for l in range(K + 1):
  for r in range(K - l):
    LR = L[l] + R[r]
    k = K - l - r
    while k > 0:
      if len(LR) and LR[0] <= LR[-1] and LR[0] < 0:
        LR.pop(0)
      elif len(LR) and LR[-1] <= LR[0] and LR[-1] < 0:
        LR.pop(len(LR) - 1)
      k -= 1
    ans = max(sum(LR), ans)
print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np
n, k = map(int, input().split())
v = list(map(int, input().split()))

left = np.cumsum([0] + v)
right = np.cumsum([0] + v[::-1])

results = []
for backn in range(min(50, k)):
    t = min(n, k - backn)
    for shift in range(t + 1):
        p = v[:t - shift] + v[n - shift:]
        p.sort()
        results.append(sum(p[backn:]))
print(max(results))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

get, rel = 0, 0
ans = 0
for i in range(K):
    rel = i
    get = K - i
    if rel == get:
        break
    right, left = 0, 0
    for j in range(get):
        point = 0
        r, l = [], []
        right = j
        left = get - right
        l = V[:left]
        r = V[N - right:]
        al = r + l
        al.sort()
        point = sum(al[rel:])
        ans = max(ans, point)
print(ans)

Submission status: Wrong Answer

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

out = 0
for i in range(min(K, N)+1):
    res = K - i
    for j in range(i+1):
        k = i - j

        Q = sorted(V[:j] + V[N-k:])

        minus = sum([q < 0 for q in Q])
        score = sum(Q[min(minus, res):])
        s = max(s, score)

print(s)

Submission status: Wrong Answer

Solution code:
from collections import defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue,datetime
sys.setrecursionlimit(10**8)
INF = float('inf')
mod = 10**9+7
eps = 10**-7
def inpl(): return list(map(int, input().split()))
def inpl_str(): return list(input().split())

N,K = inpl()
vv = inpl()

ans = 0
for lnum in range(K+1):
    for rnum  in range(K-lnum+1):
        torukazu = vv[:lnum] + vv[N-rnum:]
        torukazu.sort()
        #print(' ' , torukazu,lnum,rnum)
        for sute in range(K-lnum-rnum+1):
            #print(torukazu[sute:])
            tmp = sum(torukazu[sute:])
            ans = max(tmp,ans)

print(ans)

Submission status: Wrong Answer

Solution code:
import bisect


def f(a: list, n: int) -> list:
    i = bisect.bisect(a, 0)
    return a[min(i, n):]


assert f([1, 2, 3], 1) == [1, 2, 3]
assert f([-1, 2, 3], 1) == [2, 3]
assert f([-2, -1, 3], 1) == [-1, 3]
assert f([-2, 0, 3], 1) == [0, 3]
assert f([-2, -1, 3], 3) == [3]


def main():
    # pass
    N, K = map(int, input().split())
    V = [int(x) for x in input().split()]
    Vr = list(reversed(V))

    # ans = -float("inf")
    cands = []
    for l in range(0, K+1):
        for r in range(0, K+1):
            n = K - r - l  # 戻すことができる最大の回数
            if n < 0:
                continue

            # l, r個ずつ取り出す
            a1 = V[:l]
            a2 = Vr[:r]
            a = a1 + a2
            a = sorted(a)

            # ans = max(ans, sum(f(a, n)))
            cands.append(sum(f(a, n)))
            # i = bisect.bisect(a, 0)
            #
            # b = a[min(i, n):]
            # score = sum(b)
            # ans = max(ans, score)

            # print(f"{l} {r} {n} {a1}+{a2}={a}")
            # print(i, a[:i], b)
            # print(f"score={score}")

    print(max(cands))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from collections import deque

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

mx = 0
for num_out in range(k+1):
	num_ret = k-num_out
	for num_out_l in range(num_out+1):
		num_out_r = num_out - num_out_l
		out = v[:num_out_l] + v[n-num_out_r:]
		out.sort()
		ret_value = 0
		for ret_v in out[:num_ret]:
			if ret_v < 0:
				ret_value += ret_v
			else:
				break
		val = sum(out) - ret_value
		if val > mx:
			mx = val

print(mx)

Submission status: Wrong Answer

Solution code:
n,k = [int(i) for i in input().split()]
d = [int(i) for i in input().split()]
ans = 0
for en in range((k//2)+1):
    if en > n:
        break
    if k-en > n:
        tmp = d[:]
        tmp.sort()
        ans = max(ans, sum(tmp[en:]))
        continue
    for dl in range(k-en+1):
        tmp = d[:dl]+d[n-(k-en-dl):]
        tmp.sort()
        ans = max(ans, sum(tmp[en:]))
print(ans)

Submission status: Wrong Answer

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

max_score = -10**27
for i in range(N):
    for j in range(N):
        if not (i+j>K or i+j>N):
    
            H=V[:i]+V[N-j:]
            H.sort()
            k=0
            flg=False
            for k in range(len(H)):
                flg=True
                if H[k]>0:
                    flg=False
                    break
            if flg:
                k+=1
            #print(k)
            if k>K-i-j:
                max_score=max(sum(H[K-i-j:]),max_score)
            else:
                max_score=max(sum(H[k:]),max_score)
            #print(max_score,i,j)

print(max_score)

Submission status: Wrong Answer

Solution code:
import bisect

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

ans = 0

for a in range(1,K+1) :
    for i in range(a+1) :
        G = []
        G.extend(V[:i])
        G.extend(V[len(V)-(a-i):])
        G.append(0)
        G = sorted(G)
        #print(G)
        bi = bisect.bisect(G,0)
        G = G[min(bi-1,K-a):]
        #G = G[(K-a):]
        #print(sum(G),K-a)
        ans = max(ans,sum(G))
print(ans)

Submission status: Wrong Answer

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

dp = [[[False for ___ in range(N)] for __ in range(N)] for _ in range(K+1)]

dp[0][0][N-1] = True

for k in range(K):
    for i in range(N):
        for j in range(i, N):
            if j+1<N and i-1>=0:
                dp[k+1][i][j] = dp[k][i][j+1] or dp[k][i-1][j]
            elif j+1<N:
                dp[k+1][i][j] = dp[k][i][j+1]
            elif i-1>=0:
                dp[k+1][i][j] = dp[k][i-1][j]
                

ans = 0
for k in range(K+1):
    for n in range(K-k+1):
        for i in range(N):
            for j in range(i, N):
                if not dp[k][i][j]:
                    continue
                on_hand = D[:i]+D[j+1:]
                m = len(on_hand)
                if m < n:
                    continue
#                print(on_hand)
#                print(k, i, j, n, sorted(on_hand,reverse=True)[:m-n], sum(sorted(on_hand,reverse=True)[:m-n]))
                ans = max(ans, sum(sorted(on_hand,reverse=True)[:m-n]))
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
Vs = list(map(int, input().split()))
max_v = 0
for k in range(0, K+1):
  for i in range(0, k+1):
    inh = sorted(Vs[:i] + Vs[i+N-k:])
    max_v = max(max_v, sum(inh[K-k:]))

print(max_v)

Submission status: Wrong Answer

Solution code:
from itertools import product

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

ans = 0
# l: 左から取り出す数
for l, r in product(range(K + 1), repeat=2):
    if l + r == 0 or l + r > N:
        continue

    A = V[:l] + V[N - r:]
    s = sum(A)

    # 入れることができる回数
    a = K - (l + r)
    if a > 0:
        A = sorted(A)
        for i in range(min(a, len(A))):
            if A[i] < 0:
                s -= A[i]
            else:
                break
    ans = max(ans, s)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
Vs = list(map(int, input().split()))
max_v = 0
for k in range(K//2, K+1):
  for i in range(0, k+1):
    inh = sorted(Vs[:i] + Vs[i+N-k:])
    max_v = max(max_v, sum(inh[K-k:]))

print(max_v)

Submission status: Wrong Answer

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

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

# 左から試していく

left_dict = {0:0}

for i in range(K,0,-1):
    left_dict[i]=max([sum(sorted(V[:i-j])[j:]) for j in range(i//2+1)])

left_dict



V = list(reversed(V))

V

right_dict = {0:0}
for i in range(K,0,-1):
    right_dict[i]=max([sum(sorted(V[:i-j])[j:]) for j in range(i//2+1)])

right_dict

m = 0
for i in range(K+1):
    for j in range(K-i+1):
        if m < left_dict[i]+right_dict[j]:
            m = left_dict[i]+right_dict[j]

print(m)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**7)
import numpy as np
# n = int(input())
# a, b = map(int, input().split())
# int_list = list(map(int, input().split()))
# l = list(input().split())

def solve():
    n, k = map(int, input().split())
    v_list = list(map(int, input().split()))

    temoti_max = 0
    for i in range(k+1):
        for j in range(k-i):
            temoti_list = []
            temoti_list +=  v_list[:i]
            #print(i, j)
            #print(temoti_list)
            temoti_list += v_list[n-j:]
            #print(temoti_list)
            temoti_list.sort()
            #print(temoti_list)
            temoti = sum(temoti_list[k-i-j:])
            #print(k-i-j, temoti_list[k-i-j:],  temoti)
            if temoti_max <= temoti:
                temoti_max = temoti
    print(temoti_max)

solve()

Submission status: Wrong Answer

Solution code:
def run(N, K, V):
    ret = 0
    L_cal = {(0, 0): 0}
    R_cal = {(0, 0): 0}
    for L_get in range(1, K+1):
        lists = V[:L_get]
        sorted_lists = sorted(lists)
        sum_lists = sum(lists)
        for L_set in range(L_get+1):
            L_cal[(L_get, L_set)] = sum_lists - sum(sorted_lists[:L_set])
    for R_get in range(1, K+1):
        lists = V[-R_get:]
        sorted_lists = sorted(lists)
        sum_lists = sum(lists)
        for R_set in range(R_get+1):
            R_cal[(R_get, R_set)] = sum_lists - sum(sorted_lists[:R_set])
    for L_K in range(K+1):
        R_K = K - L_K
        for L_get in range(L_K+1):
            L_set = L_K - L_get
            for R_get in range(R_K+1):
                R_set = R_K - R_get
                if L_set <= L_get and R_set <= R_get:
                    ret = max(ret, L_cal[(L_get, L_set)]+R_cal[(R_get, R_set)])
    return ret


def main():
    N, K = map(int, input().split())
    V = list(map(int, input().split()))
    print(run(N, K, V))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys
from math import ceil
from itertools import chain

def solve(N: int, K: int, V: "List[int]"):
    # i: num to pick, j: num to pick from left
    ans = -10**10
    for i in range(min(K+1, N)):
        max_n = min(K-i, i)
        for j in range(i+1):
            V2 = sorted(chain(V[:j], V[N-(i-j):]))
            k=0
            while True:
                if k>=max_n or V2[k]>=0:
                    break
                k+=1
            ans = max(sum(V2[k:]), ans)
    print(ans)

def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    V = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, K, V)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
read = input 
rn = lambda :list(map(int, read().split()))
# O(N^2)
n, k = rn()
V = rn() # 0 ~ n-1
best = 0
for i in range(0, n + 1): # [0, n]
    for j in range(i + 1, n+1): # [i + 1, n]
        # take [0,i), [j, n)
        yes = V[0:i] + V[j: n]
        if len(yes) > k:
            continue
        leftout = k - len(yes)
        yes.sort()
        skip = 0
        for skip in range(len(yes)):
            if yes[skip] <0 and leftout > 0:
                leftout -= 1
            else :
                break
        if best < sum(yes[skip:]):
            best = sum(yes[skip:])
#             print(yes[skip:], k, best)
print(best)

Submission status: Wrong Answer

Solution code:
from itertools import*
import math
from collections import*
from heapq import*
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf = float("inf")
mod = 10**9+7
from functools import reduce
import sys
sys.setrecursionlimit(10**7)

n,k = map(int,input().split())
D = deque()
V = list(map(int,input().split()))
V1 = deepcopy(V)
for i in range(n):
    D.append(V[i])
ans = 0
if 2*n <= k:
    for j in range(n):
        if V[j]>0:
            ans += V[j]
    print(ans)
else:
    for i in range(0,k+1):
        for j in range(0,k+1):
                if i + j> k:
                    continue
                else:
                    cur = 0
                    if i+j >=n:
                        cur = sum(V)
                    else:
                        cur += sum(V[:i])
                        cur += sum(V[n-j:n])
                    V1=V[:i]+V[n-j:]
                    V1.sort()
                    cur -= sum(V1[:k-i-j])
                    ans = max(ans,cur)
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
for i in range(1,K+1):
    for j in range(i+1):
        rec = V[:i-j]+V[N-j:]
        rec.sort(reverse=True)
        recsum = sum(rec[:2*i-K])
        if recsum > ans:
            ans=recsum
print(ans)

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
V=list(map(int,input().split()))
L=[]
ans=0
for i in range(n):
    for j in range(n-i):
        if i+j>k:
            continue
        tmp=V[:i]+V[n-j:]
        tmp.sort(reverse=True)
        for l in range(k-i-j):
            if tmp and tmp[-1]<0:
                tmp.pop()
            else:
                break
        ans=max(ans,sum(tmp))
print(ans)

Submission status: Wrong Answer

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

from itertools import accumulate
Lcumsum = [0] + list(accumulate(V))
Rcumsum = [0] + list(accumulate(V[::-1]))
#print(Lcumsum)
#print(Rcumsum)

ans = 0
               
for l in range(min(N,K)):
  for r in range(min(N-l,K-l)):
    Lsum = Lcumsum[l]
    Rsum = Rcumsum[r]
    Nsum = sum(sorted([v for v in (V[:l] + V[N-r:]) if v<0])[:K-l-r])
    ans = max(ans, Lsum+Rsum-Nsum)
    #print(l,r,Lsum,Rsum,Nsum,Lsum+Rsum-Nsum)
    
print(ans)

Submission status: Wrong Answer

Solution code:
read = input 
rn = lambda :list(map(int, read().split()))
# O(N^2)
n, k = rn()
V = rn() # 0 ~ n-1
best = 0
for i in range(0, n + 1): # [0, n]
    for j in range(i + 1, n+1): # [i + 1, n]
        # take [0,i), [j, n)
        yes = V[0:i] + V[j: n]
        if len(yes) > k:
            continue
        leftout = k - len(yes)
        yes.sort()
        skip = 0
        for skip in range(len(yes)):
            if yes[skip] <0 and leftout > 0:
                leftout -= 1
            else :
                break
        if best < sum(yes[skip:]):
            best = sum(yes[skip:])
            print(yes[skip:], k, best)
print(best)

Submission status: Wrong Answer

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

sV=sorted(V)
ans=0
for i in range(N):
  for j in range(N-i):
    if i+j<=K:
      if i+j>=N:
        L=sV
      else:
        L=V[:i]+V[N-j:]
        L.sort()
      k=bisect.bisect_left(L,0)
      kans=sum(L[min(max(0,K-i-j),k):])
      ans=max(ans,kans)
print(ans)

Submission status: Wrong Answer

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

ans = 0
for x in range(1, K+1):
    for i in range(x+1):
        Ts = Vs[:i] + Vs[N-(x-i):]
        sumOut = sum(Ts)
        Ts.sort()

        for T in Ts[:K-x]:
            if T < 0:
                sumOut -= T
            else:
                break

        ans = max(ans, sumOut)

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
v=list(map(int, input().split()))
ans=0
for i in range(n+1):
    for j in range(n-i+1):
        if k>=i+j:
            houseki=[]
            houseki+=v[:i]
            houseki+=v[n-j:]
            houseki=sorted(houseki)
            p=k-i-j
            ans=max(ans, sum(houseki[p:]))
print(ans)