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):
from heapq import *
n, k = map(int, input().split())
d = list(map(int, input().split()))

res = 0
num = min(k, n)
for a in range(num + 1):
    for b in range(num + 1 - a):
        trash = k - (a + b)
        s = d[:a] + d[-b:] if b != 0 else d[:a]
        heapify(s)
        for _ in range(min(trash, a + b)):
            tmp = heappop(s)
            if tmp > 0:
                heappush(s, tmp)
                break
        res = max(res, sum(s))
print(res)

Solution (Python):
N,K = list(map(int,input().split()))
V = list(map(int,input().split()))
out = 0
for left in range(0,K+1):
    for right in range(0,K-left+1):
        if left==0 and right==0:
            continue
        if left + right <= N:
            if right!=0:
                box = V[0:left] + V[N-right:N]
            else:
                box = V[0:left]
            S = sum(box)
            box.sort(reverse=True)
            t = -1
            Life = K-(left+right)
            while Life>0 and abs(t)<=len(box) and box[t]<0:
                S = S-box[t]
                Life += -1
                t += -1
            if S>out:
                out = S
print(max(out,0))

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()

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

ans = -float("inf")

left = v[:]
right = v[::-1]

import bisect

for i in range(n + 1):
    for j in range(n + 1 - i):
        if i + j > k:
            continue
        left_and_right = left[:i] + right[:j]
        left_and_right.sort()
        cnt = k - (i + j)
        idx = bisect.bisect_left(left_and_right, 0)
        minus = sum(left_and_right[:min(cnt, idx)])
        ans = max(sum(left_and_right) - minus, ans)

print(ans)

Solution (Python):







import sys
readline = sys.stdin.readline

N,K = map(int,readline().split())
V = [0] + list(map(int,readline().split())) + [0]
N += 2
K += 2

leftsum = 0
leftminus = []
ans = -(10 ** 9)
for left in range(min(N,K)):
  leftsum += V[left]
  if V[left] < 0:
    leftminus += [V[left]]
  rightsum = 0
  rightminus = []
  limit = min(N - (left + 1) , K - (left + 1))
  for right in range(N - 1, N - limit - 1, -1):
    rightsum += V[right]
    if V[right] < 0:
      rightminus += [V[right]]
    allsum = leftsum + rightsum
    rest = max(K - ((left + 1) + (N - right)),0)
    allminus = sorted(leftminus + rightminus)
    allsum -= sum(allminus[:rest])
    if allsum > ans:
      ans = allsum
print(ans)

Solution (Python):
def resolve():
    N, K = list(map(int, input().split()))
    V = list(map(int, input().split()))
    start = (K+1)//2 if K%2==1 else K//2
    maxvalue = 0
    for npop in range(start, K+1):
        if npop >= N:
            npush = K - N
            negs = sorted([v for v in V if v < 0])
            posis = [v for v in V if v >= 0]
            maxvalue = max(maxvalue, sum(posis)+(sum(negs)-sum(negs[:npush])))
            continue

        for nleft in range(npop+1):
            nright = npop-nleft
            picked = V[:nleft] + V[N-nright:]
            negs = sorted([v for v in picked if v < 0])
            posis = [v for v in picked if v >= 0]
            npush = min(K-npop, len(negs))
            maxvalue = max(maxvalue, sum(posis)+(sum(negs)-sum(negs[:npush])))
    print(maxvalue)
            


if '__main__' == __name__:
    resolve()

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

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

minus=[[[0]*(N+1) for _ in range(N+1)] for _ in range(N+1)]

vdeque=deque(V)

for left in range(N+1):
    for right in range(N-left+1):
        v=deepcopy(vdeque)
        q=[]

        tleft=left
        while tleft>0:
            vi=deque.popleft(v)
            if vi<0:
                heapq.heappush(q,vi)
            tleft-=1
        
        tright=right

        while tright>0:
            vj=deque.pop(v)
            if vj<0:
                heapq.heappush(q,vj)
            tright-=1
        
        for k in range(1,N+1):
            if len(q)>0:
                minus[k][left][right]=minus[k-1][left][right]-heapq.heappop(q)
            else:
                minus[k][left][right]=minus[k-1][left][right]

