import copy,heapq



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

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



ans=0

left=copy.copy(V)

for i in range(1,N):

    left[i]+=left[i-1]

right=copy.copy(V)

for i in range(1,N):

    right[-i-1]+=right[-i]

for i in range(0,N+1):

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

        dispose=K-i-j

        if 0>dispose:

            continue

        test=0

        q=[]

        if i>0:

            test+=left[i-1]

            q+=V[:i]

        if j>0:

            test+=right[-j]

            q+=V[N-j:N]



        heapq.heapify(q)

        while dispose>0 and q:

            x=heapq.heappop(q)

            if 0>x:

                test-=x

                dispose-=1

            else:

                break

        ans=max(ans,test)



print(ans)

import random



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

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



if K==0:

    print((0))

    exit()



if 0:

    V=[random.randint(-10**7,10**7) for i in range(N)]



ans=-float("inf")



for i in range(N+1):

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

        if i+j>K:

            continue

        tmp=[V[k] for k in range(i)]

        for k in range(j):

            tmp.append(V[-1-k])

        tmp.sort()

        rest=K-i-j

        minus=0

        base=sum(tmp)

        for k in range(min(rest,len(tmp))):

            if tmp[k]<0:

                minus+=tmp[k]

            else:

                break

        test=base-minus

        ans=max(ans,test)



print(ans)

import copy, heapq

N, K = list(map(int, input().split()))
V = list(map(int, input().split()))
ans = 0
left = copy.copy(V)
for i in range(1, N):
    left[i] += left[i - 1]
right = copy.copy(V)
for i in range(1, N):
    right[-i - 1] += right[-i]
for i in range(0, N + 1):
    for j in range(0, N - i + 1):
        dispose = K - i - j
        if 0 > dispose:
            continue
        test = 0
        q = []
        if i > 0:
            test += left[i - 1]
            q += V[:i]
        if j > 0:
            test += right[-j]
            q += V[N - j : N]
        heapq.heapify(q)
        while dispose > 0 and q:
            x = heapq.heappop(q)
            if 0 > x:
                test -= x
                dispose -= 1
            else:
                break
        ans = max(ans, test)
print(ans)

import random

N, K = list(map(int, input().split()))
V = list(map(int, input().split()))
if K == 0:
    print((0))
    exit()
if 0:
    V = [random.randint(-(10**7), 10**7) for i in range(N)]
ans = -float("inf")
for i in range(N + 1):
    for j in range(N - i + 1):
        if i + j > K:
            continue
        tmp = [V[k] for k in range(i)]
        for k in range(j):
            tmp.append(V[-1 - k])
        tmp.sort()
        rest = K - i - j
        minus = 0
        base = sum(tmp)
        for k in range(min(rest, len(tmp))):
            if tmp[k] < 0:
                minus += tmp[k]
            else:
                break
        test = base - minus
        ans = max(ans, test)
print(ans)

-import copy, heapq +import random -ans = 0 -left = copy.copy(V) -for i in range(1, N): -    left[i] += left[i - 1] -right = copy.copy(V) -for i in range(1, N): -    right[-i - 1] += right[-i] -for i in range(0, N + 1): -    for j in range(0, N - i + 1): -        dispose = K - i - j -        if 0 > dispose: +if K == 0: +    print((0)) +    exit() +if 0: +    V = [random.randint(-(10**7), 10**7) for i in range(N)] +ans = -float("inf") +for i in range(N + 1): +    for j in range(N - i + 1): +        if i + j > K: -        test = 0 -        q = [] -        if i > 0: -            test += left[i - 1] -            q += V[:i] -        if j > 0: -            test += right[-j] -            q += V[N - j : N] -        heapq.heapify(q) -        while dispose > 0 and q: -            x = heapq.heappop(q) -            if 0 > x: -                test -= x -                dispose -= 1 +        tmp = [V[k] for k in range(i)] +        for k in range(j): +            tmp.append(V[-1 - k]) +        tmp.sort() +        rest = K - i - j +        minus = 0 +        base = sum(tmp) +        for k in range(min(rest, len(tmp))): +            if tmp[k] < 0: +                minus += tmp[k] +        test = base - minus