import heapq

from collections import deque

from copy import deepcopy



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

U = deque(list(map(int,input().split())))



Max = -10**9



for a in range(min(n,k)+1):

  for b in range(min(n,k)+1-a):

    

    V = deepcopy(U)

    Heap = []

    heapq.heapify(Heap)

    

    if a >= 1:

      for i in range(a):

        heapq.heappush(Heap,V.pop())

        

    if b >= 1:  

      for i in range(b):

        heapq.heappush(Heap,V.popleft())

        

    if k-a-b >= 1:

      for i in range(k-a-b):

        

        if len(Heap) >= 1:

          Min = heapq.heappop(Heap)

                 

          if Min >= 0:

            heapq.heappush(Heap,Min)

            break



    Max = max(Max,sum(Heap))

    

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

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



cum = [0]

rcum = [0]



for i in range(N):

    cum.append(cum[-1]+V[i])

    rcum.append(rcum[-1]+V[-1-i])



ans = 0



for i in range(N+1):

    for j in range(N+1):



        if i+j > min(N,K):

            continue



        value = cum[i]+rcum[j]

        neg = []



        for ii in range(i):

            if V[ii] < 0:

                neg.append(V[ii])



        for jj in range(j):

            if V[-1-jj] < 0:

                neg.append(V[-1-jj])



        neg.sort()



        value -= sum(neg[:min(K-i-j,len(neg))])



        ans = max(ans,value)



print(ans)
import heapq
from collections import deque
from copy import deepcopy

n, k = list(map(int, input().split()))
U = deque(list(map(int, input().split())))
Max = -(10**9)
for a in range(min(n, k) + 1):
    for b in range(min(n, k) + 1 - a):
        V = deepcopy(U)
        Heap = []
        heapq.heapify(Heap)
        if a >= 1:
            for i in range(a):
                heapq.heappush(Heap, V.pop())
        if b >= 1:
            for i in range(b):
                heapq.heappush(Heap, V.popleft())
        if k - a - b >= 1:
            for i in range(k - a - b):
                if len(Heap) >= 1:
                    Min = heapq.heappop(Heap)
                    if Min >= 0:
                        heapq.heappush(Heap, Min)
                        break
        Max = max(Max, sum(Heap))
print(Max)

N, K = list(map(int, input().split()))
V = list(map(int, input().split()))
cum = [0]
rcum = [0]
for i in range(N):
    cum.append(cum[-1] + V[i])
    rcum.append(rcum[-1] + V[-1 - i])
ans = 0
for i in range(N + 1):
    for j in range(N + 1):
        if i + j > min(N, K):
            continue
        value = cum[i] + rcum[j]
        neg = []
        for ii in range(i):
            if V[ii] < 0:
                neg.append(V[ii])
        for jj in range(j):
            if V[-1 - jj] < 0:
                neg.append(V[-1 - jj])
        neg.sort()
        value -= sum(neg[: min(K - i - j, len(neg))])
        ans = max(ans, value)
print(ans)

-import heapq -from collections import deque -from copy import deepcopy - -n, k = list(map(int, input().split())) -U = deque(list(map(int, input().split()))) -Max = -(10**9) -for a in range(min(n, k) + 1): -    for b in range(min(n, k) + 1 - a): -        V = deepcopy(U) -        Heap = [] -        heapq.heapify(Heap) -        if a >= 1: -            for i in range(a): -                heapq.heappush(Heap, V.pop()) -        if b >= 1: -            for i in range(b): -                heapq.heappush(Heap, V.popleft()) -        if k - a - b >= 1: -            for i in range(k - a - b): -                if len(Heap) >= 1: -                    Min = heapq.heappop(Heap) -                    if Min >= 0: -                        heapq.heappush(Heap, Min) -                        break -        Max = max(Max, sum(Heap)) -print(Max) +N, K = list(map(int, input().split())) +V = list(map(int, input().split())) +cum = [0] +rcum = [0] +for i in range(N): +    cum.append(cum[-1] + V[i]) +    rcum.append(rcum[-1] + V[-1 - i]) +ans = 0 +for i in range(N + 1): +    for j in range(N + 1): +        if i + j > min(N, K): +            continue +        value = cum[i] + rcum[j] +        neg = [] +        for ii in range(i): +            if V[ii] < 0: +                neg.append(V[ii]) +        for jj in range(j): +            if V[-1 - jj] < 0: +                neg.append(V[-1 - jj]) +        neg.sort() +        value -= sum(neg[: min(K - i - j, len(neg))]) +        ans = max(ans, value) +print(ans)