# coding: utf-8

import copy

import numpy as np



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

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

rev = copy.copy(v)

rev.reverse()



lcs = [0]+np.cumsum(v)

rcs = [0]+np.cumsum(rev)

# print(lcs,rcs)



ans = -10**20

for l in range(n+1):

  for r in range(n+1-l):

    able = True

    d = k - ( l + r )

    if d < 0:

      able = False

      break

    # m = lcs[l] + rcs[r]

    t = v[0:l] + rev[0:r]

    m = sum(t)

    # print(t,l,r,m)

    t.sort()

    # print(t)

    d = min(d,l+r)

    for i in range(d):

      if 0 > t[i]:

        m -= t[i]

      else:

        break

    # print(m)

    ans = max(ans,m)



print(ans)

    
# coding: utf-8

import copy



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

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

rev = copy.copy(v)

rev.reverse()



ans = -10**20

for l in range(n+1):

  for r in range(n+1-l):

    able = True

    d = k - ( l + r )

    if d < 0:

      able = False

      break

    t = v[0:l] + rev[0:r]

    m = sum(t)

    t.sort()

    d = min(d,l+r)

    for i in range(d):

      if 0 > t[i]:

        m -= t[i]

      else:

        break

    ans = max(ans,m)



print(ans)
# coding: utf-8
import copy
import numpy as np

n, k = list(map(int, input().split()))
v = list(map(int, input().split()))
rev = copy.copy(v)
rev.reverse()
lcs = [0] + np.cumsum(v)
rcs = [0] + np.cumsum(rev)
# print(lcs,rcs)
ans = -(10**20)
for l in range(n + 1):
    for r in range(n + 1 - l):
        able = True
        d = k - (l + r)
        if d < 0:
            able = False
            break
        # m = lcs[l] + rcs[r]
        t = v[0:l] + rev[0:r]
        m = sum(t)
        # print(t,l,r,m)
        t.sort()
        # print(t)
        d = min(d, l + r)
        for i in range(d):
            if 0 > t[i]:
                m -= t[i]
            else:
                break
        # print(m)
        ans = max(ans, m)
print(ans)

# coding: utf-8
import copy

n, k = list(map(int, input().split()))
v = list(map(int, input().split()))
rev = copy.copy(v)
rev.reverse()
ans = -(10**20)
for l in range(n + 1):
    for r in range(n + 1 - l):
        able = True
        d = k - (l + r)
        if d < 0:
            able = False
            break
        t = v[0:l] + rev[0:r]
        m = sum(t)
        t.sort()
        d = min(d, l + r)
        for i in range(d):
            if 0 > t[i]:
                m -= t[i]
            else:
                break
        ans = max(ans, m)
print(ans)

-import numpy as np -lcs = [0] + np.cumsum(v) -rcs = [0] + np.cumsum(rev) -# print(lcs,rcs) -        # m = lcs[l] + rcs[r] -        # print(t,l,r,m) -        # print(t) -        # print(m)