from heapq import *
n, k = map(int, input().split())
d = list(map(int, input().split()))

res = 0
num = min(k, n)
for a in range(num + 1):
    for b in range(num + 1 - a):
        trash = k - (a + b)
        s = d[:a] + d[-b:] if b != 0 else d[:a]
        heapify(s)
        for _ in range(min(trash, a + b)):
            tmp = heappop(s)
            if tmp > 0:
                heappush(s, tmp)
                break
        res = max(res, sum(s))
print(res) from heapq import * n, k = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split k d = list(map(int, input().split())) d list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split res = 0 res 0 num = min(k, n) num min(k, n) min k n for a in range(num + 1):
    for b in range(num + 1 - a):
        trash = k - (a + b)
        s = d[:a] + d[-b:] if b != 0 else d[:a]
        heapify(s)
        for _ in range(min(trash, a + b)):
            tmp = heappop(s)
            if tmp > 0:
                heappush(s, tmp)
                break
        res = max(res, sum(s)) a range(num + 1) range num + 1 num 1 for b in range(num + 1 - a):
        trash = k - (a + b)
        s = d[:a] + d[-b:] if b != 0 else d[:a]
        heapify(s)
        for _ in range(min(trash, a + b)):
            tmp = heappop(s)
            if tmp > 0:
                heappush(s, tmp)
                break
        res = max(res, sum(s)) b range(num + 1 - a) range num + 1 - a num + 1 num 1 a trash = k - (a + b) trash k - (a + b) k a + b a b s = d[:a] + d[-b:] if b != 0 else d[:a] s d[:a] + d[-b:] if b != 0 else d[:a] b != 0 b 0 d[:a] + d[-b:] d[:a] d :a a d[-b:] d -b: -b b d[:a] d :a a heapify(s) heapify s for _ in range(min(trash, a + b)):
            tmp = heappop(s)
            if tmp > 0:
                heappush(s, tmp)
                break
         _ range(min(trash, a + b)) range min(trash, a + b) min trash a + b a b tmp = heappop(s) tmp heappop(s) heappop s if tmp > 0:
                heappush(s, tmp)
                break
         tmp > 0 tmp 0 heappush(s, tmp) heappush s tmp break res = max(res, sum(s)) res max(res, sum(s)) max res sum(s) sum s print(res) print res d = list(map(int, input().split())) list(map(int, input().split())) d n, k = map(int, input().split()) map(int, input().split()) n k = map(int, input().split()) map(int, input().split()) k trash = k - (a + b) k - (a + b) trash num = min(k, n) min(k, n) num s = d[:a] + d[-b:] if b != 0 else d[:a] d[:a] + d[-b:] if b != 0 else d[:a] s tmp = heappop(s) heappop(s) tmp res = max(res, sum(s)) max(res, sum(s)) res res = 0 0 res
from heapq import *
n, k = map(int, input().split())
d = list(map(int, input().split()))

res = 0
num = min(k, n)
for a in range(num + 1):
    for b in range(num + 1 - a):
        trash = k - (a + b)
        s = d[:a] + d[-b:] if b != 0 else d[:a]
        heapify(s)
        for _ in range(min(trash, a + b)):
            tmp = heappop(s)
            if tmp > 0:
                heappush(s, tmp)
                break
        res = max(res, sum(s))
print(res)
