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:
N,K = map(int,input().split())
V = list(map(int,input().split()))
Neg = []

Lvalue = [[-10 ** 24 for _ in range(K)] for i in range(K)]
Rvalue = [[-10 ** 24 for _ in range(K)] for i in range(K)]

K = min(K,N)

for i in range(K):
    Lvalue[i][i] = sum(V[:(i+1)])
    for n in range(i+1):
        if V[n] < 0:
            Neg.append(V[n])
        Neg.sort()
    for j in range(K-i-1):
        if Neg != []:
            Lvalue[i][i+j+1] = Lvalue[i][i+j] - Neg[0]
            Neg.pop(0)
        else:
            Lvalue[i][i + j + 1] = Lvalue[i][i+j]


V = V[::-1]

Neg = []
for i in range(K):
    Rvalue[i][i] = sum(V[:(i+1)])
    for n in range(i+1):
        if V[n] < 0:
            Neg.append(V[n])
        Neg.sort()
    for j in range(K-i-1):
        if Neg != []:
            Rvalue[i][i+j+1] = Rvalue[i][i+j] - Neg[0]
            Neg.pop(0)
        else:
            Rvalue[i][i + j + 1] = Rvalue[i][i+j]

Lmax = [0 for i in range(K+1)]
Rmax = [0 for i in range(K+1)]

for i in range(K):
    for j in range(K):
        lmax = Lvalue[j][i]
        if Lmax[i+1] < lmax:
            Lmax[i+1] = lmax

for i in range(K):
    for j in range(K):
        rmax = Rvalue[j][i]
        if Rmax[i+1] < rmax:
            Rmax[i+1] = rmax

record = 0
for i in range(K+1):
    for j in range(K+1-i):
        score1 = Lmax[i]+Rmax[j]
        if record < score1:
            record = score1

print(record)

Submission status: Wrong Answer

Solution code:
[_, input_k] = map(int, input().split())

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

max_value = 0

for a in range(input_k + 1):
    for b in range(input_k - a + 1):
        if (b != 0):
            hold_jewelries = input_v[:a] + input_v[-b:]
        else:
            hold_jewelries = input_v[:a]
        hold_jewelries = sorted(hold_jewelries)

        remove_length = input_k - a - b 
        hold_minus = sum(1 for value in hold_jewelries if value < 0)

        hold_value = sum(hold_jewelries[min(hold_minus, remove_length):])

        if (hold_value > max_value):
            max_value = hold_value

print(max_value)

Submission status: Wrong Answer

Solution code:
import numpy as np 

N, K = map(int, input().split())
vs = np.array([int(v) for v in input().split()])
ak = min(N + 1, K + 1)
max_value = 0

for a in range(ak):
    bk = min(N - a + 1, K - a + 1)
    for b in range(bk):
        n_remove = K - a - b
        l = np.sum(vs[:a]) if a > 0 else 0
        r = np.sum(vs[-b:]) if b > 0 else 0
        print(l, r)
        
        if a > 0 and b > 0:
            pos = np.r_[vs[:a], vs[-b:]]
            order = np.argsort(pos)
            pos = pos[order]
        elif a > 0:
            pos = vs[:a]
            order = np.argsort(pos)
            pos = pos[order]
        elif b > 0:
            pos = vs[-b:]
            order = np.argsort(pos)
            pos = pos[order]
        else:
            pos = np.array([])
            
        n_minus = len(np.where(pos < 0)[0])
        
        value = l + r - np.sum(pos[:n_minus]) if n_minus < n_remove else l + r - np.sum(pos[:n_remove])
        print(a, b, value)
        max_value = max(value, max_value)

print(max_value)

Submission status: Wrong Answer

Solution code:
[_, input_k] = map(int, input().split())

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

max_value = 0

for a in range(input_k + 1):
    for b in range(input_k - a + 1):
        if (b != 0):
            hold_jewelries = input_v[:a] + input_v[-b:]
        else:
            hold_jewelries = input_v[:a]
        hold_jewelries = sorted(hold_jewelries)

        remove_length = input_k - a - b 
        hold_minus = sum(1 for value in hold_jewelries if value < 0)

        hold_value = hold_jewelries[min(hold_minus, remove_length):]

        if (sum(hold_value) > max_value):
            max_jewelries = sum(hold_jewelries)

print(max_value)

Submission status: Wrong Answer

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

