import copy
from collections import deque
N,K = map(int,input().split())
A = list(map(int,input().split()))
ans = 0
for k in range(K+1): #0-K
  for left in range(N+1): #0-N
    for right in range(N-left+1):
      push = k - (left + right)
      if push < 0 or push > left+right:
        continue
      Acopy = copy.copy(A)
      Acopy = deque(Acopy)
      B = []
      for t in range(left):
        temp = Acopy.popleft()
        B.append(temp)
      for t in range(right):
        #print(left,right)
        temp = Acopy.pop()
        B.append(temp)
      B.sort()
      wa = sum(B)-sum(B[:push])
      ans = max(ans,wa)
print(ans) import copy copy from collections import deque 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 ans = 0 ans 0 for k in range(K+1): #0-K
  for left in range(N+1): #0-N
    for right in range(N-left+1):
      push = k - (left + right)
      if push < 0 or push > left+right:
        continue
      Acopy = copy.copy(A)
      Acopy = deque(Acopy)
      B = []
      for t in range(left):
        temp = Acopy.popleft()
        B.append(temp)
      for t in range(right):
        #print(left,right)
        temp = Acopy.pop()
        B.append(temp)
      B.sort()
      wa = sum(B)-sum(B[:push])
      ans = max(ans,wa) k range(K+1) range K+1 K 1 for left in range(N+1): #0-N
    for right in range(N-left+1):
      push = k - (left + right)
      if push < 0 or push > left+right:
        continue
      Acopy = copy.copy(A)
      Acopy = deque(Acopy)
      B = []
      for t in range(left):
        temp = Acopy.popleft()
        B.append(temp)
      for t in range(right):
        #print(left,right)
        temp = Acopy.pop()
        B.append(temp)
      B.sort()
      wa = sum(B)-sum(B[:push])
      ans = max(ans,wa) left range(N+1) range N+1 N 1 for right in range(N-left+1):
      push = k - (left + right)
      if push < 0 or push > left+right:
        continue
      Acopy = copy.copy(A)
      Acopy = deque(Acopy)
      B = []
      for t in range(left):
        temp = Acopy.popleft()
        B.append(temp)
      for t in range(right):
        #print(left,right)
        temp = Acopy.pop()
        B.append(temp)
      B.sort()
      wa = sum(B)-sum(B[:push])
      ans = max(ans,wa) right range(N-left+1) range N-left+1 N-left N left 1 push = k - (left + right) push k - (left + right) k left + right left right if push < 0 or push > left+right:
        continue
       push < 0 or push > left+right push < 0 push 0 push > left+right push left+right left right continue Acopy = copy.copy(A) Acopy copy.copy(A) copy.copy copy copy A Acopy = deque(Acopy) Acopy deque(Acopy) deque Acopy B = [] B [] for t in range(left):
        temp = Acopy.popleft()
        B.append(temp)
       t range(left) range left temp = Acopy.popleft() temp Acopy.popleft() Acopy.popleft Acopy popleft B.append(temp) B.append B append temp for t in range(right):
        #print(left,right)
        temp = Acopy.pop()
        B.append(temp)
       t range(right) range right temp = Acopy.pop() temp Acopy.pop() Acopy.pop Acopy pop B.append(temp) B.append B append temp B.sort() B.sort B sort wa = sum(B)-sum(B[:push]) wa sum(B)-sum(B[:push]) sum(B) sum B sum(B[:push]) sum B[:push] B :push push ans = max(ans,wa) ans max(ans,wa) max ans wa print(ans) print ans Acopy = deque(Acopy) deque(Acopy) Acopy A = list(map(int,input().split())) list(map(int,input().split())) A push = k - (left + right) k - (left + right) push temp = Acopy.pop() Acopy.pop() temp wa = sum(B)-sum(B[:push]) sum(B)-sum(B[:push]) wa B = [] [] B ans = 0 0 ans N,K = map(int,input().split()) map(int,input().split()) N K = map(int,input().split()) map(int,input().split()) K Acopy = copy.copy(A) copy.copy(A) Acopy temp = Acopy.popleft() Acopy.popleft() temp ans = max(ans,wa) max(ans,wa) ans
import copy
from collections import deque
N,K = map(int,input().split())
A = list(map(int,input().split()))
ans = 0
for k in range(K+1): #0-K
  for left in range(N+1): #0-N
    for right in range(N-left+1):
      push = k - (left + right)
      if push < 0 or push > left+right:
        continue
      Acopy = copy.copy(A)
      Acopy = deque(Acopy)
      B = []
      for t in range(left):
        temp = Acopy.popleft()
        B.append(temp)
      for t in range(right):
        #print(left,right)
        temp = Acopy.pop()
        B.append(temp)
      B.sort()
      wa = sum(B)-sum(B[:push])
      ans = max(ans,wa)
print(ans)
