n, k = gets.split.map(&:to_i)
hs = n.times.map { gets.to_i }.sort!

diffs = Array.new(n - 1, 0)
(n - 1).times do |i|
  diffs[i] = hs[i + 1] - hs[i]
end

ans = 10 ** 9 - 1

(n - 2).times do |i|
  a = diffs[i..k - 1].inject(:+)
  ans = a if a <= a
end

puts ans
