from collections import deque
import heapq
N,K = map(int,input().split())
A = list(map(int,input().split()))
res = 0
# 左からa個
for a in range(N+1):
  # 右からb個
  for b in range(N+1):
    if a+b>N or a+b>K: continue
    hands = []
    A1 = deque(A[::])
    for _ in range(a):
      hands.append(A1.popleft())
    for _ in range(b):
      hands.append(A1.pop())
    hands.sort()
    hands = deque(hands)
    rem = K - a - b
    for _ in range(rem):
      if hands and hands[0] < 0:
        hands.popleft()
      else:
        break
    res = max(res, sum(hands))

print(res) from collections import deque import heapq heapq N,K = map(int,input().split()) N map(int,input().split()) map int input().split() ().split () input split K A = list(map(int,input().split())) A list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split res = 0 res 0 for a in range(N+1):
  # 右からb個
  for b in range(N+1):
    if a+b>N or a+b>K: continue
    hands = []
    A1 = deque(A[::])
    for _ in range(a):
      hands.append(A1.popleft())
    for _ in range(b):
      hands.append(A1.pop())
    hands.sort()
    hands = deque(hands)
    rem = K - a - b
    for _ in range(rem):
      if hands and hands[0] < 0:
        hands.popleft()
      else:
        break
    res = max(res, sum(hands)) a range(N+1) range N+1 N 1 for b in range(N+1):
    if a+b>N or a+b>K: continue
    hands = []
    A1 = deque(A[::])
    for _ in range(a):
      hands.append(A1.popleft())
    for _ in range(b):
      hands.append(A1.pop())
    hands.sort()
    hands = deque(hands)
    rem = K - a - b
    for _ in range(rem):
      if hands and hands[0] < 0:
        hands.popleft()
      else:
        break
    res = max(res, sum(hands)) b range(N+1) range N+1 N 1 if a+b>N or a+b>K: continue
     a+b>N or a+b>K a+b>N a+b a b N a+b>K a+b a b K hands = [] hands [] A1 = deque(A[::]) A1 deque(A[::]) deque A[::] A :: for _ in range(a):
      hands.append(A1.popleft())
     _ range(a) range a hands.append(A1.popleft()) hands.append hands append A1.popleft() A1.popleft A1 popleft for _ in range(b):
      hands.append(A1.pop())
     _ range(b) range b hands.append(A1.pop()) hands.append hands append A1.pop() A1.pop A1 pop hands.sort() hands.sort hands sort hands = deque(hands) hands deque(hands) deque hands rem = K - a - b rem K - a - b K - a K a b for _ in range(rem):
      if hands and hands[0] < 0:
        hands.popleft()
      else:
        break
     _ range(rem) range rem if hands and hands[0] < 0:
        hands.popleft()
      else:
        break
     hands and hands[0] < 0 hands hands[0] < 0 hands[0] hands 0 0 hands.popleft() hands.popleft hands popleft break res = max(res, sum(hands)) res max(res, sum(hands)) max res sum(hands) sum hands print(res) print res N,K = map(int,input().split()) map(int,input().split()) N hands = [] [] hands A1 = deque(A[::]) deque(A[::]) A1 rem = K - a - b K - a - b rem res = max(res, sum(hands)) max(res, sum(hands)) res res = 0 0 res hands = deque(hands) deque(hands) hands A = list(map(int,input().split())) list(map(int,input().split())) A K = map(int,input().split()) map(int,input().split()) K
from collections import deque
import heapq
N,K = map(int,input().split())
A = list(map(int,input().split()))
res = 0
# 左からa個
for a in range(N+1):
  # 右からb個
  for b in range(N+1):
    if a+b>N or a+b>K: continue
    hands = []
    A1 = deque(A[::])
    for _ in range(a):
      hands.append(A1.popleft())
    for _ in range(b):
      hands.append(A1.pop())
    hands.sort()
    hands = deque(hands)
    rem = K - a - b
    for _ in range(rem):
      if hands and hands[0] < 0:
        hands.popleft()
      else:
        break
    res = max(res, sum(hands))

print(res)