# ABC128D - equeue

# exhaustive search

def main():

    N, K = tuple(map(int, input().rstrip().split()))

    V = tuple(map(int, input().rstrip().split()))

    lim = min(N, K)

    ans = set()

    for l in range(N + 1):

        for r in range(N + 1):

            if l + r > lim:

                break

            # picked jewels (l from left, r from right)

            cur = sorted(V[:l] + V[-r:]) if r != 0 else sorted(V[:l])

            neg = sum(i < 0 for i in cur)

            rest = K - (l + r)

            # return unworthy ones as much as possible

            if neg > rest:

                ans |= {sum(cur[rest:])}

            else:

                ans |= {sum(i for i in cur if i >= 0)}

    print((max(ans)))





if __name__ == "__main__":

    main()
# D - equeue

def main():

    N, K, *V = list(map(int, open(0).read().split()))

    lim = min(N, K)

    available_choices = set()



    for l in range(lim + 1):

        for r in range(lim + 1):

            if l + r > lim:

                break



            choice = V[:l] + V[-r:] if r else V[:l]

            cnt_negatives = sum(i < 0 for i in choice)

            cnt_rest = K - (l + r)



            if cnt_negatives > cnt_rest:

                choice.sort()

                available_choices.add(sum(choice[cnt_rest:]))

            else:

                available_choices.add(sum(i for i in choice if i > 0))



    print((max(available_choices)))





if __name__ == "__main__":

    main()

# ABC128D - equeue
# exhaustive search
def main():
    N, K = tuple(map(int, input().rstrip().split()))
    V = tuple(map(int, input().rstrip().split()))
    lim = min(N, K)
    ans = set()
    for l in range(N + 1):
        for r in range(N + 1):
            if l + r > lim:
                break
            # picked jewels (l from left, r from right)
            cur = sorted(V[:l] + V[-r:]) if r != 0 else sorted(V[:l])
            neg = sum(i < 0 for i in cur)
            rest = K - (l + r)
            # return unworthy ones as much as possible
            if neg > rest:
                ans |= {sum(cur[rest:])}
            else:
                ans |= {sum(i for i in cur if i >= 0)}
    print((max(ans)))


if __name__ == "__main__":
    main()

# D - equeue
def main():
    N, K, *V = list(map(int, open(0).read().split()))
    lim = min(N, K)
    available_choices = set()
    for l in range(lim + 1):
        for r in range(lim + 1):
            if l + r > lim:
                break
            choice = V[:l] + V[-r:] if r else V[:l]
            cnt_negatives = sum(i < 0 for i in choice)
            cnt_rest = K - (l + r)
            if cnt_negatives > cnt_rest:
                choice.sort()
                available_choices.add(sum(choice[cnt_rest:]))
            else:
                available_choices.add(sum(i for i in choice if i > 0))
    print((max(available_choices)))


if __name__ == "__main__":
    main()

-# ABC128D - equeue -# exhaustive search +# D - equeue -    N, K = tuple(map(int, input().rstrip().split())) -    V = tuple(map(int, input().rstrip().split())) +    N, K, *V = list(map(int, open(0).read().split())) -    ans = set() -    for l in range(N + 1): -        for r in range(N + 1): +    available_choices = set() +    for l in range(lim + 1): +        for r in range(lim + 1): -            # picked jewels (l from left, r from right) -            cur = sorted(V[:l] + V[-r:]) if r != 0 else sorted(V[:l]) -            neg = sum(i < 0 for i in cur) -            rest = K - (l + r) -            # return unworthy ones as much as possible -            if neg > rest: -                ans |= {sum(cur[rest:])} +            choice = V[:l] + V[-r:] if r else V[:l] +            cnt_negatives = sum(i < 0 for i in choice) +            cnt_rest = K - (l + r) +            if cnt_negatives > cnt_rest: +                choice.sort() +                available_choices.add(sum(choice[cnt_rest:])) -                ans |= {sum(i for i in cur if i >= 0)} -    print((max(ans))) +                available_choices.add(sum(i for i in choice if i > 0)) +    print((max(available_choices)))