from collections import deque
import heapq
def f(d,n):
    a = list(d)
    heapq.heapify(a)
    while n > 0 and len(a) > 0:
        tmp = heapq.heappop(a)
        if tmp >= 0:
            heapq.heappush(a,tmp)
            break
        n -= 1
    return sum(a)
#マイナスを取り除いて和を求める関数をあらかじめ作っておく
#優先度付きキューを使うと楽
N, K = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
for i in range(1,min(N,K)+1):
    d = deque()
    for k in range(i):
        d.append(V[k])
    ans = max(ans,f(d,K-i))
    for k in range(i):
        d.pop()
        d.appendleft(V[N-1-k])
        ans = max(ans,f(d,K-i))
print(ans)
#解説中のa+b,bについてループで全探索している from collections import deque import heapq heapq def f(d,n):
    a = list(d)
    heapq.heapify(a)
    while n > 0 and len(a) > 0:
        tmp = heapq.heappop(a)
        if tmp >= 0:
            heapq.heappush(a,tmp)
            break
        n -= 1
    return sum(a)
#マイナスを取り除いて和を求める関数をあらかじめ作っておく
#優先度付きキューを使うと楽 f a = list(d) a list(d) list d heapq.heapify(a) heapq.heapify heapq heapify a while n > 0 and len(a) > 0:
        tmp = heapq.heappop(a)
        if tmp >= 0:
            heapq.heappush(a,tmp)
            break
        n -= 1
     n > 0 and len(a) > 0 n > 0 n 0 len(a) > 0 len(a) len a 0 tmp = heapq.heappop(a) tmp heapq.heappop(a) heapq.heappop heapq heappop a if tmp >= 0:
            heapq.heappush(a,tmp)
            break
         tmp >= 0 tmp 0 heapq.heappush(a,tmp) heapq.heappush heapq heappush a tmp break n -= 1 n 1 return sum(a) sum(a) sum a d d n n 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 i in range(1,min(N,K)+1):
    d = deque()
    for k in range(i):
        d.append(V[k])
    ans = max(ans,f(d,K-i))
    for k in range(i):
        d.pop()
        d.appendleft(V[N-1-k])
        ans = max(ans,f(d,K-i)) i range(1,min(N,K)+1) range 1 min(N,K)+1 min(N,K) min N K 1 d = deque() d deque() deque for k in range(i):
        d.append(V[k])
     k range(i) range i d.append(V[k]) d.append d append V[k] V k ans = max(ans,f(d,K-i)) ans max(ans,f(d,K-i)) max ans f(d,K-i) f d K-i K i for k in range(i):
        d.pop()
        d.appendleft(V[N-1-k])
        ans = max(ans,f(d,K-i)) k range(i) range i d.pop() d.pop d pop d.appendleft(V[N-1-k]) d.appendleft d appendleft V[N-1-k] V N-1-k N-1 N 1 k ans = max(ans,f(d,K-i)) ans max(ans,f(d,K-i)) max ans f(d,K-i) f d K-i K i print(ans) print ans K = map(int, input().split()) map(int, input().split()) K d = deque() deque() d ans = max(ans,f(d,K-i)) max(ans,f(d,K-i)) ans ans = max(ans,f(d,K-i)) max(ans,f(d,K-i)) ans def f(d,n):
    a = list(d)
    heapq.heapify(a)
    while n > 0 and len(a) > 0:
        tmp = heapq.heappop(a)
        if tmp >= 0:
            heapq.heappush(a,tmp)
            break
        n -= 1
    return sum(a)
#マイナスを取り除いて和を求める関数をあらかじめ作っておく
#優先度付きキューを使うと楽 def f(d,n):
    a = list(d)
    heapq.heapify(a)
    while n > 0 and len(a) > 0:
        tmp = heapq.heappop(a)
        if tmp >= 0:
            heapq.heappush(a,tmp)
            break
        n -= 1
    return sum(a)
#マイナスを取り除いて和を求める関数をあらかじめ作っておく
#優先度付きキューを使うと楽 f ans = 0 0 ans V = list(map(int, input().split())) list(map(int, input().split())) V N, K = map(int, input().split()) map(int, input().split()) N
from collections import deque
import heapq
def f(d,n):
    a = list(d)
    heapq.heapify(a)
    while n > 0 and len(a) > 0:
        tmp = heapq.heappop(a)
        if tmp >= 0:
            heapq.heappush(a,tmp)
            break
        n -= 1
    return sum(a)
#マイナスを取り除いて和を求める関数をあらかじめ作っておく
#優先度付きキューを使うと楽
N, K = map(int, input().split())
V = list(map(int, input().split()))
ans = 0
for i in range(1,min(N,K)+1):
    d = deque()
    for k in range(i):
        d.append(V[k])
    ans = max(ans,f(d,K-i))
    for k in range(i):
        d.pop()
        d.appendleft(V[N-1-k])
        ans = max(ans,f(d,K-i))
print(ans)
#解説中のa+b,bについてループで全探索している
