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.

Solution (Python):
[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 or i+j > N:
      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)

Solution (Python):
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()
        S = sum(nums)
        for n in range(min(K-i-j, len(nums))):
            if nums[n] >= 0:
                break
            S -= nums[n]
        ans = max(ans, S)
print(ans)

Solution (Python):
from copy import deepcopy

N, K = map(int, input().split())
V = tuple(map(int, input().split()))
max_score = -1000000000
for i in range(K+1):
    for j in range(K - i + 1):
        if i + j > N:
            continue
        
        hand = list(V[:i])
        
        hand.extend(V[N-j:])
        
        
        score = sum(hand)
        counter = 0
        for h in sorted(hand):
            counter += 1
            if K - i - j - counter < 0:
                break
            if h < 0:
                score -= h
        if max_score < score:
            max_score = score
print(max_score)

Solution (Python):
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 > min(n, k):
            continue
        trushes = sorted(ltrushes[i] + rtrushes[j], reverse = True)
        total.append(lsums[i] + rsums[j] + sum(trushes[:k - i - j]))

print(max(total))

Solution (Python):
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:]
        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)

Solution (Python):
import heapq
from itertools import chain

R = lambda: map(int, input().split())
n, k = R()
v = list(R())
r = 0
for i in range(1, min(n, k) + 1):
    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)

Solution (Python):
inf = float('inf')

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

volume = max(N, K)
take_max = min(N, K)

dpl = [[-inf] * (volume + 1) for _ in range(N + 1)]
dpr = [[-inf] * (volume + 1) for _ in range(N + 1)]
dpl[0][0] = dpr[0][0] = 0

for i, vv in enumerate(v, 1):
    for take in range(i + 1):
        dpl[i][take] = dpl[i - 1][take]
        if take > 0:
            dpl[i][take] = max(dpl[i][take], dpl[i - 1][take - 1] + vv)

for i, vv in enumerate(reversed(v), 1):
    for take in range(i + 1):
        dpr[i][take] = max(dpr[i][take], dpr[i - 1][take])
        if take > 0:
            dpr[i][take] = max(dpr[i][take], dpr[i - 1][take - 1] + vv)

ans = 0
for take_frm_left in range(take_max + 1):
    for take_frm_right in range(take_max + 1):
        if take_frm_left + take_frm_right > take_max: break
        for use_frm_left in range(take_frm_left + 1):
            remove_frm_left = take_frm_left - use_frm_left
            vl = dpl[take_frm_left][use_frm_left]
            for use_frm_right in range(take_frm_right + 1):
                remove_frm_right = take_frm_right - use_frm_right
                if (take_frm_left + take_frm_right + remove_frm_left + remove_frm_right) > K: continue
                vr = dpr[take_frm_right][use_frm_right]
                
                ans = max(ans, vl + vr)
print(ans)

Solution (Python):
n,k,*v=map(int,open(0).read().split())
m=0
for i in range(n):
 for j in range(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)

Solution (Python):
from collections import deque
from copy import deepcopy
import heapq

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

ans = 0
for i in range(k+1):
    for nl in range(i+1):
        nr = min(k - nl, n-nl)
        ansl, ansr = 0, 0
        for n_trash in range(nl):
            ansll, ansrr = 0, 0
            hands = sorted(v[:nl-n_trash])
            for vv in hands:
                if vv < 0 and n_trash > 0:
                    n_trash -= 1
                else:
                    ansll += vv
            ansl = max(ansl, ansll)

        for n_trash in range(nr):
            ansrr = 0
            hands = sorted(v[-(nr-n_trash):])
            
            for vv in hands:
                if vv < 0 and n_trash > 0:
                    n_trash -= 1
                else:
                    ansrr += vv
            ansr = max(ansr, ansrr)
        
        ans = max(ans, ansl + ansr)

print(ans)

Solution (Python):
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+1):
        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)

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

res = 0

for i in range(min(n, k)+1):
    for j in range(min(n, k)+1 - i):
        if j != 0:
            v_sub = v[:i] + v[-j:]
        else:
            v_sub = v[:i]
        if len(v_sub) > 0:
            v_sub.sort()
            for r in range(k - (i + j)+1):
                res = max(res, sum(v_sub))
                v_sub.pop(0)
                if len(v_sub) == 0:
                    break

print(res)

Solution (Python):
import heapq
import copy

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

M = min(N, K)

