Problem: Your friend gave you a dequeue D as a birthday present.  D is a horizontal cylinder that contains a row of N jewels.  The _values_ of the jewels are V_1, V_2, ..., V_N from left to right. There may be jewels with negative values.  In the beginning, you have no jewel in your hands.  You can perform at most K operations on D, chosen from the following, at most K times (possibly zero):    * Operation A: Take out the leftmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty.    * Operation B: Take out the rightmost jewel contained in D and have it in your hand. You cannot do this operation when D is empty.    * Operation C: Choose a jewel in your hands and insert it to the left end of D. You cannot do this operation when you have no jewel in your hand.    * Operation D: Choose a jewel in your hands and insert it to the right end of D. You cannot do this operation when you have no jewel in your hand.  Find the maximum possible sum of the values of jewels in your hands after the operations.

Submission attempts (in order):
import itertools as it
import collections as col
import heapq
import sys
sys.setrecursionlimit(10**9)
"""
N=int(input())
D=[]
for i in range(N):
  s,p=input().split()
  D.append((s, int(p), i+1))
D.sort(key=lambda d: (d[0], -d[1], d[2]))

for d in D:
  print(d[2])
"""
"""
N,M=map(int, input().split())
K=[]
S=[]
for i in range(M):
  k,*si=map(int, input().split())
  K.append(k)
  S.append(list(sorted(si)))
P=list(map(int, input().split()))

L=it.product([0,1], repeat=N)
cnt=0
#print(S)
for l in L:
  ok=True
  for i,si in enumerate(S):
    pi=P[i]
    sumS=len(list(filter(lambda it: l[it-1]==1, si)))
    #print(l, sumS)
    if sumS%2 != pi: ok=False
  if ok: cnt += 1
print(cnt)
"""
N,K=map(int,input().split())
V=col.deque(map(int, input().split()))
M=max(N,K)

ans=0

for l in range(M):
  for r in range(M):
    if l+r<=min(N,K):
      H=[]
      heapq.heapify(H)
      U=col.deque(V)
      for i in range(l):
        heapq.heappush(H, U.popleft())
      for i in range(r):
        heapq.heappush(H, U.pop())
      #print((l,r),U, H)
      i=0
      while H and i<K-l-r:
        rm=heapq.heappop(H)
        i += 1
        if rm>0:
          heapq.heappush(H, rm)
          break
      print(sum(H), H)
      ans=max(ans, sum(H))
print(ans)


import itertools as it
import collections as col
import heapq
import sys
sys.setrecursionlimit(10**9)
"""
N=int(input())
D=[]
for i in range(N):
  s,p=input().split()
  D.append((s, int(p), i+1))
D.sort(key=lambda d: (d[0], -d[1], d[2]))

for d in D:
  print(d[2])
"""
"""
N,M=map(int, input().split())
K=[]
S=[]
for i in range(M):
  k,*si=map(int, input().split())
  K.append(k)
  S.append(list(sorted(si)))
P=list(map(int, input().split()))

L=it.product([0,1], repeat=N)
cnt=0
#print(S)
for l in L:
  ok=True
  for i,si in enumerate(S):
    pi=P[i]
    sumS=len(list(filter(lambda it: l[it-1]==1, si)))
    #print(l, sumS)
    if sumS%2 != pi: ok=False
  if ok: cnt += 1
print(cnt)
"""
N,K=map(int,input().split())
V=list(map(int, input().split()))
M=max(N,K)

ans=0

#print(M)
for l in range(M+1):
  for r in range(M+1):
    if l+r<=min(N,K):
      H=[]
      U=list(V)
      H+=U[:l] + U[N-r:]
      H.sort(reverse=True)
      #print((l,r),U, H)
      for i in range(K-l-r):
        if H and H[-1] < 0: H.pop()
        else: break
      #print(sum(H), H)
      ans=max(ans, sum(H))
print(ans)


Verdicts: Wrong Answer

Accepted