program main
  implicit none
  integer :: n, k, m, res, l
  integer, allocatable :: v(:), x(:)
  read (*, *) n, k
  allocate (v(n))
  read (*, *) v(:)
  res = 0
  do m = 0, min(n, k)  ! 取り出す数
    do l = 0, m  ! 左端から取り出す数
      x = [ v(1:l), v(n - m + l + 1:n) ]
      call quicksort(x)
      res = max(res, point(x, k - m))
    end do
  end do
  print "(i0)", res

  contains

  function point(x, r)  ! Can return up to r
    integer, intent(in) :: x(:)
    integer :: point, i, d, r
    point = 0
    d = 0
    do i = 1, size(x)
      if (x(i) < 0 .and. d < r) then
        d = d + 1
      else
        point = point + x(i)
      end if
    end do
  end function point

  recursive subroutine quicksort(a)
    integer, intent(inout) :: a(:)
    integer :: pivot, i, j, k, tmp, n
    real(4) :: r
    n = size(a)
    if (n <= 1) return
    i = 1
    j = n
    call random_number(r)
    k = 1 + floor(r * n)
    k = max(1, min(k, n))
    pivot = a(k)
    do
      do while (.not. pivot <= a(i))
        i = i + 1
      end do
      do while (.not. a(j) <= pivot)
        j = j - 1
      end do
      if (i >= j) exit
      tmp = a(i)
      a(i) = a(j)
      a(j) = tmp
      i = i + 1
      j = j - 1
    end do
    call quicksort(a(1:i - 1))
    call quicksort(a(j + 1:n))
  end subroutine quicksort
end program main
