#include using namespace std; struct uns_t {} uns; template auto vec(Element init, Head arg, Args ...args) { if constexpr (sizeof...(Args) == 0) return std::vector(arg, init); else return std::vector(arg, vec(init, args...)); } template auto vec(uns_t, Head arg, Args ...args) { return vec(Element(), arg, args...); } int main() { int64_t n, k; cin >> n >> k; auto a = vec(uns, n); for (auto &e : a) cin >> e; if (k == 0) { int64_t now = 0; for (int i = 0; i < n; ++i) { now -= abs(a[0] - a[i]); } int64_t ans = now; for (int i = 1; i < n; ++i) { auto d = a[i] - a[i - 1]; now -= d * i; now += d * (n - i); ans = min(ans, now); } cout << ans << endl; return 0; } int64_t now = 0; for (int i = 0; i < n; ++i) { if (i < k) { now += abs(a[0] - a[i]); } else { now -= abs(a[0] - a[i]); } } int64_t ans = now; int l = 0, r = k; for (int i = 1; i < n; ++i) { auto d = a[i] - a[i - 1]; auto pl = l, pr = r; while (r <= i || (r < n && (abs(a[i] - a[r]) < abs(a[i] - a[l])))) { now -= abs(a[i - 1] - a[l]) * 2; now += abs(a[i - 1] - a[r]) * 2; ++l; ++r; } now -= d * l; now += d * (i - l); now -= d * (r - i); now += d * (n - r); ans = min(ans, now); } cout << ans << endl; }