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 = map(int, input().split())
V = list(map(int, input().split()))

ans = float('-inf')
for l in range(min(N+1, K+1)):
  for r in range(min(N-l+1, K-l+1)):
    arr = V[:l] + V[N-r:]
    t = sum(arr)
    arr.sort()
    for i in range(min(l+r, K-l-r)):
      a = arr[i]
      if a >= 0:
        break
      t -= a
    ans = ans if ans > t else t
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): 
    tu = k-i 
    g = i - (k-i) 
    for j in range(i+1): 
        A = []
        r = i-j
        for L in range(j):
            A.append(V[L])
        for R in range(r):
            A.append(V[-(R+1)])
        A=sorted(A,reverse=1)
        B=[]
        B.append(A[0])
        for kk in range(1,len(A)):
            B.append(B[kk-1]+A[kk])
        pt=0
        
        
        if max(0,g-1) < min((i),len(B)) :
            pt = max(B[max(0,g-1):min((i),len(B))])
        
        
        ans = max(ans,pt)
print(ans)

Solution (Python):
import sys
range = xrange
input = raw_input

n,k = [int(x) for x in input().split()]
V = [int(x) for x in input().split()]

bestsumma = -10**18

for pick in range(min(k+1,n+1)):
    for leftpick in range(pick+1):
        rightpick = pick - leftpick
       
        picks = V[:leftpick]
        if rightpick:
            picks += V[-rightpick:]
        picks.sort(reverse=True)
        
        cumsum = [0]
        for p in picks:
            cumsum.append(cumsum[-1] + p)

        for put_back in range(0, k - pick+1):
            left = pick - put_back
            
            if len(cumsum)>left>=0:
                score = cumsum[left]

                if score > bestsumma:
                    bestsumma = score

print bestsumma

Solution (Python):

def main():
    buf = input()
    buflist = buf.split()
    N = int(buflist[0])
    K = int(buflist[1])
    buf = input()
    buflist = buf.split()
    V = list(map(int, buflist))
    max_value = 0
    for i in range(min(N+1, K+1)):
        push_limit = K - i
        for j in range(i+1):
            jewel = []
            for k in range(-i+j, j):
                jewel.append(V[k])
            jewel = list(sorted(jewel))
            value = sum(jewel)
            for k in range(push_limit):
                if k >= len(jewel):
                    break
                if jewel[k] < 0:
                    value += -jewel[k]
                else:
                    break
            if value > max_value:
                max_value = value
    print(max_value)


if __name__ == '__main__':
    main()

Solution (Python):
N,K = map(int,input().split())
*V, = map(int,input().split())
ans = 0
for l in range(min(N,K+1)):
    for r in range(min(N-l+1,K-l+1)):
        h = V[:l]+V[N-r:]
        h.sort(reverse=True)
        for _ in [0]*(K-(l+r)):
            if h and h[-1]<0:h.pop()
        ans = max(ans,sum(h))
print(ans)

Solution (Python):