Lvalue = [[0 for _ in range(K)] for i in range(K)]
Rvalue = [[0 for _ in range(K)] for i in range(K)]

K = min(K,N)

for i in range(K):
    Lvalue[i][i] = sum(V[:(i+1)])
    for n in range(i+1):
        if V[n] < 0:
            Neg.append(V[n])
        Neg.sort()
    for j in range(K-i-1):
        if Neg != []:
            Lvalue[i][i+j+1] = Lvalue[i][i+j] - Neg[0]
            Neg.pop(0)
        else:
            Lvalue[i][i + j + 1] = Lvalue[i][i+j]


V = V[::-1]

Neg = []
for i in range(K):
    Rvalue[i][i] = sum(V[:(i+1)])
    for n in range(i+1):
        if V[n] < 0:
            Neg.append(V[n])
        Neg.sort()
    for j in range(K-i-1):
        if Neg != []:
            Rvalue[i][i+j+1] = Rvalue[i][i+j] - Neg[0]
            Neg.pop(0)
        else:
            Rvalue[i][i + j + 1] = Rvalue[i][i+j]

Lmax = [0 for i in range(K+1)]
Rmax = [0 for i in range(K+1)]

for i in range(K):
    for j in range(K):
        lmax = Lvalue[j][i]
        if Lmax[i+1] < lmax:
            Lmax[i+1] = lmax

for i in range(K):
    for j in range(K):
        rmax = Rvalue[j][i]
        if Rmax[i+1] < rmax:
            Rmax[i+1] = rmax

record = 0
for i in range(K+1):
    for j in range(K+1-i):
        score1 = Lmax[i]+Rmax[j]
        if record < score1:
            record = score1

print(record)

Submission status: Wrong Answer

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

Lvalue = [[0 for _ in range(K)] for i in range(K)]
Rvalue = [[0 for _ in range(K)] for i in range(K)]

K = min(K,N)

for i in range(K):
    Lvalue[i][i] = sum(V[:(i+1)])
    for n in range(i+1):
        if V[n] < 0:
            Neg.append(V[n])
        Neg.sort()
    for j in range(K-i-1):
        if Neg != []:
            Lvalue[i][i+j+1] = Lvalue[i][i+j] - Neg[0]
            Neg.pop(0)
        else:
            Lvalue[i][i + j + 1] = Lvalue[i][i+j]


V = V[::-1]

Neg = []
for i in range(K):
    Rvalue[i][i] = sum(V[:(i+1)])
    for n in range(i+1):
        if V[n] < 0:
            Neg.append(V[n])
        Neg.sort()
    for j in range(K-i-1):
        if Neg != []:
            Rvalue[i][i+j+1] = Rvalue[i][i+j] - Neg[0]
            Neg.pop(0)
        else:
            Rvalue[i][i + j + 1] = Rvalue[i][i+j]

Lmax = [0 for i in range(K+1)]
Rmax = [0 for i in range(K+1)]

for i in range(K):
    for j in range(K):
        lmax = Lvalue[j][i]
        if Lmax[i+1] < lmax:
            Lmax[i+1] = lmax

for i in range(K):
    for j in range(K):
        rmax = Rvalue[j][i]
        if Rmax[i+1] < rmax:
            Rmax[i+1] = rmax

record = 0
for i in range(K+1):
    for j in range(K+1-i):
        score1 = Lmax[i]+Rmax[j]
        score2 = Lmax[j]+Rmax[i]
        S = max(score1,score2)
        if record < S:
            record = S

print(record)

Submission status: Wrong Answer

Solution code:
[input_n, input_k] = map(int, input().split())

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

max_jewelries = 0

for a in range(input_k + 1):
    for b in range(input_k - a + 1):
        if (b):
            hold_jewelries = input_v[:a] + input_v[-b:]
        else:
            hold_jewelries = input_v[:a]
        remove_length = input_k - a - b 
        hold_jewelries = sorted(hold_jewelries)
        hold_minus = sum(1 for jewelry in hold_jewelries if jewelry < 0)
        hold_jewelries = hold_jewelries[min(hold_minus, remove_length):]

        if (sum(hold_jewelries) > max_jewelries):
            max_jewelries = sum(hold_jewelries)

print(max_jewelries)

Submission status: Wrong Answer

Solution code:
[input_n, input_k] = map(int, input().split())

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

max_jewelries = 0

