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 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[:i - j] + v[n - j:]) -

          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, 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 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[: i - j] + v[n - j :])
    - 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, islice +from itertools import islice -    sum(v[m] for m in chain(list(range(i - j)), list(range(n - j, n)))) +    sum(v[: i - j] + v[n - j :])