use std::io::*; fn f(a: &[i64], b: i64, x: i64) -> bool { let mut cnt1 = 0; let mut cnt2 = 0; for i in 0..a.len() { if a[i] > x { cnt1 += a[i] - x; } else { cnt2 += x - a[i]; } } if cnt1 + b >= cnt2 { true } else { false } } fn main() { let mut s: String = String::new(); std::io::stdin().read_to_string(&mut s).ok(); let mut itr = s.trim().split_whitespace(); let b: i64 = itr.next().unwrap().parse().unwrap(); let n: usize = itr.next().unwrap().parse().unwrap(); let c: Vec = (0..n) .map(|_| itr.next().unwrap().parse().unwrap()) .collect(); let mut l = 0; let mut r = *c.iter().max().unwrap() + 1; for _ in 0..1000 { let mid = (l + r) >> 1; if f(&c, b, mid) { l = mid; } else { r = mid; } } let mut ans = 0; for i in 0..n { ans += (c[i] - l).abs(); } println!("{}", ans); }