import java.util.Scanner

object Main {
  def main(args: Array[String]): Unit = {
    val sc = new Scanner(System.in)
    val Array(n, k) = sc.nextLine.split(" ").map(_.toInt)
    val trees = List.fill(n)(sc.nextLine.toInt).sorted.reverse
    println(trees.sliding(2).map(item => item.head - item.last).sliding(k-1).map(_.sum).min)
  }
}
