N, K, *V = map(int, open(0).read().split())

# i: 操作回数
# j: 取り出す個数
# k: 左から取り出す範囲 [0, k)
ans = 0
for i in range(K + 1):
    for j in range(min(N + 1, i + 1)):
        for k in range(j + 1):
            right = N - j + k
            tmp = V[:k] + V[right:]
            tmp.sort()
            ans = max(ans, sum(tmp) - sum([t for t in tmp if t < 0][: i - j]))
print(ans) N, K, *V = map(int, open(0).read().split()) N map(int, open(0).read().split()) map int open(0).read().split() (0).read().split (0).read() (0).read (0) open 0 read split K *V V ans = 0 ans 0 for i in range(K + 1):
    for j in range(min(N + 1, i + 1)):
        for k in range(j + 1):
            right = N - j + k
            tmp = V[:k] + V[right:]
            tmp.sort()
            ans = max(ans, sum(tmp) - sum([t for t in tmp if t < 0][: i - j])) i range(K + 1) range K + 1 K 1 for j in range(min(N + 1, i + 1)):
        for k in range(j + 1):
            right = N - j + k
            tmp = V[:k] + V[right:]
            tmp.sort()
            ans = max(ans, sum(tmp) - sum([t for t in tmp if t < 0][: i - j])) j range(min(N + 1, i + 1)) range min(N + 1, i + 1) min N + 1 N 1 i + 1 i 1 for k in range(j + 1):
            right = N - j + k
            tmp = V[:k] + V[right:]
            tmp.sort()
            ans = max(ans, sum(tmp) - sum([t for t in tmp if t < 0][: i - j])) k range(j + 1) range j + 1 j 1 right = N - j + k right N - j + k N - j N j k tmp = V[:k] + V[right:] tmp V[:k] + V[right:] V[:k] V :k k V[right:] V right: right tmp.sort() tmp.sort tmp sort t for t in tmp if t < 0 for t in tmp if t < 0 t tmp t < 0 t 0 if t < 0 t ans = max(ans, sum(tmp) - sum([t for t in tmp if t < 0][: i - j])) ans max(ans, sum(tmp) - sum([t for t in tmp if t < 0][: i - j])) max ans sum(tmp) - sum([t for t in tmp if t < 0][: i - j]) sum(tmp) sum tmp sum([t for t in tmp if t < 0][: i - j]) sum [t for t in tmp if t < 0][: i - j] [t for t in tmp if t < 0] : i - j i - j i j print(ans) print ans tmp = V[:k] + V[right:] V[:k] + V[right:] tmp N, K, *V = map(int, open(0).read().split()) map(int, open(0).read().split()) N right = N - j + k N - j + k right ans = max(ans, sum(tmp) - sum([t for t in tmp if t < 0][: i - j])) max(ans, sum(tmp) - sum([t for t in tmp if t < 0][: i - j])) ans ans = 0 0 ans K, *V = map(int, open(0).read().split()) map(int, open(0).read().split()) K
N, K, *V = map(int, open(0).read().split())

# i: 操作回数
# j: 取り出す個数
# k: 左から取り出す範囲 [0, k)
ans = 0
for i in range(K + 1):
    for j in range(min(N + 1, i + 1)):
        for k in range(j + 1):
            right = N - j + k
            tmp = V[:k] + V[right:]
            tmp.sort()
            ans = max(ans, sum(tmp) - sum([t for t in tmp if t < 0][: i - j]))
print(ans)
