import sys
from collections import deque
import heapq

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, k = map(int, input().split())
    V = list(map(int, input().split()))

    res = 0
    for i in range(1, k + 1):
        # i回取り出す
        for j in range(i + 1):
            que = deque(V)
            get = []
            heapq.heapify(get)
            # j回前から取り出す
            for x in range(j):
                if que:
                    v = que.popleft()
                    heapq.heappush(get, v)

            # i - j回前から取り出す
            for _ in range(i - j):
                if que:
                    v = que.pop()
                    heapq.heappush(get, v)

            score = sum(get)
            res = max(res, score)

            # 宝石の価値が低い順にk - i回戻す
            for _ in range(k - i):
                if get:
                    heapq.heappop(get)

                score = sum(get)
                res = max(res, score)

    print(res)


if __name__ == '__main__':
    resolve() import sys sys from collections import deque import heapq heapq sys.setrecursionlimit(10 ** 7) sys.setrecursionlimit sys setrecursionlimit 10 ** 7 10 7 f_inf = float('inf') f_inf float('inf') float 'inf' mod = 10 ** 9 + 7 mod 10 ** 9 + 7 10 ** 9 10 9 7 def resolve():
    n, k = map(int, input().split())
    V = list(map(int, input().split()))

    res = 0
    for i in range(1, k + 1):
        # i回取り出す
        for j in range(i + 1):
            que = deque(V)
            get = []
            heapq.heapify(get)
            # j回前から取り出す
            for x in range(j):
                if que:
                    v = que.popleft()
                    heapq.heappush(get, v)

            # i - j回前から取り出す
            for _ in range(i - j):
                if que:
                    v = que.pop()
                    heapq.heappush(get, v)

            score = sum(get)
            res = max(res, score)

            # 宝石の価値が低い順にk - i回戻す
            for _ in range(k - i):
                if get:
                    heapq.heappop(get)

                score = sum(get)
                res = max(res, score)

    print(res) resolve 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 i in range(1, k + 1):
        # i回取り出す
        for j in range(i + 1):
            que = deque(V)
            get = []
            heapq.heapify(get)
            # j回前から取り出す
            for x in range(j):
                if que:
                    v = que.popleft()
                    heapq.heappush(get, v)

            # i - j回前から取り出す
            for _ in range(i - j):
                if que:
                    v = que.pop()
                    heapq.heappush(get, v)

            score = sum(get)
            res = max(res, score)

            # 宝石の価値が低い順にk - i回戻す
            for _ in range(k - i):
                if get:
                    heapq.heappop(get)

                score = sum(get)
                res = max(res, score)

     i range(1, k + 1) range 1 k + 1 k 1 for j in range(i + 1):
            que = deque(V)
            get = []
            heapq.heapify(get)
            # j回前から取り出す
            for x in range(j):
                if que:
                    v = que.popleft()
                    heapq.heappush(get, v)

            # i - j回前から取り出す
            for _ in range(i - j):
                if que:
                    v = que.pop()
                    heapq.heappush(get, v)

            score = sum(get)
            res = max(res, score)

            # 宝石の価値が低い順にk - i回戻す
            for _ in range(k - i):
                if get:
                    heapq.heappop(get)

                score = sum(get)
                res = max(res, score)

     j range(i + 1) range i + 1 i 1 que = deque(V) que deque(V) deque V get = [] get [] heapq.heapify(get) heapq.heapify heapq heapify get for x in range(j):
                if que:
                    v = que.popleft()
                    heapq.heappush(get, v)

            # i - j回前から取り出す
             x range(j) range j if que:
                    v = que.popleft()
                    heapq.heappush(get, v)

            # i - j回前から取り出す
             que v = que.popleft() v que.popleft() que.popleft que popleft heapq.heappush(get, v) heapq.heappush heapq heappush get v for _ in range(i - j):
                if que:
                    v = que.pop()
                    heapq.heappush(get, v)

             _ range(i - j) range i - j i j if que:
                    v = que.pop()
                    heapq.heappush(get, v)

             que v = que.pop() v que.pop() que.pop que pop heapq.heappush(get, v) heapq.heappush heapq heappush get v score = sum(get) score sum(get) sum get res = max(res, score) res max(res, score) max res score for _ in range(k - i):
                if get:
                    heapq.heappop(get)

                score = sum(get)
                res = max(res, score)

     _ range(k - i) range k - i k i if get:
                    heapq.heappop(get)

                 get heapq.heappop(get) heapq.heappop heapq heappop get score = sum(get) score sum(get) sum get res = max(res, score) res max(res, score) max res score print(res) print res if __name__ == '__main__':
    resolve() __name__ == '__main__' __name__ '__main__' resolve() resolve def resolve():
    n, k = map(int, input().split())
    V = list(map(int, input().split()))

    res = 0
    for i in range(1, k + 1):
        # i回取り出す
        for j in range(i + 1):
            que = deque(V)
            get = []
            heapq.heapify(get)
            # j回前から取り出す
            for x in range(j):
                if que:
                    v = que.popleft()
                    heapq.heappush(get, v)

            # i - j回前から取り出す
            for _ in range(i - j):
                if que:
                    v = que.pop()
                    heapq.heappush(get, v)

            score = sum(get)
            res = max(res, score)

            # 宝石の価値が低い順にk - i回戻す
            for _ in range(k - i):
                if get:
                    heapq.heappop(get)

                score = sum(get)
                res = max(res, score)

    print(res) def resolve():
    n, k = map(int, input().split())
    V = list(map(int, input().split()))

    res = 0
    for i in range(1, k + 1):
        # i回取り出す
        for j in range(i + 1):
            que = deque(V)
            get = []
            heapq.heapify(get)
            # j回前から取り出す
            for x in range(j):
                if que:
                    v = que.popleft()
                    heapq.heappush(get, v)

            # i - j回前から取り出す
            for _ in range(i - j):
                if que:
                    v = que.pop()
                    heapq.heappush(get, v)

            score = sum(get)
            res = max(res, score)

            # 宝石の価値が低い順にk - i回戻す
            for _ in range(k - i):
                if get:
                    heapq.heappop(get)

                score = sum(get)
                res = max(res, score)

    print(res) resolve mod = 10 ** 9 + 7 10 ** 9 + 7 mod f_inf = float('inf') float('inf') f_inf
import sys
from collections import deque
import heapq

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, k = map(int, input().split())
    V = list(map(int, input().split()))

    res = 0
    for i in range(1, k + 1):
        # i回取り出す
        for j in range(i + 1):
            que = deque(V)
            get = []
            heapq.heapify(get)
            # j回前から取り出す
            for x in range(j):
                if que:
                    v = que.popleft()
                    heapq.heappush(get, v)

            # i - j回前から取り出す
            for _ in range(i - j):
                if que:
                    v = que.pop()
                    heapq.heappush(get, v)

            score = sum(get)
            res = max(res, score)

            # 宝石の価値が低い順にk - i回戻す
            for _ in range(k - i):
                if get:
                    heapq.heappop(get)

                score = sum(get)
                res = max(res, score)

    print(res)


if __name__ == '__main__':
    resolve()
