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:
N, K = map(int, input().split())
D = list(map(int, input().split()))
 
def rec(k, d, h, mod):
    if not d or k == 0:
        return sum(h)
 
    if mod == 1:
        h.append(d.pop(0))
    elif mod == 2:
        h.append(d.pop(-1))
    elif mod == 3 and h:
        d.append(min(h))
        h.remove(min(h))
    elif mod == 4 and h:
        d.insert(0, min(h))
        h.remove(min(h))
 
    m = max(rec(k - 1, d.copy(), h.copy(), i) for i in range(1, 6))
    return m
 
print(max(rec(K, D[:], [], i) for i in range(1, 6)))

Submission status: Runtime Error

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

first_sum = sum(V)

answer = 0
for i in range(min(N + 1, K + 1)):
  for j in range(i + 1):
    left = j
    right = i - j
    
    q = []
    for k in range(left):
      q.append(V[k])
    for k in range(N - right, N):
      q.append(V[k])
    q.sort()
    
    tmp = sum(q)
    for k in range(min(K - i, len(q))):
      if q[k] < 0:
        tmp -= q[k]
      else:
        break
    answer = max(answer, tmp)
    
print(answer)N, K = map(int, input().split())
V = list(map(int, input().split()))

first_sum = sum(V)

answer = 0
for i in range(min(N + 1, K + 1)):
  for j in range(i + 1):
    left = j
    right = i - j
    
    q = []
    for k in range(left):
      q.append(V[k])
    for k in range(N - right, N):
      q.append(V[k])
    q.sort()
    
    tmp = sum(q)
    for k in range(min(K - i, len(q))):
      if q[k] < 0:
        tmp -= q[k]
      else:
        break
    answer = max(answer, tmp)
    
print(answer)

Submission status: Runtime Error

Solution code:
from heapq import heappush, heappop


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

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

ans = 0
for back in range(K + 1):
    for l in range(K - back + 1):
        r = K - back - l
        S = 0
        hq = []
        for i in range(l):
            heappush(hq, V[i])
            S += V[i]
        for j in range(r):
            heappush(hq, V[N - 1 - j])
            S += V[N - 1 - j]
        for k in range(back):
            if len(hq) > 0:
                if hq[0] < 0:
                    S -= heappop(hq)

        ans = max(ans, S)

print(ans)

Submission status: Runtime Error

Solution code:
from heapq import heappush, heappop


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

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

