import heapq

res=0
n,k=map(int,input().split())
v=list(map(int,input().split()))
r=min(n,k)
for a in range(r+1):#操作Aの回数
    for b in range(r-a+1):#Bの回数
        d=v[:a]
        d.extend(v[n-b:])
        s=sum(d)
        heapq.heapify(d)

        l=k-(a+b) #捨てる回数print(d)
        for _ in range(l):
            if not d:
              break
            t=heapq.heappop(d)
            if t<0:
               s-=t
            else:
                break
        if s>res:
            res=s
print(res)  import heapq heapq res=0 res 0 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 r=min(n,k) r min(n,k) min n k for a in range(r+1):#操作Aの回数
    for b in range(r-a+1):#Bの回数
        d=v[:a]
        d.extend(v[n-b:])
        s=sum(d)
        heapq.heapify(d)

        l=k-(a+b) #捨てる回数print(d)
        for _ in range(l):
            if not d:
              break
            t=heapq.heappop(d)
            if t<0:
               s-=t
            else:
                break
        if s>res:
            res=s a range(r+1) range r+1 r 1 for b in range(r-a+1):#Bの回数
        d=v[:a]
        d.extend(v[n-b:])
        s=sum(d)
        heapq.heapify(d)

        l=k-(a+b) #捨てる回数print(d)
        for _ in range(l):
            if not d:
              break
            t=heapq.heappop(d)
            if t<0:
               s-=t
            else:
                break
        if s>res:
            res=s b range(r-a+1) range r-a+1 r-a r a 1 d=v[:a] d v[:a] v :a a d.extend(v[n-b:]) d.extend d extend v[n-b:] v n-b: n-b n b s=sum(d) s sum(d) sum d heapq.heapify(d) heapq.heapify heapq heapify d l=k-(a+b) l k-(a+b) k a+b a b for _ in range(l):
            if not d:
              break
            t=heapq.heappop(d)
            if t<0:
               s-=t
            else:
                break
         _ range(l) range l if not d:
              break
             not d d break t=heapq.heappop(d) t heapq.heappop(d) heapq.heappop heapq heappop d if t<0:
               s-=t
            else:
                break
         t<0 t 0 s-=t s t break if s>res:
            res=s s>res s res res=s res s print(res) print res k=map(int,input().split()) map(int,input().split()) k d=v[:a] v[:a] d s=sum(d) sum(d) s res=0 0 res r=min(n,k) min(n,k) r t=heapq.heappop(d) heapq.heappop(d) t res=s s res l=k-(a+b) k-(a+b) l n,k=map(int,input().split()) map(int,input().split()) n s-=t t s v=list(map(int,input().split())) list(map(int,input().split())) v
import heapq

res=0
n,k=map(int,input().split())
v=list(map(int,input().split()))
r=min(n,k)
for a in range(r+1):#操作Aの回数
    for b in range(r-a+1):#Bの回数
        d=v[:a]
        d.extend(v[n-b:])
        s=sum(d)
        heapq.heapify(d)

        l=k-(a+b) #捨てる回数print(d)
        for _ in range(l):
            if not d:
              break
            t=heapq.heappop(d)
            if t<0:
               s-=t
            else:
                break
        if s>res:
            res=s
print(res) 
