

fun main(arr:Array<String>) {
    val (itemCount, maxCount) = readLine()!!.split(" ").map { it.toInt() }
    items.addAll(readLine()!!.split(" ").map { it.toInt() })
    val ans = getAns(0, maxCount, listOf(), 0)
    println(ans)
}

val items = mutableListOf<Int>()
val dic = mutableMapOf<Long, Long>()

fun getAns(key:Long, remain:Int,  minusList:List<Int>, subtotal:Long):Long {
    if(remain <= 0) {
        return subtotal
    }
    if(dic.containsKey(key)) {
        return dic[key]!!
    }
    var ans = subtotal
    for(i in (0 until items.size)) {
        val newKey = 1L shl i;
        if(key.and(newKey) == 0L) {
            val newMinus = mutableListOf<Int>()
            newMinus.addAll(minusList)
            if(items[i] < 0) {
                newMinus.add(items[i])
            }
            ans = Math.max(getAns(key + newKey, remain - 1, newMinus.sorted(), subtotal + items[i]), ans)
            break
        }
    }

    for(i in (0 until  items.size).reversed()) {
        val newKey = 1L shl i;
        if(key.and(newKey) == 0L) {
            val newMinus = mutableListOf<Int>()
            newMinus.addAll(minusList)
            if(items[i] < 0) {
                newMinus.add(items[i])
            }
            ans = Math.max(getAns(key + newKey, remain - 1, newMinus.sorted(), subtotal + items[i]),ans)
            break
        }
    }

    if(remain <= minusList.size) {
        ans = Math.max(ans, subtotal - minusList.take(remain).sum())
    } else if(key == (1L shl items.size) - 1) {
        ans = Math.max(ans, subtotal - minusList.sum())
    }
    dic[key] = ans
    return ans
}
