#include using namespace std; using lint = long long; template using V = vector; template using VV = V< V >; #pragma GCC target ("avx") struct Bitvector { using T = uint32_t; static constexpr int W = 32; static int popcnt(T x) { return __builtin_popcount(x); } const int n; V b; V<> c; Bitvector(int n) : n(n), b(n / W + 1), c(n / W + 1) {} void set(int i) { b[i / W] |= (T)1 << i % W; } void build() { for (int i = 0; i < n / W; ++i) c[i + 1] = c[i] + popcnt(b[i]); } bool operator[](int i) const { return b[i / W] >> i % W & 1; } int rank(int i) const { return c[i / W] + popcnt(b[i / W] & ~(~(T)0 << i % W)); } int rank(bool v, int i) const { return v ? rank(i) : i - rank(i); } }; template struct WaveletMatrix { const int n, h; V b; V<> z; template WaveletMatrix(Itr first, Itr last) : n(distance(first, last)), h(__lg(2 * *max_element(first, last) + 1)), b(h, Bitvector(n)), z(h) { assert(*min_element(first, last) >= 0); V a(first, last), na(n); for (int j = 0; j < h; ++j) { for (int i = 0; i < n; ++i) { if (a[i] >> h + ~j & 1) b[j].set(i); else ++z[j]; } b[j].build(); int p = 0, q = z[j]; for (int i = 0; i < n; ++i) (b[j][i] ? na[q++] : na[p++]) = a[i]; swap(a, na); } } T operator[](int i) const { T res = 0; for (int j = 0; j < h; ++j) { T v = b[j][i]; res |= v << h + ~j; i = v * z[j] + b[j].rank(v, i); } return res; } int rank(int l, int r, T x) const { if (x < 0 or x >= (T)1 << h) return 0; for (int j = 0; j < h; ++j) { int v = x >> h + ~j & 1; l = v * z[j] + b[j].rank(v, l); r = v * z[j] + b[j].rank(v, r); } return r - l; } T kth(int l, int r, int k) const { assert(0 <= k and k < r - l); T res = 0; for (int j = 0; j < h; ++j) { int p = b[j].rank(0, l), q = b[j].rank(0, r); T v = k >= q - p; k -= v * (q - p); res |= v << h + ~j; l = v * z[j] + (v ? l - p : p); r = v * z[j] + (v ? r - q : q); } return res; } T rkth(int l, int r, int k) const { return kth(l, r, r - l + ~k); } int cnt(int l, int r, T x) const { if ((x = max(x, 0)) >= (T)1 << h) return 0; int res = 0; for (int j = 0; j < h; ++j) { int p = b[j].rank(1, l), q = b[j].rank(1, r); int v = x >> h + ~j & 1; res += !v * (q - p); l = v * z[j] + (v ? p : l - p); r = v * z[j] + (v ? q : r - q); } return res + r - l; } int cnt(int l, int r, T x, T y) const { return cnt(l, r, x) - cnt(l, r, y); } }; struct WaveletSum { using T = lint; const int n, h; V b; V<> z; VV c; template WaveletSum(Itr first, Itr last) : n(distance(first, last)), h(__lg(2 * *max_element(first, last) + 1)), b(h, Bitvector(n)), z(h), c(h + 1, V(n + 1)) { assert(*min_element(first, last) >= 0); V<> w(first, last), nw(n); for (int j = 0; j < h; ++j) { for (int i = 0; i < n; ++i) { if (w[i] >> h + ~j & 1) b[j].set(i); else ++z[j]; } b[j].build(); int p = 0, q = z[j]; for (int i = 0; i < n; ++i) (b[j][i] ? nw[q++] : nw[p++]) = w[i]; swap(w, nw); } } T acc(int l, int r, int x, int y, int j, int s, int t) const { if (y <= s or t <= x) return 0; if (x <= s and t <= y) return c[j][r] - c[j][l]; int m = s + t >> 1, p = b[j].rank(0, l), q = b[j].rank(0, r); return acc(p, q, x, y, j + 1, s, m) + acc(z[j] + l - p, z[j] + r - q, x, y, j + 1, m, t); } T acc(int l, int r, int x, int y) const { return acc(l, r, x, y, 0, 0, 1 << h); } void set(int i, T x) { for (int j = 0; j < h; ++j) { c[j][i + 1] = x; int v = b[j][i]; i = v * z[j] + b[j].rank(v, i); } c[h][i + 1] = x; } void build() { for (int j = 0; j <= h; ++j) { for (int i = 0; i < n; ++i) c[j][i + 1] += c[j][i]; } } }; int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n, k; cin >> n >> k; V<> a(n); for (auto&& e : a) cin >> e; WaveletMatrix wm(begin(a), end(a)); WaveletSum ws(begin(a), end(a)); for (int i = 0; i < n; ++i) { ws.set(i, a[i]); } ws.build(); lint res = 9e18; for (int l = 0; l + k <= n; ++l) { int r = l + k; lint m = wm.kth(l, r, k / 2); int cl = wm.cnt(l, r, 0, m), cu = wm.cnt(l, r, m, 1 << 30); lint curr = 0; curr += cl * m - ws.acc(l, r, 0, m); curr += ws.acc(l, r, m, 1 << 30) - cu * m; res = min(res, curr); } cout << res << '\n'; }