for a in range(input_k + 1):
    for b in range(input_k - a + 1):
        if (b):
            hold_jewelries = input_v[:a] + input_v[-b:]
        else:
            hold_jewelries = input_v[:a]
        print(hold_jewelries)
        remove_length = input_k - a - b 
        hold_jewelries = sorted(hold_jewelries)
        hold_minus = sum(1 for jewelry in hold_jewelries if jewelry < 0)
        hold_jewelries = hold_jewelries[min(hold_minus, remove_length):]

        if (sum(hold_jewelries) > max_jewelries):
            max_jewelries = sum(hold_jewelries)

print(max_jewelries)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = 0
for left in range(n + 1):
    for right in range(n + 1 - left):
        if left > n - right or left + right > k:
            continue
        li = v[:left] + v[n - right:]
        rem = k - left - right
        ans = max(ans, sum(sorted(li)[rem:]))
print(ans)

Submission status: Wrong Answer

Solution code:
[input_n, input_k] = map(int, input().split())

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

max_jewelries = 0

for a in range(input_k):
    for b in range(input_k - a):
        if (b):
            hold_jewelries = input_v[:a] + input_v[-b:]
        else:
            hold_jewelries = input_v[:a]
        remove_length = input_k - a - b
        hold_jewelries = sorted(hold_jewelries)
        hold_minus = sum(1 for jewelry in hold_jewelries if jewelry < 0)
        hold_jewelries = hold_jewelries[min(hold_minus, remove_length):]

        if (sum(hold_jewelries) > max_jewelries):
            max_jewelries = sum(hold_jewelries)

print(max_jewelries)

Submission status: Wrong Answer

Solution code:
from bisect import bisect_left, bisect_right
from collections import Counter, defaultdict, deque, OrderedDict
from copy import deepcopy
from fractions import gcd
from functools import lru_cache, reduce
from math import ceil, floor
from sys import setrecursionlimit

import heapq as hq
import itertools as its
import operator as op


# globals
inf = float('inf')
N = 0


def set_inputs():
    global N
    return


def main():
    setrecursionlimit(100000)
    set_inputs()
    # ----------MAIN----------
    n, k = get_li()
    V = get_li()
    ans = 0
    for v in range(k+1):
        forward = V[:v]
        backward = V[n-1:n-(k-v)-1:-1]
        tmp = sum(forward + backward)
        while True:
            minf = min(forward + [inf])
            minb = min(backward + [inf])
            if min(minf, minb) >= 0:
                break
            last = inf
            if len(forward) > 0:
                last = forward[-1]
            if len(backward) > 0:
                last = min(last, backward[-1])
            if last >= 0 and abs(min(minf, minb)) < last:
                break
            if minf < minb:
                i = forward.index(minf)
                if i == len(forward) - 1:
                    forward.pop(i)
                elif len(backward) == 0:
                    forward.pop(i)
                    forward.pop()
                elif forward[-1] < backward[-1]:
                    forward.pop(i)
                    forward.pop()
                else:
                    forward.pop(i)
                    backward.pop()
            else:
                i = backward.index(minb)
                if i == len(backward) - 1:
                    backward.pop(i)
                elif len(forward) == 0:
                    backward.pop(i)
                    backward.pop()
                elif backward[-1] < forward[-1]:
                    backward.pop(i)
                    backward.pop()
                else:
                    backward.pop(i)
                    forward.pop()
            tmp = sum(forward + backward)
        ans = max(ans, tmp)
    print(ans)
    return


def get_int():
    return int(input())


def get_float():
    return float(input())


def get_str():
    return input().strip()


def get_li():
    return [int(i) for i in input().split()]


def get_lf():
    return [float(f) for f in input().split()]


def get_lc():
    return list(input().strip())


def get_data(n, types, sep=None):
    if len(types) == 1:
        return [types[0](input().strip()) for _ in range(n)]
    return list(zip(*(
        [t(x) for t, x in zip(types, input().strip().split(sep=sep))]
        for _ in range(n)
    )))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

def intinput(): return map(int,sys.stdin.readline().split())

n,k=intinput()
l = list(intinput())
f=0
for i in range(k+1):
    for j in range(k-i+1):
        li = l[:i] + l[n-j:]
        li.sort()
        cnt = k-i-j
        for h in range(len(li)):
            if cnt <= 0:
                break
            if li[h]<0:
                li[h]=0
                cnt -= 1
        f = max(f,sum(li))
