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()))

R = min(N,K)
point = 0
max_point = 0
hand = []

for i in range(R+1):
    for a in range(i+1):
        if i==a:
            hand = V[:a]
        else:
            hand = V[:a]+V[-(i-a):]
        negahand = [k for k in hand if k < 0]
        negahand.sort()
        if K-i > len(negahand):
            trash = -sum(negahand)
        else:
            trash = -sum(negahand[:K-i])
        point = sum(hand) + trash
        max_point = max(max_point,point)

print(max_point)

Solution (Python):
[n,k],li = [list(map(int,i.split())) for i in open(0)] 
ans = 0
for i in range(1,min(n+1,k+1)):
    for j in range(i+1):
        l = li[:(i-j)] + li[n-j:]
        re_num = min(k-i,len(l))
        l.sort()
        f = 0
        for p in range(re_num):
            if l[p] > 0:
                break
            f += 1
        l = l[f:]
        ans = max(ans,sum(l))
print(ans)

Solution (Python):

from collections import deque

N,K=map(int,input().split())
V=list(map(int,input().split()))
V=V[:]+V[:]
def check(k):
    res=0
    
    
    for i in range(k+1):
        if k<N:
            v=V[N-k+i:N-k+i+k]
        else:
            v=V[:N]
        v.sort(reverse=True)
        for i in range(K-k):
            if not v:
                break
            if v[-1]<0:
                v.pop()
            else:
                break
        res=max(res,sum(v))
        if not k<N:
            break
    return res

res=0
for k in range(min(N,K)+1):
    res=max(res,check(k))
print(res)

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

r=min(n,k)
ans=0

for a in range(r+1):
    for b in range(r+1):
        jew=[]
        if a+b>r:
            break
        for i in range(a):
            jew.append(v[i])
        for i in range(n-1,n-b-1,-1):
            jew.append(v[i])
        
        if len(jew)==0:
            continue

        jew.sort(reverse=True)

        for i in range(k-(a+b)):
            if len(jew)>0:
                if jew[-1]<0:
                    jew.pop(-1)
                else:
                    break
        ans=max(ans,sum(jew))
print(ans)

Solution (Python):


import sys 
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
N, K = map(int, readline().split())
V = list(map(int,readline().split()))

ans = 0
for l in range(min(K,N)+1):
    for r in range(min(K,N)-l+1):
        res = K - (l+r)
        minus = [] 
        total = sum(V[:l])+sum(V[N-r:])
        for i in range(l):
            if V[i] < 0:
                minus.append(V[i])        
        for i in range(r):
            if V[-i-1] < 0:
                minus.append(V[-i-1])
        minus.sort()
        S = len(minus)
        rep = sum(minus[:min(S+1,res)])
        total -= rep
        
        ans = max(total,ans)
print(ans)

Solution (Python):



from collections import deque


INT = lambda: int(input())
INTM = lambda: map(int,input().split())
STRM = lambda: map(str,input().split())
STR = lambda: str(input())
LIST = lambda: list(map(int,input().split()))
LISTS = lambda: list(map(str,input().split()))

def do():
    n,k=INTM()
    d=LIST()
    
    nkmin=min(n,k)
    stop=10**9
    ans=0
    for i1 in range(1,1+nkmin):
        for i2 in range(i1+1):
            temp=d[0:i2]+d[n-(i1-i2):]
            
            temp=sorted(temp)
            
            for i3 in range(i1):
                if temp[i3]>=0 or i3>=k-i1:
                    stop=i3
                    break
                stop=10**9
            if stop!=10**9:
                
                ans=max(sum(temp[i3:]),ans)
    
    print(ans)



