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

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

ans = 0



from heapq import heappush, heappop

for i in range(1, k+1):

  for j in range(1, i+1):

    for k in range(0, j+1):

      minus = []

      l = min(n, k)

      r = min(n-l, j-l)

      cur = 0

      for ll in range(l):

        cur += v[ll]

        if v[ll]<0:

          heappush(minus, v[ll])

      for rr in range(r):

        cur += v[n-rr-1]

        if v[n-rr-1]<0:

          heappush(minus, v[n-rr-1])

      rm = i-l-r

      while rm>0 and len(minus)>=rm:

        rm -= 1

        t = heappop(minus)

        cur -= t

      ans = max(ans, cur)

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

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

ans = 0



from heapq import heappush, heappop

for i in range(1, k+1):

  for j in range(0, i+1):

    minus = []

    l = min(n, j)

    r = min(n-l, i-l)

    cur = 0

    for ll in range(l):

      cur += v[ll]

      if v[ll]<0:

        heappush(minus, v[ll])

    for rr in range(r):

      cur += v[n-rr-1]

      if v[n-rr-1]<0:

        heappush(minus, v[n-rr-1])

    rm = k-l-r

    while rm>0 and len(minus)>0:

      rm -= 1

      cur -= heappop(minus)

    ans = max(ans, cur)

print(ans)
n, k = list(map(int, input().split()))
v = list(map(int, input().split()))
ans = 0
from heapq import heappush, heappop

for i in range(1, k + 1):
    for j in range(1, i + 1):
        for k in range(0, j + 1):
            minus = []
            l = min(n, k)
            r = min(n - l, j - l)
            cur = 0
            for ll in range(l):
                cur += v[ll]
                if v[ll] < 0:
                    heappush(minus, v[ll])
            for rr in range(r):
                cur += v[n - rr - 1]
                if v[n - rr - 1] < 0:
                    heappush(minus, v[n - rr - 1])
            rm = i - l - r
            while rm > 0 and len(minus) >= rm:
                rm -= 1
                t = heappop(minus)
                cur -= t
            ans = max(ans, cur)
print(ans)

n, k = list(map(int, input().split()))
v = list(map(int, input().split()))
ans = 0
from heapq import heappush, heappop

for i in range(1, k + 1):
    for j in range(0, i + 1):
        minus = []
        l = min(n, j)
        r = min(n - l, i - l)
        cur = 0
        for ll in range(l):
            cur += v[ll]
            if v[ll] < 0:
                heappush(minus, v[ll])
        for rr in range(r):
            cur += v[n - rr - 1]
            if v[n - rr - 1] < 0:
                heappush(minus, v[n - rr - 1])
        rm = k - l - r
        while rm > 0 and len(minus) > 0:
            rm -= 1
            cur -= heappop(minus)
        ans = max(ans, cur)
print(ans)

-    for j in range(1, i + 1): -        for k in range(0, j + 1): -            minus = [] -            l = min(n, k) -            r = min(n - l, j - l) -            cur = 0 -            for ll in range(l): -                cur += v[ll] -                if v[ll] < 0: -                    heappush(minus, v[ll]) -            for rr in range(r): -                cur += v[n - rr - 1] -                if v[n - rr - 1] < 0: -                    heappush(minus, v[n - rr - 1]) -            rm = i - l - r -            while rm > 0 and len(minus) >= rm: -                rm -= 1 -                t = heappop(minus) -                cur -= t -            ans = max(ans, cur) +    for j in range(0, i + 1): +        minus = [] +        l = min(n, j) +        r = min(n - l, i - l) +        cur = 0 +        for ll in range(l): +            cur += v[ll] +            if v[ll] < 0: +                heappush(minus, v[ll]) +        for rr in range(r): +            cur += v[n - rr - 1] +            if v[n - rr - 1] < 0: +                heappush(minus, v[n - rr - 1]) +        rm = k - l - r +        while rm > 0 and len(minus) > 0: +            rm -= 1 +            cur -= heappop(minus) +        ans = max(ans, cur)