#include using namespace std; int n, k; int64_t calc(const vector &s, int64_t x, int i, int l) { return s.at(l) - l * x - (s.at(i) - s.at(l) - (i - l) * x) + (s.at(l + k) - s.at(i) - (l + k - i) * x) - (s.at(n) - s.at(l + k) - (n - l - k) * x); } int main() { cin >> n >> k; vector a(n); for (int i = 0; i < n; i++) { cin >> a.at(i); } vector s(n + 1); for (int i = 0; i < n; i++) { s.at(i + 1) = s.at(i) + a.at(i); } int64_t ans = 1e18; int l = 0; for (int i = 0; i < n; i++) { while (l < i and l + k <= i and calc(s, a.at(i), i, l) > calc(s, a.at(i), i, l + 1)) { l++; } ans = min(ans, calc(s, a.at(i), i, l)); } cout << ans << endl; }