import sys, math
from collections import defaultdict, deque, Counter
from bisect import bisect_left, bisect_right
from itertools import combinations, permutations, product
from heapq import heappush, heappop
from functools import lru_cache
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 1000000007
sys.setrecursionlimit(1000000)

N, K = rl()
V = rl()
dp = [[[-float('inf')]*(K+1) for _ in range(N+1)] for _ in range(N+1)]
dp[0][0][0] = 0

for i in range(N):
	for j in range(N):
		for k in range(K):
			dp[i][j][k+1] = max(dp[i][j][k+1], dp[i][j][k])
			# if i+j >= K or i+j >= N:
			# 	print('?', i,j,k,dp[i][j][k+1], dp[i][j][k])
			# 	dp[i][j][k+1] = max(dp[i][j][k+1], dp[i][j][k])
			# 	continue
			ni, nj, nk = i+1, j, k+1
			if ni+nj <= N:
				dp[ni][nj][nk] = max(dp[ni][nj][nk], dp[i][j][k]+V[i])
				if V[i] < 0 and nk+1 <= K:
					dp[ni][nj][nk+1] = max(dp[ni][nj][nk+1], dp[i][j][k])
			ni, nj, nk = i, j+1, k+1
			if ni+nj <= N:
				dp[ni][nj][nk] = max(dp[ni][nj][nk], dp[i][j][k]+V[N-j-1])
				if V[N-j-1] < 0 and nk+1 <= K:
					dp[ni][nj][nk+1] = max(dp[ni][nj][nk+1], dp[i][j][k])

ans = 0
for i in range(N+1):
	for j in range(N+1):
		ans = max(ans, dp[i][j][K])
