import heapq

from itertools import chain



R = lambda: list(map(int, input().split()))

n, k = R()

v = list(R())

r = 0

for i in range(1, min(n, k) + 1):

    for j in range(i):

        s = 0

        h = []

        for m in chain(list(range(i - j)), list(range(n - j, n))):

            x = v[m]

            s += x

            if x < 0:

                heapq.heappush(h, x)

        j = i

        while h and j < k:

            s -= heapq.heappop(h)

            j += 1

        r = max(r, s)

print(r)

from itertools import chain

 

R = lambda: list(map(int, input().split()))

n, k = R()

v = list(R())

a = sorted((x, i) for i, x in enumerate(v) if x < 0)

r = 0

for i in range(1, min(n, k) + 1):

    for j in range(i):

        r1, r2 = list(range(i - j)), list(range(n - j, n))

        s = sum(v[m] for m in chain(r1, r2))

        j = i

        for x, m in a:

            if j == k:

                break

            if m in r1 or m in r2:

                s -= x

                j += 1

        r = max(r, s)

print(r)

import heapq
from itertools import chain

R = lambda: list(map(int, input().split()))
n, k = R()
v = list(R())
r = 0
for i in range(1, min(n, k) + 1):
    for j in range(i):
        s = 0
        h = []
        for m in chain(list(range(i - j)), list(range(n - j, n))):
            x = v[m]
            s += x
            if x < 0:
                heapq.heappush(h, x)
        j = i
        while h and j < k:
            s -= heapq.heappop(h)
            j += 1
        r = max(r, s)
print(r)

from itertools import chain

R = lambda: list(map(int, input().split()))
n, k = R()
v = list(R())
a = sorted((x, i) for i, x in enumerate(v) if x < 0)
r = 0
for i in range(1, min(n, k) + 1):
    for j in range(i):
        r1, r2 = list(range(i - j)), list(range(n - j, n))
        s = sum(v[m] for m in chain(r1, r2))
        j = i
        for x, m in a:
            if j == k:
                break
            if m in r1 or m in r2:
                s -= x
                j += 1
        r = max(r, s)
print(r)

-import heapq +a = sorted((x, i) for i, x in enumerate(v) if x < 0) -        s = 0 -        h = [] -        for m in chain(list(range(i - j)), list(range(n - j, n))): -            x = v[m] -            s += x -            if x < 0: -                heapq.heappush(h, x) +        r1, r2 = list(range(i - j)), list(range(n - j, n)) +        s = sum(v[m] for m in chain(r1, r2)) -        while h and j < k: -            s -= heapq.heappop(h) -            j += 1 +        for x, m in a: +            if j == k: +                break +            if m in r1 or m in r2: +                s -= x +                j += 1