#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; #define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++) #define rep(i, n) repr(i, 0, n) #define reprrev(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--) #define reprev(i, n) reprrev(i, 0, n) #define repi(itr, ds) for (auto itr = ds.begin(); itr != ds.end(); itr++) #define chmin(mi, val) mi = min(mi, val) #define chmax(ma, val) ma = max(ma, val) #define all(a) (a).begin(), (a).end() #define rall(a) (a).rbegin(), (a).rend() #define mp make_pair #define mt make_tuple #define INF 1050000000 #define INFR INT_MAX #define INFL (long long)(4e18) #define INFLR LLONG_MAX #define EPS (1e-10) #define MOD 1000000007 //#define MOD 998244353 #define PI 3.141592653589793238 #define RMAX 4294967295 using vi = vector; using vvi = vector>; using vvvi = vector>>; using vvvvi = vector>>>; using ll = long long; using vll = vector; using vvll = vector>; using vvvll = vector>>; using vd = vector; using vvd = vector>; using vb = vector; using vvb = vector>; using vc = vector; using vvc = vector>; using vs = vector; using vvs = vector>; using Pi = pair; using vPi = vector; using vvPi = vector>; using vvvPi = vector>>; using vvvvPi = vector>>>; using Pll = pair; using vPll = vector; using Pd = pair; using vPd = vector; template using vec = vector; template using pql = priority_queue, greater>; using Comp = complex; // vvvvvvvvvvvvvvvvvvvvvvv debug output vvvvvvvvvvvvvvvvvvvvvvv // vector input template istream &operator>>(istream &is, vector &vec) { for (T &x : vec) is >> x; return is; } // pair template ostream &operator<<(ostream &os, const pair &pair_var) { os << "(" << pair_var.first << ", " << pair_var.second << ")"; return os; } // vector template ostream &operator<<(ostream &os, const vector &vec) { os << "{"; for (int i = 0; i < vec.size(); i++) { os << vec[i] << (i + 1 == vec.size() ? "" : ", "); } os << "}"; return os; } // deque template ostream &operator<<(ostream &os, const deque &vec) { os << "{"; for (int i = 0; i < vec.size(); i++) { os << vec[i] << (i + 1 == vec.size() ? "" : ", "); } os << "}"; return os; } // map template ostream &operator<<(ostream &os, const map &map_var) { os << "{"; repi(itr, map_var) { os << *itr; itr++; if (itr != map_var.end()) os << ", "; itr--; } os << "}"; return os; } // set template ostream &operator<<(ostream &os, const set &set_var) { os << "{"; repi(itr, set_var) { os << *itr; itr++; if (itr != set_var.end()) os << ", "; itr--; } os << "}"; return os; } // multiset template ostream &operator<<(ostream &os, const multiset &set_var) { os << "{"; repi(itr, set_var) { os << *itr; itr++; if (itr != set_var.end()) os << ", "; itr--; } os << "}"; return os; } #define DUMPOUT cerr void dump_func() { DUMPOUT << endl; } template void dump_func(Head &&head, Tail &&... tail) { DUMPOUT << head; if (sizeof...(Tail) > 0) { DUMPOUT << ", "; } dump_func(std::move(tail)...); } #ifdef DEBUG_ #define DEB #define dump(...) \ DUMPOUT << " " << string(#__VA_ARGS__) << ": " \ << "[" << to_string(__LINE__) << ":" << __FUNCTION__ << "]" \ << endl \ << " ", \ dump_func(__VA_ARGS__) #else #define DEB if (false) #define dump(...) #endif // ^^^^^^^^^^^^^^^^^^^^^^^ debug output ^^^^^^^^^^^^^^^^^^^^^^^ string YN(bool y, int id = 0) { if (id) cout << id; return (y ? "YES" : "NO"); } string yn(bool y, int id = 0) { if (id) cout << id; return (y ? "Yes" : "No"); } string ON(bool y, int id = 0) { if (id) cout << id; return (y ? "OK" : "NG"); } int dir4[4][2] = {{0, -1}, {-1, 0}, {1, 0}, {0, 1}}; int dir8[8][2] = {{-1, -1}, {0, -1}, {1, -1}, {-1, 0}, {1, 0}, {-1, 1}, {0, 1}, {1, 1}}; char dirchar[4] = {'<', '^', '>', 'v'}; // [a,b) int irand(int a, int b) { static mt19937 Rand(static_cast(time(nullptr))); uniform_int_distribution dist(a, b - 1); return dist(Rand); } // [a,b) double drand(int a, int b) { static mt19937 Rand(static_cast(time(nullptr))); uniform_real_distribution dist(a, b); return dist(Rand); } // https://qiita.com/IgnorantCoder/items/3101d6276e9bdddf872c template inline auto transform(const A &v, F &&f) { using result_type = decltype(std::declval()(std::declval())); vector y(v.size()); std::transform(std::cbegin(v), std::cend(v), std::begin(y), f); return y; } // 多次元vector生成 template vector make_v(size_t size, const T &init) { return vector(size, init); } template auto make_v(size_t size, Ts... rest) { return vector(size, make_v(rest...)); } template T Max(vector a) { return *max_element(all(a)); } template T Min(vector a) { return *min_element(all(a)); } template T Sum(vector a) { return accumulate(all(a), (T)0); } // mapでカウントとかする template void Add(map &m, T item) { if (m.find(item) == m.end()) { m[item] = 1; } else { m[item]++; } } // mapでカウントとかする template void Erase(map &m, T item) { if (m.find(item) == m.end()) { } else { if (m[item] == 1) { m.erase(item); } else { m[item]--; } } } // デフォルト値つきのmapのget template U Get(map m, T key, U def) { if (m.find(key) == m.end()) { return def; } else { return m[key]; } } template inline bool Contains(const set &t, const T &key) { return t.find(key) != t.end(); } template inline bool Contains(const map &t, const T &key) { return t.find(key) != t.end(); } template struct Edge { int from, to; T cost; bool operator<(Edge e) { return cost < e.cost; } }; template ostream &operator<<(ostream &os, Edge &edge) { os << "(" << edge.from << "->" << edge.to << ":" << edge.cost << ")"; return os; } template class Graph { int n; bool directed; vector>> edges; public: Graph(int n, bool directed) : n(n), directed(directed), edges(vector>>(n)) {} void add_edge(int s, int t, T cost) { edges[s].push_back(Edge{s, t, cost}); if (!directed) { edges[t].push_back(Edge{t, s, cost}); } } Graph() {} vector> operator[](size_t i) const { return edges[i]; } int size() const { return n; } }; //====================================================== class BitVector { vector cumulative_sum; int pos = 1; vector pos_select; public: vector> select; // select[b][n] = n 番目(0-indexed)の b の位置 BitVector(int n) : cumulative_sum(n + 1), pos_select(2, 0), select(2, vector(n)) { cumulative_sum[0] = 0; } // 数 b を追加。n 回呼ばれる void push(int b) { cumulative_sum[pos] = cumulative_sum[pos - 1] + b; select[b][pos_select[b]] = pos - 1; pos++; pos_select[b]++; } int operator[](int idx) const { return cumulative_sum[idx + 1] - cumulative_sum[idx]; } // [0, n) に x がいくつあるか int rank(int n, int b) const { if (n < 0) return 0; n = min(n, size()); if (b == 1) { return cumulative_sum[n]; } else { return n - cumulative_sum[n]; } } int size() const { return cumulative_sum.size() - 1; } }; ostream &operator<<(ostream &os, const BitVector &bv) { os << "{"; for (int i = 0; i < bv.size(); i++) { os << bv[i] << (i + 1 == bv.size() ? "" : ", "); } os << "}"; return os; } class WaveletMatrix { int len; // 数列の長さ int digit = 0; // 値の桁数 vector bit_vectors; vector num_zero; map start; // sorted における各値の開始位置 vector sorted; public: WaveletMatrix(vector v) { len = v.size(); int max_value = *max_element(all(v)); while (max_value > 0) { max_value >>= 1; digit++; } if (digit == 0) digit++; bit_vectors.reserve(digit); num_zero = vector(digit, 0); rep(d, digit) { bit_vectors.emplace_back(len); rep(i, len) { bit_vectors[d].push(v[i] >> (digit - 1 - d) & 1); } vector temp; temp.reserve(len); rep(i, len) { if ((v[i] >> (digit - 1 - d) & 1) == 0) { temp.push_back(v[i]); num_zero[d]++; } } rep(i, len) { if ((v[i] >> (digit - 1 - d) & 1) == 1) { temp.push_back(v[i]); } } v = temp; } reprev(i, len) { start[v[i]] = i; } sorted = v; } // n 番目(0-indexed)の値を得る int operator[](int n) const { int ans = 0; rep(d, digit) { ans <<= 1; ans += bit_vectors[d][n]; if (bit_vectors[d][n] == 1) { n = num_zero[d] + bit_vectors[d].rank(n, 1); } else { n = bit_vectors[d].rank(n, 0); } } return ans; } // [0, n) に x がいくつ含まれるか int rank(int n, int x) { if (start.find(x) == start.end()) return 0; rep(d, digit) { if ((x >> (digit - 1 - d) & 1) == 1) { n = num_zero[d] + bit_vectors[d].rank(n, 1); } else { n = bit_vectors[d].rank(n, 0); } } return n - start[x]; } // n 番目(0-indexed)の x の位置(無ければ-1) int select(int n, int x) { if (start.find(x) == start.end() || sorted[start[x] + n] != x) return -1; n = start[x] + n; reprev(d, digit) { if ((x >> (digit - 1 - d) & 1) == 1) { n = bit_vectors[d].select[1][n - num_zero[d]]; } else { n = bit_vectors[d].select[0][n]; } } return n; } // [l, r) で n 番目(0-indexed)に小さい値 int quantile(int l, int r, int n) { rep(d, digit) { if (bit_vectors[d].rank(r, 0) - bit_vectors[d].rank(l, 0) > n) { // 0 l = bit_vectors[d].rank(l, 0); r = bit_vectors[d].rank(r, 0); } else { // 1 n -= bit_vectors[d].rank(r, 0) - bit_vectors[d].rank(l, 0); l = num_zero[d] + bit_vectors[d].rank(l, 1); r = num_zero[d] + bit_vectors[d].rank(r, 1); } } return sorted[l]; } // [l, r) で出現回数が多い順に k 個の値と頻度を返す vector> topk(int l, int r, int k) { vector> res; res.reserve(k); priority_queue> pq; pq.push(mt(r - l, 0, l, r)); while (!pq.empty() && res.size() < k) { int dummy, d; tie(dummy, d, l, r) = pq.top(); pq.pop(); if (d == digit) { // 終わり res.push_back(mp(sorted[l], r - l)); continue; } int l0 = bit_vectors[d].rank(l, 0); int r0 = bit_vectors[d].rank(r, 0); int l1 = num_zero[d] + bit_vectors[d].rank(l, 1); int r1 = num_zero[d] + bit_vectors[d].rank(r, 1); pq.push(mt(r0 - l0, d + 1, l0, r0)); pq.push(mt(r1 - l1, d + 1, l1, r1)); } return res; } // [l, r) に含まれる x 未満の値の個数 int less_freq(int l, int r, int x) { if (x <= 0) return 0; if (l >= r) return 0; int ans = 0; rep(d, digit) { if ((x >> (digit - 1 - d) & 1) == 1) { ans += bit_vectors[d].rank(r, 0) - bit_vectors[d].rank(l, 0); l = num_zero[d] + bit_vectors[d].rank(l, 1); r = num_zero[d] + bit_vectors[d].rank(r, 1); } else { l = bit_vectors[d].rank(l, 0); r = bit_vectors[d].rank(r, 0); } } return ans; } // [l, r) に含まれる [a, b) の範囲の値の和 long long range_sum(int l, int r, int a, int b) { long long ans = 0; stack> pq; pq.push(mt(r - l, 0, l, r)); while (!pq.empty()) { int dummy, d; tie(dummy, d, l, r) = pq.top(); pq.pop(); if (d == digit) { // 終わり if (a <= sorted[l] && sorted[l] < b) { ans += (long long)sorted[l] * (r - l); } continue; } int l0 = bit_vectors[d].rank(l, 0); int r0 = bit_vectors[d].rank(r, 0); int l1 = num_zero[d] + bit_vectors[d].rank(l, 1); int r1 = num_zero[d] + bit_vectors[d].rank(r, 1); if (l0 < r0) pq.push(mt(r0 - l0, d + 1, l0, r0)); if (l1 < r1) pq.push(mt(r1 - l1, d + 1, l1, r1)); } return ans; } }; int main() { int N, K; cin >> N >> K; vi A(N); cin >> A; WaveletMatrix wm(A); ll ans = INFL; rep(i, N - K + 1) { // [i, i+K) で揃えるコスト int median = wm.quantile(i, i + K, K / 2); int less_num = wm.less_freq(i, i + K, median); int more_num = K - less_num; ll less_sum = wm.range_sum(i, i + K, 0, median); ll more_sum = wm.range_sum(i, i + K, median, INF); ll cost = 0; cost += (ll)median * less_num - less_sum; cost += more_sum - (ll)median * more_num; chmin(ans, cost); } cout << ans << endl; return 0; }