leftacum=[0]*(N+1)
rightacum=[0]*(N+1)

for i in range(N):
    leftacum[i+1]=leftacum[i]+V[i]
    rightacum[i+1]=rightacum[i]+V[N-i-1]

ans=-10**10

for left in range(N+1):
    for right in range(N-left+1):
        k=min(N,K-left-right)
        if k<0:
            continue

        ans=max(ans,leftacum[left]+rightacum[right]+minus[k][left][right])

print(ans)

Solution (Python):
from collections import deque
from heapq import *
n, k = map(int, input().split())
v = list(map(int, input().split()))

ans = 0
num = min(k, n)
for a in range(num + 1):
    for b in range(num + 1 - a):
        trash = k - (a + b)
        get = v[:a] + v[-b:] if b != 0 else v[:a]
        heapify(get)
        for _ in range(min(trash, a + b)):
            temp = heappop(get)
            if temp > 0:
                heappush(get, temp)
                break
        SUM = sum(get)
        if ans < SUM:ans = SUM
print(ans)

Solution (Python):

from collections import deque, defaultdict
from itertools import combinations, combinations_with_replacement
import bisect
from math import factorial, sqrt, log, ceil, floor

def main():
    
    N, K = map(int, input().split())
    Vs = list(map(int, input().split()))

    max_value = None
    for l in range(min([K+1, N+1])):
        for r in range(min([K+1-l, N-l+1])):
            for b in range(K - l - r + 1):
                values = list(Vs[:l])
                if r > 0:
                    values += list(Vs[-r:])
                values = sorted(values)
                for i in range(b):
                    if values and values[0] < 0:
                        values.pop(0)
                    else:
                        break
                value = sum(values)
                if max_value is None or max_value < value:
                    max_value = value

    print(max_value)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque
from heapq import heappush, heappop
n, k = map(int, input().split())
v = list(map(int, input().split()))

ans = 0
for i in range(n + 1):
    for j in range(n + 1):
        rest = k - i - j
        if i + j > n or rest < 0:
            continue
        tans = 0
        q = deque(v)
        poppen = []
        for _ in range(i):
            tans += q[0]
            heappush(poppen, q.popleft())
        for _ in range(j):
            tans += q[-1]
            heappush(poppen, q.pop())
        for _ in range(rest):
            if not poppen or poppen[0] >= 0:
                break
            tans -= heappop(poppen)
        ans = max(ans, tans)
print(ans)

Solution (Python):
import bisect

abcd = []
n, k = map(int, input().split()) 
v_list = list(map(int, input().split())) 

for a in range(0, k+1):
    for b in range(0, k+1-a):
        for c in range(0, k+1-a-b):
            d = k-a-b-c
            abcd.append([a,b,c,d])


ans = 0
for curr_abcd in abcd:
    a,b,c,d = curr_abcd
    cd = c+d
    if a + b >= n:
        have = sorted(v_list)
    else:
        have = sorted(v_list[0:a] + v_list[n-b:n]) 

    minus_cnt = bisect.bisect_right(have,0)
    have = have[ min(minus_cnt, cd) : ]
    curr_sum = sum(have) 
    ans = max(ans, curr_sum)

print(ans)

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
import heapq
def main():
    n,k=map(int,input().split())
    V=list(map(int,input().split()))
    ans=0
    for a in range(min(k+1,n+1)):
        for b in range(min(k-a,n-a)+1):
            cd=k-(a+b)
            H=[]
            sum_V=0
            for i in range(n):
                v=V[i]
                if i+1<=a or i>=n-b:
                    sum_V+=v
                    if v<0:
                        heapq.heappush(H,V[i])
            while H and cd>0:
                sum_V-=heapq.heappop(H)
                cd-=1
            ans=max(sum_V,ans)
    print(ans)


if __name__=='__main__':
    main()

Solution (Python):
import sys
def input(): return sys.stdin.readline().rstrip()
import heapq
def main():
    n,k=map(int,input().split())
    V=list(map(int,input().split()))
    ans=0
    for a in range(min(k+1,n+1)):
        for b in range(min(k-a,n-a)+1):
            cd=k-(a+b)
            H=[]
            sum_V=0
            for i in range(n):
                v=V[i]
                if i+1<=a or i>=n-b:
                    sum_V+=v
                    if v<0:
                        heapq.heappush(H,V[i])
            while H and cd>0:
                sum_V-=heapq.heappop(H)
                cd-=1
            ans=max(sum_V,ans)
    print(ans)


