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

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

ans=0

nums=min(n,k)

for j in range(1,nums+1):

    #スライドするときの数 j=0,1,2,3,4......numsまでとることができる

    #n<kの時max n　かつかくjに対して取り除けるのはk-j回めまでである

    #n>=kの時max k かつ　かくｊに対して取り除けるのはk-j回までである

    for m in range(j+1):

        sublist=lists[:m]+lists[n+m-j:]

        sublist=sorted(sublist)

        sublist.append(0)

        del_some=0

        while del_some<k-j and min(sublist)<0:

            del sublist[0]

            del_some+=1

        p=sum(sublist)

        ans=max(ans,p)

print(ans)
# equeue



from collections import deque

import heapq





def main():

    N, K = list(map(int, input().split()))

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

    res = 0

    for pull in range(K+1):

        push = K - pull

        if push < 0 or push > N:

            continue

        for left_push in range(push+1):

            tmp_que = deque(V)

            tmp_heap = []

            heapq.heapify(tmp_heap)

            right_push = push - left_push

            for _ in range(left_push):

                heapq.heappush(tmp_heap, tmp_que.popleft())

            for _ in range(right_push):

                heapq.heappush(tmp_heap, tmp_que.pop())



            tmp_res = sum(tmp_que)

            for i in range(min(push, pull)):

                x = heapq.heappop(tmp_heap)

                if x >= 0:

                    heapq.heappush(tmp_heap,x)

                    break





            res = max(res, sum(tmp_heap))

    print(res)

    return





if __name__ == "__main__":

    main()
n, k = list(map(int, input().split()))
lists = list(map(int, input().split()))
ans = 0
nums = min(n, k)
for j in range(1, nums + 1):
    # スライドするときの数 j=0,1,2,3,4......numsまでとることができる
    # n<kの時max n　かつかくjに対して取り除けるのはk-j回めまでである
    # n>=kの時max k かつ　かくｊに対して取り除けるのはk-j回までである
    for m in range(j + 1):
        sublist = lists[:m] + lists[n + m - j :]
        sublist = sorted(sublist)
        sublist.append(0)
        del_some = 0
        while del_some < k - j and min(sublist) < 0:
            del sublist[0]
            del_some += 1
        p = sum(sublist)
        ans = max(ans, p)
print(ans)

# equeue
from collections import deque
import heapq


def main():
    N, K = list(map(int, input().split()))
    V = list(map(int, input().split()))
    res = 0
    for pull in range(K + 1):
        push = K - pull
        if push < 0 or push > N:
            continue
        for left_push in range(push + 1):
            tmp_que = deque(V)
            tmp_heap = []
            heapq.heapify(tmp_heap)
            right_push = push - left_push
            for _ in range(left_push):
                heapq.heappush(tmp_heap, tmp_que.popleft())
            for _ in range(right_push):
                heapq.heappush(tmp_heap, tmp_que.pop())
            tmp_res = sum(tmp_que)
            for i in range(min(push, pull)):
                x = heapq.heappop(tmp_heap)
                if x >= 0:
                    heapq.heappush(tmp_heap, x)
                    break
            res = max(res, sum(tmp_heap))
    print(res)
    return


if __name__ == "__main__":
    main()

-n, k = list(map(int, input().split())) -lists = list(map(int, input().split())) -ans = 0 -nums = min(n, k) -for j in range(1, nums + 1): -    # スライドするときの数 j=0,1,2,3,4......numsまでとることができる -    # n<kの時max n　かつかくjに対して取り除けるのはk-j回めまでである -    # n>=kの時max k かつ　かくｊに対して取り除けるのはk-j回までである -    for m in range(j + 1): -        sublist = lists[:m] + lists[n + m - j :] -        sublist = sorted(sublist) -        sublist.append(0) -        del_some = 0 -        while del_some < k - j and min(sublist) < 0: -            del sublist[0] -            del_some += 1 -        p = sum(sublist) -        ans = max(ans, p) -print(ans) +# equeue +from collections import deque +import heapq + + +def main(): +    N, K = list(map(int, input().split())) +    V = list(map(int, input().split())) +    res = 0 +    for pull in range(K + 1): +        push = K - pull +        if push < 0 or push > N: +            continue +        for left_push in range(push + 1): +            tmp_que = deque(V) +            tmp_heap = [] +            heapq.heapify(tmp_heap) +            right_push = push - left_push +            for _ in range(left_push): +                heapq.heappush(tmp_heap, tmp_que.popleft()) +            for _ in range(right_push): +                heapq.heappush(tmp_heap, tmp_que.pop()) +            tmp_res = sum(tmp_que) +            for i in range(min(push, pull)): +                x = heapq.heappop(tmp_heap) +                if x >= 0: +                    heapq.heappush(tmp_heap, x) +                    break +            res = max(res, sum(tmp_heap)) +    print(res) +    return + + +if __name__ == "__main__": +    main()