from sys import stdin, setrecursionlimit
from bisect import bisect_left


def main():
    input = stdin.buffer.readline
    n, k = map(int, input().split())
    v = list(map(int, input().split()))

    ans = 0

    for i in range(1, k + 1):
        if i < n:
            for j in range(i + 1):
                cand = v[: j] + v[n - i + j:]
                cand.sort()
                idx = min(bisect_left(cand, 0), k - i)
                if idx > 0 or i > 0:
                    ans = max(ans, sum(cand[idx:]))

        else:
            cand = v
            cand.sort()
            idx = bisect_left(cand, 0)
            ans = max(ans, sum(cand[idx:]))

    print(ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main() from sys import stdin, setrecursionlimit from bisect import bisect_left def main():
    input = stdin.buffer.readline
    n, k = map(int, input().split())
    v = list(map(int, input().split()))

    ans = 0

    for i in range(1, k + 1):
        if i < n:
            for j in range(i + 1):
                cand = v[: j] + v[n - i + j:]
                cand.sort()
                idx = min(bisect_left(cand, 0), k - i)
                if idx > 0 or i > 0:
                    ans = max(ans, sum(cand[idx:]))

        else:
            cand = v
            cand.sort()
            idx = bisect_left(cand, 0)
            ans = max(ans, sum(cand[idx:]))

    print(ans) main input = stdin.buffer.readline input stdin.buffer.readline stdin.buffer stdin buffer readline 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 ans = 0 ans 0 for i in range(1, k + 1):
        if i < n:
            for j in range(i + 1):
                cand = v[: j] + v[n - i + j:]
                cand.sort()
                idx = min(bisect_left(cand, 0), k - i)
                if idx > 0 or i > 0:
                    ans = max(ans, sum(cand[idx:]))

        else:
            cand = v
            cand.sort()
            idx = bisect_left(cand, 0)
            ans = max(ans, sum(cand[idx:]))

     i range(1, k + 1) range 1 k + 1 k 1 if i < n:
            for j in range(i + 1):
                cand = v[: j] + v[n - i + j:]
                cand.sort()
                idx = min(bisect_left(cand, 0), k - i)
                if idx > 0 or i > 0:
                    ans = max(ans, sum(cand[idx:]))

        else:
            cand = v
            cand.sort()
            idx = bisect_left(cand, 0)
            ans = max(ans, sum(cand[idx:]))

     i < n i n for j in range(i + 1):
                cand = v[: j] + v[n - i + j:]
                cand.sort()
                idx = min(bisect_left(cand, 0), k - i)
                if idx > 0 or i > 0:
                    ans = max(ans, sum(cand[idx:]))

         j range(i + 1) range i + 1 i 1 cand = v[: j] + v[n - i + j:] cand v[: j] + v[n - i + j:] v[: j] v : j j v[n - i + j:] v n - i + j: n - i + j n - i n i j cand.sort() cand.sort cand sort idx = min(bisect_left(cand, 0), k - i) idx min(bisect_left(cand, 0), k - i) min bisect_left(cand, 0) bisect_left cand 0 k - i k i if idx > 0 or i > 0:
                    ans = max(ans, sum(cand[idx:]))

         idx > 0 or i > 0 idx > 0 idx 0 i > 0 i 0 ans = max(ans, sum(cand[idx:])) ans max(ans, sum(cand[idx:])) max ans sum(cand[idx:]) sum cand[idx:] cand idx: idx cand = v cand v cand.sort() cand.sort cand sort idx = bisect_left(cand, 0) idx bisect_left(cand, 0) bisect_left cand 0 ans = max(ans, sum(cand[idx:])) ans max(ans, sum(cand[idx:])) max ans sum(cand[idx:]) sum cand[idx:] cand idx: idx print(ans) print ans if __name__ == "__main__":
    setrecursionlimit(10000)
    main() __name__ == "__main__" __name__ "__main__" setrecursionlimit(10000) setrecursionlimit 10000 main() main def main():
    input = stdin.buffer.readline
    n, k = map(int, input().split())
    v = list(map(int, input().split()))

    ans = 0

    for i in range(1, k + 1):
        if i < n:
            for j in range(i + 1):
                cand = v[: j] + v[n - i + j:]
                cand.sort()
                idx = min(bisect_left(cand, 0), k - i)
                if idx > 0 or i > 0:
                    ans = max(ans, sum(cand[idx:]))

        else:
            cand = v
            cand.sort()
            idx = bisect_left(cand, 0)
            ans = max(ans, sum(cand[idx:]))

    print(ans) def main():
    input = stdin.buffer.readline
    n, k = map(int, input().split())
    v = list(map(int, input().split()))

    ans = 0

    for i in range(1, k + 1):
        if i < n:
            for j in range(i + 1):
                cand = v[: j] + v[n - i + j:]
                cand.sort()
                idx = min(bisect_left(cand, 0), k - i)
                if idx > 0 or i > 0:
                    ans = max(ans, sum(cand[idx:]))

        else:
            cand = v
            cand.sort()
            idx = bisect_left(cand, 0)
            ans = max(ans, sum(cand[idx:]))

    print(ans) main
from sys import stdin, setrecursionlimit
from bisect import bisect_left


def main():
    input = stdin.buffer.readline
    n, k = map(int, input().split())
    v = list(map(int, input().split()))

    ans = 0

    for i in range(1, k + 1):
        if i < n:
            for j in range(i + 1):
                cand = v[: j] + v[n - i + j:]
                cand.sort()
                idx = min(bisect_left(cand, 0), k - i)
                if idx > 0 or i > 0:
                    ans = max(ans, sum(cand[idx:]))

        else:
            cand = v
            cand.sort()
            idx = bisect_left(cand, 0)
            ans = max(ans, sum(cand[idx:]))

    print(ans)


if __name__ == "__main__":
    setrecursionlimit(10000)
    main()
