#include using namespace std; using lint = long long; struct fast_ios { fast_ios(){ cin.tie(nullptr), ios::sync_with_stdio(false), cout << fixed << setprecision(20); }; } fast_ios_; #define FOR(i, begin, end) for(int i=(begin),i##_end_=(end);i> N >> K; vector A(N); for (auto &a : A) cin >> a; vector cum(N + 1); REP(i, N) cum[i + 1] = cum[i] + A[i]; lint ret = 1LL << 60; int l = 0, r = K; REP(m, N) { while (r < N and abs(A[m] - A[l]) > abs(A[m] - A[r])) l++, r++; lint tmp = A[m] * (m - l) - (cum[m] - cum[l]) + (cum[r] - cum[m]) - A[m] * (r - m); tmp -= (cum[N] - cum[r]) - A[m] * (N - r) + A[m] * l - cum[l]; ret = min(ret, tmp); } cout << ret << '\n'; }