fun main(args: Array<String>) {

    val (n,k) = readLine()!!.split(" ").map { it.toInt() }
    val v = readLine()!!.split(" ").map { it.toInt() }
    val f = listOf(0)+v.summed()
    val b = listOf(0)+v.reversed().summed()
    
    var ans = 0
    for(takeL in 0..Math.min(n,k)){
        for(takeR in 0..Math.min(n-takeL,k-takeL)) {
            var remove = k-takeL-takeR
            if(remove<0) remove = 0
            if(remove>takeL+takeR) remove = takeL+takeR
            val vv = v.take(takeL)+v.takeLast(takeR)
            val vvv = vv.sorted().take(remove).filter { it<0 }
            val tmp0 = f[takeL] + b[takeR]
            val tmp1 =  vvv.sum()
            ans = Math.max(ans, tmp0-tmp1)
        }
    }
    println(ans)
}

inline fun List<Int>.summed(): List<Int> {
    var sum = 0
    return this.map { sum += it; sum }
}
