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)
n,k = list(map(int, input().split()))

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



ans = 0

ab_max = min(n,k)

for l in range(ab_max+1):

    for r in range(ab_max+1):

        if l+r > k: continue

        if l > n-r: continue

        ls = vl[:l]

        rs = vl[n-r:]

        # print(ls,rs)

        vals = ls + rs

        rem = k-(l+r)

        # print(rem)

        vals.sort()

        curr_sum = sum(vals)

        for i in range(rem):

            if i >= len(vals): break

            if vals[i] < 0: curr_sum -= vals[i]

            else: break

        # print(curr_sum)

        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)

n, k = list(map(int, input().split()))
vl = list(map(int, input().split()))
ans = 0
ab_max = min(n, k)
for l in range(ab_max + 1):
    for r in range(ab_max + 1):
        if l + r > k:
            continue
        if l > n - r:
            continue
        ls = vl[:l]
        rs = vl[n - r :]
        # print(ls,rs)
        vals = ls + rs
        rem = k - (l + r)
        # print(rem)
        vals.sort()
        curr_sum = sum(vals)
        for i in range(rem):
            if i >= len(vals):
                break
            if vals[i] < 0:
                curr_sum -= vals[i]
            else:
                break
        # print(curr_sum)
        ans = max(ans, curr_sum)
print(ans)

-from bisect import bisect_left, bisect_right - -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: +ab_max = min(n, k) +for l in range(ab_max + 1): +    for r in range(ab_max + 1): +        if l + r > k: +            continue +        if l > 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(ls,rs) +        vals = ls + rs +        rem = k - (l + r) +        # print(rem) +        vals.sort() +        curr_sum = sum(vals) +        for i in range(rem): +            if i >= len(vals): +                break +            if vals[i] < 0: +                curr_sum -= vals[i] +            else: +                break +        # print(curr_sum) +        ans = max(ans, curr_sum)