import heapq
n, k = map(int, input().split())
r = min(n, k)
v = list(map(int, input().split()))
ans = 0
for i in range(1, r+1):
  for j in range(i+1):
    a = j
    b = i-j
    v_temp = v[:a]
    v_temp.extend(v[n-b:])
    heapq.heapify(v_temp)
    for l in range(k-i):
      get = heapq.heappop(v_temp)
      if get>=0:
        heapq.heappush(v_temp, get)
        break
      if not v_temp:
        break
    v_temp.append(0)
    ans = max(ans, sum(v_temp))
print(ans) import heapq heapq n, k = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split k r = min(n, k) r min(n, k) min n 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(1, r+1):
  for j in range(i+1):
    a = j
    b = i-j
    v_temp = v[:a]
    v_temp.extend(v[n-b:])
    heapq.heapify(v_temp)
    for l in range(k-i):
      get = heapq.heappop(v_temp)
      if get>=0:
        heapq.heappush(v_temp, get)
        break
      if not v_temp:
        break
    v_temp.append(0)
    ans = max(ans, sum(v_temp)) i range(1, r+1) range 1 r+1 r 1 for j in range(i+1):
    a = j
    b = i-j
    v_temp = v[:a]
    v_temp.extend(v[n-b:])
    heapq.heapify(v_temp)
    for l in range(k-i):
      get = heapq.heappop(v_temp)
      if get>=0:
        heapq.heappush(v_temp, get)
        break
      if not v_temp:
        break
    v_temp.append(0)
    ans = max(ans, sum(v_temp)) j range(i+1) range i+1 i 1 a = j a j b = i-j b i-j i j v_temp = v[:a] v_temp v[:a] v :a a v_temp.extend(v[n-b:]) v_temp.extend v_temp extend v[n-b:] v n-b: n-b n b heapq.heapify(v_temp) heapq.heapify heapq heapify v_temp for l in range(k-i):
      get = heapq.heappop(v_temp)
      if get>=0:
        heapq.heappush(v_temp, get)
        break
      if not v_temp:
        break
     l range(k-i) range k-i k i get = heapq.heappop(v_temp) get heapq.heappop(v_temp) heapq.heappop heapq heappop v_temp if get>=0:
        heapq.heappush(v_temp, get)
        break
       get>=0 get 0 heapq.heappush(v_temp, get) heapq.heappush heapq heappush v_temp get break if not v_temp:
        break
     not v_temp v_temp break v_temp.append(0) v_temp.append v_temp append 0 ans = max(ans, sum(v_temp)) ans max(ans, sum(v_temp)) max ans sum(v_temp) sum v_temp print(ans) print ans ans = 0 0 ans get = heapq.heappop(v_temp) heapq.heappop(v_temp) get r = min(n, k) min(n, k) r v = list(map(int, input().split())) list(map(int, input().split())) v b = i-j i-j b a = j j a v_temp = v[:a] v[:a] v_temp k = map(int, input().split()) map(int, input().split()) k ans = max(ans, sum(v_temp)) max(ans, sum(v_temp)) ans n, k = map(int, input().split()) map(int, input().split()) n
import heapq
n, k = map(int, input().split())
r = min(n, k)
v = list(map(int, input().split()))
ans = 0
for i in range(1, r+1):
  for j in range(i+1):
    a = j
    b = i-j
    v_temp = v[:a]
    v_temp.extend(v[n-b:])
    heapq.heapify(v_temp)
    for l in range(k-i):
      get = heapq.heappop(v_temp)
      if get>=0:
        heapq.heappush(v_temp, get)
        break
      if not v_temp:
        break
    v_temp.append(0)
    ans = max(ans, sum(v_temp))
print(ans)