import heapq 
N,K = map(int,input().split())
A = list(map(int,input().split()))
m = min(N,K) #KがNより大きい場合を防ぐ
M = 0 
for i in range(m+1):
    for j in range(m-i+1):
        A1 = A[:i] #前から取る
        A3 = A[N-j:] #後ろから取る
        new_A = A1+A3
        new_A.sort(reverse=True)
        h = K - len(new_A)
        while h >0 and new_A:
            if new_A[-1] > 0:
                break
            else:
                new_A.pop()
                h-=1
        M = max(M,sum(new_A))
print(M) import heapq heapq N,K = map(int,input().split()) N map(int,input().split()) map int input().split() ().split () input split K A = list(map(int,input().split())) A list(map(int,input().split())) list map(int,input().split()) map int input().split() ().split () input split m = min(N,K) m min(N,K) min N K M = 0 M 0 for i in range(m+1):
    for j in range(m-i+1):
        A1 = A[:i] #前から取る
        A3 = A[N-j:] #後ろから取る
        new_A = A1+A3
        new_A.sort(reverse=True)
        h = K - len(new_A)
        while h >0 and new_A:
            if new_A[-1] > 0:
                break
            else:
                new_A.pop()
                h-=1
        M = max(M,sum(new_A)) i range(m+1) range m+1 m 1 for j in range(m-i+1):
        A1 = A[:i] #前から取る
        A3 = A[N-j:] #後ろから取る
        new_A = A1+A3
        new_A.sort(reverse=True)
        h = K - len(new_A)
        while h >0 and new_A:
            if new_A[-1] > 0:
                break
            else:
                new_A.pop()
                h-=1
        M = max(M,sum(new_A)) j range(m-i+1) range m-i+1 m-i m i 1 A1 = A[:i] A1 A[:i] A :i i A3 = A[N-j:] A3 A[N-j:] A N-j: N-j N j new_A = A1+A3 new_A A1+A3 A1 A3 new_A.sort(reverse=True) new_A.sort new_A sort reverse=True reverse True h = K - len(new_A) h K - len(new_A) K len(new_A) len new_A while h >0 and new_A:
            if new_A[-1] > 0:
                break
            else:
                new_A.pop()
                h-=1
         h >0 and new_A h >0 h 0 new_A if new_A[-1] > 0:
                break
            else:
                new_A.pop()
                h-=1
         new_A[-1] > 0 new_A[-1] new_A -1 0 break new_A.pop() new_A.pop new_A pop h-=1 h 1 M = max(M,sum(new_A)) M max(M,sum(new_A)) max M sum(new_A) sum new_A print(M) print M K = map(int,input().split()) map(int,input().split()) K A1 = A[:i] A[:i] A1 m = min(N,K) min(N,K) m h-=1 1 h A = list(map(int,input().split())) list(map(int,input().split())) A h = K - len(new_A) K - len(new_A) h A3 = A[N-j:] A[N-j:] A3 N,K = map(int,input().split()) map(int,input().split()) N new_A = A1+A3 A1+A3 new_A M = max(M,sum(new_A)) max(M,sum(new_A)) M M = 0 0 M
import heapq 
N,K = map(int,input().split())
A = list(map(int,input().split()))
m = min(N,K) #KがNより大きい場合を防ぐ
M = 0 
for i in range(m+1):
    for j in range(m-i+1):
        A1 = A[:i] #前から取る
        A3 = A[N-j:] #後ろから取る
        new_A = A1+A3
        new_A.sort(reverse=True)
        h = K - len(new_A)
        while h >0 and new_A:
            if new_A[-1] > 0:
                break
            else:
                new_A.pop()
                h-=1
        M = max(M,sum(new_A))
print(M)