import sys
import bisect
N, K = map(int, sys.stdin.readline().split())
V = list(map(int, sys.stdin.readline().split()))

ans = 0

max_take = min(N, K)

for A in range(max_take + 1):
    for B in range(max_take - A + 1):
        #if A > 0 and B > 0:
        list = V[:A] + V[N - B:]
        total_sum = sum(list) 
        list.sort()
        n = bisect.bisect_left(list, 0)
        if n <= K - (A + B):
            total_sum -= sum(list[:n])
        else:
            total_sum -= sum(list[:K - (A + B)])
        '''
        elif A == 0 and B > 0:
            list = V[N - B:]
            total_sum = sum(list)
            list.sort()
            n = bisect.bisect_left(list, 0)
            if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
        elif A > 0 and B == 0:
            list = V[:A]
            total_sum = sum(list)
            list.sort()
            n = bisect.bisect_left(list, 0)
            if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
        else:
            continue'''
        
        if total_sum > ans:
            ans = total_sum
            
print(ans) import sys sys import bisect bisect N, K = map(int, sys.stdin.readline().split()) N map(int, sys.stdin.readline().split()) map int sys.stdin.readline().split() sys.stdin.readline().split sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline split K V = list(map(int, sys.stdin.readline().split())) V list(map(int, sys.stdin.readline().split())) list map(int, sys.stdin.readline().split()) map int sys.stdin.readline().split() sys.stdin.readline().split sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline split ans = 0 ans 0 max_take = min(N, K) max_take min(N, K) min N K for A in range(max_take + 1):
    for B in range(max_take - A + 1):
        #if A > 0 and B > 0:
        list = V[:A] + V[N - B:]
        total_sum = sum(list) 
        list.sort()
        n = bisect.bisect_left(list, 0)
        if n <= K - (A + B):
            total_sum -= sum(list[:n])
        else:
            total_sum -= sum(list[:K - (A + B)])
        '''
        elif A == 0 and B > 0:
            list = V[N - B:]
            total_sum = sum(list)
            list.sort()
            n = bisect.bisect_left(list, 0)
            if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
        elif A > 0 and B == 0:
            list = V[:A]
            total_sum = sum(list)
            list.sort()
            n = bisect.bisect_left(list, 0)
            if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
        else:
            continue'''
        
        if total_sum > ans:
            ans = total_sum
             A range(max_take + 1) range max_take + 1 max_take 1 for B in range(max_take - A + 1):
        #if A > 0 and B > 0:
        list = V[:A] + V[N - B:]
        total_sum = sum(list) 
        list.sort()
        n = bisect.bisect_left(list, 0)
        if n <= K - (A + B):
            total_sum -= sum(list[:n])
        else:
            total_sum -= sum(list[:K - (A + B)])
        '''
        elif A == 0 and B > 0:
            list = V[N - B:]
            total_sum = sum(list)
            list.sort()
            n = bisect.bisect_left(list, 0)
            if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
        elif A > 0 and B == 0:
            list = V[:A]
            total_sum = sum(list)
            list.sort()
            n = bisect.bisect_left(list, 0)
            if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
        else:
            continue'''
        
        if total_sum > ans:
            ans = total_sum
             B range(max_take - A + 1) range max_take - A + 1 max_take - A max_take A 1 list = V[:A] + V[N - B:] list V[:A] + V[N - B:] V[:A] V :A A V[N - B:] V N - B: N - B N B total_sum = sum(list) total_sum sum(list) sum list list.sort() list.sort list sort n = bisect.bisect_left(list, 0) n bisect.bisect_left(list, 0) bisect.bisect_left bisect bisect_left list 0 if n <= K - (A + B):
            total_sum -= sum(list[:n])
        else:
            total_sum -= sum(list[:K - (A + B)])
         n <= K - (A + B) n K - (A + B) K A + B A B total_sum -= sum(list[:n]) total_sum sum(list[:n]) sum list[:n] list :n n total_sum -= sum(list[:K - (A + B)]) total_sum sum(list[:K - (A + B)]) sum list[:K - (A + B)] list :K - (A + B) K - (A + B) K A + B A B '''
        elif A == 0 and B > 0:
            list = V[N - B:]
            total_sum = sum(list)
            list.sort()
            n = bisect.bisect_left(list, 0)
            if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
        elif A > 0 and B == 0:
            list = V[:A]
            total_sum = sum(list)
            list.sort()
            n = bisect.bisect_left(list, 0)
            if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
        else:
            continue''' if total_sum > ans:
            ans = total_sum
             total_sum > ans total_sum ans ans = total_sum ans total_sum print(ans) print ans N, K = map(int, sys.stdin.readline().split()) map(int, sys.stdin.readline().split()) N total_sum = sum(list) sum(list) total_sum total_sum -= sum(list[:n]) sum(list[:n]) total_sum list = V[:A] + V[N - B:] V[:A] + V[N - B:] list ans = 0 0 ans ans = total_sum total_sum ans V = list(map(int, sys.stdin.readline().split())) list(map(int, sys.stdin.readline().split())) V max_take = min(N, K) min(N, K) max_take n = bisect.bisect_left(list, 0) bisect.bisect_left(list, 0) n total_sum -= sum(list[:K - (A + B)]) sum(list[:K - (A + B)]) total_sum K = map(int, sys.stdin.readline().split()) map(int, sys.stdin.readline().split()) K
import sys
import bisect
N, K = map(int, sys.stdin.readline().split())
V = list(map(int, sys.stdin.readline().split()))

ans = 0

max_take = min(N, K)

for A in range(max_take + 1):
    for B in range(max_take - A + 1):
        #if A > 0 and B > 0:
        list = V[:A] + V[N - B:]
        total_sum = sum(list) 
        list.sort()
        n = bisect.bisect_left(list, 0)
        if n <= K - (A + B):
            total_sum -= sum(list[:n])
        else:
            total_sum -= sum(list[:K - (A + B)])
        '''
        elif A == 0 and B > 0:
            list = V[N - B:]
            total_sum = sum(list)
            list.sort()
            n = bisect.bisect_left(list, 0)
            if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
        elif A > 0 and B == 0:
            list = V[:A]
            total_sum = sum(list)
            list.sort()
            n = bisect.bisect_left(list, 0)
            if n <= K - (A + B):
                total_sum -= sum(list[:n])
            else:
                total_sum -= sum(list[:K - (A + B)])
        else:
            continue'''
        
        if total_sum > ans:
            ans = total_sum
            
print(ans)