if __name__=='__main__':
    main()

Solution (Python):
def main():
    n, k, *v = map(int, open(0).read().split())
    w = v[::-1]

    ans = 0
    m = min(n, k)
    for i in range(m + 1):
        for j in range(m + 1 - i):
            d = k - (i + j)
            g = v[:i] + w[:j]
            ps = sum(x for x in g if x > 0)
            ng = [x for x in g if x < 0]
            ng.sort()
            val = ps + sum(ng[d:])
            ans = max(ans, val)

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
n,k=map(int,input().split())
v=list(map(int,input().split()))
times=min(n,k)
ans=0
for i in range(times+1):
    l=v[:i]
    for j in range(times-i+1):
        r=l+v[n-j:]
        rest=k-i-j
        val=sum(r)
        r=sorted(r)
        if len(r)>0:
            for p in range(min(rest,len(r))):
                if r[p]<0:
                    val-=r[p]
                else:
                    break
        if val>ans:
            ans=val
print(ans)

Solution (Python):
import sys
import collections
import itertools

sys.setrecursionlimit(10 ** 8)
ni = lambda: int(sys.stdin.readline())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
ns = lambda: sys.stdin.readline().rstrip()

N, K = nm()
V = nl()
assert len(V) == N
INF = 10 ** 9


def solve():
    ans = 0
    for i in range(K + 1):
        if i > N:
            break
        for j in range(K + 1 - i):
            if i + j > N:
                break
            v = V[:i] + V[N - j :]
            v.sort()
            s = sum(v)
            k = 0
            while k < K - i - j:
                if k >= len(v):
                    break
                if v[k] >= 0:
                    break
                s -= v[k]
                k += 1
            if ans < s:
                ans = s
                
    return ans


print(solve())

Solution (Python):
import heapq  
N,K=map(int,input().split())
V=list(map(int,input().split()))
ans=0
for i in range(1,min(N,K)+1):
 for t in range(i+1):
  B=V[0:i-t]+V[N-t:N] 
  total=sum(B)
  cut=0
  for j in  range(min(i,K-i)):
   heapq.heapify(B)
   cut=min(cut,cut+heapq.heappop(B))
  ans=max(ans,total-cut)
print(ans)

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

if k<n*2:
    ans=0
    for i in range(k+1):
        for j in range(k-i+1):
            v_r=v[:i]
            v_l=v[(n-j):]
            sute_cnt=k-(i+j)
            v_new=v_r+v_l
            v_new.sort()
            
            s=sum(v_new)
            if not v_new:
                continue
            for indx in range(len(v_new)):
                if v_new[indx]<0 and sute_cnt>0:
                    s-=v_new[indx]
                    sute_cnt-=1
                else:
                    break
            ans=max(ans,s)
    print(ans)

else:
    ans=0
    for i in range(n):
        if v[i]>=0:
            ans+=v[i]
    print(ans)

Solution (Python):
from collections import deque
n,k=map(int,input().split())
v=list(map(int,input().split()))
cs_l=[0]
cs_r=[0]
cs1,cs2=0,0
for i in range(n):
  cs1+=v[i]
  cs2+=v[-i-1]
  cs_l.append(cs1)
  cs_r.append(cs2)

ans=0
for i in range(1,k+1):
  for l in range(i+1): 
    r=i-l
    if r+l>=n:
      point=cs_r[-1]
      minus=[vi for vi in v if vi<0]
      n_minus=min(n-k,len(minus))
    else:
      point=cs_l[l]+cs_r[r]
      minus=[vi for vi in v[:l] if vi<0]
      if r>0:
        minus+=[vi for vi in v[-r:] if vi<0]
      n_minus=min(k-r-l,len(minus))
    if n_minus>0:
      minus.sort()
      point-=sum(minus[:n_minus])
    ans=max(ans,point)

print(ans)

Solution (Python):
from sys import stdin, setrecursionlimit