n, k = [int(_) for _ in input().split()]
v = [int(_) for _ in 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
            del nv[0]
        ans = max(ans, sum(nv))

print(ans)

Solution (Python):
import functools
import heapq


def main():
    from sys import stdin, stdout

    def read():
        return stdin.readline().rstrip('\n')

    def read_array(sep=None, maxsplit=-1):
        return read().split(sep, maxsplit)

    def read_int():
        return int(read())

    def read_int_array(sep=None, maxsplit=-1):
        return [int(a) for a in read_array(sep, maxsplit)]

    def write(*args, **kwargs):
        sep = kwargs.get('sep', ' ')
        end = kwargs.get('end', '\n')
        stdout.write(sep.join(str(a) for a in args) + end)

    def write_array(array, **kwargs):
        sep = kwargs.get('sep', ' ')
        end = kwargs.get('end', '\n')
        stdout.write(sep.join(str(a) for a in array) + end)

    N, K = read_int_array()
    nums = read_int_array()
    ans = 0

    @functools.lru_cache(None)
    def dp(i, j, handsum):
        nonlocal ans
        left = j + 1 - i
        taken = N - left
        if taken == K:
            ans = max(ans, handsum)
            return
        moves = K - taken

        if i <= j:
            dp(i+1, j, handsum + nums[i])
            dp(i, j-1, handsum + nums[j])

        can_return = sum(sorted([x for x in (nums[:i] + nums[j+1:]) if x < 0])[:moves])
        ans = max(ans, handsum - can_return)

    dp(0, N-1, 0)
    write(ans)

main()

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

for l in range(min(K,N)+1):
    ANS_l=V[:l]

    for r in range(min(K,N)+1-l):
        ANS=ANS_l+V[N-r:]

        

        ANS.sort()

        SUM=sum(ANS)
        SCORE=max(SCORE,SUM)

        

        for u in range(min(len(ANS),K-l-r)):
            SUM-=ANS[u]
            SCORE=max(SCORE,SUM)

            
print(SCORE)

Solution (Python):
def merge_sort(arr):
    if len(arr) <= 1:
        return arr

    mid = len(arr) // 2
    
    left = arr[:mid]
    right = arr[mid:]

    
    left = merge_sort(left)
    right = merge_sort(right)

    
    return merge(left, right)


def merge(left, right):
    merged = []
    l_i, r_i = 0, 0

    
    while l_i < len(left) and r_i < len(right):
        
        if left[l_i] <= right[r_i]:
            merged.append(left[l_i])
            l_i += 1
        else:
            merged.append(right[r_i])
            r_i += 1

    
    if l_i < len(left):
        merged.extend(left[l_i:])
    if r_i < len(right):
        merged.extend(right[r_i:])
    return merged



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

qa = [0]
takea = [[]]
now = 0

for i in range(N):
    now += que[i]
    qa.append(now)
    takea.append(que[0:i+1])

qb = [0]
takeb = [[]]
now = 0

for i in reversed(range(N)):
    now += que[i]
    qb.append(now)

    takeb.append(que[i:])

max = 0


for i in range(1, N+1):
    
    for j in range(i+1):

        qtotal = qa[j]+qb[i-j]
        taketotal = takea[j]+takeb[i-j]
        taketotal = merge_sort(taketotal)
        discardable = K-i
        for k in taketotal:
            if (discardable==0):
                break;

            if (0<=k):
                break;
            else:
                qtotal-=k
                discardable-=1
        if(max<qtotal):
            max = qtotal
    if(i==K):
        break;

print(max)


'''
A = int(input())
'''

'''
A, B = map(int, input().split())
'''

'''
N = input()
A = list(map(int, input().split()));
'''

'''
a = []
while True:
    n = input()
    if n == -1:
        break
    a.append(n)
'''

'''
import sys

a = []
for line in sys.stdin:
    a.append(int(line))
'''


'''
N, M = map(int, raw_input().split())
a = []
for i in range(M):
    a.append(map(int, raw_input().split()))
'''


'''
class UnionFind:
    def __init__(self, n):
        self.par = [i for i in range(n+1)]
        self.rank = [0] * (n+1)

    
    def find(self, x):
        if self.par[x] == x:
            return x
        else:
            self.par[x] = self.find(self.par[x])
            return self.par[x]

    
    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)
        if self.rank[x] < self.rank[y]:
            self.par[x] = y
        else:
            self.par[y] = x
            if self.rank[x] == self.rank[y]:
                self.rank[x] += 1

    
    def same_check(self, x, y):
        return self.find(x) == self.find(y)
'''


'''
def merge_sort(arr):
    if len(arr) <= 1:
        return arr

    mid = len(arr) // 2
    
    left = arr[:mid]
    right = arr[mid:]

    
    left = merge_sort(left)
    right = merge_sort(right)

    
    return merge(left, right)


def merge(left, right):
    merged = []
    l_i, r_i = 0, 0

    
    while l_i < len(left) and r_i < len(right):
        
        if left[l_i] <= right[r_i]:
            merged.append(left[l_i])
            l_i += 1
        else:
            merged.append(right[r_i])
            r_i += 1

    
    if l_i < len(left):
        merged.extend(left[l_i:])
    if r_i < len(right):
        merged.extend(right[r_i:])
    return merged

'''

Solution (Python):
from collections import deque

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

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

ans = 0
for left in range(min(N + 1, K + 1)):
    for right in range(min(N + 1 - left, K + 1 - left)):
        dq = deque(V)
        l = []
        for i in range(left):
            l.append(dq.popleft())
        for i in range(right):
            l.append(dq.pop())
        l.sort(key=lambda x: -x)
        for _ in range(K - left - right):
            if not l:
                break
            if l[-1] < 0:
                l.pop()
        ans = max(ans, sum(l))
        
print(ans)

Solution (Python):
N, K = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
for n in range(1, min(K,N)+1):
    m = min(K-n, n)
    for l in range(n+1):
        r = n-l
        A = V[:l] + (V[-r:] if r!=0 else [])
        A.sort()
        an = 0
        for i, a in enumerate(A):
            if i<m and a<0:
                continue
            else:
                an += a
        ans = max(ans, an)
print(ans)

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

ans = 0
for l in range(K+1):
  for r in range(K+1):
    if l+r<=K and l+r<=N:
      T = V[:l]+(V[-r:] if r>0 else [])
      T = sorted(T)[::-1]
      for _ in range(K-l-r):
        if len(T)>0 and T[-1]<0:
          T.pop()
      ans = max(ans, sum(T))
print ans

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


ans = 0
for i in range(1, k+1):
    if i > n:
        continue
    toreru = min(k-i, i)
    for left in range(i+1):
        right = i-left
        
        
        
        now = v[:left] + v[-1:-right-1:-1]
        now.sort()
        ko = bisect_left(now, 0)
        if ko <= toreru:
            ans = max(ans, sum(now[ko:]))
            
        else:
            ans = max(ans, sum(now[toreru:]))
            
print(ans)

Solution (Python):
import sys
from collections import deque
from collections import defaultdict
import math
sys.setrecursionlimit(20000000)


n,k = map(int,input().split())
v = list(map(int,input().split()))
ans = 0
for i in range(k+1):
        for j in range(k-i+1):
                x = v[:i]
                if n< i+j:
                        continue
                if i+j > k:
                        continue
                if j > 0:
                        x = x + v[-j:]
                x.sort(key=lambda x : -x)
                kai = i+ j
                while kai < k and x and x[-1] < 0:
                        x.pop()
                        kai+=1
                ans = max(ans,sum(x))
print(ans)

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

ans = 0
for L in range(min(N, K) + 1):
    for R in range(min(N - L, K - L) + 1):
        js = V[:L] + V[-R:] if R >= 1 else V[:L]
        t = K - L - R
        s = 0
        for j in sorted(js):
            if j < 0 and t > 0:
                s += -j
                t -= 1
        ans = max(ans, sum(js) + s)

print(ans)

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LS()
    return l
sys.setrecursionlimit(1000000)
mod = 1000000007


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


def B():
    n = I()
    s = [None for i in range(n)]
    for i in range(n):
        l = input().split()
        s[i] = l+[i]
    s.sort(key = lambda x:(x[0],-int(x[1])))
    for i,a,b in s:
        print(b+1)
    return


def C():
    def dfs(d,k):
        if d == n:
            l.append(k)
        else:
            dfs(d+1,k+[0])
            dfs(d+1,k+[1])
    n,m = LI()
    l = []
    dfs(0,[])
    qu = LIR(m)
    p = LI()
    ans = 0
    for k in l:
        s = 0
        for q in range(m):
            num = qu[q][0]
            lis = list(map(lambda x:x-1, qu[q][1:]))
            su = 0
            for i in lis:
                su ^= k[i]
            if su == p[q]:
                s += 1
        if s == m:
            ans += 1
    print(ans)
    return


def D():
    n,k = LI()
    v = LI()
    ans = -float("inf")
    for l in range(min(k,n)+1):
        for r in range(min(k,n)-l+1):
            m = 0
            q = v[:l]+v[n-r:]
            q.sort()
            res = max(0,k-r-l)
            for i in range(len(q)):
                if q[i] < 0 and res > 0:
                    res -= 1
                else:
                    m += q[i]
            if m > ans:
                ans = m
    print(ans)
    return


def E():
    n = I()

    return


def F():
    n = I()

    return


if __name__ == "__main__":
    D()

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,copy,functools
import random

sys.setrecursionlimit(10**7)
inf = 10**20
eps = 1.0 / 10**10
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 list(map(int, sys.stdin.readline().split()))
def LLI(): return [list(map(int, l.split())) for l in sys.stdin.readlines()]
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 I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
def pf(s): return print(s, flush=True)
def pe(s): return print(str(s), file=sys.stderr)


def main():
    n,k = LI()
    a = LI()
    r = 0
    for i in range(1,min(n,k)+1):
        for j in range(i+1):
            ii = i - j
            t = a[:j]
            if ii > 0:
                t += a[-ii:]
            t.sort()
            l = min(k - i,i-1)
            for ll in range(l):
                if t[ll] >= 0:
                    l = ll
                    break
            s = sum(t[l:])
            if r < s:
                r = s

    return r


print(main())

Solution (Python):
N,K=map(int,input().split())
V=[int(i) for i in input().split()]
ans=0
for i in range(N):
  for j in range(i,N+1):
    c=0
    if i+N-j<=K:
      k=K-(i+N-j)
      s=V[:i]+V[j:]
      s.sort()
      c=0
      
      for t in s:
        if k>=1 and t<0:
          k-=1
        else:
          c+=t
    ans=max(c,ans)
print(ans)