import heapq

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

a=list(map(int,input().split()))

ans=0

for i in range(k+1):

    tmp=a[:i]

    for j in range(k+1-i):

        if i+j>n:

            cal=a.copy()

            zan=k-n

        else:

            cal=tmp+a[n-j:]

            zan=k-i-j

        

        heapq.heapify(cal)

        ans=max(ans,sum(cal))

        for _ in range(zan):

            if len(cal)>0:

                heapq.heappop(cal)

                ans=max(ans,sum(cal))

            else:

                break

print(ans)

#n=int(input())

#n,m=list(map(int,input().split()))

#a=list(map(int,input().split()))

input_list = lambda : list(map(int,input().split()))



from collections import Counter 

from collections import defaultdict

from collections import deque

import itertools

import heapq

import sys

sys.setrecursionlimit(10**6)



n,m=input_list()

a=deque(input_list())

ans=0

for get_num in range(m+1):

    ret_num=min(m-get_num,get_num)

    if get_num>n:

        get_num=n

        ret_num=min(m-get_num,get_num)

    

    for get_left in range(get_num+1):

        cal=[]

        get_right=get_num-get_left

        val=0

        for i in range(get_left):

            val+=a[i]

            heapq.heappush(cal,a[i])

        for i in range(get_right):

            ii=n-1-i

            val+=a[ii]

            heapq.heappush(cal,a[ii])

        for _ in range(ret_num):

            p_val=heapq.heappop(cal)

            if p_val>0:

                break

            val-=p_val

        ans=max(ans,val)

print(ans)





        
import heapq

n, k = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 0
for i in range(k + 1):
    tmp = a[:i]
    for j in range(k + 1 - i):
        if i + j > n:
            cal = a.copy()
            zan = k - n
        else:
            cal = tmp + a[n - j :]
            zan = k - i - j
        heapq.heapify(cal)
        ans = max(ans, sum(cal))
        for _ in range(zan):
            if len(cal) > 0:
                heapq.heappop(cal)
                ans = max(ans, sum(cal))
            else:
                break
print(ans)

# n=int(input())
# n,m=list(map(int,input().split()))
# a=list(map(int,input().split()))
input_list = lambda: list(map(int, input().split()))
from collections import Counter
from collections import defaultdict
from collections import deque
import itertools
import heapq
import sys

sys.setrecursionlimit(10**6)
n, m = input_list()
a = deque(input_list())
ans = 0
for get_num in range(m + 1):
    ret_num = min(m - get_num, get_num)
    if get_num > n:
        get_num = n
        ret_num = min(m - get_num, get_num)
    for get_left in range(get_num + 1):
        cal = []
        get_right = get_num - get_left
        val = 0
        for i in range(get_left):
            val += a[i]
            heapq.heappush(cal, a[i])
        for i in range(get_right):
            ii = n - 1 - i
            val += a[ii]
            heapq.heappush(cal, a[ii])
        for _ in range(ret_num):
            p_val = heapq.heappop(cal)
            if p_val > 0:
                break
            val -= p_val
        ans = max(ans, val)
print(ans)

+# n=int(input()) +# n,m=list(map(int,input().split())) +# a=list(map(int,input().split())) +input_list = lambda: list(map(int, input().split())) +from collections import Counter +from collections import defaultdict +from collections import deque +import itertools +import sys -n, k = list(map(int, input().split())) -a = list(map(int, input().split())) +sys.setrecursionlimit(10**6) +n, m = input_list() +a = deque(input_list()) -for i in range(k + 1): -    tmp = a[:i] -    for j in range(k + 1 - i): -        if i + j > n: -            cal = a.copy() -            zan = k - n -        else: -            cal = tmp + a[n - j :] -            zan = k - i - j -        heapq.heapify(cal) -        ans = max(ans, sum(cal)) -        for _ in range(zan): -            if len(cal) > 0: -                heapq.heappop(cal) -                ans = max(ans, sum(cal)) -            else: +for get_num in range(m + 1): +    ret_num = min(m - get_num, get_num) +    if get_num > n: +        get_num = n +        ret_num = min(m - get_num, get_num) +    for get_left in range(get_num + 1): +        cal = [] +        get_right = get_num - get_left +        val = 0 +        for i in range(get_left): +            val += a[i] +            heapq.heappush(cal, a[i]) +        for i in range(get_right): +            ii = n - 1 - i +            val += a[ii] +            heapq.heappush(cal, a[ii]) +        for _ in range(ret_num): +            p_val = heapq.heappop(cal) +            if p_val > 0: +            val -= p_val +        ans = max(ans, val)