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

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



m=[]

M = min(n,k) +1

for i in range(M):

 for j in range(M-i):

  # print(i,j)

  t=sorted(v[:i]+v[:~j:-1])



  while t and(k-i-j)*t[0]<0:t.pop(0);j+=1

  m.append(sum(t))

print((max(m)))

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

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

import heapq 

m = min(N,K)

M = 0

for i in range(m+1):

    for j in range(m-i+1):

        v1 = V[:i]

        v3 = V[N-j:]

        # print(v1,v3)

        v = v1+v3

        heapq.heapify(v)

        h = K - len(v)

        while v and len(v) and h >0:

            q = heapq.heappop(v)

            h-=1

            if q > 0:

                heapq.heappush(v,q)

                break

        tmp = sum(v)

        # print(v,v3)

        M = max(M,tmp)

print(M)
n, k = list(map(int, input().split()))
v = list(map(int, input().split()))
m = []
M = min(n, k) + 1
for i in range(M):
    for j in range(M - i):
        # print(i,j)
        t = sorted(v[:i] + v[:~j:-1])
        while t and (k - i - j) * t[0] < 0:
            t.pop(0)
            j += 1
        m.append(sum(t))
print((max(m)))

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

m = min(N, K)
M = 0
for i in range(m + 1):
    for j in range(m - i + 1):
        v1 = V[:i]
        v3 = V[N - j :]
        # print(v1,v3)
        v = v1 + v3
        heapq.heapify(v)
        h = K - len(v)
        while v and len(v) and h > 0:
            q = heapq.heappop(v)
            h -= 1
            if q > 0:
                heapq.heappush(v, q)
                break
        tmp = sum(v)
        # print(v,v3)
        M = max(M, tmp)
print(M)

-n, k = list(map(int, input().split())) -v = list(map(int, input().split())) -m = [] -M = min(n, k) + 1 -for i in range(M): -    for j in range(M - i): -        # print(i,j) -        t = sorted(v[:i] + v[:~j:-1]) -        while t and (k - i - j) * t[0] < 0: -            t.pop(0) -            j += 1 -        m.append(sum(t)) -print((max(m))) +N, K = list(map(int, input().split())) +V = list(map(int, input().split())) +import heapq + +m = min(N, K) +M = 0 +for i in range(m + 1): +    for j in range(m - i + 1): +        v1 = V[:i] +        v3 = V[N - j :] +        # print(v1,v3) +        v = v1 + v3 +        heapq.heapify(v) +        h = K - len(v) +        while v and len(v) and h > 0: +            q = heapq.heappop(v) +            h -= 1 +            if q > 0: +                heapq.heappush(v, q) +                break +        tmp = sum(v) +        # print(v,v3) +        M = max(M, tmp) +print(M)