ans = 0
for i in range(M + 1):
    for j in range(M - i + 1):
        l = V[:i] + V[N - 1:N - j - 1:-1]
        t = sum(l)
        l.sort()
        for n in range(min(K - i - j, len(l))):
            if l[n] >= 0:
                break
            t -= l[n]
        if t > ans:
            ans = t

print(ans)

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

value = max_value = 0
for j in range(K//2+1):
    for i in range(K-j+1):
        if K-j >= N:
            value = V
        else:
            if i == K-j:
                value = V[:K-j]
            else:
                value = V[:i]+V[-(K-j-i):]
        value.sort()
        


        jj = min(len(value)-1,j)
        if value[jj] <= 0:
            s = sum(value[jj:])
        else:
            s = sum([i for i in value if i>0])

        if  s > max_value:
            max_value = s
print(max_value)

Solution (Python):
from collections import deque
from copy import deepcopy

length,kaisu=map(int,input().split())
A=deque(map(int,input().split()))
maxhist=[0]
for left in range(kaisu+1):
  for right in range(kaisu-left+1):
    l=deepcopy(A)
    point=[]
    dropable=kaisu-left-right
    for i in range(left):
      try:
        num=l.popleft()
        point.append(num)
      except:
        pass
    for j in range(right):
      try:
        num2=l.pop()
        point.append(num2)
      except:
        pass
    droptime=0
    while(droptime<dropable):
      if point==[]:
        break
      elif min(point)>0:
        break
      else:
        try:
          point.sort(reverse=True)
          point.pop(-1)
          droptime+=1
        except:
          break
    maxhist.append(sum(point))
print(max(maxhist))

Solution (Python):
n, k = map(int, input().split())
vs = list(map(int, input().split()))
m = 0
r = min(n, k)
for a in range(r+1):
    for b in range(r-a+1):
        cd = k - (a + b)
        if b == 0:
            hold = sorted(vs[:a])
        else:
            hold = sorted(vs[:a] + vs[-b:])
        ap = hold[min(len([1 for x in hold if x < 0]), cd):]
        s = sum(ap)
        m = max(s, m)
print(m)

Solution (Python):
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:]
        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)

Solution (Python):
import itertools

n, k = [int(i) for i in input().split()]
v = [int(i) for i in input().split()]
v2 = list(reversed(v))

answer = 0
for i in range(k+1):
    for j in range(k-i+1):
        for l in range(k-i-j+1):
            if i + j >= n:
                持っている宝石 = v[:]
            else:
                持っている宝石 = v[:i] + v2[:j]
            持っている宝石.sort()
            価値の合計 = sum(持っている宝石)

            for o in range(l):
                if o < len(持っている宝石):
                    if 持っている宝石[o] < 0:
                        価値の合計 -= 持っている宝石[o]
            answer = max(answer, 価値の合計)

print(answer)

Solution (Python):
import sys
from collections import deque
input = sys.stdin.readline

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

for i in range(K+1): 
    for j in range(K-i+1): 
        hand = deque()
        if i + j <= N:
            for n in range(i): 
                hand.append(V[n]) 
            for m in range(j): 
                hand.append(V[N-m-1])
            for k in range(K-i-j):
                if len(hand) > 0 and min(hand) < 0:
                    hand.remove(min(hand)) 
            score.append(sum(hand))

print(max(score))

Solution (Python):
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 + 1):
        if (i + j) > M:
            break
        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(K - i - j, len(ch))):
            c += -heapq.heappop(ch)
        if c > ans:
            ans = c

print(ans)

Solution (Python):

"""
Created on Sun May 26 20:56:05 2019

@author: Yamazaki Kenichi
"""
import heapq

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

def a(K):
    if K > N:
        return N
    else:
        return K

ans = 0
for i in range(K):
    for j in range(a(K-i)+1):
        tmp = 0
        minus = [0]
        for k in range(a(K-i)):
            tmp += V[k-j]
            heapq.heappush(minus,V[k-j])

        for k2 in range(i):
            if minus[0] < 0:
                tmp += (-heapq.heappop(minus))
            else:
                break

        ans = max(ans,tmp)
    
print(ans)

Solution (Python):
from itertools import accumulate


def make_dp(vvv, n, k):
    dp = [-(10 ** 9)] * (k + 1)
    dp[0] = 0

    for i in range(min(n, k)):
        i += 1
        vv = vvv[:i]
        vv.sort(reverse=True)
        sm = sum(vv)
        dp[i] = max(dp[i], sm)
        while i < k and vv and vv[-1] < 0:
            sm -= vv.pop()
            i += 1
            dp[i] = max(dp[i], sm)

    return list(accumulate(dp, func=max))


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