def main():
    input = stdin.buffer.readline
    n, k = map(int, input().split())
    v = list(map(int, input().split()))

    max = 0

    for i in range(k):
        if i <= n:
            for j in range(i + 1):
                candidate = v[0:j + 1]
                candidate.extend(v[n - i + j:n])
                candidate.sort()
                for d in range(k - i):
                    tmp = sum(candidate[d:n])
                    if tmp > max:
                        max = tmp
        else:
            candidate = v
            candidate.sort()
            for d in range(k - i):
                tmp = sum(candidate[d:n])
                if tmp > max:
                    max = tmp
    print(max)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()

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

if k <= n:
    ans = - 10 ** 9
    for i in range(k + 1):
        for j in range(i + 1):
            tmp = v[:j]
            if i != j:
                tmp += v[-(i - j):]
            tmp.sort()
            ans = max(ans, sum(tmp[k - i:]))
            if k != i:
                ans = max(ans, sum(tmp[k - i - 1:]))

elif k >= 2 * n:
    v.sort(reverse = True)
    ans = 0
    for num in v:
        if num <= 0:
            break
        ans += num

else:
    ans = - 10 ** 9
    for i in range(n + 1):
        for j in range(i + 1):
            tmp = v[:j]
            if i != j:
                tmp += v[-(i - j):]
            tmp.sort(reverse = True)
            ans_tmp = 0
            for l, num in enumerate(tmp):
                if l >= 2 * i - k and num <= 0:
                    break
                ans_tmp += num
            ans = max(ans, ans_tmp)

print(ans)

Solution (Python):


import heapq

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

max_try = min(n, k)
ans = 0

for a in range(0, max_try+1):
    for b in range(0, max_try+1-a):
        c = k-(a+b)
        
        
        ans_tmp = 0
        garbage = []
        heapq.heapify(garbage)
        for i in range(a):
            num = v[i]
            ans_tmp += num

            if num < 0:
                heapq.heappush(garbage, num)

        for i in range(b):
            num = v[-1-i]
            ans_tmp += num

            if num < 0:
                heapq.heappush(garbage, num)

        
        for i in range(c):
            
            if len(garbage) == 0:
                break
            num = -heapq.heappop(garbage)
            
            ans_tmp += num
        
        
        ans = max(ans, ans_tmp)

print(ans)

Solution (Python):
n,k=map(int,input().split())
v=list(map(int,input().split()))
def get(t,s): 
    return sorted(v[:s]+v[n-(t-s):])

def f(t):
    res=0
    for i in range(t+1):
        a=get(min(t,n),min(i,n))
        tmp=sum(a)
        res=max(res,tmp)
        for j in range(min(k-t,t,n)):
            tmp-=a[j]
            res=max(res,tmp)
    return res

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

Solution (Python):

def main():
    N, K, *V = map(int, open(0).read().split())
    lim = min(N, K)
    available_choices = set()

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

            choice = V[:l] + V[-r:] if r else V[:l]
            choice.sort()
            cnt_negatives, cnt_rest = sum(i < 0 for i in choice), K - (l + r)

            if cnt_negatives > cnt_rest:
                available_choices.add(sum(choice[cnt_rest:]))
            else:
                available_choices.add(sum(choice[cnt_negatives:]))

    print(max(available_choices))


if __name__ == "__main__":
    main()

Solution (Python):

def main():
    N, K, *V = map(int, open(0).read().split())
    lim = min(N, K)
    available_choices = set()

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

            choice = V[:l] + V[-r:] if r else V[:l]
            cnt_negatives = sum(i < 0 for i in choice)
            cnt_rest = K - (l + r)

            if cnt_negatives > cnt_rest:
                choice.sort()
                available_choices.add(sum(choice[cnt_rest:]))
            else:
                available_choices.add(sum(i for i in choice if i > 0))

    print(max(available_choices))


if __name__ == "__main__":
    main()

Solution (Python):
from sys import stdin
from collections import deque
N,K = [int(x) for x in stdin.readline().rstrip().split()]
V = [int(x) for x in stdin.readline().rstrip().split()]
ans = 0
rev_V = V[::-1]

