#import heapq

import copy



N,K=list(map(int,input().split()))

V=list(map(int,input().split()))

V.append(0)#右から取る時用



S=[0]*(N+1)#累積和

for i in range(N):

    S[i+1]=S[i]+V[i]



#左からi番目まで見たときに，持っている負の宝石

left=[[] for _ in range(N+1)]

for i in range(N):

    left[i+1]=copy.deepcopy(left[i])

    if V[i]<0:

        left[i+1].append(V[i])

        left[i+1].sort()

       

        

ans=0

#左からi個，右からj個とったときについて考える

for i in range(N+1):

    temp=S[i]

    rem=copy.deepcopy(left[i])

    #heapq.heapify(rem)

    for j in range(N+1-i):

        if i+j>K:

            break

        temp+=S[-1]-S[N-j]

        if V[N-j]<0:

            #heapq.heappush(rem,V[N-j])

            rem.append(V[N-j])

            rem.sort()

        ret=K-(i+j)#負の宝石を返却できる回数

        if ret>0:

            for k in range(min(ret,len(rem))):

                temp-=rem[k]

        ans=max(ans,temp)

        temp=S[i]

        

print(ans)





import sys

input = sys.stdin.readline



def I(): return int(eval(input()))

def MI(): return list(map(int, input().split()))

def LI(): return list(map(int, input().split()))



def main():

    mod=10**9+7

    N,K=MI()

    V=LI()

    

    ans=0

    import heapq

    

    #左からa個，右からb個とって，あまり分で負数を捨てる

    for a in range(K+1):

        for b in range(K+1):

            if a+b>min(K,N):

                break

            hq=[]

            heapq.heapify(hq)

            for i in range(a):

                heapq.heappush(hq,V[i])

            for j in range(b):

                heapq.heappush(hq,V[N-1-j])

                

            temp=0

            rem=K-(a+b)

            for i in range(a+b):

                v=heapq.heappop(hq)

                if v>=0:

                    temp+=v

                else:

                    if rem>0:

                        rem-=1

                    else:

                        temp+=v

                        

            ans=max(ans,temp)

            

    print(ans)

                

            



main()

# import heapq
import copy

N, K = list(map(int, input().split()))
V = list(map(int, input().split()))
V.append(0)  # 右から取る時用
S = [0] * (N + 1)  # 累積和
for i in range(N):
    S[i + 1] = S[i] + V[i]
# 左からi番目まで見たときに，持っている負の宝石
left = [[] for _ in range(N + 1)]
for i in range(N):
    left[i + 1] = copy.deepcopy(left[i])
    if V[i] < 0:
        left[i + 1].append(V[i])
        left[i + 1].sort()
ans = 0
# 左からi個，右からj個とったときについて考える
for i in range(N + 1):
    temp = S[i]
    rem = copy.deepcopy(left[i])
    # heapq.heapify(rem)
    for j in range(N + 1 - i):
        if i + j > K:
            break
        temp += S[-1] - S[N - j]
        if V[N - j] < 0:
            # heapq.heappush(rem,V[N-j])
            rem.append(V[N - j])
            rem.sort()
        ret = K - (i + j)  # 負の宝石を返却できる回数
        if ret > 0:
            for k in range(min(ret, len(rem))):
                temp -= rem[k]
        ans = max(ans, temp)
        temp = S[i]
print(ans)

import sys

input = sys.stdin.readline


def I():
    return int(eval(input()))


def MI():
    return list(map(int, input().split()))


def LI():
    return list(map(int, input().split()))


def main():
    mod = 10**9 + 7
    N, K = MI()
    V = LI()
    ans = 0
    import heapq

    # 左からa個，右からb個とって，あまり分で負数を捨てる
    for a in range(K + 1):
        for b in range(K + 1):
            if a + b > min(K, N):
                break
            hq = []
            heapq.heapify(hq)
            for i in range(a):
                heapq.heappush(hq, V[i])
            for j in range(b):
                heapq.heappush(hq, V[N - 1 - j])
            temp = 0
            rem = K - (a + b)
            for i in range(a + b):
                v = heapq.heappop(hq)
                if v >= 0:
                    temp += v
                else:
                    if rem > 0:
                        rem -= 1
                    else:
                        temp += v
            ans = max(ans, temp)
    print(ans)


main()

-# import heapq -import copy +import sys -N, K = list(map(int, input().split())) -V = list(map(int, input().split())) -V.append(0)  # 右から取る時用 -S = [0] * (N + 1)  # 累積和 -for i in range(N): -    S[i + 1] = S[i] + V[i] -# 左からi番目まで見たときに，持っている負の宝石 -left = [[] for _ in range(N + 1)] -for i in range(N): -    left[i + 1] = copy.deepcopy(left[i]) -    if V[i] < 0: -        left[i + 1].append(V[i]) -        left[i + 1].sort() -ans = 0 -# 左からi個，右からj個とったときについて考える -for i in range(N + 1): -    temp = S[i] -    rem = copy.deepcopy(left[i]) -    # heapq.heapify(rem) -    for j in range(N + 1 - i): -        if i + j > K: -            break -        temp += S[-1] - S[N - j] -        if V[N - j] < 0: -            # heapq.heappush(rem,V[N-j]) -            rem.append(V[N - j]) -            rem.sort() -        ret = K - (i + j)  # 負の宝石を返却できる回数 -        if ret > 0: -            for k in range(min(ret, len(rem))): -                temp -= rem[k] -        ans = max(ans, temp) -        temp = S[i] -print(ans) +input = sys.stdin.readline + + +def I(): +    return int(eval(input())) + + +def MI(): +    return list(map(int, input().split())) + + +def LI(): +    return list(map(int, input().split())) + + +def main(): +    mod = 10**9 + 7 +    N, K = MI() +    V = LI() +    ans = 0 +    import heapq + +    # 左からa個，右からb個とって，あまり分で負数を捨てる +    for a in range(K + 1): +        for b in range(K + 1): +            if a + b > min(K, N): +                break +            hq = [] +            heapq.heapify(hq) +            for i in range(a): +                heapq.heappush(hq, V[i]) +            for j in range(b): +                heapq.heappush(hq, V[N - 1 - j]) +            temp = 0 +            rem = K - (a + b) +            for i in range(a + b): +                v = heapq.heappop(hq) +                if v >= 0: +                    temp += v +                else: +                    if rem > 0: +                        rem -= 1 +                    else: +                        temp += v +            ans = max(ans, temp) +    print(ans) + + +main()