use std::io::*; fn main() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.trim().split_whitespace(); let n: usize = itr.next().unwrap().parse().unwrap(); let k: usize = itr.next().unwrap().parse().unwrap(); let a: Vec = (0..n) .map(|_| itr.next().unwrap().parse().unwrap()) .collect(); let mut b = Vec::new(); let mut c = Vec::new(); for i in 0..n { b.push((a[0] - a[i]).abs()); c.push((a[n - 1] - a[i]).abs()); } b.sort(); c.sort(); let mut ans1 = 0; let mut ans2 = 0; for i in 0..k { ans1 += b[i]; ans2 += c[i]; } for i in k..n { ans1 -= b[i]; ans2 -= c[i]; } println!("{}", std::cmp::min(ans1, ans2)); }