complete 8566.rs

This commit is contained in:
2026-07-13 14:27:21 +09:00
parent 873ea096e2
commit 0a52264f6c

View File

@@ -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::<usize>().unwrap());
let (n, p) = (iter.next().unwrap(), iter.next().unwrap());
let defs = (0..n).map(|_| iter.next().unwrap()).collect::<Vec<_>>();
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!();
}