# D - equeue

import heapq

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

ans = 0
for l in range(k+1):
    for r in range(k+1-l):
        if l+r>n:
            continue
        tmp = 0
        s = []
        for i in range(l):
            tmp += v[i]
            heapq.heappush(s, v[i])

        for i in range(n-r, n):
            tmp += v[i]
            heapq.heappush(s, v[i])
            
        for i in range(k-l-r):
            if len(s)==0:
                break
            if s[0]>0:
                break
            tmp -= heapq.heappop(s)
        
        ans = max(ans, tmp)

print(ans) import heapq heapq n, k = map(int, input().split()) n map(int, input().split()) map int input().split() ().split () input split k v = list(int(x) for x in input().split()) v list(int(x) for x in input().split()) list int(x) int x ans = 0 ans 0 for l in range(k+1):
    for r in range(k+1-l):
        if l+r>n:
            continue
        tmp = 0
        s = []
        for i in range(l):
            tmp += v[i]
            heapq.heappush(s, v[i])

        for i in range(n-r, n):
            tmp += v[i]
            heapq.heappush(s, v[i])
            
        for i in range(k-l-r):
            if len(s)==0:
                break
            if s[0]>0:
                break
            tmp -= heapq.heappop(s)
        
        ans = max(ans, tmp) l range(k+1) range k+1 k 1 for r in range(k+1-l):
        if l+r>n:
            continue
        tmp = 0
        s = []
        for i in range(l):
            tmp += v[i]
            heapq.heappush(s, v[i])

        for i in range(n-r, n):
            tmp += v[i]
            heapq.heappush(s, v[i])
            
        for i in range(k-l-r):
            if len(s)==0:
                break
            if s[0]>0:
                break
            tmp -= heapq.heappop(s)
        
        ans = max(ans, tmp) r range(k+1-l) range k+1-l k+1 k 1 l if l+r>n:
            continue
         l+r>n l+r l r n continue tmp = 0 tmp 0 s = [] s [] for i in range(l):
            tmp += v[i]
            heapq.heappush(s, v[i])

         i range(l) range l tmp += v[i] tmp v[i] v i heapq.heappush(s, v[i]) heapq.heappush heapq heappush s v[i] v i for i in range(n-r, n):
            tmp += v[i]
            heapq.heappush(s, v[i])
            
         i range(n-r, n) range n-r n r n tmp += v[i] tmp v[i] v i heapq.heappush(s, v[i]) heapq.heappush heapq heappush s v[i] v i for i in range(k-l-r):
            if len(s)==0:
                break
            if s[0]>0:
                break
            tmp -= heapq.heappop(s)
        
         i range(k-l-r) range k-l-r k-l k l r if len(s)==0:
                break
             len(s)==0 len(s) len s 0 break if s[0]>0:
                break
             s[0]>0 s[0] s 0 0 break tmp -= heapq.heappop(s) tmp heapq.heappop(s) heapq.heappop heapq heappop s ans = max(ans, tmp) ans max(ans, tmp) max ans tmp print(ans) print ans k = map(int, input().split()) map(int, input().split()) k tmp -= heapq.heappop(s) heapq.heappop(s) tmp tmp += v[i] v[i] tmp tmp = 0 0 tmp tmp += v[i] v[i] tmp ans = max(ans, tmp) max(ans, tmp) ans v = list(int(x) for x in input().split()) list(int(x) for x in input().split()) v ans = 0 0 ans s = [] [] s n, k = map(int, input().split()) map(int, input().split()) n
# D - equeue

import heapq

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

ans = 0
for l in range(k+1):
    for r in range(k+1-l):
        if l+r>n:
            continue
        tmp = 0
        s = []
        for i in range(l):
            tmp += v[i]
            heapq.heappush(s, v[i])

        for i in range(n-r, n):
            tmp += v[i]
            heapq.heappush(s, v[i])
            
        for i in range(k-l-r):
            if len(s)==0:
                break
            if s[0]>0:
                break
            tmp -= heapq.heappop(s)
        
        ans = max(ans, tmp)

print(ans)