from collections import deque
from copy import deepcopy
import heapq

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

minus=[[[0]*(N+1) for _ in range(N+1)] for _ in range(N+1)]

vdeque=deque(V)

for left in range(N+1):
    for right in range(N-left+1):
        v=deepcopy(vdeque)
        q=[]

        tleft=left
        while tleft>0:
            vi=deque.popleft(v)
            if vi<0:
                heapq.heappush(q,vi)
            tleft-=1
        
        tright=right

        while tright>0:
            vj=deque.pop(v)
            if vj<0:
                heapq.heappush(q,vj)
            tright-=1
        
        for k in range(1,N+1):
            if len(q)>0:
                minus[k][left][right]=minus[k-1][left][right]-heapq.heappop(q)
            else:
                minus[k][left][right]=minus[k-1][left][right]

leftacum=[0]*(N+1)
rightacum=[0]*(N+1)

for i in range(N):
    leftacum[i+1]=leftacum[i]+V[i]
    rightacum[i+1]=rightacum[i]+V[N-i-1]

ans=-10**10

for left in range(N+1):
    for right in range(N-left+1):
        k=min(N,K-left-right)
        if k<0:
            continue

        ans=max(ans,leftacum[left]+rightacum[right]+minus[k][left][right])

print(ans) from collections import deque from copy import deepcopy import heapq heapq N,K=list(map(int,input().split())) N list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split K V=list(map(int,input().split())) V list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split [[0]*(N+1) for _ in range(N+1)] for _ in range(N+1) for _ in range(N+1) _ range(N+1) range N+1 N 1 for _ in range(N+1) [[0]*(N+1) for _ in range(N+1)] minus=[[[0]*(N+1) for _ in range(N+1)] for _ in range(N+1)] minus [[[0]*(N+1) for _ in range(N+1)] for _ in range(N+1)] vdeque=deque(V) vdeque deque(V) deque V for left in range(N+1):
    for right in range(N-left+1):
        v=deepcopy(vdeque)
        q=[]

        tleft=left
        while tleft>0:
            vi=deque.popleft(v)
            if vi<0:
                heapq.heappush(q,vi)
            tleft-=1
        
        tright=right

        while tright>0:
            vj=deque.pop(v)
            if vj<0:
                heapq.heappush(q,vj)
            tright-=1
        
        for k in range(1,N+1):
            if len(q)>0:
                minus[k][left][right]=minus[k-1][left][right]-heapq.heappop(q)
            else:
                minus[k][left][right]=minus[k-1][left][right] left range(N+1) range N+1 N 1 for right in range(N-left+1):
        v=deepcopy(vdeque)
        q=[]

        tleft=left
        while tleft>0:
            vi=deque.popleft(v)
            if vi<0:
                heapq.heappush(q,vi)
            tleft-=1
        
        tright=right

        while tright>0:
            vj=deque.pop(v)
            if vj<0:
                heapq.heappush(q,vj)
            tright-=1
        
        for k in range(1,N+1):
            if len(q)>0:
                minus[k][left][right]=minus[k-1][left][right]-heapq.heappop(q)
            else:
                minus[k][left][right]=minus[k-1][left][right] right range(N-left+1) range N-left+1 N-left N left 1 v=deepcopy(vdeque) v deepcopy(vdeque) deepcopy vdeque q=[] q [] tleft=left tleft left while tleft>0:
            vi=deque.popleft(v)
            if vi<0:
                heapq.heappush(q,vi)
            tleft-=1
        
         tleft>0 tleft 0 vi=deque.popleft(v) vi deque.popleft(v) deque.popleft deque popleft v if vi<0:
                heapq.heappush(q,vi)
             vi<0 vi 0 heapq.heappush(q,vi) heapq.heappush heapq heappush q vi tleft-=1 tleft 1 tright=right tright right while tright>0:
            vj=deque.pop(v)
            if vj<0:
                heapq.heappush(q,vj)
            tright-=1
        
         tright>0 tright 0 vj=deque.pop(v) vj deque.pop(v) deque.pop deque pop v if vj<0:
                heapq.heappush(q,vj)
             vj<0 vj 0 heapq.heappush(q,vj) heapq.heappush heapq heappush q vj tright-=1 tright 1 for k in range(1,N+1):
            if len(q)>0:
                minus[k][left][right]=minus[k-1][left][right]-heapq.heappop(q)
            else:
                minus[k][left][right]=minus[k-1][left][right] k range(1,N+1) range 1 N+1 N 1 if len(q)>0:
                minus[k][left][right]=minus[k-1][left][right]-heapq.heappop(q)
            else:
                minus[k][left][right]=minus[k-1][left][right] len(q)>0 len(q) len q 0 minus[k][left][right]=minus[k-1][left][right]-heapq.heappop(q) minus[k][left][right] [left] [k] minus k left right minus[k-1][left][right]-heapq.heappop(q) minus[k-1][left][right] [left] [k-1] minus k-1 k 1 left right heapq.heappop(q) heapq.heappop heapq heappop q minus[k][left][right]=minus[k-1][left][right] minus[k][left][right] [left] [k] minus k left right minus[k-1][left][right] [left] [k-1] minus k-1 k 1 left right leftacum=[0]*(N+1) leftacum [0]*(N+1) [0] 0 N+1 N 1 rightacum=[0]*(N+1) rightacum [0]*(N+1) [0] 0 N+1 N 1 for i in range(N):
    leftacum[i+1]=leftacum[i]+V[i]
    rightacum[i+1]=rightacum[i]+V[N-i-1] i range(N) range N leftacum[i+1]=leftacum[i]+V[i] leftacum[i+1] leftacum i+1 i 1 leftacum[i]+V[i] leftacum[i] leftacum i V[i] V i rightacum[i+1]=rightacum[i]+V[N-i-1] rightacum[i+1] rightacum i+1 i 1 rightacum[i]+V[N-i-1] rightacum[i] rightacum i V[N-i-1] V N-i-1 N-i N i 1 ans=-10**10 ans -10**10 -10 10 for left in range(N+1):
    for right in range(N-left+1):
        k=min(N,K-left-right)
        if k<0:
            continue

        ans=max(ans,leftacum[left]+rightacum[right]+minus[k][left][right]) left range(N+1) range N+1 N 1 for right in range(N-left+1):
        k=min(N,K-left-right)
        if k<0:
            continue

        ans=max(ans,leftacum[left]+rightacum[right]+minus[k][left][right]) right range(N-left+1) range N-left+1 N-left N left 1 k=min(N,K-left-right) k min(N,K-left-right) min N K-left-right K-left K left right if k<0:
            continue

         k<0 k 0 continue ans=max(ans,leftacum[left]+rightacum[right]+minus[k][left][right]) ans max(ans,leftacum[left]+rightacum[right]+minus[k][left][right]) max ans leftacum[left]+rightacum[right]+minus[k][left][right] leftacum[left]+rightacum[right] leftacum[left] leftacum left rightacum[right] rightacum right minus[k][left][right] [left] [k] minus k left right print(ans) print ans K=list(map(int,input().split())) list(map(int,input().split())) K tleft=left left tleft tright-=1 1 tright vdeque=deque(V) deque(V) vdeque vi=deque.popleft(v) deque.popleft(v) vi N,K=list(map(int,input().split())) list(map(int,input().split())) N V=list(map(int,input().split())) list(map(int,input().split())) V ans=-10**10 -10**10 ans rightacum=[0]*(N+1) [0]*(N+1) rightacum leftacum=[0]*(N+1) [0]*(N+1) leftacum k=min(N,K-left-right) min(N,K-left-right) k ans=max(ans,leftacum[left]+rightacum[right]+minus[k][left][right]) max(ans,leftacum[left]+rightacum[right]+minus[k][left][right]) ans minus=[[[0]*(N+1) for _ in range(N+1)] for _ in range(N+1)] [[[0]*(N+1) for _ in range(N+1)] for _ in range(N+1)] minus tleft-=1 1 tleft vj=deque.pop(v) deque.pop(v) vj tright=right right tright v=deepcopy(vdeque) deepcopy(vdeque) v q=[] [] q
from collections import deque
from copy import deepcopy
import heapq

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

minus=[[[0]*(N+1) for _ in range(N+1)] for _ in range(N+1)]

vdeque=deque(V)

for left in range(N+1):
    for right in range(N-left+1):
        v=deepcopy(vdeque)
        q=[]

        tleft=left
        while tleft>0:
            vi=deque.popleft(v)
            if vi<0:
                heapq.heappush(q,vi)
            tleft-=1
        
        tright=right

        while tright>0:
            vj=deque.pop(v)
            if vj<0:
                heapq.heappush(q,vj)
            tright-=1
        
        for k in range(1,N+1):
            if len(q)>0:
                minus[k][left][right]=minus[k-1][left][right]-heapq.heappop(q)
            else:
                minus[k][left][right]=minus[k-1][left][right]

leftacum=[0]*(N+1)
rightacum=[0]*(N+1)

for i in range(N):
    leftacum[i+1]=leftacum[i]+V[i]
    rightacum[i+1]=rightacum[i]+V[N-i-1]

ans=-10**10

for left in range(N+1):
    for right in range(N-left+1):
        k=min(N,K-left-right)
        if k<0:
            continue

        ans=max(ans,leftacum[left]+rightacum[right]+minus[k][left][right])

print(ans)