fun main(args: Array<String>) {
    val sc = java.util.Scanner(System.`in`)
    val n = sc.nextInt()
    val k = sc.nextInt()
    val d: List<Int> = sc.asSequence().take(n).map(String::toInt).toList()


    val ret = (0..k).map { left ->
        val la = d.take(left)
        (0..Math.min(k - left, n - left)).map { right ->
            val a: List<Int> = d.takeLast(right).plus(la).sorted().toList()
            val minus: Int = a.take(k - left - right).takeWhile { it < 0 }.sum()
            a.sum() - minus
        }.max() ?: 0
    }.max() ?: 0

    println(ret)
}
