# coding: utf-8

import sys

sys.setrecursionlimit(10 ** 7)



sr = lambda: sys.stdin.readline().rstrip()

ir = lambda: int(sr())

lr = lambda: list(map(int, sr().split()))



# 持っている宝石をDに詰めるのは最後

N, K = lr()

V = lr()

answer = 0

V = V[::-1] + V[::-1]

for x in range(K+1):

    # x回Dから取り出す、K-x回戻すことが可能

    x = min(x, N)

    for i in range(N-1, N-1+x+1):

        take = V[i-x+1:i+1]

        take.sort(reverse=True)

        result = sum(take)

        if result > answer:

            answer = result

        for j in range(K-x):

            if take:

                result -= take.pop()

                if result > answer:

                    answer = result



print(answer)

# coding: utf-8

import sys



sr = lambda: sys.stdin.readline().rstrip()

ir = lambda: int(sr())

lr = lambda: list(map(int, sr().split()))



N, K = lr()

V = lr()

scores = [0]

limit = min(N, K)

for i in range(limit+1): # iは左から取り出す個数

    for j in range(limit-i+1): # jは右から取り出す個数

        stone = V[:i] + V[N-j:]

        stone.sort()

        result = sum(stone)

        scores.append(result)

        for k in range(min(len(stone), K-(i+j))): # kは戻す個数

            if stone[k] > 0:

                break

            result -= stone[k] 

            scores.append(result)



answer = max(scores)

print(answer)

# 07
# coding: utf-8
import sys

sys.setrecursionlimit(10**7)
sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
# 持っている宝石をDに詰めるのは最後
N, K = lr()
V = lr()
answer = 0
V = V[::-1] + V[::-1]
for x in range(K + 1):
    # x回Dから取り出す、K-x回戻すことが可能
    x = min(x, N)
    for i in range(N - 1, N - 1 + x + 1):
        take = V[i - x + 1 : i + 1]
        take.sort(reverse=True)
        result = sum(take)
        if result > answer:
            answer = result
        for j in range(K - x):
            if take:
                result -= take.pop()
                if result > answer:
                    answer = result
print(answer)

# coding: utf-8
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))
N, K = lr()
V = lr()
scores = [0]
limit = min(N, K)
for i in range(limit + 1):  # iは左から取り出す個数
    for j in range(limit - i + 1):  # jは右から取り出す個数
        stone = V[:i] + V[N - j :]
        stone.sort()
        result = sum(stone)
        scores.append(result)
        for k in range(min(len(stone), K - (i + j))):  # kは戻す個数
            if stone[k] > 0:
                break
            result -= stone[k]
            scores.append(result)
answer = max(scores)
print(answer)
# 07

-sys.setrecursionlimit(10**7) -# 持っている宝石をDに詰めるのは最後 -answer = 0 -V = V[::-1] + V[::-1] -for x in range(K + 1): -    # x回Dから取り出す、K-x回戻すことが可能 -    x = min(x, N) -    for i in range(N - 1, N - 1 + x + 1): -        take = V[i - x + 1 : i + 1] -        take.sort(reverse=True) -        result = sum(take) -        if result > answer: -            answer = result -        for j in range(K - x): -            if take: -                result -= take.pop() -                if result > answer: -                    answer = result +scores = [0] +limit = min(N, K) +for i in range(limit + 1):  # iは左から取り出す個数 +    for j in range(limit - i + 1):  # jは右から取り出す個数 +        stone = V[:i] + V[N - j :] +        stone.sort() +        result = sum(stone) +        scores.append(result) +        for k in range(min(len(stone), K - (i + j))):  # kは戻す個数 +            if stone[k] > 0: +                break +            result -= stone[k] +            scores.append(result) +answer = max(scores) +# 07