# equeue

from collections import deque
import heapq


def main():
    N, K = 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() from collections import deque import heapq heapq def main():
    N, K = 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 main N, K = map(int, input().split()) N map(int, input().split()) map int input().split() ().split () input split K V = list(map(int, input().split())) V list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split res = 0 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))
     pull range(K+1) range K+1 K 1 push = K - pull push K - pull K pull if push < 0 or push > N:
            continue
         push < 0 or push > N push < 0 push 0 push > N 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))
     left_push range(push+1) range push+1 push 1 tmp_que = deque(V) tmp_que deque(V) deque V tmp_heap = [] tmp_heap [] heapq.heapify(tmp_heap) heapq.heapify heapq heapify tmp_heap right_push = push - left_push right_push push - left_push push left_push for _ in range(left_push):
                heapq.heappush(tmp_heap, tmp_que.popleft())
             _ range(left_push) range left_push heapq.heappush(tmp_heap, tmp_que.popleft()) heapq.heappush heapq heappush tmp_heap tmp_que.popleft() tmp_que.popleft tmp_que popleft for _ in range(right_push):
                heapq.heappush(tmp_heap, tmp_que.pop())

             _ range(right_push) range right_push heapq.heappush(tmp_heap, tmp_que.pop()) heapq.heappush heapq heappush tmp_heap tmp_que.pop() tmp_que.pop tmp_que pop tmp_res = sum(tmp_que) tmp_res sum(tmp_que) sum tmp_que for i in range(min(push, pull)):
                x = heapq.heappop(tmp_heap)
                if x >= 0:
                    heapq.heappush(tmp_heap,x)
                    break


             i range(min(push, pull)) range min(push, pull) min push pull x = heapq.heappop(tmp_heap) x heapq.heappop(tmp_heap) heapq.heappop heapq heappop tmp_heap if x >= 0:
                    heapq.heappush(tmp_heap,x)
                    break


             x >= 0 x 0 heapq.heappush(tmp_heap,x) heapq.heappush heapq heappush tmp_heap x break res = max(res, sum(tmp_heap)) res max(res, sum(tmp_heap)) max res sum(tmp_heap) sum tmp_heap print(res) print res return if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main def main():
    N, K = 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 def main():
    N, K = 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 main
# equeue

from collections import deque
import heapq


def main():
    N, K = 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()