PROGRAM ATCODER

implicit none
integer(16) :: n,k,a,b,cd,i,j,h,c=1
integer(16),allocatable :: v(:),m(:),ans(:)

read*, n,k
allocate(v(n),m(n),ans(k**2))
read*, v(1:n)

call hs(n,v)

do i = 0, min(n,k), 1
  a = i
  do j = 0, min(n,k), 1
    m = 10**9
    b = j
    if ( a + b >= min(n,k) + 1 ) exit
    cd = k - (a+b)
    if ( a /= 0 ) then
      do h = 1, a, 1
        m(h) = v(h)
      end do
    end if
    if ( b /= 0 ) then
      do h = 1, b, 1
        m(n+1-h) = v(n+1-h)
      end do
    end if
    call hs(n,m)
    do h = 1, cd
      if ( m(h) > 0 ) then
        cd = h - 1
        exit
      end if      
    end do
    m(1:cd) = 0
    do h = 1, n, 1
      if ( m(h) == 10**9 ) then
        m(h) = 0
      end if
    end do
    ans(c) = sum(m)
    c = c + 1
  end do
end do

print'(i0)', maxval(ans)

contains
  subroutine hs(n,array)
  implicit none
  integer(16),intent(in) :: n
  integer(16),intent(inout) :: array(1:n)
  
  integer ::i,k,j,l
  integer :: t
  
  if(n <= 0)then
     write(6,*)"Error, at heapsort"; stop
  endif
  if(n == 1)return
 
  l=n/2+1
  k=n
  do while(k /= 1)
     if(l > 1)then
        l=l-1
        t=array(L)
     else
        t=array(k)
        array(k)=array(1)
        k=k-1
        if(k == 1) then
           array(1)=t
           exit
        endif
     endif
     i=l
     j=l+l
     do while(j <= k)
        if(j < k)then
           if(array(j) < array(j+1))j=j+1
        endif
        if (t < array(j))then
           array(i)=array(j)
           i=j
           j=j+j
        else
           j=k+1
        endif
     enddo
     array(i)=t
  enddo
 
  return
  end subroutine hs

END PROGRAM ATCODER