if __name__=='__main__':
    do()

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, tan, asin, acos, atan, radians, degrees, log2, gcd
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from heapq import heappush, heappop
from functools import reduce, lru_cache
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def TUPLE(): return tuple(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7 





N, K = MAP()
V = LIST()

ans = 0
tmp_sum = 0

for i in range(1, min(K, N)+1):
	left = K-i
	for j in range(i+1):
		tmp = V[:j] + V[N-(i-j):]
		tmp.sort()
		idx = bisect_left(tmp, 0)
		ans = max(ans, sum(tmp) - sum(tmp[:min(idx, left)]))

print(ans)

Solution (Python):
def main():
    n, k = map(int, input().split())
    v = list(map(int, input().split()))
    reversed_v = [0] + v[::-1]
    v = [0] + v
    ans = -float("inf")
    for i in range(n + 1):
        for j in range(n + 1):
            if i + j > min(n, k):
                continue
            now_k = k - i - j
            if now_k < 0:
                continue
            q = []
            for l in range(1, i + 1):
                q.append(v[l])
            for l in range(1, j + 1):
                q.append(reversed_v[l])
            q.sort()
            for l in range(len(q)):
                if q[l] < 0 and now_k > 0:
                    q[l] = 0
                    now_k -= 1
            ans = max(ans, sum(q))
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from collections import deque

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

w = deque(v)
ans = 0
for i in range(k+1):
    if i > n:
        break
    s = v[:i]
    for j in range(k+1-i):
        if j > n-i:
            break
        t = sorted(v[n-j:] + s, reverse=True)
        u = sum(t)
        for l in range(k+1-i-j):
            ans = max(ans, u)
            if not t:
                break
            u -= t.pop()
print(ans)

Solution (Python):
import sys

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

k=k0
if n<=k:
    v.sort()
    vsum=sum(v)
    kk=k-n
    vsum=0
    for i in range(n):
        if i>kk:
            vsum+=v[i]
        elif i<=kk and v[i]>0:
            vsum+=v[i]
    print(max(0,vsum))
    sys.exit()

vmax=-10**10

kk=k0
for k in range(kk):
    k2=kk-k-1
    
    vv=[[0]*(k+1) for i in range(k+1+1)]
    for i in range(k+1):
        vv[0][i]=v[n-k+i-1]
    vsum=sum(vv[0])
    vmax=max(vsum,vmax)


    vvv=vv[0].copy()
    vvv.sort()
    for i in range(min(k2,k+1)):
        if vvv[i]<0:
            vsum-=vvv[i]
    vmax=max(vsum,vmax)


    for i in range(k+1):
        vv[i+1]=vv[i].copy()
        vv[i+1].pop(0)
        vv[i+1].append(v[i])
        vsum=sum(vv[i+1])
        vmax=max(vsum,vmax)


        vvv=vv[i+1].copy()
        vvv.sort()
        for i in range(min(k2,k+1)):
            if vvv[i]<0:
                vsum-=vvv[i]
        vmax=max(vsum,vmax)

    
print(max(0,vmax))

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) - i + 1):
        lis = sorted(v[:i] + v[n - j:])
        for l in range(k - i - j + 1):
            ans = max(ans, sum(lis[l:]))
print(ans)

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

for i in range(M+1):
  for j in range(M-i+1):
    G = V[:i]+V[N-j:]
    G.sort()
    for j in range(K-i-j+1):
      ans = max(ans,sum(G[j:]))

print(ans)

Solution (Python):
n,k = map(int,input().split())
v = list(map(int,input().split()))
ans = 0
for i in range(min(n+1,k+1)):
    for j in range(min(n+1-i,k+1-i)):
        g = []
        x = 0
        while x < i:
            g.append(v[x])
            x += 1
        y = 0
        z = -1
        while y < j:
            g.append(v[z])
            y += 1
            z -= 1
        g.sort()
        x = k - i - j
        for z in range(min(len(g),x)):
            if g[z] < 0:
                g[z] = 0
            else:
                break
        ans = max(ans,sum(g))
print(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):
        lis = []
        for k in range(i):
            lis.append(V[j+k])
            
        lis.sort(reverse=True)
        cnt = 0
        while (len(lis) != 0 and i+cnt < K and lis[-1] < 0):
            del lis[-1]
            cnt += 1
        ans = max(ans,sum(lis))
print(ans)

Solution (Python):


from collections import deque
import heapq


def main():
    N, K = map(int, input().split())
    V = list(map(int, input().split()))
    res = 0
    for pull in range(K+1):
        push = K - pull
        if push < 0 or push > N:
            continue
        for left_push in range(push+1):
            tmp_que = deque(V)
            tmp_heap = []
            heapq.heapify(tmp_heap)
            right_push = push - left_push
            for _ in range(left_push):
                heapq.heappush(tmp_heap, tmp_que.popleft())
            for _ in range(right_push):
                heapq.heappush(tmp_heap, tmp_que.pop())

            tmp_res = sum(tmp_que)
            for i in range(min(push, pull)):
                x = heapq.heappop(tmp_heap)
                if x >= 0:
                    heapq.heappush(tmp_heap,x)
                    break


            res = max(res, sum(tmp_heap))
    print(res)
    return


