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

res = 0
num = min(k, n)
for i in range(num + 1):
    for j in range(num + 1 - i):
        l = min(k - (i + j), i + j)
        s = d[:i] + d[-j:] if j != 0 else d[:i]
        heapify(s)
        for _ in range(l):
            tmp = heappop(s)
            if tmp > 0:
                heappush(s, tmp)
        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 i in range(num + 1):
    for j in range(num + 1 - i):
        l = min(k - (i + j), i + j)
        s = d[:i] + d[-j:] if j != 0 else d[:i]
        heapify(s)
        for _ in range(l):
            tmp = heappop(s)
            if tmp > 0:
                heappush(s, tmp)
        res = max(res, sum(s)) i range(num + 1) range num + 1 num 1 for j in range(num + 1 - i):
        l = min(k - (i + j), i + j)
        s = d[:i] + d[-j:] if j != 0 else d[:i]
        heapify(s)
        for _ in range(l):
            tmp = heappop(s)
            if tmp > 0:
                heappush(s, tmp)
        res = max(res, sum(s)) j range(num + 1 - i) range num + 1 - i num + 1 num 1 i l = min(k - (i + j), i + j) l min(k - (i + j), i + j) min k - (i + j) k i + j i j i + j i j s = d[:i] + d[-j:] if j != 0 else d[:i] s d[:i] + d[-j:] if j != 0 else d[:i] j != 0 j 0 d[:i] + d[-j:] d[:i] d :i i d[-j:] d -j: -j j d[:i] d :i i heapify(s) heapify s for _ in range(l):
            tmp = heappop(s)
            if tmp > 0:
                heappush(s, tmp)
         _ range(l) range l tmp = heappop(s) tmp heappop(s) heappop s if tmp > 0:
                heappush(s, tmp)
         tmp > 0 tmp 0 heappush(s, tmp) heappush s tmp res = max(res, sum(s)) res max(res, sum(s)) max res sum(s) sum s print(res) print res l = min(k - (i + j), i + j) min(k - (i + j), i + j) l tmp = heappop(s) heappop(s) tmp d = list(map(int, input().split())) list(map(int, input().split())) d res = 0 0 res k = map(int, input().split()) map(int, input().split()) k res = max(res, sum(s)) max(res, sum(s)) res n, k = map(int, input().split()) map(int, input().split()) n s = d[:i] + d[-j:] if j != 0 else d[:i] d[:i] + d[-j:] if j != 0 else d[:i] s num = min(k, n) min(k, n) num
from heapq import *
n, k = map(int, input().split())
d = list(map(int, input().split()))

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