import copy,heapq

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

ans=0
left=copy.copy(V)
for i in range(1,N):
    left[i]+=left[i-1]
right=copy.copy(V)
for i in range(1,N):
    right[-i-1]+=right[-i]
for i in range(0,N+1):
    for j in range(0,N-i+1):
        dispose=K-i-j
        if 0>dispose:
            continue
        test=0
        q=[]
        if i>0:
            test+=left[i-1]
            q+=V[:i]
        if j>0:
            test+=right[-j]
            q+=V[N-j:N]

        heapq.heapify(q)
        while dispose>0 and q:
            x=heapq.heappop(q)
            if 0>x:
                test-=x
                dispose-=1
            else:
                break
        ans=max(ans,test)

print(ans) import copy,heapq copy heapq N,K=map(int,input().split()) N 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 ans=0 ans 0 left=copy.copy(V) left copy.copy(V) copy.copy copy copy V for i in range(1,N):
    left[i]+=left[i-1] i range(1,N) range 1 N left[i]+=left[i-1] left[i] left i left[i-1] left i-1 i 1 right=copy.copy(V) right copy.copy(V) copy.copy copy copy V for i in range(1,N):
    right[-i-1]+=right[-i] i range(1,N) range 1 N right[-i-1]+=right[-i] right[-i-1] right -i-1 -i i 1 right[-i] right -i i for i in range(0,N+1):
    for j in range(0,N-i+1):
        dispose=K-i-j
        if 0>dispose:
            continue
        test=0
        q=[]
        if i>0:
            test+=left[i-1]
            q+=V[:i]
        if j>0:
            test+=right[-j]
            q+=V[N-j:N]

        heapq.heapify(q)
        while dispose>0 and q:
            x=heapq.heappop(q)
            if 0>x:
                test-=x
                dispose-=1
            else:
                break
        ans=max(ans,test) i range(0,N+1) range 0 N+1 N 1 for j in range(0,N-i+1):
        dispose=K-i-j
        if 0>dispose:
            continue
        test=0
        q=[]
        if i>0:
            test+=left[i-1]
            q+=V[:i]
        if j>0:
            test+=right[-j]
            q+=V[N-j:N]

        heapq.heapify(q)
        while dispose>0 and q:
            x=heapq.heappop(q)
            if 0>x:
                test-=x
                dispose-=1
            else:
                break
        ans=max(ans,test) j range(0,N-i+1) range 0 N-i+1 N-i N i 1 dispose=K-i-j dispose K-i-j K-i K i j if 0>dispose:
            continue
         0>dispose 0 dispose continue test=0 test 0 q=[] q [] if i>0:
            test+=left[i-1]
            q+=V[:i]
         i>0 i 0 test+=left[i-1] test left[i-1] left i-1 i 1 q+=V[:i] q V[:i] V :i i if j>0:
            test+=right[-j]
            q+=V[N-j:N]

         j>0 j 0 test+=right[-j] test right[-j] right -j j q+=V[N-j:N] q V[N-j:N] V N-j:N N-j N j N heapq.heapify(q) heapq.heapify heapq heapify q while dispose>0 and q:
            x=heapq.heappop(q)
            if 0>x:
                test-=x
                dispose-=1
            else:
                break
         dispose>0 and q dispose>0 dispose 0 q x=heapq.heappop(q) x heapq.heappop(q) heapq.heappop heapq heappop q if 0>x:
                test-=x
                dispose-=1
            else:
                break
         0>x 0 x test-=x test x dispose-=1 dispose 1 break ans=max(ans,test) ans max(ans,test) max ans test print(ans) print ans left=copy.copy(V) copy.copy(V) left test=0 0 test dispose=K-i-j K-i-j dispose q+=V[N-j:N] V[N-j:N] q test+=right[-j] right[-j] test test+=left[i-1] left[i-1] test q+=V[:i] V[:i] q ans=0 0 ans N,K=map(int,input().split()) map(int,input().split()) N K=map(int,input().split()) map(int,input().split()) K x=heapq.heappop(q) heapq.heappop(q) x dispose-=1 1 dispose V=list(map(int,input().split())) list(map(int,input().split())) V ans=max(ans,test) max(ans,test) ans right=copy.copy(V) copy.copy(V) right q=[] [] q test-=x x test
import copy,heapq

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

ans=0
left=copy.copy(V)
for i in range(1,N):
    left[i]+=left[i-1]
right=copy.copy(V)
for i in range(1,N):
    right[-i-1]+=right[-i]
for i in range(0,N+1):
    for j in range(0,N-i+1):
        dispose=K-i-j
        if 0>dispose:
            continue
        test=0
        q=[]
        if i>0:
            test+=left[i-1]
            q+=V[:i]
        if j>0:
            test+=right[-j]
            q+=V[N-j:N]

        heapq.heapify(q)
        while dispose>0 and q:
            x=heapq.heappop(q)
            if 0>x:
                test-=x
                dispose-=1
            else:
                break
        ans=max(ans,test)

print(ans)
