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

ans = 0
num = min(k, n)
for a in range(num + 1):
    for b in range(num + 1 - a):
        trash = k - (a + b)
        get = v[:a] + v[-b:] if b != 0 else v[:a]
        heapify(get)
        for _ in range(min(trash, a + b)):
            temp = heappop(get)
            if temp > 0:
                heappush(get, temp)
                break
        SUM = sum(get)
        if ans < SUM:ans = SUM
print(ans) from collections import deque from heapq import * 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 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)
        get = v[:a] + v[-b:] if b != 0 else v[:a]
        heapify(get)
        for _ in range(min(trash, a + b)):
            temp = heappop(get)
            if temp > 0:
                heappush(get, temp)
                break
        SUM = sum(get)
        if ans < SUM:ans = SUM a range(num + 1) range num + 1 num 1 for b in range(num + 1 - a):
        trash = k - (a + b)
        get = v[:a] + v[-b:] if b != 0 else v[:a]
        heapify(get)
        for _ in range(min(trash, a + b)):
            temp = heappop(get)
            if temp > 0:
                heappush(get, temp)
                break
        SUM = sum(get)
        if ans < SUM:ans = SUM 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 get = v[:a] + v[-b:] if b != 0 else v[:a] get v[:a] + v[-b:] if b != 0 else v[:a] b != 0 b 0 v[:a] + v[-b:] v[:a] v :a a v[-b:] v -b: -b b v[:a] v :a a heapify(get) heapify get for _ in range(min(trash, a + b)):
            temp = heappop(get)
            if temp > 0:
                heappush(get, temp)
                break
         _ range(min(trash, a + b)) range min(trash, a + b) min trash a + b a b temp = heappop(get) temp heappop(get) heappop get if temp > 0:
                heappush(get, temp)
                break
         temp > 0 temp 0 heappush(get, temp) heappush get temp break SUM = sum(get) SUM sum(get) sum get if ans < SUM:a ans < SUM ans SUM print(ans) print ans n, k = map(int, input().split()) map(int, input().split()) n ans = 0 0 ans SUM = sum(get) sum(get) SUM num = min(k, n) min(k, n) num temp = heappop(get) heappop(get) temp v = list(map(int, input().split())) list(map(int, input().split())) v get = v[:a] + v[-b:] if b != 0 else v[:a] v[:a] + v[-b:] if b != 0 else v[:a] get k = map(int, input().split()) map(int, input().split()) k trash = k - (a + b) k - (a + b) trash
from collections import deque
from heapq import *
n, k = map(int, input().split())
v = list(map(int, input().split()))

ans = 0
num = min(k, n)
for a in range(num + 1):
    for b in range(num + 1 - a):
        trash = k - (a + b)
        get = v[:a] + v[-b:] if b != 0 else v[:a]
        heapify(get)
        for _ in range(min(trash, a + b)):
            temp = heappop(get)
            if temp > 0:
                heappush(get, temp)
                break
        SUM = sum(get)
        if ans < SUM:ans = SUM
print(ans)