import heapq

n, k = map(int, input().split())
V = list(map(int, input().split()))

ans = 0
for i in range(min(n, k)+1):  # a+b
    for a in range(i+1):  # 操作A
        b = i - a  # 操作B
        L = V[:a] + V[n-b:]
        heapq.heapify(L)
        total = sum(L)
        for j in range(k-i):
            if not L:
                break
            if L[0] >= 0:
                break
            total -= heapq.heappop(L)
        ans = max(ans, total)

print(ans) import heapq 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 for i in range(min(n, k)+1):  # a+b
    for a in range(i+1):  # 操作A
        b = i - a  # 操作B
        L = V[:a] + V[n-b:]
        heapq.heapify(L)
        total = sum(L)
        for j in range(k-i):
            if not L:
                break
            if L[0] >= 0:
                break
            total -= heapq.heappop(L)
        ans = max(ans, total) i range(min(n, k)+1) range min(n, k)+1 min(n, k) min n k 1 for a in range(i+1):  # 操作A
        b = i - a  # 操作B
        L = V[:a] + V[n-b:]
        heapq.heapify(L)
        total = sum(L)
        for j in range(k-i):
            if not L:
                break
            if L[0] >= 0:
                break
            total -= heapq.heappop(L)
        ans = max(ans, total) a range(i+1) range i+1 i 1 b = i - a b i - a i a L = V[:a] + V[n-b:] L V[:a] + V[n-b:] V[:a] V :a a V[n-b:] V n-b: n-b n b heapq.heapify(L) heapq.heapify heapq heapify L total = sum(L) total sum(L) sum L for j in range(k-i):
            if not L:
                break
            if L[0] >= 0:
                break
            total -= heapq.heappop(L)
         j range(k-i) range k-i k i if not L:
                break
             not L L break if L[0] >= 0:
                break
             L[0] >= 0 L[0] L 0 0 break total -= heapq.heappop(L) total heapq.heappop(L) heapq.heappop heapq heappop L ans = max(ans, total) ans max(ans, total) max ans total print(ans) print ans k = map(int, input().split()) map(int, input().split()) k total = sum(L) sum(L) total b = i - a i - a b total -= heapq.heappop(L) heapq.heappop(L) total L = V[:a] + V[n-b:] V[:a] + V[n-b:] L ans = max(ans, total) max(ans, total) ans n, k = map(int, input().split()) map(int, input().split()) n V = list(map(int, input().split())) list(map(int, input().split())) V ans = 0 0 ans
import heapq

n, k = map(int, input().split())
V = list(map(int, input().split()))

ans = 0
for i in range(min(n, k)+1):  # a+b
    for a in range(i+1):  # 操作A
        b = i - a  # 操作B
        L = V[:a] + V[n-b:]
        heapq.heapify(L)
        total = sum(L)
        for j in range(k-i):
            if not L:
                break
            if L[0] >= 0:
                break
            total -= heapq.heappop(L)
        ans = max(ans, total)

print(ans)