from itertools import accumulate

n, k = map(int, input().split())
v = list(map(int, input().split()))

ans = 0
acc = [0] + list(accumulate(v))
for l in range(min(n, k) + 1):
    for r in range(min(n, k) - l + 1):
        minus = sorted(v[:l] + v[n - r:])
        bag = acc[l] - acc[0] + acc[n] - acc[n - r]
        bag -= sum(vi for vi in minus[:k - l - r] if vi < 0)
        ans = max(ans, bag)
print(ans) from itertools import accumulate 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 acc = [0] + list(accumulate(v)) acc [0] + list(accumulate(v)) [0] 0 list(accumulate(v)) list accumulate(v) accumulate v for l in range(min(n, k) + 1):
    for r in range(min(n, k) - l + 1):
        minus = sorted(v[:l] + v[n - r:])
        bag = acc[l] - acc[0] + acc[n] - acc[n - r]
        bag -= sum(vi for vi in minus[:k - l - r] if vi < 0)
        ans = max(ans, bag) l range(min(n, k) + 1) range min(n, k) + 1 min(n, k) min n k 1 for r in range(min(n, k) - l + 1):
        minus = sorted(v[:l] + v[n - r:])
        bag = acc[l] - acc[0] + acc[n] - acc[n - r]
        bag -= sum(vi for vi in minus[:k - l - r] if vi < 0)
        ans = max(ans, bag) r range(min(n, k) - l + 1) range min(n, k) - l + 1 min(n, k) - l min(n, k) min n k l 1 minus = sorted(v[:l] + v[n - r:]) minus sorted(v[:l] + v[n - r:]) sorted v[:l] + v[n - r:] v[:l] v :l l v[n - r:] v n - r: n - r n r bag = acc[l] - acc[0] + acc[n] - acc[n - r] bag acc[l] - acc[0] + acc[n] - acc[n - r] acc[l] - acc[0] + acc[n] acc[l] - acc[0] acc[l] acc l acc[0] acc 0 acc[n] acc n acc[n - r] acc n - r n r bag -= sum(vi for vi in minus[:k - l - r] if vi < 0) bag sum(vi for vi in minus[:k - l - r] if vi < 0) sum vi ans = max(ans, bag) ans max(ans, bag) max ans bag print(ans) print ans minus = sorted(v[:l] + v[n - r:]) sorted(v[:l] + v[n - r:]) minus bag -= sum(vi for vi in minus[:k - l - r] if vi < 0) sum(vi for vi in minus[:k - l - r] if vi < 0) bag k = map(int, input().split()) map(int, input().split()) k acc = [0] + list(accumulate(v)) [0] + list(accumulate(v)) acc bag = acc[l] - acc[0] + acc[n] - acc[n - r] acc[l] - acc[0] + acc[n] - acc[n - r] bag v = list(map(int, input().split())) list(map(int, input().split())) v ans = 0 0 ans n, k = map(int, input().split()) map(int, input().split()) n ans = max(ans, bag) max(ans, bag) ans
from itertools import accumulate

n, k = map(int, input().split())
v = list(map(int, input().split()))

ans = 0
acc = [0] + list(accumulate(v))
for l in range(min(n, k) + 1):
    for r in range(min(n, k) - l + 1):
        minus = sorted(v[:l] + v[n - r:])
        bag = acc[l] - acc[0] + acc[n] - acc[n - r]
        bag -= sum(vi for vi in minus[:k - l - r] if vi < 0)
        ans = max(ans, bag)
print(ans)
