#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 #include #include #include // Yay!! #define endl codeforces // macros for iterator #define ALL(v) std::begin(v), std::end(v) #define ALLR(v) std::rbegin(v), std::rend(v) // alias using ll = std::int64_t; using ull = std::uint64_t; using pii = std::pair; using tii = std::tuple; using pll = std::pair; using tll = std::tuple; template using vec = std::vector; template using vvec = vec>; // variadic min/max template const T& var_min(const T &t) { return t; } template const T& var_max(const T &t) { return t; } template const T& var_min(const T &t, const Tail&... tail) { return std::min(t, var_min(tail...)); } template const T& var_max(const T &t, const Tail&... tail) { return std::max(t, var_max(tail...)); } // variadic chmin/chmax template void chmin(T &t, const Tail&... tail) { t = var_min(t, tail...); } template void chmax(T &t, const Tail&... tail) { t = var_max(t, tail...); } // multi demension array template struct multi_dim_array { using type = std::array::type, Head>; }; template struct multi_dim_array { using type = std::array; }; template using mdarray = typename multi_dim_array::type; // fill container template void fill_seq(T &t, F f, Args... args) { if constexpr (std::is_invocable::value) { t = f(args...); } else { for (ssize_t i = 0; i < t.size(); i++) fill_seq(t[i], f, args..., i); } } // make multi dimension vector template vec make_v(ssize_t sz) { return vec(sz); } template auto make_v(ssize_t hs, Tail&&... ts) { auto v = std::move(make_v(std::forward(ts)...)); return vec(hs, v); } // init namespace init__ { struct InitIO { InitIO() { std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false); std::cout << std::fixed << std::setprecision(30); } } init_io; } namespace utility { template using validate_integer = typename std::enable_if::value, ll>::type; template auto popcount(T n) -> validate_integer { return __builtin_popcount(n); } // 0 indexed template auto msb(T n) -> validate_integer { return 64 - __builtin_clzll(n) - 1; } template constexpr auto ceil_pow2(T s) -> validate_integer { ll ret = 1; while (ret < s) ret *= 2; return ret; } } namespace utility { constexpr ll ceil_div(ll a, ll b) { return a / b + !!(a % b); } } namespace succinct { template constexpr T ceil_log2(T n) { for (int i = 0; i < 63; i++) { T mask = 1ll << i; if (n <= mask) return i; } return -1; } template constexpr T ceil_even(T n) { return n + (n & 1); } template struct FullyIndexableDictionary { using size_type = ssize_t; private: constexpr static size_type sz_log2 = ceil_even(ceil_log2(MaxSize)); constexpr static size_type chunk_sz = sz_log2 * sz_log2; constexpr static size_type block_sz = sz_log2 / 2; constexpr static size_type block_per_chunk = chunk_sz / block_sz; size_type sz, cnt1; vec chunk, block, dat; public: FullyIndexableDictionary() { } template FullyIndexableDictionary(F f, size_type sz) : sz(sz), cnt1(0), chunk(utility::ceil_div(sz, chunk_sz)), block(chunk.size() * block_per_chunk), dat(block.size()) { size_type idx = 0; for (size_type i = 0; i < chunk.size(); i++) { chunk[i] = cnt1; size_type tmp = 0; for (size_type j = 0; j < block_per_chunk; j++, idx++) { block[idx] = tmp; for (size_type k = 0; k < block_sz; k++) { auto v = f(idx * block_sz + k); size_type mask = (1ll << k) * v; dat[idx] |= mask; tmp += v; } } cnt1 += tmp; } } size_type rank(bool b, size_type pos) const { if (!b) return pos - rank(!b, pos); if (pos == sz) return sum(b); size_type ret = 0; ret += chunk[pos / chunk_sz]; ret += block[pos / block_sz]; auto mask = (1ll << (pos % block_sz)) - 1; return ret + utility::popcount(dat[pos / block_sz] & mask); } size_type rank(bool b, size_type l, size_type r) const { return rank(b, r) - rank(b, l); } size_type select(bool b, size_type n) const { if (rank(b, sz) < n) return -1; size_type ok = sz, ng = 0; while (1 < std::abs(ok - ng)) { size_type mid = (ok + ng) / 2; (n <= rank(b, mid) ? ok : ng) = mid; } return ok; } bool operator [](size_type i) const { size_type offset = i % block_sz; return (dat[i / block_sz] >> offset) & 1; } size_type sum(bool b) const { return b ? cnt1 : sz - cnt1; } }; } namespace succinct { template class WaveletMatrix { using data_type = FullyIndexableDictionary; public: using size_type = typename data_type::size_type; private: size_type sz; std::array mat; std::array, MaxValueLog> acc_sum; void update_range_aux(bool bt, size_type &l, size_type &r, size_type i) const { const auto &dic = mat[i]; l = dic.rank(bt, l) + bt * dic.sum(0); r = dic.rank(bt, r) + bt * dic.sum(0); } size_type range_freq_aux(size_type l, size_type r, T bound) const { if ((1ll << MaxValueLog) - 1 <= bound) return r - l; size_type ret = 0; for (size_type i = 0; i < MaxValueLog && l < r; i++) { T mask = 1ll << (MaxValueLog - (i + 1)); bool bt = !!(bound & mask); if (bt) ret += mat[i].rank(0, l, r); update_range_aux(bt, l, r, i); } return ret; } size_type range_sum_aux(size_type l, size_type r, T bound) const { if ((1ll << MaxValueLog) - 1 <= bound) return acc_sum[0][r] - acc_sum[0][l]; T ret = 0; for (size_type i = 0; i < MaxValueLog && l < r; i++) { T mask = 1ll << (MaxValueLog - (i + 1)); bool bt = !!(bound & mask); if (bt) ret += acc_sum[i][r] - acc_sum[i][l]; update_range_aux(bt, l, r, i); } return ret; } public: WaveletMatrix(vec dat) : sz(dat.size()) { vec buf(sz); for (size_type i = 0; i < MaxValueLog; i++) { size_type mask = 1ll << (MaxValueLog - i - 1); auto f = [&](size_type idx) -> bool { if (dat.size() <= idx) return false; return !!(dat[idx] & mask); }; mat[i] = std::move(data_type(f, dat.size())); size_type l = 0, r = 0; acc_sum[i].resize(sz + 1, 0); for (size_type j = 0; j < dat.size(); j++) { auto e = dat[j]; if (e & mask) { r++; buf[sz - r] = e; } else { buf[l] = e; acc_sum[i][j + 1] += e; l++; } } for (size_type k = 0; k < dat.size(); k++) acc_sum[i][k + 1] += acc_sum[i][k]; std::reverse(buf.begin() + l, buf.end()); std::swap(buf, dat); } } size_type rank(T t, size_type pos) const { size_type mask = 1ll << (MaxValueLog - 1); size_type l = 0, r = pos; for (size_type i = 0; i < MaxValueLog; i++, mask /= 2) { bool bt = !!(t & mask); update_range_aux(bt, l, r, i); } return r - l; } size_type select(T t, size_type n) const { std::array larr, rarr; size_type l = 0, r = sz; for (size_type i = 0, mask = (1ll << (MaxValueLog - 1)); i < MaxValueLog; i++, mask /= 2) { larr[i] = l, rarr[i] = r; bool bt = !!(t & mask); update_range_aux(bt, l, r, i); } for (size_type i = 0, mask = 1; i < MaxValueLog; i++, mask *= 2) { size_type idx = MaxValueLog - (i + 1); const auto &dic = mat[idx]; bool bt = !!(t & mask); n += dic.rank(bt, larr[idx]); size_type ra = dic.sel(bt, n); if (ra == -1) return -1; n = ra; } return n; } // k >= 0 T quantile(size_type l, size_type r, size_type k) const { T ret = 0; for (size_type i = 0; i < MaxValueLog && l < r; i++) { T mask = 1ll << (MaxValueLog - (i + 1)); const auto &dic = mat[i]; size_type cl = dic.rank(1, l), cr = dic.rank(1, r); if (k < cr - cl) { l = cl + dic.sum(0); r = cr + dic.sum(0); ret |= mask; } else { k -= cr - cl; l -= cl; r -= cr; } } return ret; } // [l, r), [min, max) size_type range_freq(size_type l, size_type r, T min, T max) const { return range_freq_aux(l, r, max) - range_freq_aux(l, r, min); } size_type range_sum(size_type l, size_type r, T min, T max) const { return range_sum_aux(l, r, max) - range_sum_aux(l, r, min); } }; } const std::size_t SIZE = 1e5 + 10; const ll inf = 5e15; int main() { ll n, k; std::cin >> n >> k; vec av(n); for (auto &&e : av) std::cin >> e; ll ans = inf; succinct::WaveletMatrix wm(av); for (ll i = 0; i + k <= n; i++) { ll mode = wm.quantile(i, i + k, k / 2); ll cost = 0; cost += mode * wm.range_freq(i, i + k, 0, mode) - wm.range_sum(i, i + k, 0, mode); cost += wm.range_sum(i, i + k, mode, inf) - mode * wm.range_freq(i, i + k, mode, inf); chmin(ans, cost); } std::cout << ans << "\n"; return 0; }