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

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



ans = 0

for l_pop in range(K + 1):

    for r_pop in range(K + 1):

        X = []

        if (l_pop + r_pop > K) or (l_pop + r_pop > N):

            continue

        else:

            X.extend(V[:l_pop])

            X.extend(V[::-1][:r_pop])



        X.sort()

        tmp = sum(X)

        for k in range(K - l_pop - r_pop):

            if k > len(X) - 1:

                break

            else:

                if X[k] < 0:

                    tmp -= X[k]



        ans = max(ans, tmp)



print(ans)

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

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



# 最終的に「持っているもの」の価値の合計を高めたい

# Dから取り出すことをpop、Dに詰めることをenqueueとする

ans = 0

for pop_cnt in range(K + 1):

    enqueue_cnt = K - pop_cnt



    if pop_cnt > N:

        continue



    for l in range(pop_cnt + 1):

        holding = []



        r = pop_cnt - l

        holding += V[:l]

        holding += V[N - r:]



        holding.sort()

        tmp_ans = sum(holding)



        for e in range(enqueue_cnt):

            if e >= len(holding):

                break



            if holding[e] < 0:

                tmp_ans += -holding[e]



        ans = max(ans, tmp_ans)



print(ans)

N, K = list(map(int, input().split()))
V = list(map(int, input().split()))
ans = 0
for l_pop in range(K + 1):
    for r_pop in range(K + 1):
        X = []
        if (l_pop + r_pop > K) or (l_pop + r_pop > N):
            continue
        else:
            X.extend(V[:l_pop])
            X.extend(V[::-1][:r_pop])
        X.sort()
        tmp = sum(X)
        for k in range(K - l_pop - r_pop):
            if k > len(X) - 1:
                break
            else:
                if X[k] < 0:
                    tmp -= X[k]
        ans = max(ans, tmp)
print(ans)

N, K = list(map(int, input().split()))
V = list(map(int, input().split()))
# 最終的に「持っているもの」の価値の合計を高めたい
# Dから取り出すことをpop、Dに詰めることをenqueueとする
ans = 0
for pop_cnt in range(K + 1):
    enqueue_cnt = K - pop_cnt
    if pop_cnt > N:
        continue
    for l in range(pop_cnt + 1):
        holding = []
        r = pop_cnt - l
        holding += V[:l]
        holding += V[N - r :]
        holding.sort()
        tmp_ans = sum(holding)
        for e in range(enqueue_cnt):
            if e >= len(holding):
                break
            if holding[e] < 0:
                tmp_ans += -holding[e]
        ans = max(ans, tmp_ans)
print(ans)

+# 最終的に「持っているもの」の価値の合計を高めたい +# Dから取り出すことをpop、Dに詰めることをenqueueとする -for l_pop in range(K + 1): -    for r_pop in range(K + 1): -        X = [] -        if (l_pop + r_pop > K) or (l_pop + r_pop > N): -            continue -        else: -            X.extend(V[:l_pop]) -            X.extend(V[::-1][:r_pop]) -        X.sort() -        tmp = sum(X) -        for k in range(K - l_pop - r_pop): -            if k > len(X) - 1: +for pop_cnt in range(K + 1): +    enqueue_cnt = K - pop_cnt +    if pop_cnt > N: +        continue +    for l in range(pop_cnt + 1): +        holding = [] +        r = pop_cnt - l +        holding += V[:l] +        holding += V[N - r :] +        holding.sort() +        tmp_ans = sum(holding) +        for e in range(enqueue_cnt): +            if e >= len(holding): -            else: -                if X[k] < 0: -                    tmp -= X[k] -        ans = max(ans, tmp) +            if holding[e] < 0: +                tmp_ans += -holding[e] +        ans = max(ans, tmp_ans)