print(ans) import sys, math sys math from collections import defaultdict, deque, Counter from bisect import bisect_left, bisect_right from itertools import combinations, permutations, product from heapq import heappush, heappop from functools import lru_cache input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline rs = lambda: input().strip() rs lambda: input().strip() input().strip() ().strip () input strip ri = lambda: int(input()) ri lambda: int(input()) int(input()) int input() input rl = lambda: list(map(int, input().split())) rl lambda: list(map(int, input().split())) list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split mod = 1000000007 mod 1000000007 sys.setrecursionlimit(1000000) sys.setrecursionlimit sys setrecursionlimit 1000000 N, K = rl() N rl() rl K V = rl() V rl() rl [[-float('inf')]*(K+1) for _ in range(N+1)] for _ in range(N+1) for _ in range(N+1) _ range(N+1) range N+1 N 1 for _ in range(N+1) [[-float('inf')]*(K+1) for _ in range(N+1)] dp = [[[-float('inf')]*(K+1) for _ in range(N+1)] for _ in range(N+1)] dp [[[-float('inf')]*(K+1) for _ in range(N+1)] for _ in range(N+1)] dp[0][0][0] = 0 dp[0][0][0] [0] [0] dp 0 0 0 0 for i in range(N):
	for j in range(N):
		for k in range(K):
			dp[i][j][k+1] = max(dp[i][j][k+1], dp[i][j][k])
			# if i+j >= K or i+j >= N:
			# 	print('?', i,j,k,dp[i][j][k+1], dp[i][j][k])
			# 	dp[i][j][k+1] = max(dp[i][j][k+1], dp[i][j][k])
			# 	continue
			ni, nj, nk = i+1, j, k+1
			if ni+nj <= N:
				dp[ni][nj][nk] = max(dp[ni][nj][nk], dp[i][j][k]+V[i])
				if V[i] < 0 and nk+1 <= K:
					dp[ni][nj][nk+1] = max(dp[ni][nj][nk+1], dp[i][j][k])
			ni, nj, nk = i, j+1, k+1
			if ni+nj <= N:
				dp[ni][nj][nk] = max(dp[ni][nj][nk], dp[i][j][k]+V[N-j-1])
				if V[N-j-1] < 0 and nk+1 <= K:
					dp[ni][nj][nk+1] = max(dp[ni][nj][nk+1], dp[i][j][k]) i range(N) range N for j in range(N):
		for k in range(K):
			dp[i][j][k+1] = max(dp[i][j][k+1], dp[i][j][k])
			# if i+j >= K or i+j >= N:
			# 	print('?', i,j,k,dp[i][j][k+1], dp[i][j][k])
			# 	dp[i][j][k+1] = max(dp[i][j][k+1], dp[i][j][k])
			# 	continue
			ni, nj, nk = i+1, j, k+1
			if ni+nj <= N:
				dp[ni][nj][nk] = max(dp[ni][nj][nk], dp[i][j][k]+V[i])
				if V[i] < 0 and nk+1 <= K:
					dp[ni][nj][nk+1] = max(dp[ni][nj][nk+1], dp[i][j][k])
			ni, nj, nk = i, j+1, k+1
			if ni+nj <= N:
				dp[ni][nj][nk] = max(dp[ni][nj][nk], dp[i][j][k]+V[N-j-1])
				if V[N-j-1] < 0 and nk+1 <= K:
					dp[ni][nj][nk+1] = max(dp[ni][nj][nk+1], dp[i][j][k]) j range(N) range N for k in range(K):
			dp[i][j][k+1] = max(dp[i][j][k+1], dp[i][j][k])
			# if i+j >= K or i+j >= N:
			# 	print('?', i,j,k,dp[i][j][k+1], dp[i][j][k])
			# 	dp[i][j][k+1] = max(dp[i][j][k+1], dp[i][j][k])
			# 	continue
			ni, nj, nk = i+1, j, k+1
			if ni+nj <= N:
				dp[ni][nj][nk] = max(dp[ni][nj][nk], dp[i][j][k]+V[i])
				if V[i] < 0 and nk+1 <= K:
					dp[ni][nj][nk+1] = max(dp[ni][nj][nk+1], dp[i][j][k])
			ni, nj, nk = i, j+1, k+1
			if ni+nj <= N:
				dp[ni][nj][nk] = max(dp[ni][nj][nk], dp[i][j][k]+V[N-j-1])
				if V[N-j-1] < 0 and nk+1 <= K:
					dp[ni][nj][nk+1] = max(dp[ni][nj][nk+1], dp[i][j][k]) k range(K) range K dp[i][j][k+1] = max(dp[i][j][k+1], dp[i][j][k]) dp[i][j][k+1] [j] [i] dp i j k+1 k 1 max(dp[i][j][k+1], dp[i][j][k]) max dp[i][j][k+1] [j] [i] dp i j k+1 k 1 dp[i][j][k] [j] [i] dp i j k ni, nj, nk = i+1, j, k+1 ni i+1 i 1 nj j nk k+1 k 1 if ni+nj <= N:
				dp[ni][nj][nk] = max(dp[ni][nj][nk], dp[i][j][k]+V[i])
				if V[i] < 0 and nk+1 <= K:
					dp[ni][nj][nk+1] = max(dp[ni][nj][nk+1], dp[i][j][k])
			 ni+nj <= N ni+nj ni nj N dp[ni][nj][nk] = max(dp[ni][nj][nk], dp[i][j][k]+V[i]) dp[ni][nj][nk] [nj] [ni] dp ni nj nk max(dp[ni][nj][nk], dp[i][j][k]+V[i]) max dp[ni][nj][nk] [nj] [ni] dp ni nj nk dp[i][j][k]+V[i] dp[i][j][k] [j] [i] dp i j k V[i] V i if V[i] < 0 and nk+1 <= K:
					dp[ni][nj][nk+1] = max(dp[ni][nj][nk+1], dp[i][j][k])
			 V[i] < 0 and nk+1 <= K V[i] < 0 V[i] V i 0 nk+1 <= K nk+1 nk 1 K dp[ni][nj][nk+1] = max(dp[ni][nj][nk+1], dp[i][j][k]) dp[ni][nj][nk+1] [nj] [ni] dp ni nj nk+1 nk 1 max(dp[ni][nj][nk+1], dp[i][j][k]) max dp[ni][nj][nk+1] [nj] [ni] dp ni nj nk+1 nk 1 dp[i][j][k] [j] [i] dp i j k ni, nj, nk = i, j+1, k+1 ni i nj j+1 j 1 nk k+1 k 1 if ni+nj <= N:
				dp[ni][nj][nk] = max(dp[ni][nj][nk], dp[i][j][k]+V[N-j-1])
				if V[N-j-1] < 0 and nk+1 <= K:
					dp[ni][nj][nk+1] = max(dp[ni][nj][nk+1], dp[i][j][k]) ni+nj <= N ni+nj ni nj N dp[ni][nj][nk] = max(dp[ni][nj][nk], dp[i][j][k]+V[N-j-1]) dp[ni][nj][nk] [nj] [ni] dp ni nj nk max(dp[ni][nj][nk], dp[i][j][k]+V[N-j-1]) max dp[ni][nj][nk] [nj] [ni] dp ni nj nk dp[i][j][k]+V[N-j-1] dp[i][j][k] [j] [i] dp i j k V[N-j-1] V N-j-1 N-j N j 1 if V[N-j-1] < 0 and nk+1 <= K:
					dp[ni][nj][nk+1] = max(dp[ni][nj][nk+1], dp[i][j][k]) V[N-j-1] < 0 and nk+1 <= K V[N-j-1] < 0 V[N-j-1] V N-j-1 N-j N j 1 0 nk+1 <= K nk+1 nk 1 K dp[ni][nj][nk+1] = max(dp[ni][nj][nk+1], dp[i][j][k]) dp[ni][nj][nk+1] [nj] [ni] dp ni nj nk+1 nk 1 max(dp[ni][nj][nk+1], dp[i][j][k]) max dp[ni][nj][nk+1] [nj] [ni] dp ni nj nk+1 nk 1 dp[i][j][k] [j] [i] dp i j k ans = 0 ans 0 for i in range(N+1):
	for j in range(N+1):
		ans = max(ans, dp[i][j][K]) i range(N+1) range N+1 N 1 for j in range(N+1):
		ans = max(ans, dp[i][j][K]) j range(N+1) range N+1 N 1 ans = max(ans, dp[i][j][K]) ans max(ans, dp[i][j][K]) max ans dp[i][j][K] [j] [i] dp i j K print(ans) print ans rs = lambda: input().strip() lambda: input().strip() rs nj, nk = i, j+1, k+1 j+1 nj V = rl() rl() V mod = 1000000007 1000000007 mod ans = 0 0 ans dp = [[[-float('inf')]*(K+1) for _ in range(N+1)] for _ in range(N+1)] [[[-float('inf')]*(K+1) for _ in range(N+1)] for _ in range(N+1)] dp input = sys.stdin.readline sys.stdin.readline input nj, nk = i+1, j, k+1 j nj ni, nj, nk = i+1, j, k+1 i+1 ni nk = i+1, j, k+1 k+1 nk K = rl() rl() K ans = max(ans, dp[i][j][K]) max(ans, dp[i][j][K]) ans ri = lambda: int(input()) lambda: int(input()) ri ni, nj, nk = i, j+1, k+1 i ni N, K = rl() rl() N nk = i, j+1, k+1 k+1 nk rl = lambda: list(map(int, input().split())) lambda: list(map(int, input().split())) rl
