from heapq import heapify,heappush,heappop
import math

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

ans = 0
sum_d = 0
d = []
heapify(d)
# i = 操作回数
# j = 取る回数 (i-j = 捨てる回数)
# k = 左から取る回数 (j-k = 右から取る回数)
for i in range(1,K+1):
	for j in range(math.ceil(i/2),min(i+1,n+1)):
		for k in range(j+1):
			#print(i,j,k,j-k,i-j,ans)
			# 左から取る
			for l in range(k):
				heappush(d,v[l])
				sum_d += v[l]
			# 右から取る
			for l in range(j-k):
				heappush(d,v[-l-1])
				sum_d += v[-l-1]
			for l in range(i-j):
				sum_d -= heappop(d)
			ans = max(sum_d,ans)
			sum_d = 0
			d = []
			heapify(d)
print(ans) from heapq import heapify,heappush,heappop import math math n,K = map(int,input().split()) n map(int,input().split()) map int input().split() ().split () input split K v = list(map(int,input().split())) v list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split ans = 0 ans 0 sum_d = 0 sum_d 0 d = [] d [] heapify(d) heapify d for i in range(1,K+1):
	for j in range(math.ceil(i/2),min(i+1,n+1)):
		for k in range(j+1):
			#print(i,j,k,j-k,i-j,ans)
			# 左から取る
			for l in range(k):
				heappush(d,v[l])
				sum_d += v[l]
			# 右から取る
			for l in range(j-k):
				heappush(d,v[-l-1])
				sum_d += v[-l-1]
			for l in range(i-j):
				sum_d -= heappop(d)
			ans = max(sum_d,ans)
			sum_d = 0
			d = []
			heapify(d) i range(1,K+1) range 1 K+1 K 1 for j in range(math.ceil(i/2),min(i+1,n+1)):
		for k in range(j+1):
			#print(i,j,k,j-k,i-j,ans)
			# 左から取る
			for l in range(k):
				heappush(d,v[l])
				sum_d += v[l]
			# 右から取る
			for l in range(j-k):
				heappush(d,v[-l-1])
				sum_d += v[-l-1]
			for l in range(i-j):
				sum_d -= heappop(d)
			ans = max(sum_d,ans)
			sum_d = 0
			d = []
			heapify(d) j range(math.ceil(i/2),min(i+1,n+1)) range math.ceil(i/2) math.ceil math ceil i/2 i 2 min(i+1,n+1) min i+1 i 1 n+1 n 1 for k in range(j+1):
			#print(i,j,k,j-k,i-j,ans)
			# 左から取る
			for l in range(k):
				heappush(d,v[l])
				sum_d += v[l]
			# 右から取る
			for l in range(j-k):
				heappush(d,v[-l-1])
				sum_d += v[-l-1]
			for l in range(i-j):
				sum_d -= heappop(d)
			ans = max(sum_d,ans)
			sum_d = 0
			d = []
			heapify(d) k range(j+1) range j+1 j 1 for l in range(k):
				heappush(d,v[l])
				sum_d += v[l]
			# 右から取る
			 l range(k) range k heappush(d,v[l]) heappush d v[l] v l sum_d += v[l] sum_d v[l] v l for l in range(j-k):
				heappush(d,v[-l-1])
				sum_d += v[-l-1]
			 l range(j-k) range j-k j k heappush(d,v[-l-1]) heappush d v[-l-1] v -l-1 -l l 1 sum_d += v[-l-1] sum_d v[-l-1] v -l-1 -l l 1 for l in range(i-j):
				sum_d -= heappop(d)
			 l range(i-j) range i-j i j sum_d -= heappop(d) sum_d heappop(d) heappop d ans = max(sum_d,ans) ans max(sum_d,ans) max sum_d ans sum_d = 0 sum_d 0 d = [] d [] heapify(d) heapify d print(ans) print ans sum_d -= heappop(d) heappop(d) sum_d v = list(map(int,input().split())) list(map(int,input().split())) v sum_d = 0 0 sum_d ans = 0 0 ans sum_d = 0 0 sum_d K = map(int,input().split()) map(int,input().split()) K sum_d += v[-l-1] v[-l-1] sum_d ans = max(sum_d,ans) max(sum_d,ans) ans d = [] [] d sum_d += v[l] v[l] sum_d d = [] [] d n,K = map(int,input().split()) map(int,input().split()) n
from heapq import heapify,heappush,heappop
import math

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

ans = 0
sum_d = 0
d = []
heapify(d)
# i = 操作回数
# j = 取る回数 (i-j = 捨てる回数)
# k = 左から取る回数 (j-k = 右から取る回数)
for i in range(1,K+1):
	for j in range(math.ceil(i/2),min(i+1,n+1)):
		for k in range(j+1):
			#print(i,j,k,j-k,i-j,ans)
			# 左から取る
			for l in range(k):
				heappush(d,v[l])
				sum_d += v[l]
			# 右から取る
			for l in range(j-k):
				heappush(d,v[-l-1])
				sum_d += v[-l-1]
			for l in range(i-j):
				sum_d -= heappop(d)
			ans = max(sum_d,ans)
			sum_d = 0
			d = []
			heapify(d)
print(ans)