if __name__ == "__main__":
    main()

Solution (Python):
N,M=list(map(int,input().split()))
l=list(map(int,input().split()))
S=min(M,N)
ans=0
for i in range(S+1):
   for j in range(S-i+1):
      mem=l[0:i]+l[N-j:]
      for k in range(min((M-i-j),len(mem))):
         if min(mem)<0:
            mem.remove(min(mem))
      ans=max(ans,sum(mem))
print(ans)

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

ans = V[0]
for l in range(min(K, N)+1):
    for r in range(min(K, N)-l+1):
        v = V[:l] + V[N-r:]
        rest = K - (l + r)
        v.sort()
        tmp = 0
        for x in v:
            if rest and x < 0:
                rest -= 1
            else:
                tmp += x
        if ans < tmp:
            ans = tmp
print(ans)

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

y = min(n, k)

ans = 0

for i in range(y):
    cnt = 0
    heap = []
    for j in range(i+1):
        if v[j] < 0:
            heap.append(v[j])
            ans = max(ans, cnt)
        cnt += v[j]
        ans = max(ans, cnt)

    rest = k - i - 1
    right = n - i - 1
    for r in range(min(rest, right)+1):
        index = -1
        cnt2 = cnt
        heap2 = heap[:]
        for _ in range(r):
            cnt2 += v[index]
            if v[index] < 0:
                heap2.append(v[index])
            index -= 1

        ans = max(ans, cnt2)
        rest2 = rest - r
        heap2.sort()
        for o in range(rest2):
            if o >= len(heap2):
                break
            cnt2 -= heap2[o]
            ans = max(ans, cnt2)

print(ans)

Solution (Python):
N, K = map(int, input().split())
V = list(map(int, input().split()))
result = 0
for a in range(N + 1):
    for b in range(N + 1):
        if a + b > K or a + b > N:
            break
        out = V[:a] + V[N-b:]
        out.sort()
        sum_val = sum(out)
        for i in range(min(a + b, K - a - b)):
            if out[i] >= 0:
                break
            sum_val -= out[i]
        result = max(result, sum_val);
print(result)

Solution (Python):
import heapq

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

ans = 0
for i in range(min(n, k)+1):  
    for a in range(i+1):  
        b = i - a  
        L = V[:a] + V[n-b:]
        heapq.heapify(L)
        total = sum(L)
        for j in range(k-i):
            if not L:
                break
            if L[0] >= 0:
                break
            total -= heapq.heappop(L)
        ans = max(ans, total)

print(ans)

Solution (Python):
n, k = map(int, input().split())
v = list(map(int, input().split()))
vv = [0] * (n + 1)
for i in range(n):
    vv[i + 1] = vv[i] + v[i]
sumv = vv[n]
ans = 0
for l in range(n):
    for r in range(l - 1, n):
        c = r - l + 1
        if n - c > k:
            continue
        x = []
        value = sumv - (vv[r + 1] - vv[l])
        for i in range(n):
            if not l <= i <= r:
                x.append(v[i])
        x.sort()
        for i in range(max(0, min(k - (n - c), len(x)))):
            if x[i] < 0:
                value -= x[i]
            else:
                break
        ans = max(value, ans)
print(ans)

Solution (Python):
import heapq, copy
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = -10 ** 10
for a in range(min(n, k) + 1):
    for b in range(min(n, k) - a + 1):
        have = []
        
        for i in range(a):
            have.append(v[i])
        for i in range(b):
            have.append(v[n-i-1])

        heapq.heapify(have)
        
        for c in range(k - a - b):
            if len(have) <= 0:
                break
            item = heapq.heappop(have)
            if item >= 0:
                have.append(item)
                break
        ans = max(ans, sum(have))

print(ans)

Solution (Python):
import heapq, copy
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = -10 ** 10
for a in range(min(n, k) + 1):
    have = []
    for i in range(a):
        have.append(v[i])

    o_have = copy.deepcopy(have)

    for b in range(min(n, k) - a + 1):
        for i in range(b):
            have.append(v[n-i-1])

        heapq.heapify(have)
        
        for c in range(k - a - b):
            if len(have) <= 0:
                break
            item = heapq.heappop(have)
            
            if item >= 0:
                have.append(item)
                break
        ans = max(ans, sum(have))
        
        have = copy.deepcopy(o_have)

