N,K=map(int,input().split())
V=list(map(int,input().split()))
import heapq
turn=min(N,K)
m=0
for A in range(turn+1):
  for B in range(turn+1-A):
    h=[]
    heapq.heapify(h)
    if A>0:
      h=V[:A]+h
    if B>0:
      h=h+V[len(V)-B:]
    heapq.heapify(h)  

    ans=sum(h)
    for i in range(K-A-B):
      if h!=[]:
        f=heapq.heappop(h)
 
        if f<0:
          ans-=f
    m=max(ans,m)


    
print(m)    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 import heapq heapq turn=min(N,K) turn min(N,K) min N K m=0 m 0 for A in range(turn+1):
  for B in range(turn+1-A):
    h=[]
    heapq.heapify(h)
    if A>0:
      h=V[:A]+h
    if B>0:
      h=h+V[len(V)-B:]
    heapq.heapify(h)  

    ans=sum(h)
    for i in range(K-A-B):
      if h!=[]:
        f=heapq.heappop(h)
 
        if f<0:
          ans-=f
    m=max(ans,m)


     A range(turn+1) range turn+1 turn 1 for B in range(turn+1-A):
    h=[]
    heapq.heapify(h)
    if A>0:
      h=V[:A]+h
    if B>0:
      h=h+V[len(V)-B:]
    heapq.heapify(h)  

    ans=sum(h)
    for i in range(K-A-B):
      if h!=[]:
        f=heapq.heappop(h)
 
        if f<0:
          ans-=f
    m=max(ans,m)


     B range(turn+1-A) range turn+1-A turn+1 turn 1 A h=[] h [] heapq.heapify(h) heapq.heapify heapq heapify h if A>0:
      h=V[:A]+h
     A>0 A 0 h=V[:A]+h h V[:A]+h V[:A] V :A A h if B>0:
      h=h+V[len(V)-B:]
     B>0 B 0 h=h+V[len(V)-B:] h h+V[len(V)-B:] h V[len(V)-B:] V len(V)-B: len(V)-B len(V) len V B heapq.heapify(h) heapq.heapify heapq heapify h ans=sum(h) ans sum(h) sum h for i in range(K-A-B):
      if h!=[]:
        f=heapq.heappop(h)
 
        if f<0:
          ans-=f
     i range(K-A-B) range K-A-B K-A K A B if h!=[]:
        f=heapq.heappop(h)
 
        if f<0:
          ans-=f
     h!=[] h [] f=heapq.heappop(h) f heapq.heappop(h) heapq.heappop heapq heappop h if f<0:
          ans-=f
     f<0 f 0 ans-=f ans f m=max(ans,m) m max(ans,m) max ans m print(m) print m V=list(map(int,input().split())) list(map(int,input().split())) V h=[] [] h h=h+V[len(V)-B:] h+V[len(V)-B:] h N,K=map(int,input().split()) map(int,input().split()) N m=0 0 m ans=sum(h) sum(h) ans f=heapq.heappop(h) heapq.heappop(h) f h=V[:A]+h V[:A]+h h m=max(ans,m) max(ans,m) m K=map(int,input().split()) map(int,input().split()) K turn=min(N,K) min(N,K) turn ans-=f f ans
N,K=map(int,input().split())
V=list(map(int,input().split()))
import heapq
turn=min(N,K)
m=0
for A in range(turn+1):
  for B in range(turn+1-A):
    h=[]
    heapq.heapify(h)
    if A>0:
      h=V[:A]+h
    if B>0:
      h=h+V[len(V)-B:]
    heapq.heapify(h)  

    ans=sum(h)
    for i in range(K-A-B):
      if h!=[]:
        f=heapq.heappop(h)
 
        if f<0:
          ans-=f
    m=max(ans,m)


    
print(m)   