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

ans = 0
for i in range(n + 1):
    for j in range(n + 1):
        rest = k - i - j
        if i + j > n or rest < 0:
            continue
        tans = 0
        q = deque(v)
        poppen = []
        for _ in range(i):
            tans += q[0]
            heappush(poppen, q.popleft())
        for _ in range(j):
            tans += q[-1]
            heappush(poppen, q.pop())
        for _ in range(rest):
            if not poppen or poppen[0] >= 0:
                break
            tans -= heappop(poppen)
        ans = max(ans, tans)
print(ans) from collections import deque from heapq import heappush, heappop 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 for i in range(n + 1):
    for j in range(n + 1):
        rest = k - i - j
        if i + j > n or rest < 0:
            continue
        tans = 0
        q = deque(v)
        poppen = []
        for _ in range(i):
            tans += q[0]
            heappush(poppen, q.popleft())
        for _ in range(j):
            tans += q[-1]
            heappush(poppen, q.pop())
        for _ in range(rest):
            if not poppen or poppen[0] >= 0:
                break
            tans -= heappop(poppen)
        ans = max(ans, tans) i range(n + 1) range n + 1 n 1 for j in range(n + 1):
        rest = k - i - j
        if i + j > n or rest < 0:
            continue
        tans = 0
        q = deque(v)
        poppen = []
        for _ in range(i):
            tans += q[0]
            heappush(poppen, q.popleft())
        for _ in range(j):
            tans += q[-1]
            heappush(poppen, q.pop())
        for _ in range(rest):
            if not poppen or poppen[0] >= 0:
                break
            tans -= heappop(poppen)
        ans = max(ans, tans) j range(n + 1) range n + 1 n 1 rest = k - i - j rest k - i - j k - i k i j if i + j > n or rest < 0:
            continue
         i + j > n or rest < 0 i + j > n i + j i j n rest < 0 rest 0 continue tans = 0 tans 0 q = deque(v) q deque(v) deque v poppen = [] poppen [] for _ in range(i):
            tans += q[0]
            heappush(poppen, q.popleft())
         _ range(i) range i tans += q[0] tans q[0] q 0 heappush(poppen, q.popleft()) heappush poppen q.popleft() q.popleft q popleft for _ in range(j):
            tans += q[-1]
            heappush(poppen, q.pop())
         _ range(j) range j tans += q[-1] tans q[-1] q -1 heappush(poppen, q.pop()) heappush poppen q.pop() q.pop q pop for _ in range(rest):
            if not poppen or poppen[0] >= 0:
                break
            tans -= heappop(poppen)
         _ range(rest) range rest if not poppen or poppen[0] >= 0:
                break
             not poppen or poppen[0] >= 0 not poppen poppen poppen[0] >= 0 poppen[0] poppen 0 0 break tans -= heappop(poppen) tans heappop(poppen) heappop poppen ans = max(ans, tans) ans max(ans, tans) max ans tans print(ans) print ans ans = max(ans, tans) max(ans, tans) ans tans += q[0] q[0] tans v = list(map(int, input().split())) list(map(int, input().split())) v n, k = map(int, input().split()) map(int, input().split()) n ans = 0 0 ans tans -= heappop(poppen) heappop(poppen) tans rest = k - i - j k - i - j rest q = deque(v) deque(v) q tans += q[-1] q[-1] tans poppen = [] [] poppen tans = 0 0 tans k = map(int, input().split()) map(int, input().split()) k
from collections import deque
from heapq import heappush, heappop
n, k = map(int, input().split())
v = list(map(int, input().split()))

ans = 0
for i in range(n + 1):
    for j in range(n + 1):
        rest = k - i - j
        if i + j > n or rest < 0:
            continue
        tans = 0
        q = deque(v)
        poppen = []
        for _ in range(i):
            tans += q[0]
            heappush(poppen, q.popleft())
        for _ in range(j):
            tans += q[-1]
            heappush(poppen, q.pop())
        for _ in range(rest):
            if not poppen or poppen[0] >= 0:
                break
            tans -= heappop(poppen)
        ans = max(ans, tans)
print(ans)
