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

y = min(n, k)

ans = 0

for i in range(y):
    cnt = 0
    heap = []
    for j in range(i+1):
        if v[j] < 0:
            heap.append(v[j])
            ans = max(ans, cnt)
        cnt += v[j]
        ans = max(ans, cnt)

    rest = k - i - 1
    right = n - i - 1
    for r in range(min(rest, right)+1):
        index = -1
        cnt2 = cnt
        heap2 = heap[:]
        for _ in range(r):
            cnt2 += v[index]
            if v[index] < 0:
                heap2.append(v[index])
            index -= 1

        ans = max(ans, cnt2)
        rest2 = rest - r
        heap2.sort()
        for o in range(rest2):
            if o >= len(heap2):
                break
            cnt2 -= heap2[o]
            ans = max(ans, cnt2)

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 y = min(n, k) y min(n, k) min n k ans = 0 ans 0 for i in range(y):
    cnt = 0
    heap = []
    for j in range(i+1):
        if v[j] < 0:
            heap.append(v[j])
            ans = max(ans, cnt)
        cnt += v[j]
        ans = max(ans, cnt)

    rest = k - i - 1
    right = n - i - 1
    for r in range(min(rest, right)+1):
        index = -1
        cnt2 = cnt
        heap2 = heap[:]
        for _ in range(r):
            cnt2 += v[index]
            if v[index] < 0:
                heap2.append(v[index])
            index -= 1

        ans = max(ans, cnt2)
        rest2 = rest - r
        heap2.sort()
        for o in range(rest2):
            if o >= len(heap2):
                break
            cnt2 -= heap2[o]
            ans = max(ans, cnt2) i range(y) range y cnt = 0 cnt 0 heap = [] heap [] for j in range(i+1):
        if v[j] < 0:
            heap.append(v[j])
            ans = max(ans, cnt)
        cnt += v[j]
        ans = max(ans, cnt)

     j range(i+1) range i+1 i 1 if v[j] < 0:
            heap.append(v[j])
            ans = max(ans, cnt)
         v[j] < 0 v[j] v j 0 heap.append(v[j]) heap.append heap append v[j] v j ans = max(ans, cnt) ans max(ans, cnt) max ans cnt cnt += v[j] cnt v[j] v j ans = max(ans, cnt) ans max(ans, cnt) max ans cnt rest = k - i - 1 rest k - i - 1 k - i k i 1 right = n - i - 1 right n - i - 1 n - i n i 1 for r in range(min(rest, right)+1):
        index = -1
        cnt2 = cnt
        heap2 = heap[:]
        for _ in range(r):
            cnt2 += v[index]
            if v[index] < 0:
                heap2.append(v[index])
            index -= 1

        ans = max(ans, cnt2)
        rest2 = rest - r
        heap2.sort()
        for o in range(rest2):
            if o >= len(heap2):
                break
            cnt2 -= heap2[o]
            ans = max(ans, cnt2) r range(min(rest, right)+1) range min(rest, right)+1 min(rest, right) min rest right 1 index = -1 index -1 cnt2 = cnt cnt2 cnt heap2 = heap[:] heap2 heap[:] heap : for _ in range(r):
            cnt2 += v[index]
            if v[index] < 0:
                heap2.append(v[index])
            index -= 1

         _ range(r) range r cnt2 += v[index] cnt2 v[index] v index if v[index] < 0:
                heap2.append(v[index])
             v[index] < 0 v[index] v index 0 heap2.append(v[index]) heap2.append heap2 append v[index] v index index -= 1 index 1 ans = max(ans, cnt2) ans max(ans, cnt2) max ans cnt2 rest2 = rest - r rest2 rest - r rest r heap2.sort() heap2.sort heap2 sort for o in range(rest2):
            if o >= len(heap2):
                break
            cnt2 -= heap2[o]
            ans = max(ans, cnt2) o range(rest2) range rest2 if o >= len(heap2):
                break
             o >= len(heap2) o len(heap2) len heap2 break cnt2 -= heap2[o] cnt2 heap2[o] heap2 o ans = max(ans, cnt2) ans max(ans, cnt2) max ans cnt2 print(ans) print ans k = map(int, input().split()) map(int, input().split()) k cnt += v[j] v[j] cnt heap2 = heap[:] heap[:] heap2 ans = max(ans, cnt2) max(ans, cnt2) ans cnt2 -= heap2[o] heap2[o] cnt2 ans = 0 0 ans ans = max(ans, cnt) max(ans, cnt) ans cnt2 += v[index] v[index] cnt2 y = min(n, k) min(n, k) y right = n - i - 1 n - i - 1 right n, k = map(int, input().split()) map(int, input().split()) n v = list(map(int, input().split())) list(map(int, input().split())) v cnt2 = cnt cnt cnt2 ans = max(ans, cnt2) max(ans, cnt2) ans rest = k - i - 1 k - i - 1 rest index = -1 -1 index ans = max(ans, cnt) max(ans, cnt) ans index -= 1 1 index rest2 = rest - r rest - r rest2 cnt = 0 0 cnt heap = [] [] heap
import heapq
n, k = map(int, input().split())
v = list(map(int, input().split()))

y = min(n, k)

ans = 0

for i in range(y):
    cnt = 0
    heap = []
    for j in range(i+1):
        if v[j] < 0:
            heap.append(v[j])
            ans = max(ans, cnt)
        cnt += v[j]
        ans = max(ans, cnt)

    rest = k - i - 1
    right = n - i - 1
    for r in range(min(rest, right)+1):
        index = -1
        cnt2 = cnt
        heap2 = heap[:]
        for _ in range(r):
            cnt2 += v[index]
            if v[index] < 0:
                heap2.append(v[index])
            index -= 1

        ans = max(ans, cnt2)
        rest2 = rest - r
        heap2.sort()
        for o in range(rest2):
            if o >= len(heap2):
                break
            cnt2 -= heap2[o]
            ans = max(ans, cnt2)

print(ans)