from collections import deque



n, k = list(map(int, input().split()))

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



ans = 0

for t in range(k//2 + 1):

    hand = deque(V[:k-t])

    rest = deque(V[k-t:])

    cur_sum = sum(hand)



    for j in range(k-t+1):

        put_back = 0

        rev_hand = sorted(hand)



        for i in range(t):

            jewel = rev_hand[i]

            if jewel < 0:

                put_back -= jewel

            else:

                break



        candidate = cur_sum + put_back

        ans = max(candidate, ans)



        to_rest = hand.pop()

        rest.appendleft(to_rest)

        to_hand = rest.pop()

        hand.appendleft(to_hand)



        cur_sum += to_hand - to_rest



print(ans)
n, k = list(map(int, input().split()))

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





def get_jewelries(box, left_pop, right_pop):

    if left_pop + right_pop >= len(box):

        return box[:]



    left = box[:left_pop]

    right = box[-right_pop:] if right_pop else []



    return left + right





candidates = []

pop_max = min(k, n)

for pop_count in range(pop_max+1):

    residue = k - pop_count



    for left_pop in range(pop_count+1):

        right_pop = pop_count - left_pop

        jewelries = get_jewelries(V, left_pop, right_pop)

        jewelries.sort(reverse=True)



        for _ in range(residue):

            if not jewelries:

                break

            if jewelries[-1] < 0:

                jewelries.pop()



        value = sum(jewelries)

        candidates.append(value)



print((max(candidates)))

from collections import deque

n, k = list(map(int, input().split()))
V = list(map(int, input().split()))
ans = 0
for t in range(k // 2 + 1):
    hand = deque(V[: k - t])
    rest = deque(V[k - t :])
    cur_sum = sum(hand)
    for j in range(k - t + 1):
        put_back = 0
        rev_hand = sorted(hand)
        for i in range(t):
            jewel = rev_hand[i]
            if jewel < 0:
                put_back -= jewel
            else:
                break
        candidate = cur_sum + put_back
        ans = max(candidate, ans)
        to_rest = hand.pop()
        rest.appendleft(to_rest)
        to_hand = rest.pop()
        hand.appendleft(to_hand)
        cur_sum += to_hand - to_rest
print(ans)

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


def get_jewelries(box, left_pop, right_pop):
    if left_pop + right_pop >= len(box):
        return box[:]
    left = box[:left_pop]
    right = box[-right_pop:] if right_pop else []
    return left + right


candidates = []
pop_max = min(k, n)
for pop_count in range(pop_max + 1):
    residue = k - pop_count
    for left_pop in range(pop_count + 1):
        right_pop = pop_count - left_pop
        jewelries = get_jewelries(V, left_pop, right_pop)
        jewelries.sort(reverse=True)
        for _ in range(residue):
            if not jewelries:
                break
            if jewelries[-1] < 0:
                jewelries.pop()
        value = sum(jewelries)
        candidates.append(value)
print((max(candidates)))

-from collections import deque - -ans = 0 -for t in range(k // 2 + 1): -    hand = deque(V[: k - t]) -    rest = deque(V[k - t :]) -    cur_sum = sum(hand) -    for j in range(k - t + 1): -        put_back = 0 -        rev_hand = sorted(hand) -        for i in range(t): -            jewel = rev_hand[i] -            if jewel < 0: -                put_back -= jewel -            else: + + +def get_jewelries(box, left_pop, right_pop): +    if left_pop + right_pop >= len(box): +        return box[:] +    left = box[:left_pop] +    right = box[-right_pop:] if right_pop else [] +    return left + right + + +candidates = [] +pop_max = min(k, n) +for pop_count in range(pop_max + 1): +    residue = k - pop_count +    for left_pop in range(pop_count + 1): +        right_pop = pop_count - left_pop +        jewelries = get_jewelries(V, left_pop, right_pop) +        jewelries.sort(reverse=True) +        for _ in range(residue): +            if not jewelries: -        candidate = cur_sum + put_back -        ans = max(candidate, ans) -        to_rest = hand.pop() -        rest.appendleft(to_rest) -        to_hand = rest.pop() -        hand.appendleft(to_hand) -        cur_sum += to_hand - to_rest -print(ans) +            if jewelries[-1] < 0: +                jewelries.pop() +        value = sum(jewelries) +        candidates.append(value) +print((max(candidates)))