using System; using static System.Console; using System.Linq; using System.Collections.Generic; class Program { static int NN => int.Parse(ReadLine()); static int[] NList => ReadLine().Split().Select(int.Parse).ToArray(); static int[][] NArr(long n) => Enumerable.Repeat(0, (int)n).Select(_ => NList).ToArray(); static int[] LList(long n) => Enumerable.Repeat(0, (int)n).Select(_ => int.Parse(ReadLine())).ToArray(); public static void Main() { Solve(); } static void Solve() { var c = NList; var (n, k) = (c[0], c[1]); var a = NList; var cum = new long[n + 1]; for (var i = 0; i < n; ++i) cum[i + 1] = cum[i] + a[i]; var ans = long.MaxValue; if (k == 0) { ans = Math.Min(a[0] * (long)n - cum[n], cum[n] - a[^1] * (long)n); } else if (k == n) { ans = a[n / 2] * (long)n / 2 - cum[n / 2] + cum[n] - cum[n / 2] - a[n / 2] * (long)(n - n / 2); } else { var pos = (2 * k - n - 1) / 2; for (var i = 0L; i <= n - k; ++i) { var x = Math.Max(0L, Math.Min(n - 1, pos)); ans = Math.Min(ans, cum[i] - a[x] * i + a[x] * (x - i) - (cum[x] - cum[i]) + cum[k + i] - cum[x] - a[x] * (k + i - x) + a[x] * (n - k - i) - cum[n] + cum[k + i]); pos += 2; } } WriteLine(ans); } }