import heapq
from collections import deque
from copy import deepcopy

n,k = map(int,input().split())
V = list(map(int,input().split()))

dq = deque()
dq.extend(V)
INF = 10**10

ans = -INF
for i in range(k+1):
  for j in range(k+1-i):           
    que = deepcopy(dq)
    a = []    
    
    if i+j <= n:
      for l in range(i):      
        heapq.heappush(a,que.popleft())
      for r in range(j):
        heapq.heappush(a,que.pop())    
      
      ans = max(ans,sum(a))        
      for ki in range(k-i-j):   
        if len(a) > 0:
          heapq.heappop(a)
        ans = max(ans,sum(a))        
        
if ans != -INF:          
  print(ans)
else:
  print(0)   import heapq heapq from collections import deque from copy import deepcopy 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 dq = deque() dq deque() deque dq.extend(V) dq.extend dq extend V INF = 10**10 INF 10**10 10 10 ans = -INF ans -INF INF for i in range(k+1):
  for j in range(k+1-i):           
    que = deepcopy(dq)
    a = []    
    
    if i+j <= n:
      for l in range(i):      
        heapq.heappush(a,que.popleft())
      for r in range(j):
        heapq.heappush(a,que.pop())    
      
      ans = max(ans,sum(a))        
      for ki in range(k-i-j):   
        if len(a) > 0:
          heapq.heappop(a)
        ans = max(ans,sum(a))        
         i range(k+1) range k+1 k 1 for j in range(k+1-i):           
    que = deepcopy(dq)
    a = []    
    
    if i+j <= n:
      for l in range(i):      
        heapq.heappush(a,que.popleft())
      for r in range(j):
        heapq.heappush(a,que.pop())    
      
      ans = max(ans,sum(a))        
      for ki in range(k-i-j):   
        if len(a) > 0:
          heapq.heappop(a)
        ans = max(ans,sum(a))        
         j range(k+1-i) range k+1-i k+1 k 1 i que = deepcopy(dq) que deepcopy(dq) deepcopy dq a = [] a [] if i+j <= n:
      for l in range(i):      
        heapq.heappush(a,que.popleft())
      for r in range(j):
        heapq.heappush(a,que.pop())    
      
      ans = max(ans,sum(a))        
      for ki in range(k-i-j):   
        if len(a) > 0:
          heapq.heappop(a)
        ans = max(ans,sum(a))        
         i+j <= n i+j i j n for l in range(i):      
        heapq.heappush(a,que.popleft())
       l range(i) range i heapq.heappush(a,que.popleft()) heapq.heappush heapq heappush a que.popleft() que.popleft que popleft for r in range(j):
        heapq.heappush(a,que.pop())    
      
       r range(j) range j heapq.heappush(a,que.pop()) heapq.heappush heapq heappush a que.pop() que.pop que pop ans = max(ans,sum(a)) ans max(ans,sum(a)) max ans sum(a) sum a for ki in range(k-i-j):   
        if len(a) > 0:
          heapq.heappop(a)
        ans = max(ans,sum(a))        
         ki range(k-i-j) range k-i-j k-i k i j if len(a) > 0:
          heapq.heappop(a)
         len(a) > 0 len(a) len a 0 heapq.heappop(a) heapq.heappop heapq heappop a ans = max(ans,sum(a)) ans max(ans,sum(a)) max ans sum(a) sum a if ans != -INF:          
  print(ans)
else:
  print(0)   ans != -INF ans -INF INF print(ans) print ans print(0) print 0 k = map(int,input().split()) map(int,input().split()) k ans = -INF -INF ans que = deepcopy(dq) deepcopy(dq) que INF = 10**10 10**10 INF ans = max(ans,sum(a)) max(ans,sum(a)) ans n,k = map(int,input().split()) map(int,input().split()) n V = list(map(int,input().split())) list(map(int,input().split())) V ans = max(ans,sum(a)) max(ans,sum(a)) ans dq = deque() deque() dq a = [] [] a
import heapq
from collections import deque
from copy import deepcopy

n,k = map(int,input().split())
V = list(map(int,input().split()))

dq = deque()
dq.extend(V)
INF = 10**10

ans = -INF
for i in range(k+1):
  for j in range(k+1-i):           
    que = deepcopy(dq)
    a = []    
    
    if i+j <= n:
      for l in range(i):      
        heapq.heappush(a,que.popleft())
      for r in range(j):
        heapq.heappush(a,que.pop())    
      
      ans = max(ans,sum(a))        
      for ki in range(k-i-j):   
        if len(a) > 0:
          heapq.heappop(a)
        ans = max(ans,sum(a))        
        
if ans != -INF:          
  print(ans)
else:
  print(0)  