# 左から取り出すパターンは最大50通り
# 右から取り出すパターンは最大50通り
# その組み合わせは2500通り
# その全通りに対して、マイナスの石を引けるだけ引くパターンを試すと50
# 125000
# すべて試す

import sys
readline = sys.stdin.readline

N,K = map(int,readline().split())
V = [0] + list(map(int,readline().split())) + [0]
N += 2
K += 2

leftsum = 0
leftminus = []
ans = -(10 ** 9)
for left in range(min(N,K)):
  leftsum += V[left]
  if V[left] < 0:
    leftminus += [V[left]]
  rightsum = 0
  rightminus = []
  limit = min(N - (left + 1) , K - (left + 1))
  for right in range(N - 1, N - limit - 1, -1):
    rightsum += V[right]
    if V[right] < 0:
      rightminus += [V[right]]
    allsum = leftsum + rightsum
    rest = max(K - ((left + 1) + (N - right)),0)
    allminus = sorted(leftminus + rightminus)
    allsum -= sum(allminus[:rest])
    if allsum > ans:
      ans = allsum
print(ans) import sys sys readline = sys.stdin.readline readline sys.stdin.readline sys.stdin sys stdin readline N,K = map(int,readline().split()) N map(int,readline().split()) map int readline().split() ().split () readline split K V = [0] + list(map(int,readline().split())) + [0] V [0] + list(map(int,readline().split())) + [0] [0] + list(map(int,readline().split())) [0] 0 list(map(int,readline().split())) list map(int,readline().split()) map int readline().split() ().split () readline split [0] 0 N += 2 N 2 K += 2 K 2 leftsum = 0 leftsum 0 leftminus = [] leftminus [] ans = -(10 ** 9) ans -(10 ** 9) 10 ** 9 10 9 for left in range(min(N,K)):
  leftsum += V[left]
  if V[left] < 0:
    leftminus += [V[left]]
  rightsum = 0
  rightminus = []
  limit = min(N - (left + 1) , K - (left + 1))
  for right in range(N - 1, N - limit - 1, -1):
    rightsum += V[right]
    if V[right] < 0:
      rightminus += [V[right]]
    allsum = leftsum + rightsum
    rest = max(K - ((left + 1) + (N - right)),0)
    allminus = sorted(leftminus + rightminus)
    allsum -= sum(allminus[:rest])
    if allsum > ans:
      ans = allsum left range(min(N,K)) range min(N,K) min N K leftsum += V[left] leftsum V[left] V left if V[left] < 0:
    leftminus += [V[left]]
   V[left] < 0 V[left] V left 0 leftminus += [V[left]] leftminus [V[left]] V[left] V left rightsum = 0 rightsum 0 rightminus = [] rightminus [] limit = min(N - (left + 1) , K - (left + 1)) limit min(N - (left + 1) , K - (left + 1)) min N - (left + 1) N left + 1 left 1 K - (left + 1) K left + 1 left 1 for right in range(N - 1, N - limit - 1, -1):
    rightsum += V[right]
    if V[right] < 0:
      rightminus += [V[right]]
    allsum = leftsum + rightsum
    rest = max(K - ((left + 1) + (N - right)),0)
    allminus = sorted(leftminus + rightminus)
    allsum -= sum(allminus[:rest])
    if allsum > ans:
      ans = allsum right range(N - 1, N - limit - 1, -1) range N - 1 N 1 N - limit - 1 N - limit N limit 1 -1 rightsum += V[right] rightsum V[right] V right if V[right] < 0:
      rightminus += [V[right]]
     V[right] < 0 V[right] V right 0 rightminus += [V[right]] rightminus [V[right]] V[right] V right allsum = leftsum + rightsum allsum leftsum + rightsum leftsum rightsum rest = max(K - ((left + 1) + (N - right)),0) rest max(K - ((left + 1) + (N - right)),0) max K - ((left + 1) + (N - right)) K (left + 1) + (N - right) left + 1 left 1 N - right N right 0 allminus = sorted(leftminus + rightminus) allminus sorted(leftminus + rightminus) sorted leftminus + rightminus leftminus rightminus allsum -= sum(allminus[:rest]) allsum sum(allminus[:rest]) sum allminus[:rest] allminus :rest rest if allsum > ans:
      ans = allsum allsum > ans allsum ans ans = allsum ans allsum print(ans) print ans V = [0] + list(map(int,readline().split())) + [0] [0] + list(map(int,readline().split())) + [0] V allsum -= sum(allminus[:rest]) sum(allminus[:rest]) allsum ans = -(10 ** 9) -(10 ** 9) ans allsum = leftsum + rightsum leftsum + rightsum allsum leftminus += [V[left]] [V[left]] leftminus readline = sys.stdin.readline sys.stdin.readline readline N,K = map(int,readline().split()) map(int,readline().split()) N rightminus += [V[right]] [V[right]] rightminus leftminus = [] [] leftminus allminus = sorted(leftminus + rightminus) sorted(leftminus + rightminus) allminus rightsum = 0 0 rightsum limit = min(N - (left + 1) , K - (left + 1)) min(N - (left + 1) , K - (left + 1)) limit N += 2 2 N leftsum += V[left] V[left] leftsum ans = allsum allsum ans leftsum = 0 0 leftsum rightminus = [] [] rightminus K += 2 2 K K = map(int,readline().split()) map(int,readline().split()) K rightsum += V[right] V[right] rightsum rest = max(K - ((left + 1) + (N - right)),0) max(K - ((left + 1) + (N - right)),0) rest
# 左から取り出すパターンは最大50通り
# 右から取り出すパターンは最大50通り
# その組み合わせは2500通り
# その全通りに対して、マイナスの石を引けるだけ引くパターンを試すと50
# 125000
# すべて試す

import sys
readline = sys.stdin.readline

N,K = map(int,readline().split())
V = [0] + list(map(int,readline().split())) + [0]
N += 2
K += 2

leftsum = 0
leftminus = []
ans = -(10 ** 9)
for left in range(min(N,K)):
  leftsum += V[left]
  if V[left] < 0:
    leftminus += [V[left]]
  rightsum = 0
  rightminus = []
  limit = min(N - (left + 1) , K - (left + 1))
  for right in range(N - 1, N - limit - 1, -1):
    rightsum += V[right]
    if V[right] < 0:
      rightminus += [V[right]]
    allsum = leftsum + rightsum
    rest = max(K - ((left + 1) + (N - right)),0)
    allminus = sorted(leftminus + rightminus)
    allsum -= sum(allminus[:rest])
    if allsum > ans:
      ans = allsum
print(ans)