#[allow(unused_imports)] use std::cmp::*; #[allow(unused_imports)] use std::collections::*; use std::io::{Write, BufWriter}; // https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); let mut next = || { iter.next().unwrap() }; input_inner!{next, $($r)*} }; ($($r:tt)*) => { let stdin = std::io::stdin(); let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock())); let mut next = move || -> String{ bytes .by_ref() .map(|r|r.unwrap() as char) .skip_while(|c|c.is_whitespace()) .take_while(|c|!c.is_whitespace()) .collect() }; input_inner!{next, $($r)*} }; } macro_rules! input_inner { ($next:expr) => {}; ($next:expr, ) => {}; ($next:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($next, $t); input_inner!{$next $($r)*} }; } macro_rules! read_value { ($next:expr, ( $($t:tt),* )) => { ( $(read_value!($next, $t)),* ) }; ($next:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($next, $t)).collect::>() }; ($next:expr, chars) => { read_value!($next, String).chars().collect::>() }; ($next:expr, usize1) => { read_value!($next, usize) - 1 }; ($next:expr, [ $t:tt ]) => {{ let len = read_value!($next, usize); (0..len).map(|_| read_value!($next, $t)).collect::>() }}; ($next:expr, $t:ty) => { $next().parse::<$t>().expect("Parse error") }; } fn solve() { let out = std::io::stdout(); let mut out = BufWriter::new(out.lock()); macro_rules! puts { ($format:expr) => (write!(out,$format).unwrap()); ($format:expr, $($args:expr),+) => (write!(out,$format,$($args),*).unwrap()) } let n; let k; let mut a; if true { input! { n_: usize, k_: usize, a_: [i64; n_], } n = n_; k = k_; a = a_; } else { n = 100000; k = 50000; a = vec![0; n]; for i in 0 .. n { a[i] = i as i64 + 1; } } let b = (k as f64 * (k as f64).ln()) as usize; let b = max(b, 1); let sz = (n + b - 1) / b; let mut acc = vec![vec![0; b + 1]; sz]; const INF: i64 = 1 << 31; let mut srt = vec![INF; b * sz]; for i in 0 .. n { srt[i] = 2 * a[i] + 1; } for i in 0 .. sz { srt[i * b .. i * b + b].sort(); for j in 0 .. b { acc[i][j + 1] = acc[i][j] + srt[i * b + j] / 2; } } let mut mi: i64 = 1 << 50; for i in 0 .. n - k + 1 { let lb = (i + b - 1) / b; let rb = (i + k) / b; // binsect let mut pass = 0; let mut fail = INF; while fail - pass > 1 { let mid = (fail + pass) / 2; let mut count = 0; if lb > rb { for j in i .. i + k { if a[j] <= mid { count += 1; } } } else { for j in i .. b * lb { if a[j] <= mid { count += 1; } } for j in lb .. rb { let idx = srt[j * b .. j * b + b].binary_search(&(2 * mid + 2)).unwrap_err(); count += idx; } for j in b * rb .. i + k { if a[j] <= mid { count += 1; } } } if count <= (k - 1) / 2 { pass = mid; } else { fail = mid; } } let med = fail; let mut cost = 0; if lb > rb { for j in i .. i + k { cost += (a[j] - med).abs(); } } else { for j in i .. b * lb { cost += (a[j] - med).abs(); } for j in lb .. rb { let idx = srt[j * b .. j * b + b].binary_search(&(2 * med + 2)).unwrap_err(); cost += idx as i64 * med - acc[j][idx] + (acc[j][b] - acc[j][idx]) - (b - idx) as i64 * med; } for j in b * rb .. i + k { cost += (a[j] - med).abs(); } } // eprintln!("i = {}, med = {}, cost = {}, h = {:?}", i, med, cost, &a[i .. i + k]); mi = min(mi, cost); } puts!("{}\n", mi); } fn main() { // In order to avoid potential stack overflow, spawn a new thread. let stack_size = 104_857_600; // 100 MB let thd = std::thread::Builder::new().stack_size(stack_size); thd.spawn(|| solve()).unwrap().join().unwrap(); }