import heapq
N, K = map(int, input().split())
d = [int(x) for x in map(int, input().split())]
e = d[::-1]

res = 0
num = min(N, K)
for i in range(num + 1):
    for j in range(num - i, -1, -1):
        s = d[:i] + e[:j]
        if not s:
            continue
        heapq.heapify(s)
        res = max(res, sum(s))
        l = min(i + j, K - (i + j))
        while l:
            heapq.heappop(s)
            res = max(res, sum(s))
            l -= 1
print(res) import heapq heapq N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K int(x) for x in map(int, input().split()) for x in map(int, input().split()) x map(int, input().split()) map int input().split() ().split () input split for x in map(int, input().split()) int(x) int x d = [int(x) for x in map(int, input().split())] d [int(x) for x in map(int, input().split())] e = d[::-1] e d[::-1] d ::-1 -1 res = 0 res 0 num = min(N, K) num min(N, K) min N K for i in range(num + 1):
    for j in range(num - i, -1, -1):
        s = d[:i] + e[:j]
        if not s:
            continue
        heapq.heapify(s)
        res = max(res, sum(s))
        l = min(i + j, K - (i + j))
        while l:
            heapq.heappop(s)
            res = max(res, sum(s))
            l -= 1 i range(num + 1) range num + 1 num 1 for j in range(num - i, -1, -1):
        s = d[:i] + e[:j]
        if not s:
            continue
        heapq.heapify(s)
        res = max(res, sum(s))
        l = min(i + j, K - (i + j))
        while l:
            heapq.heappop(s)
            res = max(res, sum(s))
            l -= 1 j range(num - i, -1, -1) range num - i num i -1 -1 s = d[:i] + e[:j] s d[:i] + e[:j] d[:i] d :i i e[:j] e :j j if not s:
            continue
         not s s continue heapq.heapify(s) heapq.heapify heapq heapify s res = max(res, sum(s)) res max(res, sum(s)) max res sum(s) sum s l = min(i + j, K - (i + j)) l min(i + j, K - (i + j)) min i + j i j K - (i + j) K i + j i j while l:
            heapq.heappop(s)
            res = max(res, sum(s))
            l -= 1 l heapq.heappop(s) heapq.heappop heapq heappop s res = max(res, sum(s)) res max(res, sum(s)) max res sum(s) sum s l -= 1 l 1 print(res) print res N, K = map(int, input().split()) map(int, input().split()) N l -= 1 1 l num = min(N, K) min(N, K) num res = max(res, sum(s)) max(res, sum(s)) res res = 0 0 res res = max(res, sum(s)) max(res, sum(s)) res l = min(i + j, K - (i + j)) min(i + j, K - (i + j)) l K = map(int, input().split()) map(int, input().split()) K s = d[:i] + e[:j] d[:i] + e[:j] s e = d[::-1] d[::-1] e d = [int(x) for x in map(int, input().split())] [int(x) for x in map(int, input().split())] d
import heapq
N, K = map(int, input().split())
d = [int(x) for x in map(int, input().split())]
e = d[::-1]

res = 0
num = min(N, K)
for i in range(num + 1):
    for j in range(num - i, -1, -1):
        s = d[:i] + e[:j]
        if not s:
            continue
        heapq.heapify(s)
        res = max(res, sum(s))
        l = min(i + j, K - (i + j))
        while l:
            heapq.heappop(s)
            res = max(res, sum(s))
            l -= 1
print(res)
