import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n, k = map(int, readline().split())
v = list(map(int, readline().split()))
ans = -float('inf')
nk = min(n, k)
for a in range(nk + 1):
    for b in range(nk - a + 1):
        vv = v[:a] + v[n - b:]
        if k - a - b >= 0:
            vv.sort()
            cnt = 0
            for check in vv[:k - a - b]:
                if check >= 0:
                    break
                cnt += check
            ans = max(ans, sum(vv) - cnt)
print(ans) import sys sys read = sys.stdin.buffer.read read sys.stdin.buffer.read sys.stdin.buffer sys.stdin sys stdin buffer read readline = sys.stdin.buffer.readline readline sys.stdin.buffer.readline sys.stdin.buffer sys.stdin sys stdin buffer readline readlines = sys.stdin.buffer.readlines readlines sys.stdin.buffer.readlines sys.stdin.buffer sys.stdin sys stdin buffer readlines sys.setrecursionlimit(10 ** 7) sys.setrecursionlimit sys setrecursionlimit 10 ** 7 10 7 n, k = map(int, readline().split()) n map(int, readline().split()) map int readline().split() ().split () readline split k v = list(map(int, readline().split())) v list(map(int, readline().split())) list map(int, readline().split()) map int readline().split() ().split () readline split ans = -float('inf') ans -float('inf') float('inf') float 'inf' nk = min(n, k) nk min(n, k) min n k for a in range(nk + 1):
    for b in range(nk - a + 1):
        vv = v[:a] + v[n - b:]
        if k - a - b >= 0:
            vv.sort()
            cnt = 0
            for check in vv[:k - a - b]:
                if check >= 0:
                    break
                cnt += check
            ans = max(ans, sum(vv) - cnt) a range(nk + 1) range nk + 1 nk 1 for b in range(nk - a + 1):
        vv = v[:a] + v[n - b:]
        if k - a - b >= 0:
            vv.sort()
            cnt = 0
            for check in vv[:k - a - b]:
                if check >= 0:
                    break
                cnt += check
            ans = max(ans, sum(vv) - cnt) b range(nk - a + 1) range nk - a + 1 nk - a nk a 1 vv = v[:a] + v[n - b:] vv v[:a] + v[n - b:] v[:a] v :a a v[n - b:] v n - b: n - b n b if k - a - b >= 0:
            vv.sort()
            cnt = 0
            for check in vv[:k - a - b]:
                if check >= 0:
                    break
                cnt += check
            ans = max(ans, sum(vv) - cnt) k - a - b >= 0 k - a - b k - a k a b 0 vv.sort() vv.sort vv sort cnt = 0 cnt 0 for check in vv[:k - a - b]:
                if check >= 0:
                    break
                cnt += check
             check vv[:k - a - b] vv :k - a - b k - a - b k - a k a b if check >= 0:
                    break
                 check >= 0 check 0 break cnt += check cnt check ans = max(ans, sum(vv) - cnt) ans max(ans, sum(vv) - cnt) max ans sum(vv) - cnt sum(vv) sum vv cnt print(ans) print ans read = sys.stdin.buffer.read sys.stdin.buffer.read read cnt = 0 0 cnt vv = v[:a] + v[n - b:] v[:a] + v[n - b:] vv cnt += check check cnt ans = max(ans, sum(vv) - cnt) max(ans, sum(vv) - cnt) ans nk = min(n, k) min(n, k) nk v = list(map(int, readline().split())) list(map(int, readline().split())) v n, k = map(int, readline().split()) map(int, readline().split()) n k = map(int, readline().split()) map(int, readline().split()) k readline = sys.stdin.buffer.readline sys.stdin.buffer.readline readline readlines = sys.stdin.buffer.readlines sys.stdin.buffer.readlines readlines ans = -float('inf') -float('inf') ans
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n, k = map(int, readline().split())
v = list(map(int, readline().split()))
ans = -float('inf')
nk = min(n, k)
for a in range(nk + 1):
    for b in range(nk - a + 1):
        vv = v[:a] + v[n - b:]
        if k - a - b >= 0:
            vv.sort()
            cnt = 0
            for check in vv[:k - a - b]:
                if check >= 0:
                    break
                cnt += check
            ans = max(ans, sum(vv) - cnt)
print(ans)