acl = make_dp(vvv, n, k)
vvv.reverse()
acr = make_dp(vvv, n, k)
acr.reverse()

ans = max(map(sum, zip(acl, acr)))
ans = min(ans, sum(v for v in vvv if v > 0))
print(ans)

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

sumval = 0
for l in range(min(K+1,N+1)):
    for r in range(min(K+1-l,N-l+1)):
        sV = sorted(V[:l]+ V[N-r:])
        
        
        
        
        
        for sute in range(min(K-r-l+1,len(sV))):
            c = sum(sV[sute:])
            sumval = max(c,sumval)

print(sumval)

Solution (Python):
from itertools import accumulate


def make_dp(vvv, n, k):
    dp = [-(10 ** 9)] * (k + 1)
    dp[0] = 0

    for i in range(min(n, k)):
        i += 1
        vv = vvv[:i]
        vv.sort(reverse=True)
        sm = sum(vv)
        dp[i] = max(dp[i], sm)
        while i < k and vv and vv[-1] < 0:
            sm -= vv.pop()
            i += 1
            dp[i] = max(dp[i], sm)

    return list(accumulate(dp, func=max))


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

acl = make_dp(vvv, n, k)
vvv.reverse()
acr = make_dp(vvv, n, k)

ans = max(acl[i] + acr[k - i] for i in range(k + 1))
ans = min(ans, sum(v for v in vvv if v > 0))
print(ans)

Solution (Python):
n,k,*v=map(int,open(0).read().split())
m=[]
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+=sum(t),
print(max(m))

Solution (Python):
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 + 1):
    for r in range(mini + 1 - 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)

Solution (Python):
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 + 1)
    for right in range(cap - left + 1)
)

print(result)

Solution (Python):
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)

Solution (Python):
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[::-1][:j])
  while t and(k-i-j)*t[0]<0:t.pop(0);j+=1
  m=max(m,sum(t))
print(m)

Solution (Python):
from heapq import heappop, heappush, heapify

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

ans = 0
for i in range(min(n, k+1)):  
  for j in range(min(k-i+1, n-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)

Solution (Python):
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+1):
    for b in range(R-a+1):
        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)

Solution (Python):
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
            inHand.sort()    
            for jj in range(ret_num):
                if inHand != [] and inHand[0] < 0:
                    del inHand[0]
            if max_sum < sum(inHand):
                max_sum = sum(inHand)

print(max_sum)

Solution (Python):
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))
  while t and(k-i-j)*t[0]<0:t.pop(0);j+=1
  m=max(m,sum(t))
print(m)

Solution (Python):
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)

Solution (Python):
n,k = map(int,input().split())
v = list(map(int,input().split()))
v_r = v[::-1]
ans = 0
for i in range(1,min(k+1,n+1)):
    for a in range(1,i+1):
        a_ = v[:a]
        b_ = v_r[:(i-a)]
        ab = a_+b_
        ab.sort()
        s = sum(ab)
        for j in range(k-i):
            if j<len(ab) and ab[j]<0:
                s -= ab[j]
            else:
                break
        ans = max(ans,s)
print(ans)

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

for i in range(K+1):
    
    for j in range(i+1):
        if N<=i:
            temp=sorted(V)
        elif j-i!=0:
            temp=sorted(V[:j]+V[j-i:])
        else:
            temp=sorted(V[:j])
        t=0
        for k in range(min(i,N)):
            if temp[k]>=0:
                t=k+1
                break
        
        
        
        if t>K-i:
            
            
            ans.append(sum(temp[K-i:]))
        elif (K-1-t)%2==1:
            if t>=2:
                
                ans.append(max(sum(temp[t-2:]),sum(temp[t:])))
            else:
                
                ans.append(sum(temp[t:]))
        else:
            
            ans.append(sum(temp[t-1:]))
print(max(ans))

Solution (Python):
N, K = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
for i in range(1, min(N,K)+1):
    for j in range(i+1):
        W = sorted(V[:j]+V[N-i+j:])
        for k in range(min(i,K-i)+1):
            ans = max(ans, sum(W[k:]))
print(ans)

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

V_minus = len([v for v in V if v < 0])
K = min(K, N + V_minus)

