import sys
input = sys.stdin.readline

def read():
    N, K = map(int, input().strip().split())
    V = list(map(int, input().strip().split()))
    return N, K, V


def solve(N, K, V):
    ans = 0
    for k in range(min(K, N) + 1):
        for l in range(0, k+1):
            s = sorted(V[:l] + V[N-k+l:])
            v = sum(s)
            ans = max(ans, v)
            if k < K:
                # 最大(min(K-k, k))回、キューに戻すことが可能
                # 負の数は戻せるだけ戻せばよい
                for m in range(min(K-k, k)):
                    if s[m] < 0:
                        v -= s[m]
                ans = max(ans, v)

    return ans


if __name__ == '__main__':
    inputs = read()
    print(solve(*inputs)) import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline def read():
    N, K = map(int, input().strip().split())
    V = list(map(int, input().strip().split()))
    return N, K, V read N, K = map(int, input().strip().split()) N map(int, input().strip().split()) map int input().strip().split() ().strip().split ().strip() ().strip () input strip split K V = list(map(int, input().strip().split())) V list(map(int, input().strip().split())) list map(int, input().strip().split()) map int input().strip().split() ().strip().split ().strip() ().strip () input strip split return N, K, V return N, K, V N K V def solve(N, K, V):
    ans = 0
    for k in range(min(K, N) + 1):
        for l in range(0, k+1):
            s = sorted(V[:l] + V[N-k+l:])
            v = sum(s)
            ans = max(ans, v)
            if k < K:
                # 最大(min(K-k, k))回、キューに戻すことが可能
                # 負の数は戻せるだけ戻せばよい
                for m in range(min(K-k, k)):
                    if s[m] < 0:
                        v -= s[m]
                ans = max(ans, v)

    return ans solve ans = 0 ans 0 for k in range(min(K, N) + 1):
        for l in range(0, k+1):
            s = sorted(V[:l] + V[N-k+l:])
            v = sum(s)
            ans = max(ans, v)
            if k < K:
                # 最大(min(K-k, k))回、キューに戻すことが可能
                # 負の数は戻せるだけ戻せばよい
                for m in range(min(K-k, k)):
                    if s[m] < 0:
                        v -= s[m]
                ans = max(ans, v)

     k range(min(K, N) + 1) range min(K, N) + 1 min(K, N) min K N 1 for l in range(0, k+1):
            s = sorted(V[:l] + V[N-k+l:])
            v = sum(s)
            ans = max(ans, v)
            if k < K:
                # 最大(min(K-k, k))回、キューに戻すことが可能
                # 負の数は戻せるだけ戻せばよい
                for m in range(min(K-k, k)):
                    if s[m] < 0:
                        v -= s[m]
                ans = max(ans, v)

     l range(0, k+1) range 0 k+1 k 1 s = sorted(V[:l] + V[N-k+l:]) s sorted(V[:l] + V[N-k+l:]) sorted V[:l] + V[N-k+l:] V[:l] V :l l V[N-k+l:] V N-k+l: N-k+l N-k N k l v = sum(s) v sum(s) sum s ans = max(ans, v) ans max(ans, v) max ans v if k < K:
                # 最大(min(K-k, k))回、キューに戻すことが可能
                # 負の数は戻せるだけ戻せばよい
                for m in range(min(K-k, k)):
                    if s[m] < 0:
                        v -= s[m]
                ans = max(ans, v)

     k < K k K for m in range(min(K-k, k)):
                    if s[m] < 0:
                        v -= s[m]
                 m range(min(K-k, k)) range min(K-k, k) min K-k K k k if s[m] < 0:
                        v -= s[m]
                 s[m] < 0 s[m] s m 0 v -= s[m] v s[m] s m ans = max(ans, v) ans max(ans, v) max ans v return ans ans N N K K V V if __name__ == '__main__':
    inputs = read()
    print(solve(*inputs)) __name__ == '__main__' __name__ '__main__' inputs = read() inputs read() read print(solve(*inputs)) print solve(*inputs) solve *inputs inputs input = sys.stdin.readline sys.stdin.readline input def solve(N, K, V):
    ans = 0
    for k in range(min(K, N) + 1):
        for l in range(0, k+1):
            s = sorted(V[:l] + V[N-k+l:])
            v = sum(s)
            ans = max(ans, v)
            if k < K:
                # 最大(min(K-k, k))回、キューに戻すことが可能
                # 負の数は戻せるだけ戻せばよい
                for m in range(min(K-k, k)):
                    if s[m] < 0:
                        v -= s[m]
                ans = max(ans, v)

    return ans def solve(N, K, V):
    ans = 0
    for k in range(min(K, N) + 1):
        for l in range(0, k+1):
            s = sorted(V[:l] + V[N-k+l:])
            v = sum(s)
            ans = max(ans, v)
            if k < K:
                # 最大(min(K-k, k))回、キューに戻すことが可能
                # 負の数は戻せるだけ戻せばよい
                for m in range(min(K-k, k)):
                    if s[m] < 0:
                        v -= s[m]
                ans = max(ans, v)

    return ans solve inputs = read() read() inputs def read():
    N, K = map(int, input().strip().split())
    V = list(map(int, input().strip().split()))
    return N, K, V def read():
    N, K = map(int, input().strip().split())
    V = list(map(int, input().strip().split()))
    return N, K, V read
import sys
input = sys.stdin.readline

def read():
    N, K = map(int, input().strip().split())
    V = list(map(int, input().strip().split()))
    return N, K, V


def solve(N, K, V):
    ans = 0
    for k in range(min(K, N) + 1):
        for l in range(0, k+1):
            s = sorted(V[:l] + V[N-k+l:])
            v = sum(s)
            ans = max(ans, v)
            if k < K:
                # 最大(min(K-k, k))回、キューに戻すことが可能
                # 負の数は戻せるだけ戻せばよい
                for m in range(min(K-k, k)):
                    if s[m] < 0:
                        v -= s[m]
                ans = max(ans, v)

    return ans


if __name__ == '__main__':
    inputs = read()
    print(solve(*inputs))