print(ans)

Solution (Python):
import heapq, copy
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = -10 ** 10
for a in range(min(n, k) + 1):
    for b in range(min(n, k) - a + 1):
        have = []
        for i in range(a):
            have.append(v[i])
        for i in range(b):
            have.append(v[-i-1])

        tmp = sum(have)
        
        have.sort()
        ans = max(ans, tmp)

        for c in range(k - a - b):
            if c >= len(have):
                break
            item = have[c]
            if item >= 0:
                break

            tmp -= item
            ans = max(ans, tmp)

print(ans)

Solution (Python):
import heapq, copy
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = -10 ** 10
for a in range(k + 1):
    for b in range(k - a + 1):
        if a + b > min(k, n):
            break
        
        have = v[:a] + v[n-b:]
        

        tmp = sum(have)
        ans = max(ans, tmp)

        have.sort()

        c = k - a - b

        for item in have:
            if c <= 0:
                break
            c -= 1
            if item >= 0:
                break

            tmp -= item
            ans = max(ans, tmp)

print(ans)

Solution (Python):
import heapq, copy
n, k = map(int, input().split())
v = list(map(int, input().split()))
ans = -10 ** 10
for a in range(k + 1):
    for b in range(k - a + 1):
        if a + b > min(k, n):
            break
        
        have = v[:a] + v[n-b:]
        

        tmp = sum(have)
        ans = max(ans, tmp)

        have.sort()

        c = k - a - b

        for item in have:
            if c <= 0:
                break
            c -= 1
            if item >= 0:
                break

            tmp -= item
        ans = max(ans, tmp)

print(ans)

Solution (Python):
import bisect

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

ans = 0
juwel = []
for l in range(M + 1):
    for r in range(M - l + 1):
        juwel = sorted(V[:l] + V[N - r:])
        s = bisect.bisect_left(juwel, 0)
        s = min(s, K - l - r)
        juwel = juwel[s:]
        ans = max(ans, sum(juwel))

print(ans)

Solution (Python):




from collections import deque
import heapq






def solve(N, K, Vi):
    ans = 0
    for ab in range(min(N, K) + 1):
        for a in range(ab + 1):
            b = ab - a
            have = Vi[:a] + Vi[N - b:]
            have.sort()
            s = sum(have)
            for i in range(K - ab):
                if len(have) <= i:
                    break
                elif have[i] < 0:
                    s += abs(have[i])
                else:
                    break
            ans = max(ans, s)
    print(ans)


if __name__ == '__main__':
    N, K = map(int, input().split())
    Vi = [int(i) for i in input().split()]
    solve(N, K, Vi)

Solution (Python):
import math,itertools,fractions,heapq,collections,bisect,sys,queue,copy

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7
dd=[(-1,0),(0,1),(1,0),(0,-1)]
ddn=[(-1,0),(-1,1),(0,1),(1,1),(1,0),(1,-1),(0,-1),(-1,-1)]

def LI(): return [int(x) for x in sys.stdin.readline().split()]

def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def LS(): return sys.stdin.readline().split()
def S(): return input()

def main():
  n,k=LI()
  v=LI()

  ans=0
  for i in range(k):
    if i+1<n:
      for l in range(i+1):
        r=i-l
  
        _ans=0
        _v=[]
        for x in range(l+1):
          _ans+=v[x]
          _v.append(v[x])
        for x in range(r):
          _ans+=v[n-x-1]
          _v.append(v[n-x-1])

        _v.sort()
        for j in range(min(len(_v),k-i-1)):
          if _v[j]<0:
            _ans-=_v[j]
          else:
            break
        
        ans=max(ans,_ans)
    else:
      _v=v.sort()
      _ans=sum(v)
      _k=n-k-1
      for j in range(_k):
        if _v[j]<0:
          _ans-=_v[j]
        else:
          break
      ans=max(ans,_ans)

  return ans


print(main())

Solution (Python):
'''
研究室PCでの解答
'''
import math

import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit


ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7 
dir = [(-1,0),(1,0),(0,-1),(0,1)]
alp = "abcdefghijklmnopqrstuvwxyz"