vc_left = [0] * (K + 1)
minus_num = [0]
for i in range(N):
    if V[i] < 0:
        minus_num.append(-V[i])
        minus_num.sort(reverse=True)
    if i + 1 <= K:
        vc_left[i + 1] = max(vc_left[i + 1], sum(V[:i + 1]), vc_left[i])
    for j in range(1, len(minus_num)):
        if i+1+j <= K:
            vc_left[i+1+j] = max(vc_left[i + 1 + j], sum(V[:i + 1])+sum(minus_num[:j]), vc_left[i+j])

V.reverse()
vc_right = [0] * (K+1)
minus_num = [0]
for i in range(N):
    if V[i] < 0:
        minus_num.append(-V[i])
        minus_num.sort(reverse=True)
    if i + 1 <= K:
        vc_right[i + 1] = max(vc_right[i + 1], sum(V[:i + 1]), vc_right[i])
    for j in range(1, len(minus_num)):
        if i+1+j <= K:
            vc_right[i+1+j] = max(vc_right[i + 1 + j], sum(V[:i + 1])+sum(minus_num[:j]), vc_right[i+j])

ans_cand = [0] * (K+1)
for i in range(K+1):
    ans_cand[i] = vc_left[i] + vc_right[K-i]
ans_max = sum([v for v in V if v > 0])
print(min(max(ans_cand), ans_max))

Solution (Python):
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];c=k-i-j
  while t and c*t[-1]<0:t.pop();c-=1
  m=max(m,sum(t))
print(m)

Solution (Python):
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-i,n-i)+1):
  t=sorted(v[:i]+v[-j:]*(j>0))[::-1];c=k-i-j
  while t and c*t[-1]<0:t.pop();c-=1
  m=max(m,sum(t))
print(m)

Solution (Python):
N, K = [int(x) for x in input().split(" ")]
D = [int(x) for x in input().split(" ")]

combs = []
lim = min(N, K)
for a in range(0, lim + 1):
    for b in range(0, lim - a + 1):
          combs.append((a, b))

maxres = 0
for a,b in combs:
    elems = sorted(D[:a] + D[N-b:])
    negative = len([e for e in elems if e < 0])
    drop = min(negative, max(0, K - a - b))
    res = sum(elems[drop:])
    maxres = max(maxres, res)
print(maxres)

Solution (Python):
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-i+1,n-i+1)):
  t=sorted(v[:i]+v[-j:]*(j>0))[::-1];c=k-i-j
  while t and c*t[-1]<0:t.pop();c-=1
  m=max(m,sum(t))
print(m)

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

pick_max = min(N, K)
ans = 0
for left in range(pick_max + 1):
    for right in range(pick_max - left + 1):
        t = v[:left] + v[N - right:]
        m = sorted(filter(lambda x: x < 0, t))[:K - left - right]
        ans = max(ans, sum(t) - sum(m))
print(ans)

Solution (Python):
from collections import Counter
from collections import deque
from functools import reduce
from pprint import pprint

