# -*- coding:utf-8 -*-

import copy



def solve():

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

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

    

    ans = 0

    M = min(N,K)

    for l in range(M+1):

        having = []

        for r in range(M+1):

            if l+r > M: break

            if l+r == 0: continue

            # 左からl, 右からr個の宝石を取り出す

            having = V[:l] + V[::-1][:r]

            having.sort(reverse=True)



            restK = K - (l+r)



            for _ in range(restK):

                if len(having)!=0 and having[-1] < 0:

                    having.pop()

                    continue

                break



            ans = max(ans, sum(having))

    

    print(ans)



if __name__ == "__main__":

    solve()

# -*- coding:utf-8 -*-



def solve():

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

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



    """方針

    Dequeueから宝石をとる。最後に好きなだけ宝石を戻す。

    dp[i][j] := 左からi個、右からj個取ったときの最大値

    """

    dp = [[0 for _ in range(K+1)] for __ in range(K+1)]

    dp[0][0] = 0  # dpを使ってなくて草



    ans = -float("inf")

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

        for j in range(0, K+1):

            if i+j > K: break

            if i+j > N: break

            if i==0 and j==0:

                ans = 0

                continue



            hasVs = Vs[0:i] + Vs[N-j:N]

            hasVs.sort(reverse=True)



            restK = K - (i+j)

            while restK > 0:

                restK -= 1

                if hasVs == []:

                    break

                if hasVs[-1] < 0:

                    hasVs.pop(-1)

                    continue

                break



            ans = max(ans, sum(hasVs))



    print(ans)







if __name__ == "__main__":

    solve()

# -*- coding:utf-8 -*-
import copy


def solve():
    N, K = list(map(int, input().split()))
    V = list(map(int, input().split()))
    ans = 0
    M = min(N, K)
    for l in range(M + 1):
        having = []
        for r in range(M + 1):
            if l + r > M:
                break
            if l + r == 0:
                continue
            # 左からl, 右からr個の宝石を取り出す
            having = V[:l] + V[::-1][:r]
            having.sort(reverse=True)
            restK = K - (l + r)
            for _ in range(restK):
                if len(having) != 0 and having[-1] < 0:
                    having.pop()
                    continue
                break
            ans = max(ans, sum(having))
    print(ans)


if __name__ == "__main__":
    solve()

# -*- coding:utf-8 -*-
def solve():
    N, K = list(map(int, input().split()))
    Vs = list(map(int, input().split()))
    """方針
    Dequeueから宝石をとる。最後に好きなだけ宝石を戻す。
    dp[i][j] := 左からi個、右からj個取ったときの最大値
    """
    dp = [[0 for _ in range(K + 1)] for __ in range(K + 1)]
    dp[0][0] = 0  # dpを使ってなくて草
    ans = -float("inf")
    for i in range(0, K + 1):
        for j in range(0, K + 1):
            if i + j > K:
                break
            if i + j > N:
                break
            if i == 0 and j == 0:
                ans = 0
                continue
            hasVs = Vs[0:i] + Vs[N - j : N]
            hasVs.sort(reverse=True)
            restK = K - (i + j)
            while restK > 0:
                restK -= 1
                if hasVs == []:
                    break
                if hasVs[-1] < 0:
                    hasVs.pop(-1)
                    continue
                break
            ans = max(ans, sum(hasVs))
    print(ans)


if __name__ == "__main__":
    solve()

-import copy - - -    V = list(map(int, input().split())) -    ans = 0 -    M = min(N, K) -    for l in range(M + 1): -        having = [] -        for r in range(M + 1): -            if l + r > M: +    Vs = list(map(int, input().split())) +    """方針 +    Dequeueから宝石をとる。最後に好きなだけ宝石を戻す。 +    dp[i][j] := 左からi個、右からj個取ったときの最大値 +    """ +    dp = [[0 for _ in range(K + 1)] for __ in range(K + 1)] +    dp[0][0] = 0  # dpを使ってなくて草 +    ans = -float("inf") +    for i in range(0, K + 1): +        for j in range(0, K + 1): +            if i + j > K: -            if l + r == 0: +            if i + j > N: +                break +            if i == 0 and j == 0: +                ans = 0 -            # 左からl, 右からr個の宝石を取り出す -            having = V[:l] + V[::-1][:r] -            having.sort(reverse=True) -            restK = K - (l + r) -            for _ in range(restK): -                if len(having) != 0 and having[-1] < 0: -                    having.pop() +            hasVs = Vs[0:i] + Vs[N - j : N] +            hasVs.sort(reverse=True) +            restK = K - (i + j) +            while restK > 0: +                restK -= 1 +                if hasVs == []: +                    break +                if hasVs[-1] < 0: +                    hasVs.pop(-1) -            ans = max(ans, sum(having)) +            ans = max(ans, sum(hasVs))