import heapq
pop = heapq.heappop
push = heapq.heappush

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

ans = 0
for i in range(min(N + 1, K + 1)):
  for j in range(min(N + 1, K + 1) - i):
    q = []
    t = 0
    for k in range(i):
      tt = V[k]
      t += tt
      push(q, tt)
    for k in range(j):
      tt = V[N - 1 - k]
      t += tt
      push(q, tt)

    for k in range(K - i - j):
      if q == []:
        break
      tt = pop(q)
      if tt >= 0:
        break
      t -= tt

    ans = max(ans, t)
print(ans) import heapq heapq pop = heapq.heappop pop heapq.heappop heapq heappop push = heapq.heappush push heapq.heappush heapq heappush N, K = list(map(int, input().split())) N list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split K V = list(map(int, input().split())) V list(map(int, input().split())) list map(int, input().split()) map int input().split() ().split () input split ans = 0 ans 0 for i in range(min(N + 1, K + 1)):
  for j in range(min(N + 1, K + 1) - i):
    q = []
    t = 0
    for k in range(i):
      tt = V[k]
      t += tt
      push(q, tt)
    for k in range(j):
      tt = V[N - 1 - k]
      t += tt
      push(q, tt)

    for k in range(K - i - j):
      if q == []:
        break
      tt = pop(q)
      if tt >= 0:
        break
      t -= tt

    ans = max(ans, t) i range(min(N + 1, K + 1)) range min(N + 1, K + 1) min N + 1 N 1 K + 1 K 1 for j in range(min(N + 1, K + 1) - i):
    q = []
    t = 0
    for k in range(i):
      tt = V[k]
      t += tt
      push(q, tt)
    for k in range(j):
      tt = V[N - 1 - k]
      t += tt
      push(q, tt)

    for k in range(K - i - j):
      if q == []:
        break
      tt = pop(q)
      if tt >= 0:
        break
      t -= tt

    ans = max(ans, t) j range(min(N + 1, K + 1) - i) range min(N + 1, K + 1) - i min(N + 1, K + 1) min N + 1 N 1 K + 1 K 1 i q = [] q [] t = 0 t 0 for k in range(i):
      tt = V[k]
      t += tt
      push(q, tt)
     k range(i) range i tt = V[k] tt V[k] V k t += tt t tt push(q, tt) push q tt for k in range(j):
      tt = V[N - 1 - k]
      t += tt
      push(q, tt)

     k range(j) range j tt = V[N - 1 - k] tt V[N - 1 - k] V N - 1 - k N - 1 N 1 k t += tt t tt push(q, tt) push q tt for k in range(K - i - j):
      if q == []:
        break
      tt = pop(q)
      if tt >= 0:
        break
      t -= tt

     k range(K - i - j) range K - i - j K - i K i j if q == []:
        break
       q == [] q [] break tt = pop(q) tt pop(q) pop q if tt >= 0:
        break
       tt >= 0 tt 0 break t -= tt t tt ans = max(ans, t) ans max(ans, t) max ans t print(ans) print ans t -= tt tt t K = list(map(int, input().split())) list(map(int, input().split())) K N, K = list(map(int, input().split())) list(map(int, input().split())) N t = 0 0 t tt = pop(q) pop(q) tt V = list(map(int, input().split())) list(map(int, input().split())) V ans = 0 0 ans pop = heapq.heappop heapq.heappop pop push = heapq.heappush heapq.heappush push t += tt tt t q = [] [] q tt = V[k] V[k] tt ans = max(ans, t) max(ans, t) ans tt = V[N - 1 - k] V[N - 1 - k] tt t += tt tt t
import heapq
pop = heapq.heappop
push = heapq.heappush

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

ans = 0
for i in range(min(N + 1, K + 1)):
  for j in range(min(N + 1, K + 1) - i):
    q = []
    t = 0
    for k in range(i):
      tt = V[k]
      t += tt
      push(q, tt)
    for k in range(j):
      tt = V[N - 1 - k]
      t += tt
      push(q, tt)

    for k in range(K - i - j):
      if q == []:
        break
      tt = pop(q)
      if tt >= 0:
        break
      t -= tt

    ans = max(ans, t)
print(ans)
