from bisect import bisect_right
N,K = map(int,input().split())
V = list(map(int,input().split()))
cmax = -10**9
for x in range(K+1):
    for y in range(K-x+1):
        A = V[:x]
        if y==0:
            A = sorted(A)
            ind = bisect_right(A,0)
            if ind>=K-x-y:
                cnt = sum(A[K-x-y:])
            else:
                cnt = sum(A[ind:])
            cmax = max(cmax,cnt)
        else:
            if y<=N-x:
                A += V[-y:]
                A = sorted(A)
                ind = bisect_right(A,0)
                if ind>=K-x-y:
                    cnt = sum(A[K-x-y:])
                else:
                    cnt = sum(A[ind:])
                cmax = max(cmax,cnt)
            else:
                A = sorted(V[:])
                ind = bisect_right(A,0)
                if ind>=K-N:
                    cnt = sum(A[K-N:])
                else:
                    cnt = sum(A[ind:])
                cmax = max(cmax,cnt)
print(cmax) from bisect import bisect_right 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 cmax = -10**9 cmax -10**9 -10 9 for x in range(K+1):
    for y in range(K-x+1):
        A = V[:x]
        if y==0:
            A = sorted(A)
            ind = bisect_right(A,0)
            if ind>=K-x-y:
                cnt = sum(A[K-x-y:])
            else:
                cnt = sum(A[ind:])
            cmax = max(cmax,cnt)
        else:
            if y<=N-x:
                A += V[-y:]
                A = sorted(A)
                ind = bisect_right(A,0)
                if ind>=K-x-y:
                    cnt = sum(A[K-x-y:])
                else:
                    cnt = sum(A[ind:])
                cmax = max(cmax,cnt)
            else:
                A = sorted(V[:])
                ind = bisect_right(A,0)
                if ind>=K-N:
                    cnt = sum(A[K-N:])
                else:
                    cnt = sum(A[ind:])
                cmax = max(cmax,cnt) x range(K+1) range K+1 K 1 for y in range(K-x+1):
        A = V[:x]
        if y==0:
            A = sorted(A)
            ind = bisect_right(A,0)
            if ind>=K-x-y:
                cnt = sum(A[K-x-y:])
            else:
                cnt = sum(A[ind:])
            cmax = max(cmax,cnt)
        else:
            if y<=N-x:
                A += V[-y:]
                A = sorted(A)
                ind = bisect_right(A,0)
                if ind>=K-x-y:
                    cnt = sum(A[K-x-y:])
                else:
                    cnt = sum(A[ind:])
                cmax = max(cmax,cnt)
            else:
                A = sorted(V[:])
                ind = bisect_right(A,0)
                if ind>=K-N:
                    cnt = sum(A[K-N:])
                else:
                    cnt = sum(A[ind:])
                cmax = max(cmax,cnt) y range(K-x+1) range K-x+1 K-x K x 1 A = V[:x] A V[:x] V :x x if y==0:
            A = sorted(A)
            ind = bisect_right(A,0)
            if ind>=K-x-y:
                cnt = sum(A[K-x-y:])
            else:
                cnt = sum(A[ind:])
            cmax = max(cmax,cnt)
        else:
            if y<=N-x:
                A += V[-y:]
                A = sorted(A)
                ind = bisect_right(A,0)
                if ind>=K-x-y:
                    cnt = sum(A[K-x-y:])
                else:
                    cnt = sum(A[ind:])
                cmax = max(cmax,cnt)
            else:
                A = sorted(V[:])
                ind = bisect_right(A,0)
                if ind>=K-N:
                    cnt = sum(A[K-N:])
                else:
                    cnt = sum(A[ind:])
                cmax = max(cmax,cnt) y==0 y 0 A = sorted(A) A sorted(A) sorted A ind = bisect_right(A,0) ind bisect_right(A,0) bisect_right A 0 if ind>=K-x-y:
                cnt = sum(A[K-x-y:])
            else:
                cnt = sum(A[ind:])
             ind>=K-x-y ind K-x-y K-x K x y cnt = sum(A[K-x-y:]) cnt sum(A[K-x-y:]) sum A[K-x-y:] A K-x-y: K-x-y K-x K x y cnt = sum(A[ind:]) cnt sum(A[ind:]) sum A[ind:] A ind: ind cmax = max(cmax,cnt) cmax max(cmax,cnt) max cmax cnt if y<=N-x:
                A += V[-y:]
                A = sorted(A)
                ind = bisect_right(A,0)
                if ind>=K-x-y:
                    cnt = sum(A[K-x-y:])
                else:
                    cnt = sum(A[ind:])
                cmax = max(cmax,cnt)
            else:
                A = sorted(V[:])
                ind = bisect_right(A,0)
                if ind>=K-N:
                    cnt = sum(A[K-N:])
                else:
                    cnt = sum(A[ind:])
                cmax = max(cmax,cnt) y<=N-x y N-x N x A += V[-y:] A V[-y:] V -y: -y y A = sorted(A) A sorted(A) sorted A ind = bisect_right(A,0) ind bisect_right(A,0) bisect_right A 0 if ind>=K-x-y:
                    cnt = sum(A[K-x-y:])
                else:
                    cnt = sum(A[ind:])
                 ind>=K-x-y ind K-x-y K-x K x y cnt = sum(A[K-x-y:]) cnt sum(A[K-x-y:]) sum A[K-x-y:] A K-x-y: K-x-y K-x K x y cnt = sum(A[ind:]) cnt sum(A[ind:]) sum A[ind:] A ind: ind cmax = max(cmax,cnt) cmax max(cmax,cnt) max cmax cnt A = sorted(V[:]) A sorted(V[:]) sorted V[:] V : ind = bisect_right(A,0) ind bisect_right(A,0) bisect_right A 0 if ind>=K-N:
                    cnt = sum(A[K-N:])
                else:
                    cnt = sum(A[ind:])
                 ind>=K-N ind K-N K N cnt = sum(A[K-N:]) cnt sum(A[K-N:]) sum A[K-N:] A K-N: K-N K N cnt = sum(A[ind:]) cnt sum(A[ind:]) sum A[ind:] A ind: ind cmax = max(cmax,cnt) cmax max(cmax,cnt) max cmax cnt print(cmax) print cmax cmax = -10**9 -10**9 cmax cnt = sum(A[ind:]) sum(A[ind:]) cnt cmax = max(cmax,cnt) max(cmax,cnt) cmax V = list(map(int,input().split())) list(map(int,input().split())) V cmax = max(cmax,cnt) max(cmax,cnt) cmax cnt = sum(A[K-N:]) sum(A[K-N:]) cnt cmax = max(cmax,cnt) max(cmax,cnt) cmax A = sorted(A) sorted(A) A ind = bisect_right(A,0) bisect_right(A,0) ind A = sorted(V[:]) sorted(V[:]) A N,K = map(int,input().split()) map(int,input().split()) N A = V[:x] V[:x] A cnt = sum(A[K-x-y:]) sum(A[K-x-y:]) cnt A = sorted(A) sorted(A) A cnt = sum(A[K-x-y:]) sum(A[K-x-y:]) cnt ind = bisect_right(A,0) bisect_right(A,0) ind K = map(int,input().split()) map(int,input().split()) K A += V[-y:] V[-y:] A cnt = sum(A[ind:]) sum(A[ind:]) cnt ind = bisect_right(A,0) bisect_right(A,0) ind cnt = sum(A[ind:]) sum(A[ind:]) cnt
from bisect import bisect_right
N,K = map(int,input().split())
V = list(map(int,input().split()))
cmax = -10**9
for x in range(K+1):
    for y in range(K-x+1):
        A = V[:x]
        if y==0:
            A = sorted(A)
            ind = bisect_right(A,0)
            if ind>=K-x-y:
                cnt = sum(A[K-x-y:])
            else:
                cnt = sum(A[ind:])
            cmax = max(cmax,cnt)
        else:
            if y<=N-x:
                A += V[-y:]
                A = sorted(A)
                ind = bisect_right(A,0)
                if ind>=K-x-y:
                    cnt = sum(A[K-x-y:])
                else:
                    cnt = sum(A[ind:])
                cmax = max(cmax,cnt)
            else:
                A = sorted(V[:])
                ind = bisect_right(A,0)
                if ind>=K-N:
                    cnt = sum(A[K-N:])
                else:
                    cnt = sum(A[ind:])
                cmax = max(cmax,cnt)
print(cmax)