for L in range(0, K+1):
        for R in range(0, K+1):
            if L + R > min(K,N):
                break
            
            tmp = V[0:L] + rev_V[0:R]
            tmp.sort()
            tmp = deque(tmp)
            num = K - (L+R)
            stone = -1
            cnt = 0
            while stone < 0:
                if cnt >= num or len(tmp) == 0:
                    break
                stone = tmp.popleft()
                if stone >= 0:
                    tmp.append(stone)
                    break
                cnt += 1
            ans = max(ans, sum(tmp))

print(ans)

Solution (Python):
n, k = map(int, input().split())
vs = [int(i) for i in input().split()]


def getv(takel, taker, drop):
    g = vs[:takel] + vs[len(vs) - taker:] + [0] * drop
    return sum(sorted(g, reverse=True)[:takel + taker])


def getm(take):
    if take >= n:
        return getv(n, 0, k - take)
    else:
        m = 0
        for takel in range(take + 1):
            m = max([m, getv(takel, take - takel, k - take)])
        return m


def solve():
    return max([getm(take) for take in range(k + 1)])


print(solve())

Solution (Python):
import bisect

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

ans = -10**20
for l in range(min(K+1, N+1)):
    for r in range(min(K-l+1, N-l+1)):
        P = V[:l]+V[N-r:]
        P.sort()
        z = bisect.bisect_left(P, 0)

        if z <= K-l-r:
            ans = max(ans, sum(P[z:]))
        else:
            ans = max(ans, sum(P[K-l-r:]))

print(ans)

Solution (Python):
N,K = map(int,input().split())
V = list(map(int,input().split()))
max_cnt = -float('inf')
for k in range(min(N,K)+1):
  for i in range(k+1):
    j = k - i
    le = V[:i]
    ri = V[-j:] if j > 0 else []
    ne = [i for i in le if i < 0]+[j for j in ri if j < 0]
    ne.sort()
    max_cnt = max(sum(le)+sum(ri) - sum(ne[:K-k]),max_cnt)
    
print(max_cnt)

Solution (Python):
from collections import deque
import heapq
N,K=map(int,input().split())
V=list(map(int,input().split()))
ans=0
  
for i in range(0,min(N,K)+1):
  for a in range(i+1):
    b=i-a
    
    l=0
    d=deque(V)
    minus=[]
    
    for j in range(a):
      p=d.popleft()
      l+=p
      if p<0:
        minus.append(p)
    for j in range(b):
      p=d.pop()
      l+=p
      if p<0:
        minus.append(p)
    heapq.heapify(minus)
    for j in range(min(len(minus),K-i)):
      k=heapq.heappop(minus)
      l-=k
    
    ans=max(ans,l)
    
print(ans)

Solution (Python):
n,k = map(int,input().split())
v = [0]+[int(i) for i in input().split()]+[0]
ans = 0
cnt1 = 0
m1 = []
for i in range(n+1):
    cnt1 += v[i]
    if v[i] < 0: m1.append(v[i])
    cnt2 = cnt1
    m2 = m1[:]
    for j in range(n-i+1):
        if i + j > k: break
        r = k - i - j
        cnt2 += v[n+1-j]
        if v[n+1-j] < 0: m2.append(v[n+1-j])
        m2.sort()
        s = cnt2
        s -= sum(m2[:r])
        ans = max(s,ans)
print(ans)

Solution (Python):
n,k = map(int,input().split())
v = [0]+[int(i) for i in input().split()]+[0]
ans = 0
cnt1 = 0
m1 = []
for i in range(n+1):
    cnt1 += v[i]
    if v[i] < 0: m1.append(v[i])
    cnt2 = cnt1
    m2 = m1[:]
    for j in range(n-i+1):
        if i + j > k: break
        r = k - i - j
        cnt2 += v[n+1-j]
        tmp = cnt2
        if v[n+1-j] < 0: m2.append(v[n+1-j])
        m2.sort()
        tmp -= sum(m2[:r])
        ans = max(tmp,ans)
print(ans)

Solution (Python):
N,K = map(int,input().split())
V = list(map(int,input().split()))
Vrev = V[::-1]
ans = 0
for a in range(N+1):
    for b in range(min(K-a,N-a)+1):
        VV = V[:a] + Vrev[:b]
        VV.sort(reverse=True)
        for k in range(min(K-a-b,len(VV))):
            if VV[-1] < 0:
                VV.pop()
        ans = max(sum(VV),ans)
