import heapq,copy

N,K = (int(x) for x in input().split())
V = list(map(int, input().split()))
v = copy.copy(V)
ans = 0
if N <= K:
    heapq.heapify(v)
    for i in range(min(K-N,len(v))):
        temp = heapq.heappop(v)
        if temp >= 0:
            ans = max(ans,sum(v)+temp)
            break
    else:
        ans = max(ans,sum(v))
for i in range(min(N,K+1)):
    for j in range(min(N,K+1)-i):
        if (i == 0 and j == 0) or i + j == N:
            continue
        else:
            l = V[:j] + V[N-1:N-1-i:-1]
            heapq.heapify(l)
            for k in range(min(K-i-j,len(l))):
                temp = heapq.heappop(l)
                if temp >= 0:
                    ans = max(ans,sum(l)+temp)
                    break
            else:
                ans = max(ans,sum(l))
print(ans) import heapq,copy heapq copy int(x) for x in input().split() for x in input().split() x input().split() ().split () input split for x in input().split() int(x) int x N,K = (int(x) for x in input().split()) N (int(x) for x in 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 v = copy.copy(V) v copy.copy(V) copy.copy copy copy V ans = 0 ans 0 if N <= K:
    heapq.heapify(v)
    for i in range(min(K-N,len(v))):
        temp = heapq.heappop(v)
        if temp >= 0:
            ans = max(ans,sum(v)+temp)
            break
    else:
        ans = max(ans,sum(v)) N <= K N K heapq.heapify(v) heapq.heapify heapq heapify v for i in range(min(K-N,len(v))):
        temp = heapq.heappop(v)
        if temp >= 0:
            ans = max(ans,sum(v)+temp)
            break
    else:
        ans = max(ans,sum(v)) i range(min(K-N,len(v))) range min(K-N,len(v)) min K-N K N len(v) len v temp = heapq.heappop(v) temp heapq.heappop(v) heapq.heappop heapq heappop v if temp >= 0:
            ans = max(ans,sum(v)+temp)
            break
     temp >= 0 temp 0 ans = max(ans,sum(v)+temp) ans max(ans,sum(v)+temp) max ans sum(v)+temp sum(v) sum v temp break ans = max(ans,sum(v)) ans max(ans,sum(v)) max ans sum(v) sum v for i in range(min(N,K+1)):
    for j in range(min(N,K+1)-i):
        if (i == 0 and j == 0) or i + j == N:
            continue
        else:
            l = V[:j] + V[N-1:N-1-i:-1]
            heapq.heapify(l)
            for k in range(min(K-i-j,len(l))):
                temp = heapq.heappop(l)
                if temp >= 0:
                    ans = max(ans,sum(l)+temp)
                    break
            else:
                ans = max(ans,sum(l)) i range(min(N,K+1)) range min(N,K+1) min N K+1 K 1 for j in range(min(N,K+1)-i):
        if (i == 0 and j == 0) or i + j == N:
            continue
        else:
            l = V[:j] + V[N-1:N-1-i:-1]
            heapq.heapify(l)
            for k in range(min(K-i-j,len(l))):
                temp = heapq.heappop(l)
                if temp >= 0:
                    ans = max(ans,sum(l)+temp)
                    break
            else:
                ans = max(ans,sum(l)) j range(min(N,K+1)-i) range min(N,K+1)-i min(N,K+1) min N K+1 K 1 i if (i == 0 and j == 0) or i + j == N:
            continue
        else:
            l = V[:j] + V[N-1:N-1-i:-1]
            heapq.heapify(l)
            for k in range(min(K-i-j,len(l))):
                temp = heapq.heappop(l)
                if temp >= 0:
                    ans = max(ans,sum(l)+temp)
                    break
            else:
                ans = max(ans,sum(l)) (i == 0 and j == 0) or i + j == N i == 0 and j == 0 i == 0 i 0 j == 0 j 0 i + j == N i + j i j N continue l = V[:j] + V[N-1:N-1-i:-1] l V[:j] + V[N-1:N-1-i:-1] V[:j] V :j j V[N-1:N-1-i:-1] V N-1:N-1-i:-1 N-1 N 1 N-1-i N-1 N 1 i -1 heapq.heapify(l) heapq.heapify heapq heapify l for k in range(min(K-i-j,len(l))):
                temp = heapq.heappop(l)
                if temp >= 0:
                    ans = max(ans,sum(l)+temp)
                    break
            else:
                ans = max(ans,sum(l)) k range(min(K-i-j,len(l))) range min(K-i-j,len(l)) min K-i-j K-i K i j len(l) len l temp = heapq.heappop(l) temp heapq.heappop(l) heapq.heappop heapq heappop l if temp >= 0:
                    ans = max(ans,sum(l)+temp)
                    break
             temp >= 0 temp 0 ans = max(ans,sum(l)+temp) ans max(ans,sum(l)+temp) max ans sum(l)+temp sum(l) sum l temp break ans = max(ans,sum(l)) ans max(ans,sum(l)) max ans sum(l) sum l print(ans) print ans ans = max(ans,sum(v)+temp) max(ans,sum(v)+temp) ans K = (int(x) for x in input().split()) (int(x) for x in input().split()) K V = list(map(int, input().split())) list(map(int, input().split())) V temp = heapq.heappop(v) heapq.heappop(v) temp l = V[:j] + V[N-1:N-1-i:-1] V[:j] + V[N-1:N-1-i:-1] l N,K = (int(x) for x in input().split()) (int(x) for x in input().split()) N ans = 0 0 ans ans = max(ans,sum(l)) max(ans,sum(l)) ans temp = heapq.heappop(l) heapq.heappop(l) temp v = copy.copy(V) copy.copy(V) v ans = max(ans,sum(v)) max(ans,sum(v)) ans ans = max(ans,sum(l)+temp) max(ans,sum(l)+temp) ans
import heapq,copy

N,K = (int(x) for x in input().split())
V = list(map(int, input().split()))
v = copy.copy(V)
ans = 0
if N <= K:
    heapq.heapify(v)
    for i in range(min(K-N,len(v))):
        temp = heapq.heappop(v)
        if temp >= 0:
            ans = max(ans,sum(v)+temp)
            break
    else:
        ans = max(ans,sum(v))
for i in range(min(N,K+1)):
    for j in range(min(N,K+1)-i):
        if (i == 0 and j == 0) or i + j == N:
            continue
        else:
            l = V[:j] + V[N-1:N-1-i:-1]
            heapq.heapify(l)
            for k in range(min(K-i-j,len(l))):
                temp = heapq.heappop(l)
                if temp >= 0:
                    ans = max(ans,sum(l)+temp)
                    break
            else:
                ans = max(ans,sum(l))
print(ans)