#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Created: Jul, 12, 2020 17:27:07 by Nobody
# $Author$
# $Date$
# $URL$
__giturl__ = "$URL$"


from sys import stdin
input = stdin.readline


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

  max_ = 0
  for i in range(N+1):
    for j in range(N+1):
      res = K-(i+j)
      if i+j == 0 or i+j > N or K-(i+j) < 0:
        continue
      temp = sorted(V[:i]+V[-j if j else len(V):])
      start = 0
      for k in range(min(res, len(temp))):
        if temp[k] < 0:
          start += 1
      max_ = max(max_, sum(temp[start:]))
      # print('--', i, j, res, start, '--')
      # print(temp)
      # print(max_)
      # print()

  print(max_)


if(__name__ == '__main__'):
  main() __giturl__ = "$URL$" __giturl__ "$URL$" from sys import stdin input = stdin.readline input stdin.readline stdin readline def main():
  N, K = list(map(int, input().split()))
  V = list(map(int, input().split()))

  max_ = 0
  for i in range(N+1):
    for j in range(N+1):
      res = K-(i+j)
      if i+j == 0 or i+j > N or K-(i+j) < 0:
        continue
      temp = sorted(V[:i]+V[-j if j else len(V):])
      start = 0
      for k in range(min(res, len(temp))):
        if temp[k] < 0:
          start += 1
      max_ = max(max_, sum(temp[start:]))
      # print('--', i, j, res, start, '--')
      # print(temp)
      # print(max_)
      # print()

  print(max_) main 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 max_ = 0 max_ 0 for i in range(N+1):
    for j in range(N+1):
      res = K-(i+j)
      if i+j == 0 or i+j > N or K-(i+j) < 0:
        continue
      temp = sorted(V[:i]+V[-j if j else len(V):])
      start = 0
      for k in range(min(res, len(temp))):
        if temp[k] < 0:
          start += 1
      max_ = max(max_, sum(temp[start:]))
      # print('--', i, j, res, start, '--')
      # print(temp)
      # print(max_)
      # print()

   i range(N+1) range N+1 N 1 for j in range(N+1):
      res = K-(i+j)
      if i+j == 0 or i+j > N or K-(i+j) < 0:
        continue
      temp = sorted(V[:i]+V[-j if j else len(V):])
      start = 0
      for k in range(min(res, len(temp))):
        if temp[k] < 0:
          start += 1
      max_ = max(max_, sum(temp[start:]))
      # print('--', i, j, res, start, '--')
      # print(temp)
      # print(max_)
      # print()

   j range(N+1) range N+1 N 1 res = K-(i+j) res K-(i+j) K i+j i j if i+j == 0 or i+j > N or K-(i+j) < 0:
        continue
       i+j == 0 or i+j > N or K-(i+j) < 0 i+j == 0 or i+j > N i+j == 0 i+j i j 0 i+j > N i+j i j N K-(i+j) < 0 K-(i+j) K i+j i j 0 continue temp = sorted(V[:i]+V[-j if j else len(V):]) temp sorted(V[:i]+V[-j if j else len(V):]) sorted V[:i]+V[-j if j else len(V):] V[:i] V :i i V[-j if j else len(V):] V -j if j else len(V): -j if j else len(V) j -j j len(V) len V start = 0 start 0 for k in range(min(res, len(temp))):
        if temp[k] < 0:
          start += 1
       k range(min(res, len(temp))) range min(res, len(temp)) min res len(temp) len temp if temp[k] < 0:
          start += 1
       temp[k] < 0 temp[k] temp k 0 start += 1 start 1 max_ = max(max_, sum(temp[start:])) max_ max(max_, sum(temp[start:])) max max_ sum(temp[start:]) sum temp[start:] temp start: start print(max_) print max_ if(__name__ == '__main__'):
  main() __name__ == '__main__' __name__ '__main__' main() main def main():
  N, K = list(map(int, input().split()))
  V = list(map(int, input().split()))

  max_ = 0
  for i in range(N+1):
    for j in range(N+1):
      res = K-(i+j)
      if i+j == 0 or i+j > N or K-(i+j) < 0:
        continue
      temp = sorted(V[:i]+V[-j if j else len(V):])
      start = 0
      for k in range(min(res, len(temp))):
        if temp[k] < 0:
          start += 1
      max_ = max(max_, sum(temp[start:]))
      # print('--', i, j, res, start, '--')
      # print(temp)
      # print(max_)
      # print()

  print(max_) def main():
  N, K = list(map(int, input().split()))
  V = list(map(int, input().split()))

  max_ = 0
  for i in range(N+1):
    for j in range(N+1):
      res = K-(i+j)
      if i+j == 0 or i+j > N or K-(i+j) < 0:
        continue
      temp = sorted(V[:i]+V[-j if j else len(V):])
      start = 0
      for k in range(min(res, len(temp))):
        if temp[k] < 0:
          start += 1
      max_ = max(max_, sum(temp[start:]))
      # print('--', i, j, res, start, '--')
      # print(temp)
      # print(max_)
      # print()

  print(max_) main input = stdin.readline stdin.readline input
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Created: Jul, 12, 2020 17:27:07 by Nobody
# $Author$
# $Date$
# $URL$
__giturl__ = "$URL$"


from sys import stdin
input = stdin.readline


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

  max_ = 0
  for i in range(N+1):
    for j in range(N+1):
      res = K-(i+j)
      if i+j == 0 or i+j > N or K-(i+j) < 0:
        continue
      temp = sorted(V[:i]+V[-j if j else len(V):])
      start = 0
      for k in range(min(res, len(temp))):
        if temp[k] < 0:
          start += 1
      max_ = max(max_, sum(temp[start:]))
      # print('--', i, j, res, start, '--')
      # print(temp)
      # print(max_)
      # print()

  print(max_)


if(__name__ == '__main__'):
  main()
