from collections import deque
import heapq
n,k = map(int,input().split())
v = deque(list(map(int,input().split())))

ans = 0
for i in range(min(n,k)+1):
    for j in range(min(n,k)-i+1):
        l = [0]
        r = [0]
        mm = [0]
        for p in range(i):
            if v[p] > 0:
                l.append(v[p])
            else:
                heapq.heappush(mm,v[p])
        for o in range(j):
            if v[-o-1] > 0:
                r.append(v[-o-1])
            else:
                heapq.heappush(mm,v[-o-1])
        for m in range(min(k-i-j,len(mm))):
            heapq.heappop(mm)
        ans = max(ans,sum(l)+sum(r)+sum(mm))
print(ans) from collections import deque import heapq heapq n,k = map(int,input().split()) n map(int,input().split()) map int input().split() ().split () input split k v = deque(list(map(int,input().split()))) v deque(list(map(int,input().split()))) deque list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split ans = 0 ans 0 for i in range(min(n,k)+1):
    for j in range(min(n,k)-i+1):
        l = [0]
        r = [0]
        mm = [0]
        for p in range(i):
            if v[p] > 0:
                l.append(v[p])
            else:
                heapq.heappush(mm,v[p])
        for o in range(j):
            if v[-o-1] > 0:
                r.append(v[-o-1])
            else:
                heapq.heappush(mm,v[-o-1])
        for m in range(min(k-i-j,len(mm))):
            heapq.heappop(mm)
        ans = max(ans,sum(l)+sum(r)+sum(mm)) i range(min(n,k)+1) range min(n,k)+1 min(n,k) min n k 1 for j in range(min(n,k)-i+1):
        l = [0]
        r = [0]
        mm = [0]
        for p in range(i):
            if v[p] > 0:
                l.append(v[p])
            else:
                heapq.heappush(mm,v[p])
        for o in range(j):
            if v[-o-1] > 0:
                r.append(v[-o-1])
            else:
                heapq.heappush(mm,v[-o-1])
        for m in range(min(k-i-j,len(mm))):
            heapq.heappop(mm)
        ans = max(ans,sum(l)+sum(r)+sum(mm)) j range(min(n,k)-i+1) range min(n,k)-i+1 min(n,k)-i min(n,k) min n k i 1 l = [0] l [0] 0 r = [0] r [0] 0 mm = [0] mm [0] 0 for p in range(i):
            if v[p] > 0:
                l.append(v[p])
            else:
                heapq.heappush(mm,v[p])
         p range(i) range i if v[p] > 0:
                l.append(v[p])
            else:
                heapq.heappush(mm,v[p])
         v[p] > 0 v[p] v p 0 l.append(v[p]) l.append l append v[p] v p heapq.heappush(mm,v[p]) heapq.heappush heapq heappush mm v[p] v p for o in range(j):
            if v[-o-1] > 0:
                r.append(v[-o-1])
            else:
                heapq.heappush(mm,v[-o-1])
         o range(j) range j if v[-o-1] > 0:
                r.append(v[-o-1])
            else:
                heapq.heappush(mm,v[-o-1])
         v[-o-1] > 0 v[-o-1] v -o-1 -o o 1 0 r.append(v[-o-1]) r.append r append v[-o-1] v -o-1 -o o 1 heapq.heappush(mm,v[-o-1]) heapq.heappush heapq heappush mm v[-o-1] v -o-1 -o o 1 for m in range(min(k-i-j,len(mm))):
            heapq.heappop(mm)
         m range(min(k-i-j,len(mm))) range min(k-i-j,len(mm)) min k-i-j k-i k i j len(mm) len mm heapq.heappop(mm) heapq.heappop heapq heappop mm ans = max(ans,sum(l)+sum(r)+sum(mm)) ans max(ans,sum(l)+sum(r)+sum(mm)) max ans sum(l)+sum(r)+sum(mm) sum(l)+sum(r) sum(l) sum l sum(r) sum r sum(mm) sum mm print(ans) print ans n,k = map(int,input().split()) map(int,input().split()) n k = map(int,input().split()) map(int,input().split()) k ans = 0 0 ans r = [0] [0] r mm = [0] [0] mm ans = max(ans,sum(l)+sum(r)+sum(mm)) max(ans,sum(l)+sum(r)+sum(mm)) ans l = [0] [0] l v = deque(list(map(int,input().split()))) deque(list(map(int,input().split()))) v
from collections import deque
import heapq
n,k = map(int,input().split())
v = deque(list(map(int,input().split())))

ans = 0
for i in range(min(n,k)+1):
    for j in range(min(n,k)-i+1):
        l = [0]
        r = [0]
        mm = [0]
        for p in range(i):
            if v[p] > 0:
                l.append(v[p])
            else:
                heapq.heappush(mm,v[p])
        for o in range(j):
            if v[-o-1] > 0:
                r.append(v[-o-1])
            else:
                heapq.heappush(mm,v[-o-1])
        for m in range(min(k-i-j,len(mm))):
            heapq.heappop(mm)
        ans = max(ans,sum(l)+sum(r)+sum(mm))
print(ans)