#include #define rep(i,a,b) for(int i=a;i=b;i--) #define fore(i,a) for(auto &i:a) #define all(x) (x).begin(),(x).end() #pragma GCC optimize ("-O3") using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); } typedef long long ll; const int inf = INT_MAX / 2; const ll infl = 1LL << 60; templatebool chmax(T &a, const T &b) { if (abool chmin(T &a, const T &b) { if (b struct BIT { BIT() {} // [L, R) int NV; vector bit; BIT(int n) { init(n); } void init(int n) { NV = 1; while (NV= 0; i--) if (tv + bit[ent + (1 << i) - 1] <= val)tv += bit[ent + (1 << i) - 1], ent += (1 << i); return ent; } V get(int L, int R) { assert(0 <= L); assert(R <= NV); assert(L <= R); V res = 0; if (R) res += operator[](R - 1); if (L) res -= operator[](L - 1); return res; } void clear() { bit.clear(); } }; // StaticHealthy2DSegtree #define def 0 using V = ll; V comp(V& l, V& r) { return l + r; }; struct SegTree { //[l,r) int NV; vector val; void init(int n) { NV = 1; while (NV < n) NV *= 2; val = vector(NV * 2, def); } V get(int x, int y, int l, int r, int k) { if (r <= x || y <= l) return def; if (x <= l && r <= y)return val[k]; auto a = get(x, y, l, (l + r) / 2, k * 2); auto b = get(x, y, (l + r) / 2, r, k * 2 + 1); return comp(a, b); } V get(int x, int y) { return get(x, y, 0, NV, 1); } void update(int i, V v) { i += NV; val[i] = v; while (i>1)i >>= 1, val[i] = comp(val[i * 2], val[i * 2 + 1]); } void compupdate(int i, V v) { update(i, comp(v, val[i + NV])); } void add(int i, V v) { update(i, val[i + NV] + v); } V operator[](int x) { return get(x, x + 1); } }; struct StaticHealthy2DSegTree { int NV; vector st; vector> index; void init(vector>> &v) { int n = v.size(); NV = 1; while (NV < n) NV *= 2; index.resize(2 * NV); rep(i, 0, n) fore(p, v[i]) index[i + NV].push_back(p.first); rrep(i, NV * 2 - 1, 1) { sort(index[i].begin(), index[i].end()); index[i].erase(unique(index[i].begin(), index[i].end()), index[i].end()); fore(j, index[i]) index[i / 2].push_back(j); } st.resize(2 * NV); rep(i, 1, NV * 2) st[i].init(index[i].size()); rep(i, 0, n) fore(p, v[i]) { int j = lower_bound(index[i + NV].begin(), index[i + NV].end(), p.first) - index[i + NV].begin(); st[i + NV].update(j, p.second); } rrep(i, NV * 2 - 1, 2) { rep(j, 0, index[i].size()) { V v = st[i][j]; int k = lower_bound(index[i / 2].begin(), index[i / 2].end(), index[i][j]) - index[i / 2].begin(); st[i / 2].compupdate(k, v); } } } V get(int sx, int tx, int sy, int ty, int k, int l, int r) { assert(k < NV * 2); assert(l < r); if (r <= sx or tx <= l) return def; if (sx <= l and r <= tx) { int syy = lower_bound(index[k].begin(), index[k].end(), sy) - index[k].begin(); int tyy = lower_bound(index[k].begin(), index[k].end(), ty) - index[k].begin(); return st[k].get(syy, tyy); } int md = (l + r) / 2; V le = get(sx, tx, sy, ty, k * 2, l, md); V ri = get(sx, tx, sy, ty, k * 2 + 1, md, r); return comp(le, ri); } V get(int sx, int tx, int sy, int ty) { return get(sx, tx, sy, ty, 1, 0, NV); } }; /*---------------------------------------------------------------------------------------------------             ∧_∧       ∧_∧  (´<_` )  Welcome to My Coding Space!      ( ´_ゝ`) /  ⌒i     /   \    | |     /   / ̄ ̄ ̄ ̄/  |   __(__ニつ/  _/ .| .|____      \/____/ (u ⊃ ---------------------------------------------------------------------------------------------------*/ int N, K, A[101010], M; BIT bit(101010); StaticHealthy2DSegTree st; //--------------------------------------------------------------------------------------------------- void _main() { cin >> N >> K; rep(i, 0, N) cin >> A[i]; vector dic; rep(i, 0, N) dic.push_back(A[i]); sort(all(dic)); dic.erase(unique(all(dic)), dic.end()); M = dic.size(); vector>> v(N); rep(i, 0, N) { int y = lower_bound(all(dic), A[i]) - dic.begin(); v[i].push_back({y, A[i]}); A[i] = y; } st.init(v); ll ans = infl; rep(i, 0, K - 1) bit.add(A[i], 1); rep(L, 0, N - K + 1) { int R = L + K; bit.add(A[R - 1], 1); int ng = 0, ok = M; while (ng + 1 != ok) { int md = (ng + ok) / 2; if (K / 2 < bit.get(0, md)) ok = md; else ng = md; } ok--; rep(d, -1, 1) { int ok2 = ok + d; if (ok2 < 0) continue; ll cent = dic[ok2]; ll low = st.get(L, R, 0, ok2); ll high = st.get(L, R, ok2 + 1, inf); ll cst = bit.get(0, ok2) * cent - low + high - bit.get(ok2 + 1, M) * cent; chmin(ans, cst); } bit.add(A[L], -1); } cout << ans << endl; }