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

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



ans = 0

m = min(n, k)

for i in range(m + 1):

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

        t = v[:i] + v[n - j:]

        t.sort()

        d = tuple([x for x in t if x < 0])[:k - i - j]

        ans = max(ans, sum(t) - sum(d))

print(ans)

def main():

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

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



    m = min(N, K)



    ans = 0

    for left in range(m + 1):

        _r = m - left

        for right in range(_r + 1):

            rest = K - left - right

            take = V[:left] + V[N - right:]

            take.sort()

            s = 0

            for x in take:

                if rest and x < 0:

                    rest -= 1

                    continue

                s += x

            ans = max(ans, s)

    print(ans)





if __name__ == '__main__':

    main()



# 操作回数はK回以内

# x=min(K,N)個取り出す

# K-x個戻せる

n, k = list(map(int, input().split()))
v = list(map(int, input().split()))
ans = 0
m = min(n, k)
for i in range(m + 1):
    for j in range(m - i + 1):
        t = v[:i] + v[n - j :]
        t.sort()
        d = tuple([x for x in t if x < 0])[: k - i - j]
        ans = max(ans, sum(t) - sum(d))
print(ans)

def main():
    N, K = list(map(int, input().split()))
    (*V,) = list(map(int, input().split()))
    m = min(N, K)
    ans = 0
    for left in range(m + 1):
        _r = m - left
        for right in range(_r + 1):
            rest = K - left - right
            take = V[:left] + V[N - right :]
            take.sort()
            s = 0
            for x in take:
                if rest and x < 0:
                    rest -= 1
                    continue
                s += x
            ans = max(ans, s)
    print(ans)


if __name__ == "__main__":
    main()
# 操作回数はK回以内
# x=min(K,N)個取り出す
# K-x個戻せる

-n, k = list(map(int, input().split())) -v = list(map(int, input().split())) -ans = 0 -m = min(n, k) -for i in range(m + 1): -    for j in range(m - i + 1): -        t = v[:i] + v[n - j :] -        t.sort() -        d = tuple([x for x in t if x < 0])[: k - i - j] -        ans = max(ans, sum(t) - sum(d)) -print(ans) +def main(): +    N, K = list(map(int, input().split())) +    (*V,) = list(map(int, input().split())) +    m = min(N, K) +    ans = 0 +    for left in range(m + 1): +        _r = m - left +        for right in range(_r + 1): +            rest = K - left - right +            take = V[:left] + V[N - right :] +            take.sort() +            s = 0 +            for x in take: +                if rest and x < 0: +                    rest -= 1 +                    continue +                s += x +            ans = max(ans, s) +    print(ans) + + +if __name__ == "__main__": +    main() +# 操作回数はK回以内 +# x=min(K,N)個取り出す +# K-x個戻せる