#include using namespace std; int main() { size_t n, k; cin >> n >> k; vector a(n); for (auto& ai : a) cin >> ai; if (k == 0) { auto calc = [a](long long x) { long long res = 0; for (auto ai : a) res -= abs(x - ai); return res; }; cout << min(calc(a.front()), calc(a.back())) << '\n'; return 0; } vector s(1); partial_sum(a.begin(), a.end(), back_inserter(s)); long long ans = LLONG_MAX; size_t l = 0, r = k - 1; for (size_t i = 0; i < n; ++i) { while (r + 1 < n && a[i] - a[l] > a[r + 1] - a[i]) l++, r++; long long now = (n - 2 * l - 2 * r + 2 * i) * a[i] + 2 * (s[l] + s[r + 1] - s[i + 1]) - s[0] - s[n]; ans = min(ans, now); } cout << ans << '\n'; }