from heapq import heappop, heappush



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

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



ans = 0



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

    for right in range(min(N, K) - left + 1):

        heap = []

        for v in V[:left]:

            heappush(heap, v)



        for v in V[N - right:]:

            heappush(heap, v)



        cnt = left + right



        v = 0

        while cnt < K and len(heap) > 0:

            v = heappop(heap)



            if v >= 0:

                heappush(heap, v)

                break

            cnt += 1



        ans = max(ans, sum(heap))

print(ans)

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

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



maxi = 0

for i in range(min(N, K)):

    for o in range(i+2):

        hand = []

        for p in range(o):

            hand.append(V[p])

        for p in range(N-(i-o)-1, N):

            hand.append(V[p])

        hand.sort()

        for j in range(K-i-1):

            if len(hand) > 0:

                if hand[0] < 0:

                    del hand[0]

                else:

                    break

        ts = 0

        for k in hand:

            ts += k

        if ts > maxi:

            maxi = ts

print(maxi)
from heapq import heappop, heappush

N, K = list(map(int, input().split()))
V = list(map(int, input().split()))
ans = 0
for left in range(min(N, K) + 1):
    for right in range(min(N, K) - left + 1):
        heap = []
        for v in V[:left]:
            heappush(heap, v)
        for v in V[N - right :]:
            heappush(heap, v)
        cnt = left + right
        v = 0
        while cnt < K and len(heap) > 0:
            v = heappop(heap)
            if v >= 0:
                heappush(heap, v)
                break
            cnt += 1
        ans = max(ans, sum(heap))
print(ans)

N, K = list(map(int, input().strip().split()))
V = list(map(int, input().strip().split()))
maxi = 0
for i in range(min(N, K)):
    for o in range(i + 2):
        hand = []
        for p in range(o):
            hand.append(V[p])
        for p in range(N - (i - o) - 1, N):
            hand.append(V[p])
        hand.sort()
        for j in range(K - i - 1):
            if len(hand) > 0:
                if hand[0] < 0:
                    del hand[0]
                else:
                    break
        ts = 0
        for k in hand:
            ts += k
        if ts > maxi:
            maxi = ts
print(maxi)

-from heapq import heappop, heappush - -N, K = list(map(int, input().split())) -V = list(map(int, input().split())) -ans = 0 -for left in range(min(N, K) + 1): -    for right in range(min(N, K) - left + 1): -        heap = [] -        for v in V[:left]: -            heappush(heap, v) -        for v in V[N - right :]: -            heappush(heap, v) -        cnt = left + right -        v = 0 -        while cnt < K and len(heap) > 0: -            v = heappop(heap) -            if v >= 0: -                heappush(heap, v) -                break -            cnt += 1 -        ans = max(ans, sum(heap)) -print(ans) +N, K = list(map(int, input().strip().split())) +V = list(map(int, input().strip().split())) +maxi = 0 +for i in range(min(N, K)): +    for o in range(i + 2): +        hand = [] +        for p in range(o): +            hand.append(V[p]) +        for p in range(N - (i - o) - 1, N): +            hand.append(V[p]) +        hand.sort() +        for j in range(K - i - 1): +            if len(hand) > 0: +                if hand[0] < 0: +                    del hand[0] +                else: +                    break +        ts = 0 +        for k in hand: +            ts += k +        if ts > maxi: +            maxi = ts +print(maxi)