from collections import deque

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

m = min(k, n)
res = 0
for A in range(0, m + 1):
    for B in range(0, m + 1):
        if A + B > m:
            continue
        if B == 0:
            L = v[:A]
        else:
            L = v[:A] + v[-B:]
        L.sort()
        L = deque(L)
        num = k - (A + B)
        for i in range(num):
            if len(L) == 0:
                continue
            elif len(L) == 1:
                if L[0] < 0:
                    L.pop()
            else:
                Remove = L.popleft()
                if Remove > 0:
                    L.appendleft(Remove)
        res = max(res, sum(L))

print(res) from collections import deque 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 m = min(k, n) m min(k, n) min k n res = 0 res 0 for A in range(0, m + 1):
    for B in range(0, m + 1):
        if A + B > m:
            continue
        if B == 0:
            L = v[:A]
        else:
            L = v[:A] + v[-B:]
        L.sort()
        L = deque(L)
        num = k - (A + B)
        for i in range(num):
            if len(L) == 0:
                continue
            elif len(L) == 1:
                if L[0] < 0:
                    L.pop()
            else:
                Remove = L.popleft()
                if Remove > 0:
                    L.appendleft(Remove)
        res = max(res, sum(L)) A range(0, m + 1) range 0 m + 1 m 1 for B in range(0, m + 1):
        if A + B > m:
            continue
        if B == 0:
            L = v[:A]
        else:
            L = v[:A] + v[-B:]
        L.sort()
        L = deque(L)
        num = k - (A + B)
        for i in range(num):
            if len(L) == 0:
                continue
            elif len(L) == 1:
                if L[0] < 0:
                    L.pop()
            else:
                Remove = L.popleft()
                if Remove > 0:
                    L.appendleft(Remove)
        res = max(res, sum(L)) B range(0, m + 1) range 0 m + 1 m 1 if A + B > m:
            continue
         A + B > m A + B A B m continue if B == 0:
            L = v[:A]
        else:
            L = v[:A] + v[-B:]
         B == 0 B 0 L = v[:A] L v[:A] v :A A L = v[:A] + v[-B:] L v[:A] + v[-B:] v[:A] v :A A v[-B:] v -B: -B B L.sort() L.sort L sort L = deque(L) L deque(L) deque L num = k - (A + B) num k - (A + B) k A + B A B for i in range(num):
            if len(L) == 0:
                continue
            elif len(L) == 1:
                if L[0] < 0:
                    L.pop()
            else:
                Remove = L.popleft()
                if Remove > 0:
                    L.appendleft(Remove)
         i range(num) range num if len(L) == 0:
                continue
            elif len(L) == 1:
                if L[0] < 0:
                    L.pop()
            else:
                Remove = L.popleft()
                if Remove > 0:
                    L.appendleft(Remove)
         len(L) == 0 len(L) len L 0 continue elif len(L) == 1:
                if L[0] < 0:
                    L.pop()
             len(L) == 1 len(L) len L 1 if L[0] < 0:
                    L.pop()
             L[0] < 0 L[0] L 0 0 L.pop() L.pop L pop Remove = L.popleft() Remove L.popleft() L.popleft L popleft if Remove > 0:
                    L.appendleft(Remove)
         Remove > 0 Remove 0 L.appendleft(Remove) L.appendleft L appendleft Remove res = max(res, sum(L)) res max(res, sum(L)) max res sum(L) sum L print(res) print res Remove = L.popleft() L.popleft() Remove num = k - (A + B) k - (A + B) num res = max(res, sum(L)) max(res, sum(L)) res v = list(map(int, input().split())) list(map(int, input().split())) v n, k = map(int, input().split()) map(int, input().split()) n res = 0 0 res L = v[:A] + v[-B:] v[:A] + v[-B:] L L = v[:A] v[:A] L m = min(k, n) min(k, n) m L = deque(L) deque(L) L k = map(int, input().split()) map(int, input().split()) k
from collections import deque

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

m = min(k, n)
res = 0
for A in range(0, m + 1):
    for B in range(0, m + 1):
        if A + B > m:
            continue
        if B == 0:
            L = v[:A]
        else:
            L = v[:A] + v[-B:]
        L.sort()
        L = deque(L)
        num = k - (A + B)
        for i in range(num):
            if len(L) == 0:
                continue
            elif len(L) == 1:
                if L[0] < 0:
                    L.pop()
            else:
                Remove = L.popleft()
                if Remove > 0:
                    L.appendleft(Remove)
        res = max(res, sum(L))

print(res)