print(f)

Submission status: Wrong Answer

Solution code:
import math
from functools import reduce
from collections import deque

def s(generator, splitter, mapper):
    return [ mapper(s) for s in generator().split(splitter) ]

# スペース区切りの入力を読み込んで数値リストにして返します。
def get_nums_l():
    return [ int(s) for s in input().split(" ")]

# 改行区切りの入力をn行読み込んで数値リストにして返します。
def get_nums_n(n):
    return [ int(input()) for _ in range(n)]

n,k = get_nums_l()
v = get_nums_l()

# 左側でpos回操作するときの最大得点
left = [0]*(k+1)
# 右側
right = [0]*(k+1)

for i in range(1,min(n, k+1)):
    score = max(left[i-1], sum(v[:i]))
    for ii in range(i//2):
        ii += 1
        score = max(score, sum(sorted(v[:i-ii],reverse=True)[:i-(ii*2)]))
    left[i] = score

for i in range(1,min(n, k+1)):
    score = max(right[i-1],sum(v[(n-i):]))
    for ii in range(i//2):
        ii += 1
        score = max(score, sum(sorted(v[n-i+ii:],reverse=True)[:i-(ii*2)]))
    right[i] = score


ans = 0

for i in range(k+1):
    ans = max(ans, left[i] + right[k-i])
print(ans)

Submission status: Wrong Answer

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

N, K = map(int, input().split())
V = list(map(int, input().split()))
have = []
ans = 0
O = min(K, N)
for i in range(O+1):
    for j in range(O-i+1):
        if i+j > N: continue
        d = K-i-j
        if d<0: continue
        have+=V[0:i]
        have+=V[len(V)-j:]
#        print('i:{}, j:{}, have:{} d:{}'.format(i, j, have, d))

        have = sorted(have)
#        print(d)
#        print(have[d:])
        ans = max(sum(have[d:]), ans)
#        print('ans:{}'.format(ans))
        have = []

print(ans)

Submission status: Wrong Answer

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

N, K = map(int, input().split())
V = list(map(int, input().split()))
have = []
ans = 0
O = min(K, N)
for i in range(O+1):
    for j in range(O-i+1):
        d = K-i-j
        if d<0: continue
        have+=V[0:i]
        have+=V[len(V)-j:]
#        print('i:{}, j:{}, have:{} d:{}'.format(i, j, have, d))

        have = sorted(have)
#        print(d)
#        print(have[d:])
        ans = max(sum(have[d:]), ans)
#        print('ans:{}'.format(ans))
        have = []

print(ans)

Submission status: Wrong Answer

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

N, K = map(int, input().split())
V = list(map(int, input().split()))
have = []
ans = 0
for i in range(K+1):
    for j in range(K-i+1):
        d = K-i-j
        if d<0: continue
        have+=V[0:i]
        have+=V[len(V)-j:]
#        print('i:{}, j:{}, have:{} d:{}'.format(i, j, have, d))

        have = sorted(have)
#        print(d)
#        print(have[d:])
        ans = max(sum(have[d:]), ans)
#        print('ans:{}'.format(ans))
        have = []

print(ans)

Submission status: Wrong Answer

Solution code:
[N,K] = list(map(int,input().split()))
V = list(map(int,input().split()))
C = [0]*(N+1)
for i in range(N):
  C[i+1] = C[i]+V[i]
ans = 0
for i in range(N+1):
  for j in range(N+1):
    if i+j > K:
      continue
    sum = C[i]+(C[N]-C[N-j])
    rem = K-i-j
    v = []
    for k in range(i):
      if V[k] < 0:
        v.append(V[k])
    for k in range(j):
      if V[N-1-k] < 0:
        v.append(V[N-1-k])
    v = sorted(v)
    for k in range(min(rem,len(v))):
      sum -= v[k]
    ans = max(ans,sum)
print(ans)

Submission status: Wrong Answer

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

maxv = 0
for il in range(K + 1):
    for ir in range(K - il + 1):
        if il + ir > N:
            break
        elif il + ir == N:
            gets = V
            d = K - N
        else:
            gets = V[:il + 1] + V[-1 - ir:]
            d = K - il - ir
        gets.sort()
        #print("il={},ir={},d={},gets={}".format(il,ir,d,gets))
        for v in gets:
            if v >= 0:
                break
            if d == 0:
                break
            #print("remove={}".format(v))
            gets.remove(v)
            d -= 1
        maxv = max(maxv, sum(gets))
        #print("sum(gets)={}".format(sum(gets)))

print(maxv)

Submission status: Wrong Answer

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

ans = 0

for l in range(k):
    for r in range(k-l):
        hand = []
        if l+r > n:
            hand = list(v)
            d = k - n
        else:
            hand = list(v[:l])
            hand.extend(list(v[len(v)-r:]))
            d = k - l - r

        now = sum(hand)

        hand.sort()
        new_s = 0

        for i in range(min(d, len(hand))):

            if hand[i] >= 0:
                break

            now -= hand[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()))

Q = min(N,K)+1
res = 0
for a in range(Q):
    for b in range(Q-a):
        W = V[:a] + V[N-b:]
        print(W)
        W.sort()
        for k in range(min(K-a-b,len(W))):
            if W[k] < 0:
                W[k] = 0
            else:
                break
        res = max(res,sum(W))

print(res)

Submission status: Wrong Answer

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

ans = 0

for l in range(k):
    for r in range(k-l):
        if l+r > n:
            continue

        d = k - l - r
        hand = []
        hand = list(v[:l])
        hand.extend(list(v[len(v)-r:]))
        now = sum(hand)

        hand.sort()
        new_s = 0

        for i in range(min(d, len(hand))):

            if hand[i] >= 0:
                break

            now -= hand[i]

        ans = max(ans, now)

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = map(int, input().split())
vs = list(map(int,input().split()))
rvs = list(reversed(vs))
def get_cand(vs):
    sums = [0]
    trushes = [[]]
    pre = 0
    trush = []
    for v in vs:
        sums.append(v+pre)
        pre += v
        if v < 0:
            trush.append(-v)
        trushes.append(list(tuple(trush)))
    return sums, trushes
lsums, ltrushes = get_cand(vs)
rsums, rtrushes = get_cand(rvs)

total = []
for i in range(len(lsums)):
    for j in range(len(rsums)):
        if i + j > k:
            continue
        trushes = sorted(ltrushes[i] + rtrushes[j], reverse = True)
        total.append(lsums[i] + rsums[j] + sum(trushes[:k - i - j]))

print(max(total))

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
for a in range(min(N, K)):
    for b in range(min(N, K) + 1 - a):
        juels = sorted(V[:a] + V[len(V)-b:])
        if not juels:
            continue
        cd = K - a - b
        now = sum(juels)
        for i in range(min(len(juels), cd)):
            if juels[i] < 0:
                now -= juels[i]
        ans = max(ans, now)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k,*v=map(int,open(0).read().split())
m=0
for i in range(n+1):
 for j in range(min(k,n)-i):
  t=sorted(v[:i]+v[:~j:-1])
  while t and(k-i-j)*t[0]<0:t.pop(0);j+=1
  m=max(m,sum(t))
print(m)

Submission status: Wrong Answer

Solution code:
n,k,*v=map(int,open(0).read().split())
m=[]
for i in range(n+1):
 for j in range(min(k,n)-i):
  t=sorted(v[:i]+v[:~j:-1])
  while t and(k-i-j)*t[0]<0:t.pop(0);j+=1
  m+=sum(t),
print(max(m))

Submission status: Wrong Answer

Solution code:
from collections import deque

N, K = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
O = min(N, K)
for i in range(O+1):
    for j in range(O-i+1):
        if i + j > O:
            break
        DV = deque(V)
        S = 0
        nums = []
        for _ in range(i):
            nums.append(DV.popleft())
        for _ in range(j):
            nums.append(DV.pop())
        nums.sort()
        d = 0
        for k, n in enumerate(nums):
            if d == K - i - j:
                break
            if n < 0:
                del nums[k]
                d += 1
            else:
                break
        ans = max(ans, sum(nums))
print(ans)

Submission status: Wrong Answer

Solution code:
from collections import deque

N, K = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
O = min(N, K)
for i in range(O+1):
    for j in range(O-i+1):
        if i + j > O:
            break
        DV = deque(V)
        S = 0
        nums = []
        for _ in range(i):
            nums.append(DV.popleft())
        for _ in range(j):
            nums.append(DV.pop())
        nums.sort()
        print(nums)
        d = 0
        for k, n in enumerate(nums):
            if d == K - i - j:
                break
            if n < 0:
                del nums[k]
                d += 1
            else:
                break
        ans = max(ans, sum(nums))
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
for a in range(min(N, K)):
    for b in range(min(N, K) + 1 - a):
        juels = sorted(V[:a] + V[len(V)-b:])
        if not juels:
            continue
        cd = K - a - b
        index = 0
        for i in range(min(len(juels), cd)):
            if juels[i] < 0:
                index = i + 1
        ans = max(ans, sum(juels[index:]))
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+1):
    for r in range(K-l):
        if l + r < N:
            bag = V[:l] + V[N-r:] + [0]
            rest_ope = K-l-r
        else:
            bag = V[:]
            rest_ope = K - N
        for _ in range(rest_ope):
            min_bag = min(bag)
            if min_bag < 0 and rest_ope > 0:
                bag.remove(min_bag)
                rest_ope -= 1
        else:
            ans = max(ans, sum(bag))

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
from itertools import chain

