import sys
def input(): return sys.stdin.readline().rstrip()
import heapq
def main():
    n,k=map(int,input().split())
    V=list(map(int,input().split()))
    ans=0
    for a in range(min(k+1,n+1)):
        for b in range(min(k-a,n-a)+1):
            cd=k-(a+b)
            H=[]
            sum_V=0
            for i in range(n):
                v=V[i]
                if i+1<=a or i>=n-b:
                    sum_V+=v
                    if v<0:
                        heapq.heappush(H,V[i])
            while H and cd>0:
                sum_V-=heapq.heappop(H)
                cd-=1
            ans=max(sum_V,ans)
    print(ans)


if __name__=='__main__':
    main() import sys sys def input(): return sys.stdin.readline().rstrip() input import heapq heapq def main():
    n,k=map(int,input().split())
    V=list(map(int,input().split()))
    ans=0
    for a in range(min(k+1,n+1)):
        for b in range(min(k-a,n-a)+1):
            cd=k-(a+b)
            H=[]
            sum_V=0
            for i in range(n):
                v=V[i]
                if i+1<=a or i>=n-b:
                    sum_V+=v
                    if v<0:
                        heapq.heappush(H,V[i])
            while H and cd>0:
                sum_V-=heapq.heappop(H)
                cd-=1
            ans=max(sum_V,ans)
    print(ans) main 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 for a in range(min(k+1,n+1)):
        for b in range(min(k-a,n-a)+1):
            cd=k-(a+b)
            H=[]
            sum_V=0
            for i in range(n):
                v=V[i]
                if i+1<=a or i>=n-b:
                    sum_V+=v
                    if v<0:
                        heapq.heappush(H,V[i])
            while H and cd>0:
                sum_V-=heapq.heappop(H)
                cd-=1
            ans=max(sum_V,ans)
     a range(min(k+1,n+1)) range min(k+1,n+1) min k+1 k 1 n+1 n 1 for b in range(min(k-a,n-a)+1):
            cd=k-(a+b)
            H=[]
            sum_V=0
            for i in range(n):
                v=V[i]
                if i+1<=a or i>=n-b:
                    sum_V+=v
                    if v<0:
                        heapq.heappush(H,V[i])
            while H and cd>0:
                sum_V-=heapq.heappop(H)
                cd-=1
            ans=max(sum_V,ans)
     b range(min(k-a,n-a)+1) range min(k-a,n-a)+1 min(k-a,n-a) min k-a k a n-a n a 1 cd=k-(a+b) cd k-(a+b) k a+b a b H=[] H [] sum_V=0 sum_V 0 for i in range(n):
                v=V[i]
                if i+1<=a or i>=n-b:
                    sum_V+=v
                    if v<0:
                        heapq.heappush(H,V[i])
             i range(n) range n v=V[i] v V[i] V i if i+1<=a or i>=n-b:
                    sum_V+=v
                    if v<0:
                        heapq.heappush(H,V[i])
             i+1<=a or i>=n-b i+1<=a i+1 i 1 a i>=n-b i n-b n b sum_V+=v sum_V v if v<0:
                        heapq.heappush(H,V[i])
             v<0 v 0 heapq.heappush(H,V[i]) heapq.heappush heapq heappush H V[i] V i while H and cd>0:
                sum_V-=heapq.heappop(H)
                cd-=1
             H and cd>0 H cd>0 cd 0 sum_V-=heapq.heappop(H) sum_V heapq.heappop(H) heapq.heappop heapq heappop H cd-=1 cd 1 ans=max(sum_V,ans) ans max(sum_V,ans) max sum_V ans print(ans) print ans if __name__=='__main__':
    main() __name__=='__main__' __name__ '__main__' main() main def main():
    n,k=map(int,input().split())
    V=list(map(int,input().split()))
    ans=0
    for a in range(min(k+1,n+1)):
        for b in range(min(k-a,n-a)+1):
            cd=k-(a+b)
            H=[]
            sum_V=0
            for i in range(n):
                v=V[i]
                if i+1<=a or i>=n-b:
                    sum_V+=v
                    if v<0:
                        heapq.heappush(H,V[i])
            while H and cd>0:
                sum_V-=heapq.heappop(H)
                cd-=1
            ans=max(sum_V,ans)
    print(ans) def main():
    n,k=map(int,input().split())
    V=list(map(int,input().split()))
    ans=0
    for a in range(min(k+1,n+1)):
        for b in range(min(k-a,n-a)+1):
            cd=k-(a+b)
            H=[]
            sum_V=0
            for i in range(n):
                v=V[i]
                if i+1<=a or i>=n-b:
                    sum_V+=v
                    if v<0:
                        heapq.heappush(H,V[i])
            while H and cd>0:
                sum_V-=heapq.heappop(H)
                cd-=1
            ans=max(sum_V,ans)
    print(ans) main def input(): return sys.stdin.readline().rstrip() def input(): return sys.stdin.readline().rstrip() input
import sys
def input(): return sys.stdin.readline().rstrip()
import heapq
def main():
    n,k=map(int,input().split())
    V=list(map(int,input().split()))
    ans=0
    for a in range(min(k+1,n+1)):
        for b in range(min(k-a,n-a)+1):
            cd=k-(a+b)
            H=[]
            sum_V=0
            for i in range(n):
                v=V[i]
                if i+1<=a or i>=n-b:
                    sum_V+=v
                    if v<0:
                        heapq.heappush(H,V[i])
            while H and cd>0:
                sum_V-=heapq.heappop(H)
                cd-=1
            ans=max(sum_V,ans)
    print(ans)


if __name__=='__main__':
    main()