https://atcoder.jp/contests/abc358/tasks/abc358_d
ソートして尺取り法っぽいことをするだけですね。
// Souvenirs #![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() -> (Vec<i64>, Vec<i64>) { let _v: Vec<usize> = read_vec(); let A: Vec<i64> = read_vec(); let B: Vec<i64> = read_vec(); (A, B) } fn F(mut A: Vec<i64>, mut B: Vec<i64>) -> i64 { A.sort(); B.sort(); let mut s: i64 = 0; let mut k: usize = 0; for a in A.into_iter() { if a >= B[k] { s += a; k += 1 } if k == B.len() { return s } } -1 } fn main() { let (A, B) = read_input(); println!("{}", F(A, B)) }