import bisect
N,K= map(int,input().split())
V = list(map(int,input().split()))
sortedV = sorted(V)
rV = V[::-1]

ans = -10**10
for nout in range(K+1):
    nin = K-nout
    if nout >= N:
        cand = sortedV
        ind = bisect.bisect_left(cand,0)
        ind = min(ind,nin)
        ans = max(ans, sum(cand[ind:]))
    else:
        for nleft in range(nout+1):
            nright = nout - nleft
            cand = V[:nleft] + rV[:nright]
            cand.sort()
            ind = bisect.bisect_left(cand,0)
            ind = min(ind,nin)
            ans = max(ans, sum(cand[ind:]))
print(ans)



 import bisect bisect 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 sortedV = sorted(V) sortedV sorted(V) sorted V rV = V[::-1] rV V[::-1] V ::-1 -1 ans = -10**10 ans -10**10 -10 10 for nout in range(K+1):
    nin = K-nout
    if nout >= N:
        cand = sortedV
        ind = bisect.bisect_left(cand,0)
        ind = min(ind,nin)
        ans = max(ans, sum(cand[ind:]))
    else:
        for nleft in range(nout+1):
            nright = nout - nleft
            cand = V[:nleft] + rV[:nright]
            cand.sort()
            ind = bisect.bisect_left(cand,0)
            ind = min(ind,nin)
            ans = max(ans, sum(cand[ind:])) nout range(K+1) range K+1 K 1 nin = K-nout nin K-nout K nout if nout >= N:
        cand = sortedV
        ind = bisect.bisect_left(cand,0)
        ind = min(ind,nin)
        ans = max(ans, sum(cand[ind:]))
    else:
        for nleft in range(nout+1):
            nright = nout - nleft
            cand = V[:nleft] + rV[:nright]
            cand.sort()
            ind = bisect.bisect_left(cand,0)
            ind = min(ind,nin)
            ans = max(ans, sum(cand[ind:])) nout >= N nout N cand = sortedV cand sortedV ind = bisect.bisect_left(cand,0) ind bisect.bisect_left(cand,0) bisect.bisect_left bisect bisect_left cand 0 ind = min(ind,nin) ind min(ind,nin) min ind nin ans = max(ans, sum(cand[ind:])) ans max(ans, sum(cand[ind:])) max ans sum(cand[ind:]) sum cand[ind:] cand ind: ind for nleft in range(nout+1):
            nright = nout - nleft
            cand = V[:nleft] + rV[:nright]
            cand.sort()
            ind = bisect.bisect_left(cand,0)
            ind = min(ind,nin)
            ans = max(ans, sum(cand[ind:])) nleft range(nout+1) range nout+1 nout 1 nright = nout - nleft nright nout - nleft nout nleft cand = V[:nleft] + rV[:nright] cand V[:nleft] + rV[:nright] V[:nleft] V :nleft nleft rV[:nright] rV :nright nright cand.sort() cand.sort cand sort ind = bisect.bisect_left(cand,0) ind bisect.bisect_left(cand,0) bisect.bisect_left bisect bisect_left cand 0 ind = min(ind,nin) ind min(ind,nin) min ind nin ans = max(ans, sum(cand[ind:])) ans max(ans, sum(cand[ind:])) max ans sum(cand[ind:]) sum cand[ind:] cand ind: ind print(ans) print ans K= map(int,input().split()) map(int,input().split()) K ind = min(ind,nin) min(ind,nin) ind rV = V[::-1] V[::-1] rV nin = K-nout K-nout nin V = list(map(int,input().split())) list(map(int,input().split())) V ans = -10**10 -10**10 ans cand = V[:nleft] + rV[:nright] V[:nleft] + rV[:nright] cand ind = bisect.bisect_left(cand,0) bisect.bisect_left(cand,0) ind ans = max(ans, sum(cand[ind:])) max(ans, sum(cand[ind:])) ans sortedV = sorted(V) sorted(V) sortedV ans = max(ans, sum(cand[ind:])) max(ans, sum(cand[ind:])) ans cand = sortedV sortedV cand N,K= map(int,input().split()) map(int,input().split()) N nright = nout - nleft nout - nleft nright ind = min(ind,nin) min(ind,nin) ind ind = bisect.bisect_left(cand,0) bisect.bisect_left(cand,0) ind
import bisect
N,K= map(int,input().split())
V = list(map(int,input().split()))
sortedV = sorted(V)
rV = V[::-1]

ans = -10**10
for nout in range(K+1):
    nin = K-nout
    if nout >= N:
        cand = sortedV
        ind = bisect.bisect_left(cand,0)
        ind = min(ind,nin)
        ans = max(ans, sum(cand[ind:]))
    else:
        for nleft in range(nout+1):
            nright = nout - nleft
            cand = V[:nleft] + rV[:nright]
            cand.sort()
            ind = bisect.bisect_left(cand,0)
            ind = min(ind,nin)
            ans = max(ans, sum(cand[ind:]))
print(ans)




