import bisect

N, K = map(int, input().split())
V = [int(i) for i in input().split()]

ans = -10**20
for l in range(min(K+1, N+1)):
    for r in range(min(K-l+1, N-l+1)):
        P = V[:l]+V[N-r:]
        P.sort()
        z = bisect.bisect_left(P, 0)

        if z <= K-l-r:
            ans = max(ans, sum(P[z:]))
        else:
            ans = max(ans, sum(P[K-l-r:]))

print(ans) import bisect bisect N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K int(i) for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) int i V = [int(i) for i in input().split()] V [int(i) for i in input().split()] ans = -10**20 ans -10**20 -10 20 for l in range(min(K+1, N+1)):
    for r in range(min(K-l+1, N-l+1)):
        P = V[:l]+V[N-r:]
        P.sort()
        z = bisect.bisect_left(P, 0)

        if z <= K-l-r:
            ans = max(ans, sum(P[z:]))
        else:
            ans = max(ans, sum(P[K-l-r:])) l range(min(K+1, N+1)) range min(K+1, N+1) min K+1 K 1 N+1 N 1 for r in range(min(K-l+1, N-l+1)):
        P = V[:l]+V[N-r:]
        P.sort()
        z = bisect.bisect_left(P, 0)

        if z <= K-l-r:
            ans = max(ans, sum(P[z:]))
        else:
            ans = max(ans, sum(P[K-l-r:])) r range(min(K-l+1, N-l+1)) range min(K-l+1, N-l+1) min K-l+1 K-l K l 1 N-l+1 N-l N l 1 P = V[:l]+V[N-r:] P V[:l]+V[N-r:] V[:l] V :l l V[N-r:] V N-r: N-r N r P.sort() P.sort P sort z = bisect.bisect_left(P, 0) z bisect.bisect_left(P, 0) bisect.bisect_left bisect bisect_left P 0 if z <= K-l-r:
            ans = max(ans, sum(P[z:]))
        else:
            ans = max(ans, sum(P[K-l-r:])) z <= K-l-r z K-l-r K-l K l r ans = max(ans, sum(P[z:])) ans max(ans, sum(P[z:])) max ans sum(P[z:]) sum P[z:] P z: z ans = max(ans, sum(P[K-l-r:])) ans max(ans, sum(P[K-l-r:])) max ans sum(P[K-l-r:]) sum P[K-l-r:] P K-l-r: K-l-r K-l K l r print(ans) print ans ans = -10**20 -10**20 ans P = V[:l]+V[N-r:] V[:l]+V[N-r:] P K = map(int, input().split()) map(int, input().split()) K V = [int(i) for i in input().split()] [int(i) for i in input().split()] V ans = max(ans, sum(P[K-l-r:])) max(ans, sum(P[K-l-r:])) ans N, K = map(int, input().split()) map(int, input().split()) N z = bisect.bisect_left(P, 0) bisect.bisect_left(P, 0) z ans = max(ans, sum(P[z:])) max(ans, sum(P[z:])) ans
import bisect

N, K = map(int, input().split())
V = [int(i) for i in input().split()]

ans = -10**20
for l in range(min(K+1, N+1)):
    for r in range(min(K-l+1, N-l+1)):
        P = V[:l]+V[N-r:]
        P.sort()
        z = bisect.bisect_left(P, 0)

        if z <= K-l-r:
            ans = max(ans, sum(P[z:]))
        else:
            ans = max(ans, sum(P[K-l-r:]))

print(ans)
