def main():

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

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

    M=min(N,K)

    result=0

    for l in range(M+1):

        for r in range(M+1-l):

            L=l

            R=r

            count=0

            minus=[]

            m=l+r

            while L>0:

                a=V[L-1]

                if a>=0:

                    count+=a

                else:

                    count+=a

                    minus.append(a)

                L-=1

            while R>0:

                a=V[-R]

                if a>=0:

                    count+=a

                else:

                    count+=a

                    minus.append(a)

                R-=1



            minus.sort()

            i_minus=0

            while K-m>0 and i_minus<len(minus):

                count-=minus[i_minus]

                m+=1

                i_minus+=1

            result=max(result,count)

    print(result)



if __name__=="__main__":

    main()
from sys import stdin

import heapq

def main():

    #入力

    readline=stdin.readline

    n,k=list(map(int,readline().split()))

    v=list(map(int,readline().split()))

    v_rev=list(reversed(v))



    m=min(n,k)

    ans=0

    for l in range(m+1):

        for r in range(m+1):

            if l+r>m:

                break



            minus=[]

            s=0

            for i in range(l):

                x=v[i]

                if x<0:

                    minus.append(x)

                s+=x



            for i in range(r):

                x=v_rev[i]

                if x<0:

                    minus.append(x)

                s+=x



            if l+r<k:

                minus.sort()

                for i in range(k-l-r):

                    if i>=len(minus):

                        break

                    s-=minus[i]



            ans=max(ans,s)



    print(ans)



if __name__=="__main__":

    main()
def main():
    N, K = list(map(int, input().split()))
    V = list(map(int, input().split()))
    M = min(N, K)
    result = 0
    for l in range(M + 1):
        for r in range(M + 1 - l):
            L = l
            R = r
            count = 0
            minus = []
            m = l + r
            while L > 0:
                a = V[L - 1]
                if a >= 0:
                    count += a
                else:
                    count += a
                    minus.append(a)
                L -= 1
            while R > 0:
                a = V[-R]
                if a >= 0:
                    count += a
                else:
                    count += a
                    minus.append(a)
                R -= 1
            minus.sort()
            i_minus = 0
            while K - m > 0 and i_minus < len(minus):
                count -= minus[i_minus]
                m += 1
                i_minus += 1
            result = max(result, count)
    print(result)


if __name__ == "__main__":
    main()

from sys import stdin
import heapq


def main():
    # 入力
    readline = stdin.readline
    n, k = list(map(int, readline().split()))
    v = list(map(int, readline().split()))
    v_rev = list(reversed(v))
    m = min(n, k)
    ans = 0
    for l in range(m + 1):
        for r in range(m + 1):
            if l + r > m:
                break
            minus = []
            s = 0
            for i in range(l):
                x = v[i]
                if x < 0:
                    minus.append(x)
                s += x
            for i in range(r):
                x = v_rev[i]
                if x < 0:
                    minus.append(x)
                s += x
            if l + r < k:
                minus.sort()
                for i in range(k - l - r):
                    if i >= len(minus):
                        break
                    s -= minus[i]
            ans = max(ans, s)
    print(ans)


if __name__ == "__main__":
    main()

+from sys import stdin +import heapq + + -    N, K = list(map(int, input().split())) -    V = list(map(int, input().split())) -    M = min(N, K) -    result = 0 -    for l in range(M + 1): -        for r in range(M + 1 - l): -            L = l -            R = r -            count = 0 +    # 入力 +    readline = stdin.readline +    n, k = list(map(int, readline().split())) +    v = list(map(int, readline().split())) +    v_rev = list(reversed(v)) +    m = min(n, k) +    ans = 0 +    for l in range(m + 1): +        for r in range(m + 1): +            if l + r > m: +                break -            m = l + r -            while L > 0: -                a = V[L - 1] -                if a >= 0: -                    count += a -                else: -                    count += a -                    minus.append(a) -                L -= 1 -            while R > 0: -                a = V[-R] -                if a >= 0: -                    count += a -                else: -                    count += a -                    minus.append(a) -                R -= 1 -            minus.sort() -            i_minus = 0 -            while K - m > 0 and i_minus < len(minus): -                count -= minus[i_minus] -                m += 1 -                i_minus += 1 -            result = max(result, count) -    print(result) +            s = 0 +            for i in range(l): +                x = v[i] +                if x < 0: +                    minus.append(x) +                s += x +            for i in range(r): +                x = v_rev[i] +                if x < 0: +                    minus.append(x) +                s += x +            if l + r < k: +                minus.sort() +                for i in range(k - l - r): +                    if i >= len(minus): +                        break +                    s -= minus[i] +            ans = max(ans, s) +    print(ans)