import bisect



abcd = []

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

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



for a in range(0, k+1):

    for b in range(0, k+1-a):

        for c in range(0, k+1-a-b):

            d = k-a-b-c

            abcd.append([a,b,c,d])





ans = 0

for curr_abcd in abcd:

    a,b,c,d = curr_abcd

    cd = c+d

    if a + b >= n:

        have = sorted(v_list)

    else:

        have = sorted(v_list[0:a] + v_list[n-b:n]) 



    minus_cnt = bisect.bisect_right(have,0)

    have = have[ min(minus_cnt, cd) : ]

    curr_sum = sum(have) 

    ans = max(ans, curr_sum)



print(ans)
from bisect import bisect_left, bisect_right



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

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



ans = 0

for get_num in range(0,k+1):

    rem = k-get_num

    for l in range(0,get_num+1):

        r = get_num-l

        if l+r > n: continue

        ls = vl[:l]

        rs = vl[n-r:]

        curr_l = ls + rs

        curr_l.sort()

        minus_cnt = bisect_left(curr_l, 0)

        if minus_cnt <= rem:

            curr_ans = sum(curr_l[minus_cnt:])

            ans = max(curr_ans,ans)

        else:

            curr_ans = sum(curr_l[rem:])

            ans = max(curr_ans,ans)



print(ans)
import bisect

abcd = []
n, k = list(map(int, input().split()))
v_list = list(map(int, input().split()))
for a in range(0, k + 1):
    for b in range(0, k + 1 - a):
        for c in range(0, k + 1 - a - b):
            d = k - a - b - c
            abcd.append([a, b, c, d])
ans = 0
for curr_abcd in abcd:
    a, b, c, d = curr_abcd
    cd = c + d
    if a + b >= n:
        have = sorted(v_list)
    else:
        have = sorted(v_list[0:a] + v_list[n - b : n])
    minus_cnt = bisect.bisect_right(have, 0)
    have = have[min(minus_cnt, cd) :]
    curr_sum = sum(have)
    ans = max(ans, curr_sum)
print(ans)

from bisect import bisect_left, bisect_right

n, k = list(map(int, input().split()))
vl = list(map(int, input().split()))
ans = 0
for get_num in range(0, k + 1):
    rem = k - get_num
    for l in range(0, get_num + 1):
        r = get_num - l
        if l + r > n:
            continue
        ls = vl[:l]
        rs = vl[n - r :]
        curr_l = ls + rs
        curr_l.sort()
        minus_cnt = bisect_left(curr_l, 0)
        if minus_cnt <= rem:
            curr_ans = sum(curr_l[minus_cnt:])
            ans = max(curr_ans, ans)
        else:
            curr_ans = sum(curr_l[rem:])
            ans = max(curr_ans, ans)
print(ans)

-import bisect +from bisect import bisect_left, bisect_right -abcd = [] -v_list = list(map(int, input().split())) -for a in range(0, k + 1): -    for b in range(0, k + 1 - a): -        for c in range(0, k + 1 - a - b): -            d = k - a - b - c -            abcd.append([a, b, c, d]) +vl = list(map(int, input().split())) -for curr_abcd in abcd: -    a, b, c, d = curr_abcd -    cd = c + d -    if a + b >= n: -        have = sorted(v_list) -    else: -        have = sorted(v_list[0:a] + v_list[n - b : n]) -    minus_cnt = bisect.bisect_right(have, 0) -    have = have[min(minus_cnt, cd) :] -    curr_sum = sum(have) -    ans = max(ans, curr_sum) +for get_num in range(0, k + 1): +    rem = k - get_num +    for l in range(0, get_num + 1): +        r = get_num - l +        if l + r > n: +            continue +        ls = vl[:l] +        rs = vl[n - r :] +        curr_l = ls + rs +        curr_l.sort() +        minus_cnt = bisect_left(curr_l, 0) +        if minus_cnt <= rem: +            curr_ans = sum(curr_l[minus_cnt:]) +            ans = max(curr_ans, ans) +        else: +            curr_ans = sum(curr_l[rem:]) +            ans = max(curr_ans, ans)