R = lambda: map(int, input().split())
n, k = R()
v = list(R())
r = 0
for i in range(min(n, k)):
    for j in range(i):
        s = 0
        h = []
        for m in chain(range(i - j), range(n - j, n)):
            x = v[m]
            s += x
            if x < 0:
                heapq.heappush(h, x)
        j = i
        while h and j < k:
            s -= heapq.heappop(h)
            j += 1
        r = max(r, s)
print(r)

Submission status: Wrong Answer

Solution code:
n,k,*v=map(int,open(0).read().split())
m=0
for i in range(n+1):
 for j in range(min(k,n)-i+1):
  t=sorted(v[:i]+v[:~j])
  while t and(k-i-j)*t[0]<0:t.pop(0);j+=1
  m=max(m,sum(t))
print(m)

Submission status: Wrong Answer

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

Q = min(N,K)
res = 0
for a in range(Q):
    for b in range(Q-a):
        W = V[:a] + V[N-b:]
        W.sort()
        for k in range(min(K-a-b,len(W))):
            if W[k] < 0:
                W[k] = 0
            else:
                break
        res = max(res,sum(W))

print(res)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

mini = min(N, K)

ans = 0
for l in range(mini):
    for r in range(mini - l):
        take = v[:l] + v[N - r:]
        ret = sorted(list(filter(lambda x: x < 0, take)))[:K-l-r]
        s = sum(take) - sum(ret)
        if s > ans:
            ans = s

