import sys



input = sys.stdin.readline





def main():

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

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

    _V = V[::-1]



    ans = 0

    for k in range(K + 1):

        n_pop = min(k, N)

        n_push = K - n_pop

        for i in range(n_pop + 1):

            l, r = i, n_pop - i

            L = V[:l] + _V[:r]

            L.sort()

            for j in range(min(n_pop, n_push)):

                if L[j] < 0:

                    L[j] = 0

                else:

                    break

            v = sum(L)

            if v > ans:

                ans = v



    print(ans)





if __name__ == "__main__":

    main()

import sys



input = sys.stdin.readline





def main():

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

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



    ans = -float("inf")

    for t in range(min(N, K) + 1):

        s = K - t

        for l in range(t + 1):

            r = t - l

            gem = V[:l]

            gem += V[-r:] if r != 0 else []

            gem.sort()

            value = sum(gem)

            for i in range(min(s, t)):

                if gem[i] < 0:

                    value -= gem[i]

                else:

                    break

            ans = max(ans, value)



    print(ans)





if __name__ == "__main__":

    main()

import sys

input = sys.stdin.readline


def main():
    N, K = list(map(int, input().split()))
    V = list(map(int, input().split()))
    _V = V[::-1]
    ans = 0
    for k in range(K + 1):
        n_pop = min(k, N)
        n_push = K - n_pop
        for i in range(n_pop + 1):
            l, r = i, n_pop - i
            L = V[:l] + _V[:r]
            L.sort()
            for j in range(min(n_pop, n_push)):
                if L[j] < 0:
                    L[j] = 0
                else:
                    break
            v = sum(L)
            if v > ans:
                ans = v
    print(ans)


if __name__ == "__main__":
    main()

import sys

input = sys.stdin.readline


def main():
    N, K = list(map(int, input().split()))
    V = list(map(int, input().split()))
    ans = -float("inf")
    for t in range(min(N, K) + 1):
        s = K - t
        for l in range(t + 1):
            r = t - l
            gem = V[:l]
            gem += V[-r:] if r != 0 else []
            gem.sort()
            value = sum(gem)
            for i in range(min(s, t)):
                if gem[i] < 0:
                    value -= gem[i]
                else:
                    break
            ans = max(ans, value)
    print(ans)


if __name__ == "__main__":
    main()

-    _V = V[::-1] -    ans = 0 -    for k in range(K + 1): -        n_pop = min(k, N) -        n_push = K - n_pop -        for i in range(n_pop + 1): -            l, r = i, n_pop - i -            L = V[:l] + _V[:r] -            L.sort() -            for j in range(min(n_pop, n_push)): -                if L[j] < 0: -                    L[j] = 0 +    ans = -float("inf") +    for t in range(min(N, K) + 1): +        s = K - t +        for l in range(t + 1): +            r = t - l +            gem = V[:l] +            gem += V[-r:] if r != 0 else [] +            gem.sort() +            value = sum(gem) +            for i in range(min(s, t)): +                if gem[i] < 0: +                    value -= gem[i] -            v = sum(L) -            if v > ans: -                ans = v +            ans = max(ans, value)