#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; using ll = long long; constexpr int INF = 1001001001; constexpr int mod = 1000000007; // constexpr int mod = 998244353; template inline bool chmax(T& x, T y){ if(x < y){ x = y; return true; } return false; } template inline bool chmin(T& x, T y){ if(x > y){ x = y; return true; } return false; } int main(){ ios::sync_with_stdio(false); cin.tie(nullptr); int N, K; cin >> N >> K; vector A(N), S(N + 1); for(int i = 0; i < N; ++i){ cin >> A[i]; S[i + 1] = S[i] + A[i]; } ll ans = 8e+18; int l = 0; for(int i = 0; i < N; ++i){ ll sum = -(A[i] * l - S[l]); sum += A[i] * (i - l) - (S[i] - S[l]); sum += (S[l + K] - S[i]) - A[i] * (l + K - i); sum -= (S[N] - S[l + K]) - A[i] * (N - (l + K)); chmin(ans, sum); while(l + K < N && abs(A[i + 1] - A[l]) > abs(A[i + 1] - A[l + K])){ ++l; } } cout << ans << endl; return 0; }