import kotlin.collections.* import kotlin.math.* @kotlin.ExperimentalStdlibApi fun main() { val (n, k) = readLine()!!.split(" ").map { it.toInt() } val a = readLine()!!.split(" ").map { it.toLong() }.sorted() val amin = a.min()!! val amax = a.max()!! fun f(ai : Long) : Long { val l = a.map { abs(ai - it) }.toMutableList().sorted() return -l.sum() + l.take(k).sum() * 2 } var l = 0; var r = n - 1 while(l + 10 < r) { val ll = (l+l+r) / 3 val rr = (l+r+r) / 3 if (f(a[ll]) < f(a[rr])) r = rr else l = ll } var ans = minOf(f(a.first()), f(a.last())) for (i in l .. r) { ans = minOf(ans, f(a[i])) } println(ans) }