from collections import deque
from heapq import heappush, heappop
from copy import deepcopy

n, k = map(int, input().split())
v = list(map(int, input().split()))

dq = deque(v)

ans = 0
for ab in range(k + 1):
    cd = k - ab
    for a in range(ab + 1):
        hp = []
        cp = deepcopy(dq)

        b = ab - a
        for _ in range(a):
            if cp:
                val = cp.popleft()
                heappush(hp, val)
        for _ in range(b):
            if cp:
                val = cp.pop()
                heappush(hp, val)

        for _ in range(cd):
            if not hp or hp[0] >= 0:
                break
            heappop(hp)

        ans = max(ans, sum(hp))

print(ans) from collections import deque from heapq import heappush, heappop from copy import deepcopy 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 dq = deque(v) dq deque(v) deque v ans = 0 ans 0 for ab in range(k + 1):
    cd = k - ab
    for a in range(ab + 1):
        hp = []
        cp = deepcopy(dq)

        b = ab - a
        for _ in range(a):
            if cp:
                val = cp.popleft()
                heappush(hp, val)
        for _ in range(b):
            if cp:
                val = cp.pop()
                heappush(hp, val)

        for _ in range(cd):
            if not hp or hp[0] >= 0:
                break
            heappop(hp)

        ans = max(ans, sum(hp)) ab range(k + 1) range k + 1 k 1 cd = k - ab cd k - ab k ab for a in range(ab + 1):
        hp = []
        cp = deepcopy(dq)

        b = ab - a
        for _ in range(a):
            if cp:
                val = cp.popleft()
                heappush(hp, val)
        for _ in range(b):
            if cp:
                val = cp.pop()
                heappush(hp, val)

        for _ in range(cd):
            if not hp or hp[0] >= 0:
                break
            heappop(hp)

        ans = max(ans, sum(hp)) a range(ab + 1) range ab + 1 ab 1 hp = [] hp [] cp = deepcopy(dq) cp deepcopy(dq) deepcopy dq b = ab - a b ab - a ab a for _ in range(a):
            if cp:
                val = cp.popleft()
                heappush(hp, val)
         _ range(a) range a if cp:
                val = cp.popleft()
                heappush(hp, val)
         cp val = cp.popleft() val cp.popleft() cp.popleft cp popleft heappush(hp, val) heappush hp val for _ in range(b):
            if cp:
                val = cp.pop()
                heappush(hp, val)

         _ range(b) range b if cp:
                val = cp.pop()
                heappush(hp, val)

         cp val = cp.pop() val cp.pop() cp.pop cp pop heappush(hp, val) heappush hp val for _ in range(cd):
            if not hp or hp[0] >= 0:
                break
            heappop(hp)

         _ range(cd) range cd if not hp or hp[0] >= 0:
                break
             not hp or hp[0] >= 0 not hp hp hp[0] >= 0 hp[0] hp 0 0 break heappop(hp) heappop hp ans = max(ans, sum(hp)) ans max(ans, sum(hp)) max ans sum(hp) sum hp print(ans) print ans ans = max(ans, sum(hp)) max(ans, sum(hp)) ans v = list(map(int, input().split())) list(map(int, input().split())) v ans = 0 0 ans cd = k - ab k - ab cd dq = deque(v) deque(v) dq cp = deepcopy(dq) deepcopy(dq) cp hp = [] [] hp val = cp.pop() cp.pop() val k = map(int, input().split()) map(int, input().split()) k val = cp.popleft() cp.popleft() val n, k = map(int, input().split()) map(int, input().split()) n b = ab - a ab - a b
from collections import deque
from heapq import heappush, heappop
from copy import deepcopy

n, k = map(int, input().split())
v = list(map(int, input().split()))

dq = deque(v)

ans = 0
for ab in range(k + 1):
    cd = k - ab
    for a in range(ab + 1):
        hp = []
        cp = deepcopy(dq)

        b = ab - a
        for _ in range(a):
            if cp:
                val = cp.popleft()
                heappush(hp, val)
        for _ in range(b):
            if cp:
                val = cp.pop()
                heappush(hp, val)

        for _ in range(cd):
            if not hp or hp[0] >= 0:
                break
            heappop(hp)

        ans = max(ans, sum(hp))

print(ans)