def main():
    n,k = map(int,ipt().split())
    v = [int(i) for i in ipt().split()]
    rv = v[::-1]
    ma = 0
    for ai in range(n+1):
        for bi in range(n-ai+1):
            d = k-ai-bi
            if d < 0:
                continue
            vs = v[:ai:]+rv[:bi:]
            vs.sort(reverse=True)
            for i in range(min(ai+bi,d)):
                if vs[-1] >= 0:
                    break
                else:
                    vs.pop()
            nm = sum(vs)
            if ma < nm:
                ma = nm

    print(ma)



    return None

if __name__ == '__main__':
    main()

Solution (Python):
N, K = map(int, input().split())
V = list(map(int, input().split()))
Ans = -float('inf')
for a in range(K+1):
    for b in range(K+1):
        if a + b > min(K, N):
            break
        c = K - a - b
        A = V[:a] + V[N - b:]
        A.sort()
        ans = sum(A)
        for x in A:
            if c == 0:
                break
            if x >= 0:
                break
            if x < 0:
                c -= 1
                ans -= x
        Ans = max(ans, Ans)
print(Ans)

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

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

t = min(n, k)
score = 0
for a in range(t+1):
    for b in range(t-a+1):
        tmp_d = d.copy()
        jems = []
        for i in range(a):
            jems.append(tmp_d.pop())
        for i in range(b):
            jems.append(tmp_d.popleft())
        jems.sort()
        for i in range(min(len(jems), k-b-a)):
            if jems[i] < 0:
                jems[i] = 0
            else:
                break
        tmp_score = sum(jems)
        if tmp_score > score:
            score = tmp_score
print(score)

Solution (Python):
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy


import heapq
sys.setrecursionlimit(10**6)

INF = 10**18
import bisect
import statistics
mod = 10**9+7


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

m = 0
for i in range(1, K+1):
    if i <= N:
        for j in range(1,i+1):
            VV = deque(copy.deepcopy(V))
            a = []
            heapq.heapify(a)
            for k in range(j):
                v = VV.popleft()
                heapq.heappush(a,v)
            for k in range(i-j):
                v = VV.pop()
                heapq.heappush(a, v)
            ans = sum(a)
            for k in range(K-i):
                if len(a) > 0:
                    v = heapq.heappop(a)
                    if v < 0:
                        ans -= v
            m = max(m,ans)
    else:
        ans = sum(V)
        a = copy.deepcopy(V)
        heapq.heapify(a)
        for k in range(K-N):
            if len(a) > 0:
                v = heapq.heappop(a)
                if v < 0:
                    ans -= v
        m = max(m, ans)
        break

print(m)

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

a=list(map(int,input().split()))
a_reverse=list(reversed(a))
b=sorted(a)
s=sum(a)

ans=[]

for i in range(k+1):
  if i>n:
    I=i-n
    for j in range(I): 
      if j<n:
        ans1=s-b[j]
    ans.append(ans1)
  elif i==n:
    ans.append(s)
  elif i==0:
    ans.append(0)
  else:
    for h in range(i+1):
      H=i-h
      c=[]
      for x in range(h):
        c.append(a[x])
      for y in range(H):
        c.append(a_reverse[y])
        
      c.sort()
      S=sum(c)
      for g in range(1,k-i+1):
        if g<=i:
          if c[g-1]<0:
            S-=c[g-1]

      ans.append(S)
      
print(max(ans))

Solution (Python):




n, k = map(int, input().strip().split())
value = list(map(int, input().strip().split()))
res = 0
for l in range(k+1):
    for r in range(k-l+1):
        if l + r > n:
            continue
        d = k - l - r
        now = 0
        s = []
        for i in range(l):
            now += value[i]
            s.append(value[i])
        for i in range(r):
            now += value[n-i-1]
            s.append(value[n-i-1])
        s.sort()
        for i in range(d):
            if i >= len(s):
                break
            if s[i] > 0:
                break
            now -= s[i]
        res = max(res, now)

print(res)

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




def RD(): return input().rstrip().decode()
def II(): return int(input())
def FI(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))



def main():
	n,k=MI()
	V=LI()

	ans=0

	for a in range(min(n+1,k+1)):
		for b in range(min(n-a+1,k-a+1)):
			S=[]
			S+=V[:a]
			S+=V[n-b:]
			S.sort()
			tmp=sum(S)
			cnt=max(0,k-a-b)
			for i in range(min(a+b,cnt)):

				if S[i]<0:
					tmp-=S[i]
				else:
					break
			ans=max(ans,tmp)
	print(ans)










