https://atcoder.jp/contests/abc297/tasks/abc297_e
BTreeSetを使えば簡単ですね。
// Kth Takoyaki Set #![allow(non_snake_case)] use std::collections::BTreeSet; //////////////////// 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<i64>) { let v = read_vec(); let K: usize = v[1]; let A = read_vec(); (K, A) } fn f(K: usize, A: Vec<i64>) -> i64 { let mut set = BTreeSet::new(); set.insert(0); for _ in 0..K { let n = set.iter().next().unwrap(); let c = *n; set.remove(&c); for a in A.iter() { set.insert(c + *a); } } match set.iter().next() { Some(n) => *n, None => 0 } } fn main() { let (K, A) = read_input(); println!("{}", f(K, A)) }