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

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

ans = 0

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

  for b in range(min(n, k)-a+1):

    bag = []

    bag += v[:a]

    bag += v[len(v)-b:]

    bag.sort()

    if len(bag) > 0:

      for c in range(k-a-b):

        if bag[0] < 0:

          bag.pop(0)

        if len(bag) == 0:

          break

    ans = max(ans, sum(bag))

      

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

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

ans = 0

for i in range(K+1):

  for j in range(N, max(N-1-(K-i), i-1), -1):

    bag = []

    bag += V[:i] + V[j:]

    bag.sort()

    idx = 0

    if len(bag) == 0:

      continue

    if len(bag) == 1:

      ans = max(ans, sum(bag))

      continue

    if len(bag) == K:

      ans = max(ans, sum(bag))

      continue

    tmp = 0

    for k in range(min(K-len(bag), len(bag))):

      if bag[k] < 0:

        tmp -= bag[k]

    ans = max(ans, sum(bag)+tmp)

print(ans)

n, k = list(map(int, input().split()))
v = list(map(int, input().split()))
ans = 0
for a in range(min(n, k) + 1):
    for b in range(min(n, k) - a + 1):
        bag = []
        bag += v[:a]
        bag += v[len(v) - b :]
        bag.sort()
        if len(bag) > 0:
            for c in range(k - a - b):
                if bag[0] < 0:
                    bag.pop(0)
                if len(bag) == 0:
                    break
        ans = max(ans, sum(bag))
print(ans)

N, K = list(map(int, input().split()))
V = list(map(int, input().split()))
ans = 0
for i in range(K + 1):
    for j in range(N, max(N - 1 - (K - i), i - 1), -1):
        bag = []
        bag += V[:i] + V[j:]
        bag.sort()
        idx = 0
        if len(bag) == 0:
            continue
        if len(bag) == 1:
            ans = max(ans, sum(bag))
            continue
        if len(bag) == K:
            ans = max(ans, sum(bag))
            continue
        tmp = 0
        for k in range(min(K - len(bag), len(bag))):
            if bag[k] < 0:
                tmp -= bag[k]
        ans = max(ans, sum(bag) + tmp)
print(ans)

-n, k = list(map(int, input().split())) -v = list(map(int, input().split())) +N, K = list(map(int, input().split())) +V = list(map(int, input().split())) -for a in range(min(n, k) + 1): -    for b in range(min(n, k) - a + 1): +for i in range(K + 1): +    for j in range(N, max(N - 1 - (K - i), i - 1), -1): -        bag += v[:a] -        bag += v[len(v) - b :] +        bag += V[:i] + V[j:] -        if len(bag) > 0: -            for c in range(k - a - b): -                if bag[0] < 0: -                    bag.pop(0) -                if len(bag) == 0: -                    break -        ans = max(ans, sum(bag)) +        idx = 0 +        if len(bag) == 0: +            continue +        if len(bag) == 1: +            ans = max(ans, sum(bag)) +            continue +        if len(bag) == K: +            ans = max(ans, sum(bag)) +            continue +        tmp = 0 +        for k in range(min(K - len(bag), len(bag))): +            if bag[k] < 0: +                tmp -= bag[k] +        ans = max(ans, sum(bag) + tmp)