import heapq 

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

A = list(map(int,input().split()))

m = min(N,K) #KがNより大きい場合を防ぐ

M = 0 

for i in range(m+1):

    for j in range(m-i+1):

        A1 = A[:i] #前から取る

        A3 = A[N-j:] #後ろから取る

        new_A = A1+A3

        new_A.sort(reverse=True)

        h = K - len(new_A)

        while h >0 and new_A:

            if new_A[-1] > 0:

                break

            else:

                new_A.pop()

                h-=1

        M = max(M,sum(new_A))

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

A = list(map(int,input().split()))

m = min(N,K)

M = 0 

for i in range(m+1):

    for j in range(m-i+1):

        v=A[:i]+A[N-j:]

        v.sort()

        v=v[::-1]

        h=K-len(v)

        while h >0 and v:

            if v[-1] > 0:

                break

            else:

                v.pop()

                h-=1

        M=max(M,sum(v))

print(M)
import heapq

N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
m = min(N, K)  # KがNより大きい場合を防ぐ
M = 0
for i in range(m + 1):
    for j in range(m - i + 1):
        A1 = A[:i]  # 前から取る
        A3 = A[N - j :]  # 後ろから取る
        new_A = A1 + A3
        new_A.sort(reverse=True)
        h = K - len(new_A)
        while h > 0 and new_A:
            if new_A[-1] > 0:
                break
            else:
                new_A.pop()
                h -= 1
        M = max(M, sum(new_A))
print(M)

N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
m = min(N, K)
M = 0
for i in range(m + 1):
    for j in range(m - i + 1):
        v = A[:i] + A[N - j :]
        v.sort()
        v = v[::-1]
        h = K - len(v)
        while h > 0 and v:
            if v[-1] > 0:
                break
            else:
                v.pop()
                h -= 1
        M = max(M, sum(v))
print(M)

-import heapq - -m = min(N, K)  # KがNより大きい場合を防ぐ +m = min(N, K) -        A1 = A[:i]  # 前から取る -        A3 = A[N - j :]  # 後ろから取る -        new_A = A1 + A3 -        new_A.sort(reverse=True) -        h = K - len(new_A) -        while h > 0 and new_A: -            if new_A[-1] > 0: +        v = A[:i] + A[N - j :] +        v.sort() +        v = v[::-1] +        h = K - len(v) +        while h > 0 and v: +            if v[-1] > 0: -                new_A.pop() +                v.pop() -        M = max(M, sum(new_A)) +        M = max(M, sum(v))