from bisect import bisect_left
N,K = map(int,input().split())
V = list(map(int,input().split()))
vmax = -10**9
for n in range(min(N,K)+1):
    A = V[:n]
    for m in range(min(N,K)-n+1):
        B = V[N-m:]
        B = B+A
        B = sorted(B)
        ind = bisect_left(B,0)
        k = min(ind,K-n-m)
        v = sum(B[k:])
        vmax = max(vmax,v)
print(vmax)        from bisect import bisect_left 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 vmax = -10**9 vmax -10**9 -10 9 for n in range(min(N,K)+1):
    A = V[:n]
    for m in range(min(N,K)-n+1):
        B = V[N-m:]
        B = B+A
        B = sorted(B)
        ind = bisect_left(B,0)
        k = min(ind,K-n-m)
        v = sum(B[k:])
        vmax = max(vmax,v) n range(min(N,K)+1) range min(N,K)+1 min(N,K) min N K 1 A = V[:n] A V[:n] V :n n for m in range(min(N,K)-n+1):
        B = V[N-m:]
        B = B+A
        B = sorted(B)
        ind = bisect_left(B,0)
        k = min(ind,K-n-m)
        v = sum(B[k:])
        vmax = max(vmax,v) m range(min(N,K)-n+1) range min(N,K)-n+1 min(N,K)-n min(N,K) min N K n 1 B = V[N-m:] B V[N-m:] V N-m: N-m N m B = B+A B B+A B A B = sorted(B) B sorted(B) sorted B ind = bisect_left(B,0) ind bisect_left(B,0) bisect_left B 0 k = min(ind,K-n-m) k min(ind,K-n-m) min ind K-n-m K-n K n m v = sum(B[k:]) v sum(B[k:]) sum B[k:] B k: k vmax = max(vmax,v) vmax max(vmax,v) max vmax v print(vmax) print vmax vmax = -10**9 -10**9 vmax B = B+A B+A B N,K = map(int,input().split()) map(int,input().split()) N ind = bisect_left(B,0) bisect_left(B,0) ind B = sorted(B) sorted(B) B k = min(ind,K-n-m) min(ind,K-n-m) k vmax = max(vmax,v) max(vmax,v) vmax K = map(int,input().split()) map(int,input().split()) K V = list(map(int,input().split())) list(map(int,input().split())) V A = V[:n] V[:n] A B = V[N-m:] V[N-m:] B v = sum(B[k:]) sum(B[k:]) v
from bisect import bisect_left
N,K = map(int,input().split())
V = list(map(int,input().split()))
vmax = -10**9
for n in range(min(N,K)+1):
    A = V[:n]
    for m in range(min(N,K)-n+1):
        B = V[N-m:]
        B = B+A
        B = sorted(B)
        ind = bisect_left(B,0)
        k = min(ind,K-n-m)
        v = sum(B[k:])
        vmax = max(vmax,v)
print(vmax)       