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

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



answer = 0

limit = min(N, K)

for a in range(limit+1): #左から取り出す

    for b in range(limit-a+1): #右から取り出す

        for c in range(K-(a+b)+1):

            use = V[:a] + V[N-b:]

            use.sort()

            s = sum(use[c:])

            answer = max(answer, s)



print(answer)
import sys

sys.setrecursionlimit(10 ** 9)



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

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

V2 = V * 2

answer = 0

for i in range(1, min(N, K)+1): # iは宝石を取り出す個数

    for j in range(0, min(i, K-i)+1): # jは何個の宝石を手離すか

        for k in range(max(0, N-i), N+1): # V2の中で何番目から取り出すか

            use = V2[k:k+i]

            use.sort()

            s = sum(use[j:])

            if s > answer:

                answer = s



print(answer)

N, K = list(map(int, input().split()))
V = list(map(int, input().split()))
answer = 0
limit = min(N, K)
for a in range(limit + 1):  # 左から取り出す
    for b in range(limit - a + 1):  # 右から取り出す
        for c in range(K - (a + b) + 1):
            use = V[:a] + V[N - b :]
            use.sort()
            s = sum(use[c:])
            answer = max(answer, s)
print(answer)

import sys

sys.setrecursionlimit(10**9)
N, K = list(map(int, input().split()))
V = list(map(int, input().split()))
V2 = V * 2
answer = 0
for i in range(1, min(N, K) + 1):  # iは宝石を取り出す個数
    for j in range(0, min(i, K - i) + 1):  # jは何個の宝石を手離すか
        for k in range(max(0, N - i), N + 1):  # V2の中で何番目から取り出すか
            use = V2[k : k + i]
            use.sort()
            s = sum(use[j:])
            if s > answer:
                answer = s
print(answer)

+import sys + +sys.setrecursionlimit(10**9) +V2 = V * 2 -limit = min(N, K) -for a in range(limit + 1):  # 左から取り出す -    for b in range(limit - a + 1):  # 右から取り出す -        for c in range(K - (a + b) + 1): -            use = V[:a] + V[N - b :] +for i in range(1, min(N, K) + 1):  # iは宝石を取り出す個数 +    for j in range(0, min(i, K - i) + 1):  # jは何個の宝石を手離すか +        for k in range(max(0, N - i), N + 1):  # V2の中で何番目から取り出すか +            use = V2[k : k + i] -            s = sum(use[c:]) -            answer = max(answer, s) +            s = sum(use[j:]) +            if s > answer: +                answer = s