if __name__ == "__main__":
	main()

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




def RD(): return input().rstrip().decode()
def II(): return int(input())
def FI(): return int(input())
def MI(): return map(int,input().split())
def MF(): return map(float,input().split())
def LI(): return list(map(int,input().split()))
def LF(): return list(map(float,input().split()))
def TI(): return tuple(map(int,input().split()))



def main():
	n,k=MI()
	V=LI()

	ans=0

	for a in range(min(n+1,k+1)):
		for b in range(min(n-a+1,k-a+1)):
			S=[]
			S+=V[:a]
			S+=V[n-b:]
			S.sort()
			tmp=sum(S)
			cnt=max(0,k-a-b)
			for i in range(min(a+b,cnt)):

				if S[i]<0:
					tmp-=S[i]
				else:
					break
			ans=max(ans,tmp)
	print(ans)










if __name__ == "__main__":
	main()

Solution (Python):



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

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

Solution (Python):



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

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

Solution (Python):
N,K=map(int,input().split())
V=list(map(int,input().split()))
r,ans=min(N,K),0
for a in range(r+1):
  for b in range(r-a+1):
    tmp,minus=0,[]
    for i in range(a):
      tmp+=V[i]
      if V[i]<0:
        minus.append(V[i])
    for j in range(b):
      tmp+=V[N-j-1]
      if V[N-j-1]<0:
        minus.append(V[N-j-1])
    tmp-=sum(sorted(minus)[:K-(a+b)])
    ans=max(ans,tmp)
print(ans)

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

ans = 0

for a in range(K + 1):
    for b in range(K - a + 1):

        
        if a + b > N:
            continue

        
        get = []
        get += V[:a]
        get += list(reversed(V))[:b]
        
        tmp = sum(get)

        
        get.sort()
        for i in range(min(K - a - b, len(get))):
            if get[i] < 0:
                tmp += -get[i]
        ans = max(ans, tmp)

print(ans)

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

ans = 0

for a in range(K + 1):
    for b in range(K - a + 1):
        if a + b > N:
            continue
        
        get = []
        get += V[:a]
        get += list(reversed(V))[:b]
        
        tmp = sum(get)

        
        get.sort()
        for i in range(min(K - a - b, len(get))):
            if get[i] < 0:
                tmp += -get[i]
        ans = max(ans, tmp)

print(ans)

Solution (Python):
N, K, *V = map(int, open(0).read().split())




ans = 0
for i in range(K + 1):
    for j in range(min(N + 1, i + 1)):
        for k in range(j + 1):
            right = N - j + k
            tmp = V[:k] + V[right:]
            tmp.sort()
            ans = max(ans, sum(tmp) - sum([t for t in tmp if t < 0][: i - j]))
print(ans)

Solution (Python):
from heapq import heappop, heapify


N, K, *V = map(int, open(0).read().split())




ans = 0
for i in range(K + 1):
    for j in range(min(N + 1, i + 1)):
        for k in range(j + 1):
            right = N - j + k
            tmp = V[:k] + V[right:]

            put_out_cnt = i - j
            heapify(tmp)
            while tmp and put_out_cnt > 0:
                heappop(tmp)
                put_out_cnt -= 1
            ans = max(ans, sum(tmp))
print(ans)

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

max_v = 0
have_minus = []
for left in range(N):
  for right in range(left,N + 1)[::-1]:
    if len(V[:left]) + len(V[right:]) > K: 
      break
    
    sum_ = sum(V[:left]) + sum(V[right:])
    have_minus = [num for num in V[:left]+V[right:] if num < 0]
    have_minus.sort()
    
    d = max(0,K - (len(V[:left]) + len(V[right:])))
    for i in range(min(d, len(have_minus))):
        sum_ -= have_minus[i]   
    
    
    max_v = max(max_v ,sum_)
print(max_v)

Solution (Python):
N,K=map(int,input().split())
V=list(map(int,input().split()))
r=min(N,K)
ans=0
for a in range(r+1):
    for b in range(r-a+1):
        tmp,minus=0,[]
        for i in range(a):
            t=V[i]
            tmp+=t
            if t<0:
                minus.append(t)
        for j in range(b):
            t=V[N-1-j]
            tmp+=t
            if t<0:
                minus.append(t)
        tmp-=sum(sorted(minus)[:K-(a+b)])
        ans=max(ans,tmp)
print(ans)