inf = float('inf')



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

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



volume = max(N, K)

take_max = min(N, K)



dpl = [[-inf] * (volume + 1) for _ in range(N + 1)]

dpr = [[-inf] * (volume + 1) for _ in range(N + 1)]

dpl[0][0] = dpr[0][0] = 0



for i, vv in enumerate(v, 1):

    for take in range(i + 1):

        dpl[i][take] = dpl[i - 1][take]

        if take > 0:

            dpl[i][take] = max(dpl[i][take], dpl[i - 1][take - 1] + vv)



for i, vv in enumerate(reversed(v), 1):

    for take in range(i + 1):

        dpr[i][take] = max(dpr[i][take], dpr[i - 1][take])

        if take > 0:

            dpr[i][take] = max(dpr[i][take], dpr[i - 1][take - 1] + vv)



ans = 0

for take_frm_left in range(take_max + 1):

    for take_frm_right in range(take_max + 1):

        if take_frm_left + take_frm_right > take_max: break

        for use_frm_left in range(take_frm_left + 1):

            remove_frm_left = take_frm_left - use_frm_left

            vl = dpl[take_frm_left][use_frm_left]

            for use_frm_right in range(take_frm_right + 1):

                remove_frm_right = take_frm_right - use_frm_right

                if (take_frm_left + take_frm_right + remove_frm_left + remove_frm_right) > K: continue

                vr = dpr[take_frm_right][use_frm_right]

                # print(vl + vr, take_frm_left, use_frm_left, take_frm_right, use_frm_right)

                ans = max(ans, vl + vr)

print(ans)

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

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



max_pick = min(K, N)

ans = 0



# 左右から取る量を決め打ち



for left in range(max_pick + 1):

    for right in range(max_pick - 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)

inf = float("inf")
N, K = list(map(int, input().split()))
v = list(map(int, input().split()))
volume = max(N, K)
take_max = min(N, K)
dpl = [[-inf] * (volume + 1) for _ in range(N + 1)]
dpr = [[-inf] * (volume + 1) for _ in range(N + 1)]
dpl[0][0] = dpr[0][0] = 0
for i, vv in enumerate(v, 1):
    for take in range(i + 1):
        dpl[i][take] = dpl[i - 1][take]
        if take > 0:
            dpl[i][take] = max(dpl[i][take], dpl[i - 1][take - 1] + vv)
for i, vv in enumerate(reversed(v), 1):
    for take in range(i + 1):
        dpr[i][take] = max(dpr[i][take], dpr[i - 1][take])
        if take > 0:
            dpr[i][take] = max(dpr[i][take], dpr[i - 1][take - 1] + vv)
ans = 0
for take_frm_left in range(take_max + 1):
    for take_frm_right in range(take_max + 1):
        if take_frm_left + take_frm_right > take_max:
            break
        for use_frm_left in range(take_frm_left + 1):
            remove_frm_left = take_frm_left - use_frm_left
            vl = dpl[take_frm_left][use_frm_left]
            for use_frm_right in range(take_frm_right + 1):
                remove_frm_right = take_frm_right - use_frm_right
                if (
                    take_frm_left + take_frm_right + remove_frm_left + remove_frm_right
                ) > K:
                    continue
                vr = dpr[take_frm_right][use_frm_right]
                # print(vl + vr, take_frm_left, use_frm_left, take_frm_right, use_frm_right)
                ans = max(ans, vl + vr)
print(ans)

N, K = list(map(int, input().split()))
v = list(map(int, input().split()))
max_pick = min(K, N)
ans = 0
# 左右から取る量を決め打ち
for left in range(max_pick + 1):
    for right in range(max_pick - 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)

-inf = float("inf") -volume = max(N, K) -take_max = min(N, K) -dpl = [[-inf] * (volume + 1) for _ in range(N + 1)] -dpr = [[-inf] * (volume + 1) for _ in range(N + 1)] -dpl[0][0] = dpr[0][0] = 0 -for i, vv in enumerate(v, 1): -    for take in range(i + 1): -        dpl[i][take] = dpl[i - 1][take] -        if take > 0: -            dpl[i][take] = max(dpl[i][take], dpl[i - 1][take - 1] + vv) -for i, vv in enumerate(reversed(v), 1): -    for take in range(i + 1): -        dpr[i][take] = max(dpr[i][take], dpr[i - 1][take]) -        if take > 0: -            dpr[i][take] = max(dpr[i][take], dpr[i - 1][take - 1] + vv) +max_pick = min(K, N) -for take_frm_left in range(take_max + 1): -    for take_frm_right in range(take_max + 1): -        if take_frm_left + take_frm_right > take_max: -            break -        for use_frm_left in range(take_frm_left + 1): -            remove_frm_left = take_frm_left - use_frm_left -            vl = dpl[take_frm_left][use_frm_left] -            for use_frm_right in range(take_frm_right + 1): -                remove_frm_right = take_frm_right - use_frm_right -                if ( -                    take_frm_left + take_frm_right + remove_frm_left + remove_frm_right -                ) > K: -                    continue -                vr = dpr[take_frm_right][use_frm_right] -                # print(vl + vr, take_frm_left, use_frm_left, take_frm_right, use_frm_right) -                ans = max(ans, vl + vr) +# 左右から取る量を決め打ち +for left in range(max_pick + 1): +    for right in range(max_pick - 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))