
from collections import deque

N, K = map(int, input().split())
X = list(map(int, input().split()))

ans = 0
for a in range(K + 1):
    for b in range(K + 1 - a):
        if a + b > N:
            continue

        q = deque(X)
        tmp = []
        for _ in range(a):
            tmp.append(q.popleft())

        for _ in range(b):
            tmp.append(q.pop())

        tmp.sort(reverse=True)
        cnt = 0
        for _ in range(K - a - b):
            if tmp and tmp[-1] < 0:
                tmp.pop()

        ans = max(ans, sum(tmp))

print(ans) from collections import deque N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K X = list(map(int, input().split())) X list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split ans = 0 ans 0 for a in range(K + 1):
    for b in range(K + 1 - a):
        if a + b > N:
            continue

        q = deque(X)
        tmp = []
        for _ in range(a):
            tmp.append(q.popleft())

        for _ in range(b):
            tmp.append(q.pop())

        tmp.sort(reverse=True)
        cnt = 0
        for _ in range(K - a - b):
            if tmp and tmp[-1] < 0:
                tmp.pop()

        ans = max(ans, sum(tmp)) a range(K + 1) range K + 1 K 1 for b in range(K + 1 - a):
        if a + b > N:
            continue

        q = deque(X)
        tmp = []
        for _ in range(a):
            tmp.append(q.popleft())

        for _ in range(b):
            tmp.append(q.pop())

        tmp.sort(reverse=True)
        cnt = 0
        for _ in range(K - a - b):
            if tmp and tmp[-1] < 0:
                tmp.pop()

        ans = max(ans, sum(tmp)) b range(K + 1 - a) range K + 1 - a K + 1 K 1 a if a + b > N:
            continue

         a + b > N a + b a b N continue q = deque(X) q deque(X) deque X tmp = [] tmp [] for _ in range(a):
            tmp.append(q.popleft())

         _ range(a) range a tmp.append(q.popleft()) tmp.append tmp append q.popleft() q.popleft q popleft for _ in range(b):
            tmp.append(q.pop())

         _ range(b) range b tmp.append(q.pop()) tmp.append tmp append q.pop() q.pop q pop tmp.sort(reverse=True) tmp.sort tmp sort reverse=True reverse True cnt = 0 cnt 0 for _ in range(K - a - b):
            if tmp and tmp[-1] < 0:
                tmp.pop()

         _ range(K - a - b) range K - a - b K - a K a b if tmp and tmp[-1] < 0:
                tmp.pop()

         tmp and tmp[-1] < 0 tmp tmp[-1] < 0 tmp[-1] tmp -1 0 tmp.pop() tmp.pop tmp pop ans = max(ans, sum(tmp)) ans max(ans, sum(tmp)) max ans sum(tmp) sum tmp print(ans) print ans X = list(map(int, input().split())) list(map(int, input().split())) X K = map(int, input().split()) map(int, input().split()) K cnt = 0 0 cnt ans = 0 0 ans q = deque(X) deque(X) q tmp = [] [] tmp ans = max(ans, sum(tmp)) max(ans, sum(tmp)) ans N, K = map(int, input().split()) map(int, input().split()) N

from collections import deque

N, K = map(int, input().split())
X = list(map(int, input().split()))

ans = 0
for a in range(K + 1):
    for b in range(K + 1 - a):
        if a + b > N:
            continue

        q = deque(X)
        tmp = []
        for _ in range(a):
            tmp.append(q.popleft())

        for _ in range(b):
            tmp.append(q.pop())

        tmp.sort(reverse=True)
        cnt = 0
        for _ in range(K - a - b):
            if tmp and tmp[-1] < 0:
                tmp.pop()

        ans = max(ans, sum(tmp))

print(ans)
