import java.util.Scanner

import scala.collection.mutable.ArrayBuffer
import scala.annotation.tailrec

object Main {
  def main(args: Array[String]): Unit = {
    val sc = new Scanner(System.in)
    val writer = new java.io.PrintWriter(System.out)
    writer.println(solve(sc))
    writer.flush()
  }

  def solve(sc: Scanner): String = {
    val (n, k) = (sc.nextInt(), sc.nextInt())
    val values = Vector.fill(n)(sc.nextLong())
    debug(n, k, values)

    getTakePatterns(k).foldLeft(ArrayBuffer.empty[Long]) { case (acc, (l, r, d)) =>
      val fromLeft = if (l > 0) values.slice(0, l) else Vector.empty[Long]
      val fromRight = if (r > 0) values.slice(l, values.size).reverse.slice(0, r) else Vector.empty[Long]
      val takes = fromLeft ++ fromRight
      val value =
        if (d > 0) {
          val dropValue = takes.sorted.take(d).foldLeft(0L) { (acc, v) =>
            if (v < 0) {
              acc + v
            } else acc
          }
          takes.sum + dropValue * -1
        } else {
          takes.sum
        }
      acc += (if (value > 0) value else 0)
    }.max.toString
  }

  def getTakePatterns(k: Int): Vector[(Int, Int, Int)] = {
    @tailrec
    def dfs(buf: ArrayBuffer[(Int, Int, Int)], i: Int): ArrayBuffer[(Int, Int, Int)] = {
      (0 to k - i).foreach { j =>
        val x = if (k - i - j > 0) k - i - j else 0
        if (i + j >= x) {
          buf += ((i, j, x))
        }
      }
      if (i < k) {
        dfs(buf, i + 1)
      } else buf
    }

    val initialBuf = ArrayBuffer.empty[(Int, Int, Int)]
    dfs(initialBuf, 0).toVector
  }

  private def debug(x: Any): Unit = {
    if (System.getenv("LOCAL_DEBUG") != null) println(x)
  }
}
