fun main(){ val (n, k) = readlnInts() val a = readlnInts().sorted()
val p = IntArray(n + 1) { 0 } for (i in 0 until n) p[i + 1] = p[i] + a[i]
val dp = Array(n + 1) { IntArray(k + 1) { -1 } }
dp[0][0] = 0 for (i in 0 until n) { for (j in 0 until k + 1) { if (dp[i][j] < 0) continue dp[i + 1][j] = Math.max(dp[i + 1][j], dp[i][j]) val ma = Math.min(k - j, (n - i) / 2) for (x in 1..ma) dp[i + 2 * x][j + x] = Math.max(dp[i + 2 * x][j + x], dp[i][j] + p[i + 2 * x] - p[i + x] - p[i + x] + p[i]) } }
println(dp[n][k]) } private fun readln() = readLine()!! private fun readlnByte() = readln().toByte() private fun readlnShort() = readln().toShort() private fun readlnInt() = readln().toInt() private fun readlnLong() = readln().toLong() private fun readlnFloat() = readln().toFloat() private fun readlnDouble() = readln().toDouble() private fun readlnBigInt(radix: Int = 10) = readln().toBigInteger(radix) private fun readlnBigDecimal() = readln().toBigDecimal()
private fun lineSequence(limit: Int = Int.MAX_VALUE) = generateSequence { readLine() }.constrainOnce().take(limit) private fun readlnStrings() = readln().split(' ') private fun readlnBytes() = readlnStrings().map { it.toByte() } private fun readlnShorts() = readlnStrings().map { it.toShort() } private fun readlnInts() = readlnStrings().map { it.toInt() } private fun readlnLongs() = readlnStrings().map { it.toLong() } private fun readlnFloats() = readlnStrings().map { it.toFloat() } private fun readlnDoubles() = readlnStrings().map { it.toDouble() }