import sys, math
from collections import defaultdict, deque, Counter
from bisect import bisect_left, bisect_right
from itertools import combinations, permutations, product
from heapq import heappush, heappop
from functools import lru_cache
input = sys.stdin.readline
rs = lambda: input().strip()
ri = lambda: int(input())
rl = lambda: list(map(int, input().split()))
mod = 1000000007
sys.setrecursionlimit(1000000)

N, K = rl()
V = rl()
dp = [[[-float('inf')]*(K+1) for _ in range(N+1)] for _ in range(N+1)]
dp[0][0][0] = 0

for i in range(N):
	for j in range(N):
		for k in range(K):
			dp[i][j][k+1] = max(dp[i][j][k+1], dp[i][j][k])
			# if i+j >= K or i+j >= N:
			# 	print('?', i,j,k,dp[i][j][k+1], dp[i][j][k])
			# 	dp[i][j][k+1] = max(dp[i][j][k+1], dp[i][j][k])
			# 	continue
			ni, nj, nk = i+1, j, k+1
			if ni+nj <= N:
				dp[ni][nj][nk] = max(dp[ni][nj][nk], dp[i][j][k]+V[i])
				if V[i] < 0 and nk+1 <= K:
					dp[ni][nj][nk+1] = max(dp[ni][nj][nk+1], dp[i][j][k])
			ni, nj, nk = i, j+1, k+1
			if ni+nj <= N:
				dp[ni][nj][nk] = max(dp[ni][nj][nk], dp[i][j][k]+V[N-j-1])
				if V[N-j-1] < 0 and nk+1 <= K:
					dp[ni][nj][nk+1] = max(dp[ni][nj][nk+1], dp[i][j][k])

ans = 0
for i in range(N+1):
	for j in range(N+1):
		ans = max(ans, dp[i][j][K])
print(ans)
