from heapq import heapify, heappop, heappush

N, K = map(int, input().split())
V = list(map(int, input().split()))
INF = 10**9

def h(X, cnt):
  S = sum(X)
  heapify(X)
  limit = cnt
  while cnt < K and cnt < 2*limit:
    m = heappop(X)
    if m >= 0:
      break
    S -= m
    cnt += 1
  return S

ans = 0

for i in range(1, min(N, K)+1):
  if i != N:
    for j in range(i+1):
      X = V[:i-j] + V[N-j:]
      ans = max(ans, h(X, i))
  else:
    ans = max(ans, h(V, i))

print(ans) from heapq import heapify, heappop, heappush 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 INF = 10**9 INF 10**9 10 9 def h(X, cnt):
  S = sum(X)
  heapify(X)
  limit = cnt
  while cnt < K and cnt < 2*limit:
    m = heappop(X)
    if m >= 0:
      break
    S -= m
    cnt += 1
  return S h S = sum(X) S sum(X) sum X heapify(X) heapify X limit = cnt limit cnt while cnt < K and cnt < 2*limit:
    m = heappop(X)
    if m >= 0:
      break
    S -= m
    cnt += 1
   cnt < K and cnt < 2*limit cnt < K cnt K cnt < 2*limit cnt 2*limit 2 limit m = heappop(X) m heappop(X) heappop X if m >= 0:
      break
     m >= 0 m 0 break S -= m S m cnt += 1 cnt 1 return S S X X cnt cnt ans = 0 ans 0 for i in range(1, min(N, K)+1):
  if i != N:
    for j in range(i+1):
      X = V[:i-j] + V[N-j:]
      ans = max(ans, h(X, i))
  else:
    ans = max(ans, h(V, i)) i range(1, min(N, K)+1) range 1 min(N, K)+1 min(N, K) min N K 1 if i != N:
    for j in range(i+1):
      X = V[:i-j] + V[N-j:]
      ans = max(ans, h(X, i))
  else:
    ans = max(ans, h(V, i)) i != N i N for j in range(i+1):
      X = V[:i-j] + V[N-j:]
      ans = max(ans, h(X, i))
   j range(i+1) range i+1 i 1 X = V[:i-j] + V[N-j:] X V[:i-j] + V[N-j:] V[:i-j] V :i-j i-j i j V[N-j:] V N-j: N-j N j ans = max(ans, h(X, i)) ans max(ans, h(X, i)) max ans h(X, i) h X i ans = max(ans, h(V, i)) ans max(ans, h(V, i)) max ans h(V, i) h V i print(ans) print ans V = list(map(int, input().split())) list(map(int, input().split())) V ans = max(ans, h(V, i)) max(ans, h(V, i)) ans K = map(int, input().split()) map(int, input().split()) K N, K = map(int, input().split()) map(int, input().split()) N ans = 0 0 ans ans = max(ans, h(X, i)) max(ans, h(X, i)) ans INF = 10**9 10**9 INF X = V[:i-j] + V[N-j:] V[:i-j] + V[N-j:] X def h(X, cnt):
  S = sum(X)
  heapify(X)
  limit = cnt
  while cnt < K and cnt < 2*limit:
    m = heappop(X)
    if m >= 0:
      break
    S -= m
    cnt += 1
  return S def h(X, cnt):
  S = sum(X)
  heapify(X)
  limit = cnt
  while cnt < K and cnt < 2*limit:
    m = heappop(X)
    if m >= 0:
      break
    S -= m
    cnt += 1
  return S h
from heapq import heapify, heappop, heappush

N, K = map(int, input().split())
V = list(map(int, input().split()))
INF = 10**9

def h(X, cnt):
  S = sum(X)
  heapify(X)
  limit = cnt
  while cnt < K and cnt < 2*limit:
    m = heappop(X)
    if m >= 0:
      break
    S -= m
    cnt += 1
  return S

ans = 0

for i in range(1, min(N, K)+1):
  if i != N:
    for j in range(i+1):
      X = V[:i-j] + V[N-j:]
      ans = max(ans, h(X, i))
  else:
    ans = max(ans, h(V, i))

print(ans)