print(ans)

Submission status: Wrong Answer

Solution code:
import heapq
import copy

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

M = min(N, K)

ans = 0
a = 0
ah = []
for i in range(M + 1):
    if i > 0:
        v = V[i - 1]
        a += v
        if v < 0:
            heapq.heappush(ah, v)

    b = a
    bh = copy.copy(ah)
    for j in range(M - i + 1):
        if j > 0:
            v = V[N - j]
            b += v
            if v < 0:
                heapq.heappush(bh, v)

        c = b
        ch = copy.copy(bh)

        for n in range(min(M - i - j, len(ch))):
            c += -heapq.heappop(ch)

        if c > ans:
            ans = c

print(ans)

Submission status: Wrong Answer

Solution code:
import numpy as np

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

mini = min(N, K)

ans = 0
for l in range(mini):
    for r in range(mini - l):
        take = v[:l] + v[N - r:]
        ret = sorted(list(filter(lambda x: x<0, take)))
        s = sum(take) - sum(ret)
        if s > ans:
            ans = s

print(ans)

Submission status: Wrong Answer

Solution code:
n,k,*v=map(int,open(0).read().split())
m=0
r=range
for i in r(n+1):
 for j in r(min(k,n)-i+1):
  t=sorted(v[:i]+v[:-j:-1])
  while t and(k-i-j)*t[0]<0:t.pop(0);j+=1
  m=max(m,sum(t))
print(m)

Submission status: Wrong Answer

Solution code:
import sys

def intinput(): return map(int,sys.stdin.readline().split())

n,k=intinput()
l = list(intinput())
f=0
for i in range(k+1):
    for j in range(k-i+1):
        li = l[:i] + l[n-j:]
        li.sort()
        cnt = k-i-j
        for h in range(len(li)):
            if cnt == 0:
                break
            if li[h]<0:
                li[h]=0
                cnt -= 1
        f = max(f,sum(li))
print(f)

Submission status: Wrong Answer

Solution code:
n, k = [int(a) for a in input().split()]
v = [int(a) for a in input().split()]