import bisect
import copy
import fractions
import math
import random
import sys
import time
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def MI(): return map(int, sys.stdin.readline().split())
def II(): return int(sys.stdin.readline())
def IS(): return input()
def C(x): return Counter(x)
def GCD_LIST(numbers): return reduce(fractions.gcd, numbers)
def LCM_LIST(numbers): return reduce(LCM, numbers)
def LCM(m, n): return (m * n // fractions.gcd(m, n))
def unite(x, y):
    
    x = root(x)
    y = root(y)
    if x == y:
        return
    
    node[x] = y
def same(x,y): return bool(root(x) == root(y))
def root(x):
    if node[x] == x:  
        return x
    else:
        node[x] = root(node[x])  
        return node[x]
def dfs(v):
    
    visited[v] = True
    for v2 in range(n):
        if graph[v][v2] == False:  
            continue
        if visited[v2] == True:  
            continue
        dfs(v2)


n, k = MI()
v = LI()
m = min(n, k)
ans = -INF
for a in range(m+1):
    for b in range(0, m+1-a):
        tmp_l = []
        for i in range(a):
            tmp_l.append(v[i])
        for j in range(b):
            tmp_l.append(v[-(j+1)])
        index = 0
        negative_sum = 0
        tmp_l.sort()
        while index < len(tmp_l) and index < k-(a+b):
            if tmp_l[index] < 0:
                negative_sum -= tmp_l[index]
            index += 1
        ans = max(ans, negative_sum + sum(tmp_l))
print(ans)

Solution (Python):
from heapq import heapify, heappop

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

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

ans = 0
for pick in range(min(K, N) + 1):
    for to_remove in range(K + 1):
        if pick + to_remove > K: break
        for left in range(pick + 1):
            right = pick - left

            t = sum_L[left] + sum_R[right]

            q = vl[:left] + vr[:right]
            heapify(q)

            cnt = 0
            while cnt < to_remove and q:
                t -= heappop(q)
                cnt += 1
            ans = max(ans, t)
print(ans)

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

maxcost = min(N,K)
o = []

A = 0
while A <= maxcost:
    B = 0
    while A + B <= maxcost:
        a = V[:A]+V[N-B:]
        b = sorted(list(filter(lambda x:x<0,a)))[:K-A-B]
        
        o.append(sum(a)-sum(b))
        B += 1
    A += 1

print(max(o))

Solution (Python):
import itertools as it
import collections as col
import heapq
import sys
sys.setrecursionlimit(10**9)
"""
N=int(input())
D=[]
for i in range(N):
  s,p=input().split()
  D.append((s, int(p), i+1))
D.sort(key=lambda d: (d[0], -d[1], d[2]))

for d in D:
  print(d[2])
"""
"""
N,M=map(int, input().split())
K=[]
S=[]
for i in range(M):
  k,*si=map(int, input().split())
  K.append(k)
  S.append(list(sorted(si)))
P=list(map(int, input().split()))

L=it.product([0,1], repeat=N)
cnt=0

for l in L:
  ok=True
  for i,si in enumerate(S):
    pi=P[i]
    sumS=len(list(filter(lambda it: l[it-1]==1, si)))
    
    if sumS%2 != pi: ok=False
  if ok: cnt += 1
print(cnt)
"""
N,K=map(int,input().split())
V=list(map(int, input().split()))
M=max(N,K)

ans=0


for l in range(M+1):
  for r in range(M+1):
    if l+r<=min(N,K):
      H=[]
      U=list(V)
      H+=U[:l] + U[N-r:]
      H.sort(reverse=True)
      
      for i in range(K-l-r):
        if H and H[-1] < 0: H.pop()
        else: break
      
      ans=max(ans, sum(H))
print(ans)

Solution (Python):
from collections import deque

n,k = map(int,input().split())
v_in = list(map(int,input().split()))
v = deque(v_in)
m = -10000001
for i in range(k+1):
    for j in range(k-i+1):
        vv = deque(v_in)
        a = []
        for x in range(i):
            try:
                a.append(vv.popleft())
            except:
                break
        for y in range(j):
            try:
                a.append(vv.pop())
            except:
                break
        a.sort(reverse=True)
        for z in range(k-i-j):
            try:
                if a[-1] < 0:
                    a.pop()
                else:
                    break
            except:
                break
        tmp = sum(a)
        if tmp > m:
            m = tmp
print(m)

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

ans = 0

for l in range(n):
    for r in range(n-l+1):
        if l+r >k:
            break
        nv = v[:l] + v[n-r:]
        nv.sort()
        for i in range(k-l-r):
            if not nv or nv[0] >= 0:
                break
            nv.pop(0)
        ans = max(ans,sum(nv))
print(ans)

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

ans = 0

for a in range(min(N, K) + 1):
    for b in range(min(N-a, K-a)+1):
        c = K - a - b
        tmp = v_list[:a:] + v_list[N-b:]
        tmp.sort()
        trash = 0
        for i in range(min(c, len(tmp))):
            t = tmp[i]
            if t < 0:
                trash += t
            else:
                break
        ans = max(sum(tmp) - trash, ans)
print(ans)

Solution (Python):
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-i+1,n-i+1)):
  t=sorted(v[:i]+v[-j:]*(j>0))[::-1];c=k-i-j
  while t and c and t[-1]<0:t.pop();c-=1
  m=max(m,sum(t))
print(m)

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x) - 1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def SI(): return input()
from itertools import accumulate

def main():
    N, K = LI()
    V = LI()
    acc = [0] + list(accumulate(V))
    ans = 0
    for i in range(min(N, K) + 1):
        for p in range(i + 1):
            tmp = acc[p] + (acc[-1] - acc[-(i - p + 1)])
            sub = sorted(V[:p] + V[N - (i - p):])
            for s in sub[:K - i]:
                if s >= 0:
                    break
                tmp -= s
            ans = max(ans, tmp)
    return ans

print(main())

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

s = 0
for i in range(K + 1): 
    for j in range(K + 1 - i): 
        if i + j > N:
            continue
        
        a = V[:i] + V[N-j:]
        r = K - i - j 

        a.sort()
        for k in range(min(len(a)+1, r+1)):
            s = max(s, sum(a[k:]))

print(s)

Solution (Python):
import copy

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