ans = 0
for back in range(K // 2 + 1):
    for l in range(K - back + 1):
        r = K - back - l
        S = 0
        hq = []
        for i in range(l):
            heappush(hq, V[i])
            S += V[i]
        for j in range(r):
            heappush(hq, V[N - 1 - j])
            S += V[N - 1 - j]
        for k in range(back):
            if len(hq) > 0 and hq[0] < 0:
                S -= heappop(hq)

        ans = max(ans, S)

print(ans)

Submission status: Runtime Error

Solution code:
from heapq import heappush, heappop


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

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

ans = 0
for back in range(K // 2 + 1):
    for l in range(K - back + 1):
        r = K - back - l
        S = 0
        hq = []
        for i in range(l):
            heappush(hq, V[i])
            S += V[i]
        for j in range(r):
            heappush(hq, V[N - 1 - j])
            S += V[N - 1 - j]
        for k in range(back):
            S -= heappop(hq)

        ans = max(ans, S)

print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline

def main():
    N,K = map(int,input().split())
    V = list(map(int,input().split()))
    
    ans = -(10**10)
    for pick in range(K+1):
        throw = min(pick,K-pick)
        for left in range(pick+1):
            right = pick-left
            p = []
            minus = 0
            for i in range(left):
                if V[i] < 0:
                    minus += 1
                p.append(V[i])
            for i in range(right):
                if V[-i-1] < 0:
                    minus += 1
                p.append(V[-i-1])
            p.sort()
            if minus >= throw:
                rest = p[throw:]
            else:
                rest = p[minus:]
            ans = max(ans,sum(rest))
    
    print(ans)
    
if __name__ == "__main__":
    main()

Submission status: Runtime Error

Solution code:
import bisect
n,K = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
ans = [0]
if K>2*n:
    K = 2*n
if K == 1:
    print(max(a[0],a[n-1],0))
    exit()
for k in range((K+1)//2,K+1):
    r = K-k
    for i in range(k+1):
        ls = a[:i]+a[n-k+i:]
        ls.sort()
        if r == 0:
            ans.append(sum(ls))
        elif ls[r-1]<0:
            ans.append(sum(ls[r:]))
        else:
            x = bisect.bisect_left(ls,0)
            ans.append(sum(ls[x:]))
print(max(ans))

Submission status: Runtime Error

Solution code:
from collections import Counter,defaultdict,deque
import sys
from itertools import permutations, combinations
from heapq import heappop, heappush
# input = sys.stdin.readline
sys.setrecursionlimit(10**8)
mod = 10**9+7
    
def inp(): # n=1
    return int(input())
def inpm(): # x=1,y=2
    return map(int,input().split())
def inpl(): # a=[1,2,3,4,5,...,n]
    return list(map(int, input().split()))
def inpls(): # a=['1','2','3',...,'n']
    return list(input().split())
def inplm(n): # x=[] 複数行
    return list(int(input()) for _ in range(n))
def inpll(n): # [[1,1,1,1],[2,2,2,2],[3,3,3,3]]
    return sorted([list(map(int, input().split())) for _ in range(n)])

def main():
    n,k = inpm()
    v = inpl()
    a = [0 for _ in range(n+1)]
    b = [0 for _ in range(n+1)]
    ans = 0
    for i in range(n):
        a[i+1] = a[i] + v[i]
        b[i+1] = b[i] + v[n-1-i]
    for x in range(k+1):
        for y in range(k-x+1):
            que = []
            for i in range(x):
                que.append(v[i])
            for i in range(y):
                que.append(v[n-1-i])
            que.sort()
            l = len(que)
            pre = a[x] + b[y]
            for z in range(min(l,k-x-y)):
                if que[z]>=0:
                    break
                pre -= que[z]
                ans = max(ans,pre)
    print(ans)

if __name__ == "__main__":
    main()

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(k+1):
  for j in range(k+1-i):
    if i+j>n:
      break  
    #手に取りだしたもの
    A=sorted(V[:i]+V[n-j:])
    a=min(n-i-j,bisect.bisect_left(A, 0))
    ans=max(ans,sum(S[a:]))
print(ans)

Submission status: Runtime Error

Solution code:
from collections import deque
import bisect

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

answer = 0
A = 0
while A <= min(N,K):
    B = 0
    while A + B <= min(N,K):
        have = deque()
        for i in range(A):
            bisect.insort_left(have, V[i])
        for i in range(B):
            bisect.insort_left(have, V[-(i+1)])
        
        throw = min(K - (A + B), len(have))
        for _ in range(throw):
            tmp = have.popleft()
            if tmp > 0:
                have.append(tmp)
                break

        answer = max(answer, sum(have))
        B += 1
    A += 1

print(answer)

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):
    left = []
    if i != 0:
        left = V[:i]
    for j in range(K-i+1):
        right = []
        if j == 0:
            sort_merge = sorted(left+right)
            ans = max(ans, sum(sort_merge[k:]))
            continue
        elif (N-i) == 0:
            sort_merge = sorted(left+right)
            ans = max(ans, sum(sort_merge[k:]))
            continue
        elif i+j > N:
            right = V[-(N-i):]
        else:
            right = V[-j:]
        for k in range(K-i-j+1):
            if k > i+j:
                continue
            sort_merge = sorted(left+right)
            ans = max(ans, sum(sort_merge[k:]))

print(ans)

Submission status: Runtime Error

Solution code:
import heapq
from copy import deepcopy

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

get_ope_max_count = min(n, k) # 取得操作の最大回数
# print(n, k)
# print(v)

ans = 0
# i = 操作Aの回数
# j = 操作Bの回数
# print('get_ope_max_count: {}'.format(get_ope_max_count))
for i in range(get_ope_max_count+1):
  b_ope_count = k - i
  # print('i: {}, b_ope_count: {}'.format(i, b_ope_count))
  for j in range(b_ope_count+1):
    h = []
    v_copy = deepcopy(v) 
    for _i in range(i):
      if len(v_copy) >= 0:
        heapq.heappush(h, v_copy.pop(0))
    for _j in range(j):
      if len(v_copy) >= 0:
        heapq.heappush(h, v_copy.pop())
    # マイナスの宝石を戻す
    return_ope_max_count = k - (i + j)
    
    for _re in range(return_ope_max_count):
      if len(h) == 0:
        break
      
      val = heapq.heappop(h)
      if val >= 0:
        # 0以上がでたらそれを戻して、戻す処理を終える
        heapq.heappush(h, val)
        break
    # print(i, j, h)
    total_val = sum(h)
    if ans < total_val:
      ans = total_val
print(ans)

Submission status: Runtime Error

Solution code:
import heapq
from copy import deepcopy

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

get_ope_max_count = min(n, k) # 取得操作の最大回数
# print(n, k)
# print(v)

ans = 0
# i = 操作Aの回数
# j = 操作Bの回数
# print('get_ope_max_count: {}'.format(get_ope_max_count))
for i in range(get_ope_max_count+1):
  b_ope_count = k - i
  # print('i: {}, b_ope_count: {}'.format(i, b_ope_count))
  for j in range(b_ope_count+1):
    h = []
    for _i in range(i):
      heapq.heappush(h, v[_i])
    for _j in range(1, j+1):
      heapq.heappush(h, v[-_j])
    # マイナスの宝石を戻す
    return_ope_max_count = k - (i + j)
    
    for _re in range(return_ope_max_count):
      if len(h) == 0:
        break
      
      val = heapq.heappop(h)
      if val >= 0:
        # 0以上がでたらそれを戻して、戻す処理を終える
        heapq.heappush(h, val)
        break
    # print(i, j, h)
    total_val = sum(h)
    if ans < total_val:
      ans = total_val
print(ans)

Submission status: Runtime Error

Solution code:
import heapq
N, K = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
for k in range(K+1):
    for i in range(k+1):
        tmp = 0
        tmp2 = []
        heapq.heapify(tmp2)

        for j in range(k-i):
            tmp += V[j]
            if V[j] < 0:
                heapq.heappush(tmp2, V[j])
        for j in range(i):
            tmp += V[N-1-j]
            if V[N-1-j] < 0:

                heapq.heappush(tmp2, V[N-1-j])

        for j in range(min(k, K-k)):
            if len(tmp2) == 0:
                break
            tmp -= heapq.heappop(tmp2)
        ans = max(tmp, ans)


print(ans)

Submission status: Runtime Error

Solution code:
import heapq
N, K = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
for k in range(K+1):
    for i in range(k+1):
        tmp = 0
        tmp2 = []
        heapq.heapify(tmp2)

        for j in range(k-i):
            tmp += V[j]
            if V[j] < 0:
                heapq.heappush(tmp2, V[j])
        for j in range(i):
            tmp += V[j]
            if V[N-1-j] < 0:

                heapq.heappush(tmp2, V[j])

        for j in range(min(k, K-k)):
            tmp -= heapq.heappop(tmp2)
        ans = max(tmp, ans)


print(ans)

Submission status: Runtime Error

Solution code:
#12:10
n,k = map(int,input().split())
v = list(map(int,input().split()))
can = []
for i in range(n):
  if i == 0:
    a = []
  elif i <= k:
    a.append(v[i-1])
  for j in range(n):
    if j == 0:
      b = []
    elif i + j <= n and i + j <= k:
      b.append(v[-j])
    rest = min(k-i-j,i+j)
    c = a + b
    c.sort()
    tmp = sum(c)
    for de in range(rest):
      if c[de] < 0:
        tmp -= c[de]
    can.append(tmp)
    #print(i,j,a,b,tmp)
print(max(can))

Submission status: Runtime Error

Solution code:
N, K=map(int, input().split())
D=list(map(int, input().split()))
limit=min(N, K)
ans=0
for A in range(limit+1):
  for B in range(limit-A+1):
    jewels=D[:A]+D[-B:] if B>0 else D[:A]
    jewels.sort()
    for k in range(min(len(jewels)-1, K-(A+B))):
      if jewels[k]>=0:
        break
      jewels.pop(0)
    ans=max(ans, sum(jewels))
print(ans)

Submission status: Runtime Error

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(K+1):
    for r in range(K-l+1):
        dp[l][r]=left[l]+right[r]
        temp=V[:l]+V[::-1][:r]#ここが不味い
        #print(temp)
        temp.sort()
        
        
        for i in range(K-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: Runtime Error

Solution code:
#define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#include <iostream>

using namespace std;
using uint = unsigned int;
using ll = long long;
using ull = unsigned long long;

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N, K;
    cin >> N >> K;
    vector<int> V(N);
    REP(i, N) {
        int v;
        cin >> v;
        V.at(i) = v;
    }
    int ans = 0;
    int O = min(N, K);
    for (int i = 1; i <= O; i++) {
        if (i * 2 < O)
            continue;
        for (int j = -i; j < 1; j++) {
            priority_queue<int, vector<int>, greater<int>> pq;
            for (int k = 0; k < i; k++) {
                pq.push(V.at((N + j + k) % N));
            }
            int sum = 0;
            REP(n, K - i) {
                if (pq.top() < 0)
                    pq.pop();
                else
                    break;
            }
            while (!pq.empty()) {
                sum += pq.top();
                pq.pop();
            }
            ans = max(ans, sum);
        }
    }
    cout << ans << "\n";
}

Submission status: Runtime Error

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    a, p = LI()
    print((a*3+p)//2)
    return

#B
def B():
    n = II()
    sp = []
    for i in range(n):
        sp.append(input().split())
    another = [(s[0], -1 * int(s[1]),num) for num,s in enumerate(sp)]
    another.sort()
    for s,p,q in another:
        print(q+1)

    return

#C
def C():
    n, m = LI()
    ks = LIR_(m)
    p = LI()
    patern = itertools.product(range(2), repeat=n)
    ans = 0
    for on in patern:
        ki = [0 for i in range(m)]
        for num, oni in enumerate(on):
            if oni:
                for i,ksi in enumerate(ks):
                    ksi = ksi[1::]
                    if num in ksi:
                        ki[i] ^= 1
        for num, pi in enumerate(p):
            if pi != ki[num]:
                break
        else:
            ans += 1
    print(ans)

            
    return

#D
def D():
    N, K = LI()#random.randint(1,50),random.randint(1,100)
    v = LI()#[random.randint(-10 ** 2, 10 ** 2) for i in range(N)]
    ans = 0
    for a in range(min(N, K) + 1):
        for b in range(min(N, K) + 1):
            if a + b > min(K,N):
                break
            if a == b == 0:
                continue
            deletes = K - a - b
            left = v[:a]
            right = v[:-b-1:-1]
            x = left + right
            x.sort()
            tmp = sum(x)
            for delete in range(min(deletes, len(x))):
                if x[delete] >= 0:
                    break
                tmp -= x[delete]
            ans = max(ans,tmp)
    
    print(ans)
                

    return


#E
def E():  
    N,q = LI()
    STX = LIR(N)
    D = IR(q)
    LV = (q - 1).bit_length()
    N0 = 1 << LV

    data = [inf] * (N0 << 1)
    INF = inf

    def update(l, r, x):
    
        #  区間[l, r)のdataの値を更新
        L = N0 + l; R = N0 + r
        while L < R:
            if R & 1:
                R -= 1
                data[R-1] = min(data[R-1], x)
            if L & 1:
                data[L-1] = min(data[L-1], x)
                L += 1
            L >>= 1; R >>= 1

    def query(x):
        L = N0 + x - 1
        #  xの最小値を求める
        s = INF
        while L >= 0:
            s = min(s, data[L])
            L = (L - 1) >> 1
        return s

    for s, t, x in STX:
        XS = max(-1, s - x)
        XT = max(-1, t - x)
        XS_index = bisect_left(D, XS)
        XT_index = bisect_left(D, XT)
        update(XS_index, XT_index, x)

    for i in range(q):
        a = query(i)
        print(a if a != inf else -1)
#F
def F():
    return

#Solve
if __name__ == '__main__':
    E()

Submission status: Runtime Error

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

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

dpa = [[0 for j in range(k+2)] for i in range(k+2)]
dpb = [[0 for j in range(k+2)] for i in range(k+2)]
suma = []
sumb = []
for i in range(k):
    suma += [vl[i]]
    sumb += [vl[-i-1]]
    suma = sorted(suma)
    sumb = sorted(sumb)
    for j in range(min(i+1, k+1-i)):
        dpa[i][j] = sum(suma[j:])
        dpb[i][j] = sum(sumb[j:])
    
ans = 0
for a in range(k+1):
    for b in range(k+1-a):
        for c in range(min(a+1, k+1-a-b)):
            for d in range(min(b+1, k+1-a-b-c)):
                tmp = dpa[a-1][c] + dpb[b-1][d]
                if tmp > ans:
                    ans = tmp

print(ans)

Submission status: Runtime Error

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

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

dpa = [[0 for j in range(k+1)] for i in range(k+1)]
dpb = [[0 for j in range(k+1)] for i in range(k+1)]
suma = []
sumb = []
for i in range(k):
    suma += [vl[i]]
    sumb += [vl[-i-1]]
    suma = sorted(suma)
    sumb = sorted(sumb)
    for j in range(min(i+1, k+1-i)):
        dpa[i][j] = sum(suma[j:])
        dpb[i][j] = sum(sumb[j:])
    
ans = 0
for a in range(k+1):
    for b in range(k+1-a):
        for c in range(min(a+1, k+1-a-b)):
            for d in range(min(b+1, k+1-a-b-c)):
                tmp = dpa[a-1][c] + dpb[b-1][d]
                if tmp > ans:
                    ans = tmp

print(ans)

Submission status: Runtime Error

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(K-a+1):
    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: Runtime Error

Solution code:
import copy
import heapq

tmp = list(map(int, input().split()))
n = tmp[0]
k = tmp[1]
v = list(map(int, input().split()))

a = [[0] * (k+1) for i in range(n+1)]
b = [[0] * (k+1) for i in range(n+1)]
ha = []
hb = []
for i in range(1, n+1):
  a[i][0] = a[i-1][0] + v[i-1]
  b[i][0] = b[i-1][0] + v[n-i]
  if v[i-1] < 0:
    heapq.heappush(ha, v[i-1])
  if v[n-i] < 0:
    heapq.heappush(hb, v[n-i])
  cha = copy.deepcopy(ha)
  chb = copy.deepcopy(hb)
  for j in range(1, k+1):
    if len(cha) != 0:
      a[i][j] = a[i][j-1] - heapq.heappop(cha)
    else:
      a[i][j] = a[i][j-1]
  for j in range(1, k+1):
    if len(chb) != 0:
      b[i][j] = b[i][j-1] - heapq.heappop(chb)
    else:
      b[i][j] = b[i][j-1]

maxv = 0
for i in range(0, k+1):
  maxa = 0
  maxb = 0
  for j in range(0,i+1):
    if a[j][i-j] > maxa:
      maxa = a[j][i-j]
  for j in range(0,k-i+1):
    if b[j][k-i-j] > maxb:
      maxb = b[j][k-i-j]
#  print('MAXA=' + str(maxa) + ' MAXB=' + str(maxb))    
  if (maxa + maxb) > maxv:
    maxv = maxa + maxb
print (maxv)

Submission status: Runtime Error

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

ans = -1
for i in range(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: Runtime Error

Solution code:
#include <bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
#define sz(x) int(x.size())
#define ALL(c) (c).begin(), (c).end()
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v, x) (std::find(v.begin(), v.end(), x) != v.end())

using namespace std;
typedef long long ll;

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
const int INF = 1001001001;
const long long INFL = (1LL<<60);
const double eps = (1e-9);

int main(int argc, char* argv[])
{
  int n, k;
  cin >> n >> k;
  vector<int> v(n);
  rep(i, n) cin >> v[i];

  ll ans = 0;
  for (int l = 0; l <= k; l++) {
    for (int r = 0; r < k-l; r++) {
      ll sum = 0;
      vector<ll> minus; minus.reserve(n);
      // 価値を求める
      for (int a = 0; a < l; a++) {
        sum += v[a];
        if (v[a] < 0) minus.push_back(v[a]);
      }
      for (int a = 0; a < r; a++) {
        sum += v[n-1-a];
        if (v[n-1-a] < 0) minus.push_back(v[n-1-a]);
      }

      // 価値がマイナスのものを，価値が低い順に最大で k - (l+r) 回捨てる
      sort(ALL(minus));
      ll mi = 0;
      for (int a = 0; a < min(k-(l+r), int(minus.size())); a++) {
        mi += minus[a];
      }
      sum -= mi;
      chmax(ans, sum);
    }
  }

  cout << ans << endl;

  return 0;
}

Submission status: Runtime Error

Solution code:
#include<bits/stdc++.h>
using namespace std;
long long int nCr[2003][2003];
int main(){
    int N,K;
    long long int sum=0,ma=0,V[500],b_r[500],f_r[500];
    cin >> N >> K;
    
    for(int i=0;i<N;i++)
        cin >> V[i];
    
    for(int k=0;k<=K;k++)
        for(int l=0;l<=min(k,N);l++){
            int r = k-l;
            sum = 0;
            long long int ls=0,rs=0;
            priority_queue<int> que;
            for(int j=0;j<l;j++){
                ls += V[j];
                if(V[j] < 0){
                    que.push(-V[j]);
                }
            }
            for(int j=N-1;j>=N-r;j--){
                rs += V[j];
                if(V[j] < 0){
                    que.push(-V[j]);
                }
            }
            for(int i=0;i<K-k;i++){
                if(que.empty())
                    break;
                sum += que.top();
                que.pop();
            }
            sum += ls+rs;
           ma = max(ma,sum);
    }
    cout << ma << endl;
    return 0;
}

Submission status: Runtime Error

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
INF = 1e10
best = -INF
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):
            score = sum(ls[c:])
            if score > best:
                best = score
            if v_ls[c] > 0:
                break
print(best)

Submission status: Runtime Error

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

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+1,K+1)):
    for j in range(max(0,min(N+1,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: Runtime Error

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+1,K+1)):
    for j in range(min(N+1,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: Runtime Error

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+1,K+1)):
    for j in range(min(N+1,K+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")