n,k,*v=list(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)
n,k,*v=list(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)
n, k, *v = list(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)

n, k, *v = list(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)

-        while t and c and t[-1] < 0: +        while t and c * t[-1] < 0: