from collections import deque

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

mini = min(n,k)
mx = 0

for a in range(mini+1):
    for b in range(mini+1-a):
        if b==0:
            bb=-n-2
        else:
            bb=b
        ttl = sum(v[:a])+sum(v[-bb:])
        #print(a,b,sum(v[:a]),sum(v[-bb:]))
        hu = [i for i in v[:a] if i<0 ]
        hu.extend([i for i in v[-bb:] if i<0] )
        hu.sort()
        ttl-= sum(hu[:k-a-b])
        mx = max(mx,ttl)
print(mx) from collections import deque 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 mini = min(n,k) mini min(n,k) min n k mx = 0 mx 0 for a in range(mini+1):
    for b in range(mini+1-a):
        if b==0:
            bb=-n-2
        else:
            bb=b
        ttl = sum(v[:a])+sum(v[-bb:])
        #print(a,b,sum(v[:a]),sum(v[-bb:]))
        hu = [i for i in v[:a] if i<0 ]
        hu.extend([i for i in v[-bb:] if i<0] )
        hu.sort()
        ttl-= sum(hu[:k-a-b])
        mx = max(mx,ttl) a range(mini+1) range mini+1 mini 1 for b in range(mini+1-a):
        if b==0:
            bb=-n-2
        else:
            bb=b
        ttl = sum(v[:a])+sum(v[-bb:])
        #print(a,b,sum(v[:a]),sum(v[-bb:]))
        hu = [i for i in v[:a] if i<0 ]
        hu.extend([i for i in v[-bb:] if i<0] )
        hu.sort()
        ttl-= sum(hu[:k-a-b])
        mx = max(mx,ttl) b range(mini+1-a) range mini+1-a mini+1 mini 1 a if b==0:
            bb=-n-2
        else:
            bb=b
         b==0 b 0 bb=-n-2 bb -n-2 -n n 2 bb=b bb b ttl = sum(v[:a])+sum(v[-bb:]) ttl sum(v[:a])+sum(v[-bb:]) sum(v[:a]) sum v[:a] v :a a sum(v[-bb:]) sum v[-bb:] v -bb: -bb bb i for i in v[:a] if i<0 for i in v[:a] if i<0 i v[:a] v :a a i<0 i 0 if i<0 i hu = [i for i in v[:a] if i<0 ] hu [i for i in v[:a] if i<0 ] i for i in v[-bb:] if i<0 for i in v[-bb:] if i<0 i v[-bb:] v -bb: -bb bb i<0 i 0 if i<0 i hu.extend([i for i in v[-bb:] if i<0] ) hu.extend hu extend [i for i in v[-bb:] if i<0] hu.sort() hu.sort hu sort ttl-= sum(hu[:k-a-b]) ttl sum(hu[:k-a-b]) sum hu[:k-a-b] hu :k-a-b k-a-b k-a k a b mx = max(mx,ttl) mx max(mx,ttl) max mx ttl print(mx) print mx n,k = map(int,input().split()) map(int,input().split()) n bb=-n-2 -n-2 bb k = map(int,input().split()) map(int,input().split()) k mini = min(n,k) min(n,k) mini v = list(map(int,input().split())) list(map(int,input().split())) v bb=b b bb ttl = sum(v[:a])+sum(v[-bb:]) sum(v[:a])+sum(v[-bb:]) ttl ttl-= sum(hu[:k-a-b]) sum(hu[:k-a-b]) ttl mx = 0 0 mx mx = max(mx,ttl) max(mx,ttl) mx hu = [i for i in v[:a] if i<0 ] [i for i in v[:a] if i<0 ] hu
from collections import deque

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

mini = min(n,k)
mx = 0

for a in range(mini+1):
    for b in range(mini+1-a):
        if b==0:
            bb=-n-2
        else:
            bb=b
        ttl = sum(v[:a])+sum(v[-bb:])
        #print(a,b,sum(v[:a]),sum(v[-bb:]))
        hu = [i for i in v[:a] if i<0 ]
        hu.extend([i for i in v[-bb:] if i<0] )
        hu.sort()
        ttl-= sum(hu[:k-a-b])
        mx = max(mx,ttl)
print(mx)
