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 = i - j, n - j

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

        j = i

        for x, m in a:

            if j == k:

                break

            if m < r1 or m >= r2:

                s -= x

                j += 1

        r = max(r, s)

print(r)

from itertools import chain, islice

 

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)

ans = max(sum(v[m] for m in chain(list(range(i - j)), list(range(n - j, n)))) -

          sum(islice((x for x, m in a if m < i - j or m >= n - j), k - i))

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

print(ans)

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 = i - j, n - j
        s = sum(v[m] for m in chain(list(range(r1)), list(range(r2, n))))
        j = i
        for x, m in a:
            if j == k:
                break
            if m < r1 or m >= r2:
                s -= x
                j += 1
        r = max(r, s)
print(r)

from itertools import chain, islice

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)
ans = max(
    sum(v[m] for m in chain(list(range(i - j)), list(range(n - j, n))))
    - sum(islice((x for x, m in a if m < i - j or m >= n - j), k - i))
    for i in range(1, min(n, k) + 1)
    for j in range(i)
)
print(ans)

-from itertools import chain +from itertools import chain, islice -r = 0 -for i in range(1, min(n, k) + 1): -    for j in range(i): -        r1, r2 = i - j, n - j -        s = sum(v[m] for m in chain(list(range(r1)), list(range(r2, n)))) -        j = i -        for x, m in a: -            if j == k: -                break -            if m < r1 or m >= r2: -                s -= x -                j += 1 -        r = max(r, s) -print(r) +ans = max( +    sum(v[m] for m in chain(list(range(i - j)), list(range(n - j, n)))) +    - sum(islice((x for x, m in a if m < i - j or m >= n - j), k - i)) +    for i in range(1, min(n, k) + 1) +    for j in range(i) +) +print(ans)