using System; using System.Collections.Generic; using System.Linq; using System.Diagnostics; using static System.Math; namespace Solution { class AC { const long INF = long.MaxValue / 2; static void Main(string[] args) { var (N, K) = Cin.Input(); var A = Cin.ReadSplitLong(); long ans = INF; var lst = new List(); for (var i = 0; i < Min(N, 70); i++) { long s = 0; lst.Clear(); for (var j = 0; j < N; j++) lst.Add(Abs(A[i] - A[j])); lst.Sort(); for (var j = 0; j < N; j++) s += (j < K ? lst[j] : -lst[j]); ans = Min(ans, s); } for(var i = N - 1; i >= Max(0, N - 1 - 70); i--) { long s = 0; lst.Clear(); for (var j = 0; j < N; j++) lst.Add(Abs(A[i] - A[j])); lst.Sort(); for (var j = 0; j < N; j++) s += (j < K ? lst[j] : -lst[j]); ans = Min(ans, s); } for(var i = Max(0, N / 2 - 35); i < Min(N, N / 2 + 35); i++) { long s = 0; lst.Clear(); for (var j = 0; j < N; j++) lst.Add(Abs(A[i] - A[j])); lst.Sort(); for (var j = 0; j < N; j++) s += (j < K ? lst[j] : -lst[j]); ans = Min(ans, s); } Console.WriteLine(ans); } } } static class Cin { public static string Str() => Console.ReadLine(); public static int Int() => int.Parse(Cin.Str()); public static long Long() => long.Parse(Cin.Str()); public static double Double() => double.Parse(Cin.Str()); private static T Conv(string value) => (T)Convert.ChangeType(value, typeof(T)); public static (T, U) ReadTuple(string[] input) => (Conv(input[0]), Conv(input[1])); public static (T, U, V) ReadTuple(string[] input) => (Conv(input[0]), Conv(input[1]), Conv(input[2])); public static (T, U, V, W) ReadTuple(string[] input) => (Conv(input[0]), Conv(input[1]), Conv(input[2]), Conv(input[3])); public static (T, U, V, W, X) ReadTuple(string[] input) => (Conv(input[0]), Conv(input[1]), Conv(input[2]), Conv(input[3]), Conv(input[4])); public static (T, U) Input() => ReadTuple(ReadSplit()); public static (T, U, V) Input() => ReadTuple(ReadSplit()); public static (T, U, V, W) Input() => ReadTuple(ReadSplit()); public static (T, U, V, W, X) Input() => ReadTuple(ReadSplit()); public static string[] ReadSplit() => Console.ReadLine().Split(); public static string[] ReadSplit(char separator) => Console.ReadLine().Split(separator); public static int[] ReadSplitInt() => Array.ConvertAll(ReadSplit(), int.Parse); public static long[] ReadSplitLong() => Array.ConvertAll(ReadSplit(), long.Parse); public static double[] ReadSplit_Double() => Array.ConvertAll(ReadSplit(), double.Parse); }