import heapq
import copy
N, K = map(int, input().split())
V = list(map(int, input().split()))

max_val = 0
left_hq = list()
left_val = 0
for left in range(N+1):
    if left > 0:
        heapq.heappush(left_hq,V[left-1])
        left_val += V[left-1]
        all_hq = copy.deepcopy(left_hq)
        all_val = left_val
        for right in range(N+1):
            if right+left > K or right+left > N :
                continue
            if right > 0:
                heapq.heappush(all_hq,V[-right])
                all_val += V[-right]
            
            temp_hq = copy.deepcopy(all_hq)
            temp_val = all_val
            for i in range(K-left-right):
                if len(temp_hq) == 0:
                    break
                else:
                    minus = heapq.heappop(temp_hq)
                    if minus > 0:
                        break
                    temp_val -= minus
                    
            if temp_val > max_val:
                max_val = temp_val
print(max_val) import heapq heapq import copy copy 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 max_val = 0 max_val 0 left_hq = list() left_hq list() list left_val = 0 left_val 0 for left in range(N+1):
    if left > 0:
        heapq.heappush(left_hq,V[left-1])
        left_val += V[left-1]
        all_hq = copy.deepcopy(left_hq)
        all_val = left_val
        for right in range(N+1):
            if right+left > K or right+left > N :
                continue
            if right > 0:
                heapq.heappush(all_hq,V[-right])
                all_val += V[-right]
            
            temp_hq = copy.deepcopy(all_hq)
            temp_val = all_val
            for i in range(K-left-right):
                if len(temp_hq) == 0:
                    break
                else:
                    minus = heapq.heappop(temp_hq)
                    if minus > 0:
                        break
                    temp_val -= minus
                    
            if temp_val > max_val:
                max_val = temp_val left range(N+1) range N+1 N 1 if left > 0:
        heapq.heappush(left_hq,V[left-1])
        left_val += V[left-1]
        all_hq = copy.deepcopy(left_hq)
        all_val = left_val
        for right in range(N+1):
            if right+left > K or right+left > N :
                continue
            if right > 0:
                heapq.heappush(all_hq,V[-right])
                all_val += V[-right]
            
            temp_hq = copy.deepcopy(all_hq)
            temp_val = all_val
            for i in range(K-left-right):
                if len(temp_hq) == 0:
                    break
                else:
                    minus = heapq.heappop(temp_hq)
                    if minus > 0:
                        break
                    temp_val -= minus
                    
            if temp_val > max_val:
                max_val = temp_val left > 0 left 0 heapq.heappush(left_hq,V[left-1]) heapq.heappush heapq heappush left_hq V[left-1] V left-1 left 1 left_val += V[left-1] left_val V[left-1] V left-1 left 1 all_hq = copy.deepcopy(left_hq) all_hq copy.deepcopy(left_hq) copy.deepcopy copy deepcopy left_hq all_val = left_val all_val left_val for right in range(N+1):
            if right+left > K or right+left > N :
                continue
            if right > 0:
                heapq.heappush(all_hq,V[-right])
                all_val += V[-right]
            
            temp_hq = copy.deepcopy(all_hq)
            temp_val = all_val
            for i in range(K-left-right):
                if len(temp_hq) == 0:
                    break
                else:
                    minus = heapq.heappop(temp_hq)
                    if minus > 0:
                        break
                    temp_val -= minus
                    
            if temp_val > max_val:
                max_val = temp_val right range(N+1) range N+1 N 1 if right+left > K or right+left > N :
                continue
             right+left > K or right+left > N right+left > K right+left right left K right+left > N right+left right left N continue if right > 0:
                heapq.heappush(all_hq,V[-right])
                all_val += V[-right]
            
             right > 0 right 0 heapq.heappush(all_hq,V[-right]) heapq.heappush heapq heappush all_hq V[-right] V -right right all_val += V[-right] all_val V[-right] V -right right temp_hq = copy.deepcopy(all_hq) temp_hq copy.deepcopy(all_hq) copy.deepcopy copy deepcopy all_hq temp_val = all_val temp_val all_val for i in range(K-left-right):
                if len(temp_hq) == 0:
                    break
                else:
                    minus = heapq.heappop(temp_hq)
                    if minus > 0:
                        break
                    temp_val -= minus
                    
             i range(K-left-right) range K-left-right K-left K left right if len(temp_hq) == 0:
                    break
                else:
                    minus = heapq.heappop(temp_hq)
                    if minus > 0:
                        break
                    temp_val -= minus
                    
             len(temp_hq) == 0 len(temp_hq) len temp_hq 0 break minus = heapq.heappop(temp_hq) minus heapq.heappop(temp_hq) heapq.heappop heapq heappop temp_hq if minus > 0:
                        break
                     minus > 0 minus 0 break temp_val -= minus temp_val minus if temp_val > max_val:
                max_val = temp_val temp_val > max_val temp_val max_val max_val = temp_val max_val temp_val print(max_val) print max_val left_hq = list() list() left_hq max_val = 0 0 max_val left_val = 0 0 left_val temp_val = all_val all_val temp_val temp_hq = copy.deepcopy(all_hq) copy.deepcopy(all_hq) temp_hq left_val += V[left-1] V[left-1] left_val all_val += V[-right] V[-right] all_val minus = heapq.heappop(temp_hq) heapq.heappop(temp_hq) minus all_val = left_val left_val all_val V = list(map(int, input().split())) list(map(int, input().split())) V K = map(int, input().split()) map(int, input().split()) K temp_val -= minus minus temp_val N, K = map(int, input().split()) map(int, input().split()) N all_hq = copy.deepcopy(left_hq) copy.deepcopy(left_hq) all_hq max_val = temp_val temp_val max_val
import heapq
import copy
N, K = map(int, input().split())
V = list(map(int, input().split()))

max_val = 0
left_hq = list()
left_val = 0
for left in range(N+1):
    if left > 0:
        heapq.heappush(left_hq,V[left-1])
        left_val += V[left-1]
        all_hq = copy.deepcopy(left_hq)
        all_val = left_val
        for right in range(N+1):
            if right+left > K or right+left > N :
                continue
            if right > 0:
                heapq.heappush(all_hq,V[-right])
                all_val += V[-right]
            
            temp_hq = copy.deepcopy(all_hq)
            temp_val = all_val
            for i in range(K-left-right):
                if len(temp_hq) == 0:
                    break
                else:
                    minus = heapq.heappop(temp_hq)
                    if minus > 0:
                        break
                    temp_val -= minus
                    
            if temp_val > max_val:
                max_val = temp_val
print(max_val)