print(ans)

Solution (Python):
from sys import stdin
import heapq
def main():
    
    readline=stdin.readline
    n,k=map(int,readline().split())
    v=list(map(int,readline().split()))
    v_rev=list(reversed(v))

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

            minus=[]
            s=0
            for i in range(l):
                x=v[i]
                if x<0:
                    minus.append(x)
                s+=x

            for i in range(r):
                x=v_rev[i]
                if x<0:
                    minus.append(x)
                s+=x

            if l+r<k:
                minus.sort()
                for i in range(k-l-r):
                    if i>=len(minus):
                        break
                    s-=minus[i]

            ans=max(ans,s)

    print(ans)

if __name__=="__main__":
    main()

Solution (Python):
N,K = map(int,input().split())
V = list(map(int,input().split()))
vmax = -10**9
for n1 in range(K+1):
    A = V[:n1]
    ind = min(n1,N)
    m = N-ind
    for n2 in range(min(K-n1,m)+1):
        B = V[N-n2:]
        C = A+B
        C = sorted(C,reverse=True)
        k = K-n1-n2
        for i in range(k):
            if len(C)==0 or C[-1]>=0:break
            C.pop()
        vmax = max(vmax,sum(C))
print(vmax)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
MOD = 10 ** 9 + 7

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

ans = 0

for l in range(min(N, K) + 1): 
    left = N - l 
    for r in range(min(left, K - l) + 1): 
        k = K - l - r 
        tmp = V[:l] + V[N - r:]
        tmp.sort(reverse = False)
        t = sum(tmp)
        for i in range(min(k, l + r)): 
            if tmp[i] < 0:
                t -= tmp[i]
            else:
                break
        
        ans = max(ans, t)

print (ans)

Solution (Python):
from bisect import bisect_left as bil
N, K, *V = map(int, open(0).read().split())


k = min(N, K)
ans = 0
for i in range(k+1):
    for j in range(k-i+1):
        U = V[:i]+V[-j:] if j != 0 else V[:i]
        U.sort()
        z = min(bil(U, 0), K-i-j)
        sm = sum(U[z:])

        if ans < sm:
            ans = sm

print(ans)

Solution (Python):
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):
        if left + right > N or left + right > K:
            continue
        L = V[:left]
        R = V[::-1][:right]

        A = L + R
        A.sort(reverse=True)
        k = K - (left + right)
        while k > 0 and A and A[-1] < 0:
            A.pop()
            k -= 1
        ans = max(ans, sum(A))
print(ans)

Solution (Python):
n,k=map(int,input().split())
a=list(map(int,input().split()))
ans=0
min_nk=min(n,k)
for i in range(min_nk+1):
    for j in range(n-min_nk+i,n+1):
        count=a[0:i]+a[j:n]
        count.sort()
       
        ans=max(ans,sum(count))
        for x in range(1,k-len(count)+1):
           
            ans=max(ans,sum(count[x:]))
print(ans)

Solution (Python):
n,k = map(int,input().split())
v = list(map(int,input().split()))
ans = 0
for i in range(min(n,k)+1):
    for j in range(min(n,k)+1-i):
        x = sorted(v[:i]+v[n-j:])
        for l in range(k-i-j+1):
            ans = max(ans,sum(x[l:]))
print(ans)

Solution (Python):
n, k = list(map(int, input().split()))
v = list(map(int, input().split()))
l = 0
r = 0
a = 0
m = min(n, k)+1
have = []
total = 0
ans = 0
for r in range(m):
    for l in range(m-r):
        have = []
        have.append(v[:r])
        if l != 0:
            have.append(v[-l:])
        else:
            have.append([])
        a = k-l-r
        data = have[0]+have[1]
        data.sort()
        for i in range(len(data)):
            if i < a and data[i] < 0:
                data[i] = 0
        total = sum(data)
        if total > ans:
            ans = total
print(ans)

