diff --git a/storage/jungol/rs/completed/8566.rs b/storage/jungol/rs/completed/8566.rs new file mode 100644 index 0000000..f392900 --- /dev/null +++ b/storage/jungol/rs/completed/8566.rs @@ -0,0 +1,38 @@ +use std::{ + cmp::Reverse, + collections::BinaryHeap, + io::{read_to_string, stdin}, +}; + +fn main() { + let temp = read_to_string(stdin()).unwrap(); + let mut iter = temp + .split_ascii_whitespace() + .map(|x| x.parse::().unwrap()); + + let (n, p) = (iter.next().unwrap(), iter.next().unwrap()); + + let defs = (0..n).map(|_| iter.next().unwrap()).collect::>(); + + let mut heap = BinaryHeap::new(); + let mut total = 0; + let mut res = Vec::new(); + + for &d in defs.iter() { + heap.push(Reverse(d)); + total += d; + + while heap.len() > 0 && total - heap.peek().unwrap().0 >= p { + total -= heap.peek().unwrap().0; + heap.pop(); + } + if total >= p { + res.push(heap.len() as isize); + } else { + res.push(-1); + } + } + + res.iter().for_each(|x| print!("{} ", x)); + println!(); +}