def value(left, right):
    l_popped = v[:left]
    r_popped = v[-right:] if right > 0 else []
    popped = l_popped + r_popped

    negative = [x for x in popped if x < 0]
    negative.sort()
    
    rest = k - (left + right)
    pushed = negative[:rest]

    return sum(popped) - sum(pushed)


cap = min(n, k)
result = max(
    value(left, right)
    for left in range(k)
    for right in range(cap - left)
)

print(result)

Submission status: Wrong Answer

Solution code:
# N = int(input())
# A = [int(x) for x in input().split()]
# V = [[0] * 100 for _ in range(100)]

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

from collections import defaultdict


def f(k):
    ret = -99999999999999999999999
    dd = defaultdict(lambda: 0)
    s = 0
    for i in range(k):
        s += V[-k + i]
        dd[V[-k + i]] += 1

    ret = max(ret, s - neg(dd, K - k))

    for i in range(k):
        rm = V[-k + i]
        ad = V[-k + i + k]
        dd[rm] -= 1
        dd[ad] += 1
        s -= rm
        s += ad
        ret = max(ret, s - neg(dd, K - k))

    return ret


def neg(d, n):
    L = sorted(d.items())
    ret = 0
    for l in L:
        v, cnt = l[0], l[1]
        if n <= 0 or v >= 0:
            break
        if n >= cnt:
            ret += (v * cnt)
        else:
            ret += (v * n)
        n -= cnt
    return ret


ans = 0
for i in range(1, min(K + 1, N)):
    ans = max(ans, f(i))
print(ans)

Submission status: Wrong Answer

Solution code:
N, K = map(int, input().split())
V = [int(i) for i in input().split()]
V2 = V[::-1]
R = min(N, K)
ans = 0
for a in range(R):
    for b in range(R-a):
        c = K - (a + b)
        t = 0
        for v in sorted(V[:a]+V2[:b]):
            if v < 0 and c > 0:
                c -=1
                continue
            t += v
        ans=max(t, ans)
print(ans)

Submission status: Wrong Answer

Solution code:
n,k,*v=map(int,open(0).read().split())
m=0
r=range
for i in r(n+1):
 for j in r(min(k,n)-i+1):
  t=sorted(v[:i]+v[-j:]*(j>0))[::-1]
  while t and(k-i-j)*t[-1]<0:t.pop();j-=1
  m=max(m,sum(t))
print(m)

Submission status: Wrong Answer

Solution code:
n, k = [int(i) for i in input().split()]
V = [int(i) for i in input().split()]
plusList = [i >= 0 for i in V]
max_sum = 0

for ki in range(k+1):
    for mi in range(ki+1):
        for ii in range(ki-mi+1):
            if ki-mi-ii-1 >= n-ii:
                inHand = V
                ret_num = mi + ki-mi-ii-1 - (n-ii) + 1
            else:
                inHand = V[:ki-mi-ii]+V[n-ii:]
                ret_num = mi
            for jj in range(ret_num):
                inHand.sort()
                del inHand[:ret_num]
            if max_sum < sum(inHand):
                max_sum = sum(inHand)
print(max_sum)

Submission status: Wrong Answer

Solution code:
from heapq import heappop, heappush, heapify

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

ans = 0
for i in range(k+1):  #　右から
  for j in range(k-i+1):  # 左から
    if i == 0:
      tmpl = vlist[:j]
    else:
      tmpl = vlist[:j] + vlist[-i:]
    heapify(tmpl)
    for _ in range(min(k - len(tmpl), len(tmpl))):
      _h = heappop(tmpl)
      if _h > 0:
        tmpl.append(_h)
        break
    tmpa = sum(tmpl)
    if tmpa > ans:
      ans = tmpa

print(ans)

Submission status: Wrong Answer

Solution code:
n, k = [int(i) for i in input().split()]
V = [int(i) for i in input().split()]
plusList = [i >= 0 for i in V]
max_sum = 0

for ki in range(k+1):
    for mi in range(ki+1):
        for ii in range(ki-mi+1):
            if ki-mi-ii-1 >= n-ii:
                inHand = V
            else:
                inHand = V[:ki-mi-ii]+V[n-ii:]
            for jj in range(mi):
                inHand.sort()
                del inHand[:mi]
            if max_sum < sum(inHand):
                max_sum = sum(inHand)
print(max_sum)