from collections import deque

from copy import deepcopy

import heapq



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

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

v_sorted = sorted(v)



ans = 0

for i in range(k+1):

    for nl in range(i+1):

        nr = min(k - nl, n-nl)

        ansl, ansr = 0, 0

        for n_trash in range(nl):

            ansll, ansrr = 0, 0

            hands = sorted(v[:nl-n_trash])

            for vv in hands:

                if vv < 0 and n_trash > 0:

                    n_trash -= 1

                else:

                    ansll += vv

            ansl = max(ansl, ansll)



        for n_trash in range(nr):

            ansrr = 0

            hands = sorted(v[-(nr-n_trash):])

            # print(hands, nr, n_trash)

            for vv in hands:

                if vv < 0 and n_trash > 0:

                    n_trash -= 1

                else:

                    ansrr += vv

            ansr = max(ansr, ansrr)

        # print(ansl, ansr)

        ans = max(ans, ansl + ansr)



print(ans)

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

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



ans = 0

for lr in range(min(n, k)+1):

    for l in range(lr+1):

        r = lr - l

        jewels = v[:l]+v[-r:] if r > 0 else v[:l]

        n_release = k - lr

        ans_cur = 0

        for j in sorted(jewels):

            if j < 0 and n_release > 0:

                n_release -= 1

            else:

                ans_cur += j

        ans = max(ans, ans_cur)

        

print(ans)

from collections import deque
from copy import deepcopy
import heapq

n, k = list(map(int, input().split()))
v = list(map(int, input().split()))
v_sorted = sorted(v)
ans = 0
for i in range(k + 1):
    for nl in range(i + 1):
        nr = min(k - nl, n - nl)
        ansl, ansr = 0, 0
        for n_trash in range(nl):
            ansll, ansrr = 0, 0
            hands = sorted(v[: nl - n_trash])
            for vv in hands:
                if vv < 0 and n_trash > 0:
                    n_trash -= 1
                else:
                    ansll += vv
            ansl = max(ansl, ansll)
        for n_trash in range(nr):
            ansrr = 0
            hands = sorted(v[-(nr - n_trash) :])
            # print(hands, nr, n_trash)
            for vv in hands:
                if vv < 0 and n_trash > 0:
                    n_trash -= 1
                else:
                    ansrr += vv
            ansr = max(ansr, ansrr)
        # print(ansl, ansr)
        ans = max(ans, ansl + ansr)
print(ans)

n, k = list(map(int, input().split()))
v = list(map(int, input().split()))
ans = 0
for lr in range(min(n, k) + 1):
    for l in range(lr + 1):
        r = lr - l
        jewels = v[:l] + v[-r:] if r > 0 else v[:l]
        n_release = k - lr
        ans_cur = 0
        for j in sorted(jewels):
            if j < 0 and n_release > 0:
                n_release -= 1
            else:
                ans_cur += j
        ans = max(ans, ans_cur)
print(ans)

-from collections import deque -from copy import deepcopy -import heapq - -v_sorted = sorted(v) -for i in range(k + 1): -    for nl in range(i + 1): -        nr = min(k - nl, n - nl) -        ansl, ansr = 0, 0 -        for n_trash in range(nl): -            ansll, ansrr = 0, 0 -            hands = sorted(v[: nl - n_trash]) -            for vv in hands: -                if vv < 0 and n_trash > 0: -                    n_trash -= 1 -                else: -                    ansll += vv -            ansl = max(ansl, ansll) -        for n_trash in range(nr): -            ansrr = 0 -            hands = sorted(v[-(nr - n_trash) :]) -            # print(hands, nr, n_trash) -            for vv in hands: -                if vv < 0 and n_trash > 0: -                    n_trash -= 1 -                else: -                    ansrr += vv -            ansr = max(ansr, ansrr) -        # print(ansl, ansr) -        ans = max(ans, ansl + ansr) +for lr in range(min(n, k) + 1): +    for l in range(lr + 1): +        r = lr - l +        jewels = v[:l] + v[-r:] if r > 0 else v[:l] +        n_release = k - lr +        ans_cur = 0 +        for j in sorted(jewels): +            if j < 0 and n_release > 0: +                n_release -= 1 +            else: +                ans_cur += j +        ans = max(ans, ans_cur)