# 初期入力
import bisect
import heapq
import sys
input = sys.stdin.readline
N,K = (int(i) for i in input().split())
V = list(map(int, input().split()))
V_sorted =sorted(V)

#取る個数take_num、返す個数reverse(t +r <=K)個全て決める

ans =[]
take_V =[] #最後に持っている宝石の価値
#Vが全てマイナスなら取らない
if V_sorted[-1] <=0:
    ans =0
else:
    take_num =min(K,N)
    for tak in range(1,take_num +1):
        for lef in range(tak+1):
            #for rev in range(reverse +1):
            
                take =[]  #取った宝石
                rig =N-(tak -lef )          #lef:左側の取る数 rig:右側の取る数
                take.extend(V[    :lef])
                take.extend(V[rig:   ])
                take.sort()
                reverse =min(K -tak,tak) #返す数はtake_numではなく、Kから引く
                ind_0 =bisect.bisect_left(take,0)
                rev =min(ind_0,reverse,K -tak)
                take_V.append(sum(take[rev:])) #返す分を除く
        
    ans =max(take_V)
print(ans) import bisect bisect import heapq heapq import sys sys input = sys.stdin.readline input sys.stdin.readline sys.stdin sys stdin readline int(i) for i in input().split() for i in input().split() i input().split() ().split () input split for i in input().split() int(i) int i N,K = (int(i) for i in input().split()) N (int(i) for i in 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 V_sorted =sorted(V) V_sorted sorted(V) sorted V ans =[] ans [] take_V =[] take_V [] if V_sorted[-1] <=0:
    ans =0
else:
    take_num =min(K,N)
    for tak in range(1,take_num +1):
        for lef in range(tak+1):
            #for rev in range(reverse +1):
            
                take =[]  #取った宝石
                rig =N-(tak -lef )          #lef:左側の取る数 rig:右側の取る数
                take.extend(V[    :lef])
                take.extend(V[rig:   ])
                take.sort()
                reverse =min(K -tak,tak) #返す数はtake_numではなく、Kから引く
                ind_0 =bisect.bisect_left(take,0)
                rev =min(ind_0,reverse,K -tak)
                take_V.append(sum(take[rev:])) #返す分を除く
        
    ans =max(take_V) V_sorted[-1] <=0 V_sorted[-1] V_sorted -1 0 ans =0 ans 0 take_num =min(K,N) take_num min(K,N) min K N for tak in range(1,take_num +1):
        for lef in range(tak+1):
            #for rev in range(reverse +1):
            
                take =[]  #取った宝石
                rig =N-(tak -lef )          #lef:左側の取る数 rig:右側の取る数
                take.extend(V[    :lef])
                take.extend(V[rig:   ])
                take.sort()
                reverse =min(K -tak,tak) #返す数はtake_numではなく、Kから引く
                ind_0 =bisect.bisect_left(take,0)
                rev =min(ind_0,reverse,K -tak)
                take_V.append(sum(take[rev:])) #返す分を除く
        
     tak range(1,take_num +1) range 1 take_num +1 take_num 1 for lef in range(tak+1):
            #for rev in range(reverse +1):
            
                take =[]  #取った宝石
                rig =N-(tak -lef )          #lef:左側の取る数 rig:右側の取る数
                take.extend(V[    :lef])
                take.extend(V[rig:   ])
                take.sort()
                reverse =min(K -tak,tak) #返す数はtake_numではなく、Kから引く
                ind_0 =bisect.bisect_left(take,0)
                rev =min(ind_0,reverse,K -tak)
                take_V.append(sum(take[rev:])) #返す分を除く
        
     lef range(tak+1) range tak+1 tak 1 take =[] take [] rig =N-(tak -lef ) rig N-(tak -lef ) N tak -lef tak lef take.extend(V[    :lef]) take.extend take extend V[    :lef] V :lef lef take.extend(V[rig:   ]) take.extend take extend V[rig:   ] V rig: rig take.sort() take.sort take sort reverse =min(K -tak,tak) reverse min(K -tak,tak) min K -tak K tak tak ind_0 =bisect.bisect_left(take,0) ind_0 bisect.bisect_left(take,0) bisect.bisect_left bisect bisect_left take 0 rev =min(ind_0,reverse,K -tak) rev min(ind_0,reverse,K -tak) min ind_0 reverse K -tak K tak take_V.append(sum(take[rev:])) take_V.append take_V append sum(take[rev:]) sum take[rev:] take rev: rev ans =max(take_V) ans max(take_V) max take_V print(ans) print ans N,K = (int(i) for i in input().split()) (int(i) for i in input().split()) N ans =[] [] ans take =[] [] take reverse =min(K -tak,tak) min(K -tak,tak) reverse ans =max(take_V) max(take_V) ans input = sys.stdin.readline sys.stdin.readline input ans =0 0 ans ind_0 =bisect.bisect_left(take,0) bisect.bisect_left(take,0) ind_0 rig =N-(tak -lef ) N-(tak -lef ) rig take_num =min(K,N) min(K,N) take_num V = list(map(int, input().split())) list(map(int, input().split())) V rev =min(ind_0,reverse,K -tak) min(ind_0,reverse,K -tak) rev K = (int(i) for i in input().split()) (int(i) for i in input().split()) K take_V =[] [] take_V V_sorted =sorted(V) sorted(V) V_sorted
# 初期入力
import bisect
import heapq
import sys
input = sys.stdin.readline
N,K = (int(i) for i in input().split())
V = list(map(int, input().split()))
V_sorted =sorted(V)

#取る個数take_num、返す個数reverse(t +r <=K)個全て決める

ans =[]
take_V =[] #最後に持っている宝石の価値
#Vが全てマイナスなら取らない
if V_sorted[-1] <=0:
    ans =0
else:
    take_num =min(K,N)
    for tak in range(1,take_num +1):
        for lef in range(tak+1):
            #for rev in range(reverse +1):
            
                take =[]  #取った宝石
                rig =N-(tak -lef )          #lef:左側の取る数 rig:右側の取る数
                take.extend(V[    :lef])
                take.extend(V[rig:   ])
                take.sort()
                reverse =min(K -tak,tak) #返す数はtake_numではなく、Kから引く
                ind_0 =bisect.bisect_left(take,0)
                rev =min(ind_0,reverse,K -tak)
                take_V.append(sum(take[rev:])) #返す分を除く
        
    ans =max(take_V)
print(ans)