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 ans = minOf(f(amin), f(amax)) if (n%2 == 1) ans = minOf(ans, f(a[n/2])) else { ans = minOf(ans, f((a[n/2-1] + a[n/2]) / 2 ) ) ans = minOf(ans, f(a[n/2-1])) ans = minOf(ans, f((a[n/2-1] + a[n/2]) / 2 +1) ) ans = minOf(ans, f(a[n/2])) } println(ans) }