https://atcoder.jp/contests/abc334/tasks/abc334_d
Rをソートして累積和を求めておいて二分探索ですね。
// Reindeer and Sleigh #![allow(non_snake_case)] //////////////////// library //////////////////// fn read<T: std::str::FromStr>() -> T { let mut line = String::new(); std::io::stdin().read_line(&mut line).ok(); line.trim().parse().ok().unwrap() } fn read_vec<T: std::str::FromStr>() -> Vec<T> { read::<String>().split_whitespace() .map(|e| e.parse().ok().unwrap()).collect() } //////////////////// process //////////////////// fn read_input() -> (usize, Vec<usize>) { let v = read_vec(); let Q = v[1]; let R = read_vec(); (Q, R) } fn accumulate(mut R: Vec<usize>) -> Vec<usize> { R.sort(); let mut A: Vec<usize> = vec![0; R.len()+1]; for i in 0..R.len() { A[i+1] = A[i] + R[i] } A } fn bin_search(first: usize, last: usize, X: usize, A: &Vec<usize>) -> usize { if last - first == 1 { first } else { let mid = (first + last) / 2; if A[mid] <= X { bin_search(mid, last, X, A) } else { bin_search(first, mid, X, A) } } } fn F(Q: usize, R: Vec<usize>) { let A = accumulate(R); for _ in 0..Q { let X: usize = read(); let n = bin_search(0, A.len(), X, &A); println!("{}", n) } } fn main() { let (Q, R) = read_input(); F(Q, R) }