from collections import deque 



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

vs_ = deque(list(map(int, input().split())))



max_v = 0



import copy



for a in range(k+1):

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

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

      vs = copy.copy(vs_)

      us = []

      for x in range(a):

        if vs:

          us.append(vs.popleft())

        else:

          break

      for y in range(b):

        if vs:

          us.append(vs.pop())

        else:

          break

      #print(us)

      us.sort(reverse=True)

      for z in range(c):

        if len(us) and us[-1]<0:

          us.pop()

        else:

          break

      if sum(us) >= max_v:

        max_v = sum(us)



print(max_v)



      

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

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



from collections import deque

V = deque(V)



import copy



ans = -10**18

for i in range(k+1):

    for j in range(k+1):

        if i+j > k:

            continue

        if i+j > n:

            continue

        V_ = copy.copy(V)

        temp = []

        for p in range(i):

            temp.append(V_.popleft())

        for q in range(j):

            temp.append(V_.pop())

        temp.sort()

        s = sum(temp)

        l = k-i-j

        for r in range(min(l, len(temp))):

            if temp[r] < 0:

                s -= temp[r]

        ans = max(ans, s)

print(ans)

from collections import deque

n, k = list(map(int, input().split()))
vs_ = deque(list(map(int, input().split())))
max_v = 0
import copy

for a in range(k + 1):
    for b in range(k + 1 - a):
        for c in range(k + 1 - a - b):
            vs = copy.copy(vs_)
            us = []
            for x in range(a):
                if vs:
                    us.append(vs.popleft())
                else:
                    break
            for y in range(b):
                if vs:
                    us.append(vs.pop())
                else:
                    break
            # print(us)
            us.sort(reverse=True)
            for z in range(c):
                if len(us) and us[-1] < 0:
                    us.pop()
                else:
                    break
            if sum(us) >= max_v:
                max_v = sum(us)
print(max_v)

n, k = list(map(int, input().split()))
V = list(map(int, input().split()))
from collections import deque

V = deque(V)
import copy

ans = -(10**18)
for i in range(k + 1):
    for j in range(k + 1):
        if i + j > k:
            continue
        if i + j > n:
            continue
        V_ = copy.copy(V)
        temp = []
        for p in range(i):
            temp.append(V_.popleft())
        for q in range(j):
            temp.append(V_.pop())
        temp.sort()
        s = sum(temp)
        l = k - i - j
        for r in range(min(l, len(temp))):
            if temp[r] < 0:
                s -= temp[r]
        ans = max(ans, s)
print(ans)

+n, k = list(map(int, input().split())) +V = list(map(int, input().split())) -n, k = list(map(int, input().split())) -vs_ = deque(list(map(int, input().split()))) -max_v = 0 +V = deque(V) -for a in range(k + 1): -    for b in range(k + 1 - a): -        for c in range(k + 1 - a - b): -            vs = copy.copy(vs_) -            us = [] -            for x in range(a): -                if vs: -                    us.append(vs.popleft()) -                else: -                    break -            for y in range(b): -                if vs: -                    us.append(vs.pop()) -                else: -                    break -            # print(us) -            us.sort(reverse=True) -            for z in range(c): -                if len(us) and us[-1] < 0: -                    us.pop() -                else: -                    break -            if sum(us) >= max_v: -                max_v = sum(us) -print(max_v) +ans = -(10**18) +for i in range(k + 1): +    for j in range(k + 1): +        if i + j > k: +            continue +        if i + j > n: +            continue +        V_ = copy.copy(V) +        temp = [] +        for p in range(i): +            temp.append(V_.popleft()) +        for q in range(j): +            temp.append(V_.pop()) +        temp.sort() +        s = sum(temp) +        l = k - i - j +        for r in range(min(l, len(temp))): +            if temp[r] < 0: +                s -= temp[r] +        ans = max(ans, s) +print(ans)