import bisect

N, K = [int(x) for x in input().split()]
V = [int(x) for x in input().split()]
M = min(N, K)

ans = 0
juwel = []
for l in range(M + 1):
    for r in range(M - l + 1):
        juwel = sorted(V[:l] + V[N - r:])
        s = bisect.bisect_left(juwel, 0)
        s = min(s, K - l - r)
        juwel = juwel[s:]
        ans = max(ans, sum(juwel))

print(ans) import bisect bisect int(x) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) int x N, K = [int(x) for x in input().split()] N [int(x) for x in input().split()] K int(x) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) int x V = [int(x) for x in input().split()] V [int(x) for x in input().split()] M = min(N, K) M min(N, K) min N K ans = 0 ans 0 juwel = [] juwel [] for l in range(M + 1):
    for r in range(M - l + 1):
        juwel = sorted(V[:l] + V[N - r:])
        s = bisect.bisect_left(juwel, 0)
        s = min(s, K - l - r)
        juwel = juwel[s:]
        ans = max(ans, sum(juwel)) l range(M + 1) range M + 1 M 1 for r in range(M - l + 1):
        juwel = sorted(V[:l] + V[N - r:])
        s = bisect.bisect_left(juwel, 0)
        s = min(s, K - l - r)
        juwel = juwel[s:]
        ans = max(ans, sum(juwel)) r range(M - l + 1) range M - l + 1 M - l M l 1 juwel = sorted(V[:l] + V[N - r:]) juwel sorted(V[:l] + V[N - r:]) sorted V[:l] + V[N - r:] V[:l] V :l l V[N - r:] V N - r: N - r N r s = bisect.bisect_left(juwel, 0) s bisect.bisect_left(juwel, 0) bisect.bisect_left bisect bisect_left juwel 0 s = min(s, K - l - r) s min(s, K - l - r) min s K - l - r K - l K l r juwel = juwel[s:] juwel juwel[s:] juwel s: s ans = max(ans, sum(juwel)) ans max(ans, sum(juwel)) max ans sum(juwel) sum juwel print(ans) print ans juwel = [] [] juwel s = min(s, K - l - r) min(s, K - l - r) s juwel = juwel[s:] juwel[s:] juwel juwel = sorted(V[:l] + V[N - r:]) sorted(V[:l] + V[N - r:]) juwel K = [int(x) for x in input().split()] [int(x) for x in input().split()] K M = min(N, K) min(N, K) M s = bisect.bisect_left(juwel, 0) bisect.bisect_left(juwel, 0) s ans = max(ans, sum(juwel)) max(ans, sum(juwel)) ans V = [int(x) for x in input().split()] [int(x) for x in input().split()] V N, K = [int(x) for x in input().split()] [int(x) for x in input().split()] N ans = 0 0 ans
import bisect

N, K = [int(x) for x in input().split()]
V = [int(x) for x in input().split()]
M = min(N, K)

ans = 0
juwel = []
for l in range(M + 1):
    for r in range(M - l + 1):
        juwel = sorted(V[:l] + V[N - r:])
        s = bisect.bisect_left(juwel, 0)
        s = min(s, K - l - r)
        juwel = juwel[s:]
        ans = max(ans, sum(juwel))

print(ans)