import sys
from collections import deque


def input():
    return sys.stdin.readline().rstrip()


def main():
    N, K = map(int, input().split())
    V = list(map(int, input().split()))
    ans = 0

    for i in range(min(N + 1, K+1)):  # 左から i 個取る　　
        for j in range(min(N + 1 - i, K - i+1)):  # 右からi個取る 計算量10^3
            res = K - i - j  # 残り行動回数
            agg = V[:i] + V[N  - j:]
            agg.sort(reverse=True)
            while res > 0 and agg:
                if agg[-1] < 0:
                    agg.pop()
                    res -= 1
                else:
                    break
            ans = max(sum(agg), ans)
    print(ans)


if __name__ == "__main__":
    main() import sys sys from collections import deque def input():
    return sys.stdin.readline().rstrip() input return sys.stdin.readline().rstrip() sys.stdin.readline().rstrip() sys.stdin.readline().rstrip sys.stdin.readline() sys.stdin.readline sys.stdin sys stdin readline rstrip def main():
    N, K = map(int, input().split())
    V = list(map(int, input().split()))
    ans = 0

    for i in range(min(N + 1, K+1)):  # 左から i 個取る　　
        for j in range(min(N + 1 - i, K - i+1)):  # 右からi個取る 計算量10^3
            res = K - i - j  # 残り行動回数
            agg = V[:i] + V[N  - j:]
            agg.sort(reverse=True)
            while res > 0 and agg:
                if agg[-1] < 0:
                    agg.pop()
                    res -= 1
                else:
                    break
            ans = max(sum(agg), ans)
    print(ans) 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 ans = 0 ans 0 for i in range(min(N + 1, K+1)):  # 左から i 個取る　　
        for j in range(min(N + 1 - i, K - i+1)):  # 右からi個取る 計算量10^3
            res = K - i - j  # 残り行動回数
            agg = V[:i] + V[N  - j:]
            agg.sort(reverse=True)
            while res > 0 and agg:
                if agg[-1] < 0:
                    agg.pop()
                    res -= 1
                else:
                    break
            ans = max(sum(agg), ans)
     i range(min(N + 1, K+1)) range min(N + 1, K+1) min N + 1 N 1 K+1 K 1 for j in range(min(N + 1 - i, K - i+1)):  # 右からi個取る 計算量10^3
            res = K - i - j  # 残り行動回数
            agg = V[:i] + V[N  - j:]
            agg.sort(reverse=True)
            while res > 0 and agg:
                if agg[-1] < 0:
                    agg.pop()
                    res -= 1
                else:
                    break
            ans = max(sum(agg), ans)
     j range(min(N + 1 - i, K - i+1)) range min(N + 1 - i, K - i+1) min N + 1 - i N + 1 N 1 i K - i+1 K - i K i 1 res = K - i - j res K - i - j K - i K i j agg = V[:i] + V[N  - j:] agg V[:i] + V[N  - j:] V[:i] V :i i V[N  - j:] V N  - j: N  - j N j agg.sort(reverse=True) agg.sort agg sort reverse=True reverse True while res > 0 and agg:
                if agg[-1] < 0:
                    agg.pop()
                    res -= 1
                else:
                    break
             res > 0 and agg res > 0 res 0 agg if agg[-1] < 0:
                    agg.pop()
                    res -= 1
                else:
                    break
             agg[-1] < 0 agg[-1] agg -1 0 agg.pop() agg.pop agg pop res -= 1 res 1 break ans = max(sum(agg), ans) ans max(sum(agg), ans) max sum(agg) sum agg ans print(ans) print ans if __name__ == "__main__":
    main() __name__ == "__main__" __name__ "__main__" main() main def main():
    N, K = map(int, input().split())
    V = list(map(int, input().split()))
    ans = 0

    for i in range(min(N + 1, K+1)):  # 左から i 個取る　　
        for j in range(min(N + 1 - i, K - i+1)):  # 右からi個取る 計算量10^3
            res = K - i - j  # 残り行動回数
            agg = V[:i] + V[N  - j:]
            agg.sort(reverse=True)
            while res > 0 and agg:
                if agg[-1] < 0:
                    agg.pop()
                    res -= 1
                else:
                    break
            ans = max(sum(agg), ans)
    print(ans) def main():
    N, K = map(int, input().split())
    V = list(map(int, input().split()))
    ans = 0

    for i in range(min(N + 1, K+1)):  # 左から i 個取る　　
        for j in range(min(N + 1 - i, K - i+1)):  # 右からi個取る 計算量10^3
            res = K - i - j  # 残り行動回数
            agg = V[:i] + V[N  - j:]
            agg.sort(reverse=True)
            while res > 0 and agg:
                if agg[-1] < 0:
                    agg.pop()
                    res -= 1
                else:
                    break
            ans = max(sum(agg), ans)
    print(ans) main def input():
    return sys.stdin.readline().rstrip() def input():
    return sys.stdin.readline().rstrip() input
import sys
from collections import deque


def input():
    return sys.stdin.readline().rstrip()


def main():
    N, K = map(int, input().split())
    V = list(map(int, input().split()))
    ans = 0

    for i in range(min(N + 1, K+1)):  # 左から i 個取る　　
        for j in range(min(N + 1 - i, K - i+1)):  # 右からi個取る 計算量10^3
            res = K - i - j  # 残り行動回数
            agg = V[:i] + V[N  - j:]
            agg.sort(reverse=True)
            while res > 0 and agg:
                if agg[-1] < 0:
                    agg.pop()
                    res -= 1
                else:
                    break
            ans = max(sum(agg), ans)
    print(ans)


if __name__ == "__main__":
    main()