Solution (Python):
import heapq
def main():
    n, k = map(int, input().split())
    v = list(map(int, input().split()))
    ans = 0
    for L_i in range(min(k+1, n)):
        R_i_max = k - L_i+1
        if n < L_i + R_i_max:
            R_i_max = n - L_i + 1
        for R_i in range(R_i_max):
            pop_i = k - L_i - R_i
            q = []
            heapq.heapify(q)
            tmp = 0
            for L_j in range(L_i):
                tmp += v[L_j]
                heapq.heappush(q, v[L_j])
            for R_j in range(n-R_i, n):
                tmp += v[R_j]
                heapq.heappush(q, v[R_j])
            for _ in range(pop_i):
                if len(q) == 0:
                    break
                x = heapq.heappop(q)
                if 0 < x:
                    break
                tmp -= x
            if ans < tmp:
                ans = tmp
    print(ans)

if __name__ == "__main__":
    main()

Solution (Python):
import heapq
n,k = map(int, input().split())
v = list(map(int, input().split()))
ans = 0
for left in range(n+1):
    for right in range(left, n+1):
        have = []
        heapq.heapify(have)
        i = 0
        while i < left:
            heapq.heappush(have, v[i])
            i += 1
        cost = left + (n - right)
        if cost > k:
            continue
        i = n-1
        while i >= right:
            heapq.heappush(have, v[i])
            i -= 1
        while k - cost > 0 and len(have) > 0:
            value = heapq.heappop(have)
            if  value >= 0:
                heapq.heappush(have,value)
                break
            cost += 1
        
        ans = max(ans, sum(have))
print(ans)

Solution (Python):
import sys
input=sys.stdin.readline
def N(): return int(input())
def NM():return map(int,input().split())
def L():return list(NM())
def LN(n):return [N() for i in range(n)]
def LL(n):return [L() for i in range(n)]
n,k=NM()
l=L()
ans=0
for a in range(n+1):
    for b in range(n-a+1):
        for c in range(a+1):
            for d in range(b+1):
                if a+b+c+d>k:
                    continue
                x=l[:a]
                y=l[-1:-b-1:-1]
                x.sort()
                y.sort()
                ans=max(ans,sum(x[c:])+sum(y[d:]))
print(ans)

Solution (Python):
n, k = map(int, input().split())
a = list(map(int, input().split()))
b = a[::-1]
ans = 0
for i in range(1,min(n+1, k+1)):
    for j in range(i+1):
        m = i - j
        l = a[:m] + b[:j]
        ml = sorted([i for i in l if i < 0])
        pl = [i for i in l if i >= 0]
        ml = ml[min(k-i, len(ml)):]
        if ml:
            minus = sum(ml)
        else:
            minus = 0
        if pl:
            plus = sum(pl)
        else:
            plus = 0
        ans = max(ans, minus + plus)        
print(ans)

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

result=0

for a in range(min(n,k)+1):
    for b in range(min(n,k)+1-a):
        tmp=v[:a]+v[n-b:]
        tmp.sort()

        lost=k-a-b

        ttt=[]
        cnt=0
        for item in tmp:
            if item<0 and cnt<lost:
                ttt.append(item)
                cnt+=1
            else:
                break
        result=max(result,sum(tmp)-sum(ttt))

print(result)

Solution (Python):
n,k = map(int,input().split())
from collections import deque
au = list(map(int,input().split()))
left = [0 for i in range(n)]
right = [0 for i in range(n)]
left[0] = au[0]
right[0] = au[-1]
ans = 0
for i in range(n-1):
    left[i+1] = left[i] + au[i+1]
    right[i+1] = right[i] + au[n-i-2]
p = min(n,k)
for a in range(p+1):
    for b in range(p+1-a):
        d = deque(au)
        x = []
        y = 0
        for i in range(a):
            u = d.popleft()
            x.append(u)
            y += u
        for j in range(b):
            v = d.pop()
            x.append(v)
            y += v
        x.sort(reverse = True)
        for ue in range(k-a-b):
            if len(x) > 0 and x[-1] < 0:
                y -= x.pop()
            else:
                break
        ans = max(ans,y)
print(ans)

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

c = min(n, k)
ans = 0
for a in range(c + 1):
    for b in range(c + 1 - a):
        w = v[:a] + v[n-b:]  
        w.sort()
        for i in range(k - (a + b)):
            if len(w) > 0 and w[0] < 0:
                del w[0]
            else:
                break
        ans = max(ans, sum(w))
print(ans)