from heapq import heapify, heappop



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

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

vr = list(reversed(vl))

sum_L = [0]

for vv in vl:

    sum_L.append(sum_L[-1] + vv)



sum_R = [0]

for vv in vr:

    sum_R.append(sum_R[-1] + vv)



ans = 0

for pick in range(min(K, N) + 1):

    for to_remove in range(K + 1):

        if pick + to_remove > K: break

        for left in range(pick + 1):

            right = pick - left



            t = sum_L[left] + sum_R[right]



            q = vl[:left] + vr[:right]

            heapify(q)



            cnt = 0

            while cnt < to_remove and q:

                t -= heappop(q)

                cnt += 1

            ans = max(ans, t)

print(ans)

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

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



pick_max = min(N, K)

ans = 0

for left in range(pick_max + 1):

    for right in range(pick_max - left + 1):

        t = v[:left] + v[N - right:]

        m = sorted([x for x in t if x < 0])[:K - left - right]

        ans = max(ans, sum(t) - sum(m))

print(ans)

from heapq import heapify, heappop

N, K = list(map(int, input().split()))
vl = list(map(int, input().split()))
vr = list(reversed(vl))
sum_L = [0]
for vv in vl:
    sum_L.append(sum_L[-1] + vv)
sum_R = [0]
for vv in vr:
    sum_R.append(sum_R[-1] + vv)
ans = 0
for pick in range(min(K, N) + 1):
    for to_remove in range(K + 1):
        if pick + to_remove > K:
            break
        for left in range(pick + 1):
            right = pick - left
            t = sum_L[left] + sum_R[right]
            q = vl[:left] + vr[:right]
            heapify(q)
            cnt = 0
            while cnt < to_remove and q:
                t -= heappop(q)
                cnt += 1
            ans = max(ans, t)
print(ans)

N, K = list(map(int, input().split(" ")))
v = list(map(int, input().split(" ")))
pick_max = min(N, K)
ans = 0
for left in range(pick_max + 1):
    for right in range(pick_max - left + 1):
        t = v[:left] + v[N - right :]
        m = sorted([x for x in t if x < 0])[: K - left - right]
        ans = max(ans, sum(t) - sum(m))
print(ans)

-from heapq import heapify, heappop - -N, K = list(map(int, input().split())) -vl = list(map(int, input().split())) -vr = list(reversed(vl)) -sum_L = [0] -for vv in vl: -    sum_L.append(sum_L[-1] + vv) -sum_R = [0] -for vv in vr: -    sum_R.append(sum_R[-1] + vv) +N, K = list(map(int, input().split(" "))) +v = list(map(int, input().split(" "))) +pick_max = min(N, K) -for pick in range(min(K, N) + 1): -    for to_remove in range(K + 1): -        if pick + to_remove > K: -            break -        for left in range(pick + 1): -            right = pick - left -            t = sum_L[left] + sum_R[right] -            q = vl[:left] + vr[:right] -            heapify(q) -            cnt = 0 -            while cnt < to_remove and q: -                t -= heappop(q) -                cnt += 1 -            ans = max(ans, t) +for left in range(pick_max + 1): +    for right in range(pick_max - left + 1): +        t = v[:left] + v[N - right :] +        m = sorted([x for x in t if x < 0])[: K - left - right] +        ans = max(ans, sum(t) - sum(m))