import sys
input = sys.stdin.readline
n,k = map(int,input().split())
a = [int(i) for i in input().split()]
b = a[::-1]
ans = 0
from heapq import heapify,heappop,heappush

for l in range(1,k+1):
  ans_l = 0
  ans_r = 0
  for j in range(1,l+1):
    left = a[:j]
    chk_l = sum(left)
    heapify(left)
    heappush(left,0)
    for jj in range(l-j):
      p = heappop(left)
      if p >= 0:
        break
      chk_l -= p
    ans_l = max(ans_l,chk_l)
    
  if l < n:
    r = k-l
    for j in range(1,r+1):
      m = min(j,n-l)
      right = b[:m]
      chk_r = sum(right)
      heapify(right)
      heappush(right,0)
      for jj in range(r-m):
        q = heappop(right)
        if q >= 0:
          break
        chk_r -= q
      ans_r = max(ans_r,chk_r)
      
  ans = max(ans,ans_r+ans_l)
print(ans) import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline n,k = map(int,input().split()) n map(int,input().split()) map int input().split() ().split () input split k int(i) for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) int i a = [int(i) for i in input().split()] a [int(i) for i in input().split()] b = a[::-1] b a[::-1] a ::-1 -1 ans = 0 ans 0 from heapq import heapify,heappop,heappush for l in range(1,k+1):
  ans_l = 0
  ans_r = 0
  for j in range(1,l+1):
    left = a[:j]
    chk_l = sum(left)
    heapify(left)
    heappush(left,0)
    for jj in range(l-j):
      p = heappop(left)
      if p >= 0:
        break
      chk_l -= p
    ans_l = max(ans_l,chk_l)
    
  if l < n:
    r = k-l
    for j in range(1,r+1):
      m = min(j,n-l)
      right = b[:m]
      chk_r = sum(right)
      heapify(right)
      heappush(right,0)
      for jj in range(r-m):
        q = heappop(right)
        if q >= 0:
          break
        chk_r -= q
      ans_r = max(ans_r,chk_r)
      
  ans = max(ans,ans_r+ans_l) l range(1,k+1) range 1 k+1 k 1 ans_l = 0 ans_l 0 ans_r = 0 ans_r 0 for j in range(1,l+1):
    left = a[:j]
    chk_l = sum(left)
    heapify(left)
    heappush(left,0)
    for jj in range(l-j):
      p = heappop(left)
      if p >= 0:
        break
      chk_l -= p
    ans_l = max(ans_l,chk_l)
    
   j range(1,l+1) range 1 l+1 l 1 left = a[:j] left a[:j] a :j j chk_l = sum(left) chk_l sum(left) sum left heapify(left) heapify left heappush(left,0) heappush left 0 for jj in range(l-j):
      p = heappop(left)
      if p >= 0:
        break
      chk_l -= p
     jj range(l-j) range l-j l j p = heappop(left) p heappop(left) heappop left if p >= 0:
        break
       p >= 0 p 0 break chk_l -= p chk_l p ans_l = max(ans_l,chk_l) ans_l max(ans_l,chk_l) max ans_l chk_l if l < n:
    r = k-l
    for j in range(1,r+1):
      m = min(j,n-l)
      right = b[:m]
      chk_r = sum(right)
      heapify(right)
      heappush(right,0)
      for jj in range(r-m):
        q = heappop(right)
        if q >= 0:
          break
        chk_r -= q
      ans_r = max(ans_r,chk_r)
      
   l < n l n r = k-l r k-l k l for j in range(1,r+1):
      m = min(j,n-l)
      right = b[:m]
      chk_r = sum(right)
      heapify(right)
      heappush(right,0)
      for jj in range(r-m):
        q = heappop(right)
        if q >= 0:
          break
        chk_r -= q
      ans_r = max(ans_r,chk_r)
      
   j range(1,r+1) range 1 r+1 r 1 m = min(j,n-l) m min(j,n-l) min j n-l n l right = b[:m] right b[:m] b :m m chk_r = sum(right) chk_r sum(right) sum right heapify(right) heapify right heappush(right,0) heappush right 0 for jj in range(r-m):
        q = heappop(right)
        if q >= 0:
          break
        chk_r -= q
       jj range(r-m) range r-m r m q = heappop(right) q heappop(right) heappop right if q >= 0:
          break
         q >= 0 q 0 break chk_r -= q chk_r q ans_r = max(ans_r,chk_r) ans_r max(ans_r,chk_r) max ans_r chk_r ans = max(ans,ans_r+ans_l) ans max(ans,ans_r+ans_l) max ans ans_r+ans_l ans_r ans_l 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 chk_r = sum(right) sum(right) chk_r left = a[:j] a[:j] left r = k-l k-l r chk_l -= p p chk_l ans_l = max(ans_l,chk_l) max(ans_l,chk_l) ans_l p = heappop(left) heappop(left) p a = [int(i) for i in input().split()] [int(i) for i in input().split()] a q = heappop(right) heappop(right) q m = min(j,n-l) min(j,n-l) m ans = 0 0 ans ans_l = 0 0 ans_l ans_r = max(ans_r,chk_r) max(ans_r,chk_r) ans_r ans = max(ans,ans_r+ans_l) max(ans,ans_r+ans_l) ans chk_l = sum(left) sum(left) chk_l ans_r = 0 0 ans_r b = a[::-1] a[::-1] b input = sys.stdin.readline sys.stdin.readline input chk_r -= q q chk_r right = b[:m] b[:m] right
import sys
input = sys.stdin.readline
n,k = map(int,input().split())
a = [int(i) for i in input().split()]
b = a[::-1]
ans = 0
from heapq import heapify,heappop,heappush

for l in range(1,k+1):
  ans_l = 0
  ans_r = 0
  for j in range(1,l+1):
    left = a[:j]
    chk_l = sum(left)
    heapify(left)
    heappush(left,0)
    for jj in range(l-j):
      p = heappop(left)
      if p >= 0:
        break
      chk_l -= p
    ans_l = max(ans_l,chk_l)
    
  if l < n:
    r = k-l
    for j in range(1,r+1):
      m = min(j,n-l)
      right = b[:m]
      chk_r = sum(right)
      heapify(right)
      heappush(right,0)
      for jj in range(r-m):
        q = heappop(right)
        if q >= 0:
          break
        chk_r -= q
      ans_r = max(ans_r,chk_r)
      
  ans = max(ans,ans_r+ans_l)
print(ans)