use std::io::{stdin, Read, StdinLock}; use std::str::FromStr; struct Input { k: usize, n: usize, f: usize, a: Vec, } fn read_input(cin_lock: &mut StdinLock) -> Input { let k = next_token(cin_lock); let n = next_token(cin_lock); let f = next_token(cin_lock); Input { k, n, f, a: next_vector_token(cin_lock, f), } } fn remain_beans(beans: i32, family: Vec, index: i32) -> i32 { if beans < 0 { return -1; } if index < 0 { return beans; } return remain_beans(beans - family[index as usize], family, index - 1); } fn solve(input: Input) { let beans = input.k * input.n; println!( "{}", remain_beans(beans as i32, input.a, input.f as i32 - 1) ); } fn next_token(cin_lock: &mut StdinLock) -> T { cin_lock .by_ref() .bytes() .map(|c| c.unwrap() as char) .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect::() .parse::() .ok() .unwrap() } fn next_vector_token(cin_lock: &mut StdinLock, n: usize) -> Vec { let cin = cin_lock.by_ref(); (0..n) .map(|_| { cin.bytes() .map(|c| c.unwrap() as char) .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect::() .parse::() .ok() .unwrap() }) .collect() } fn main() { let cin = stdin(); let mut cin_lock = cin.lock(); let input = read_input(&mut cin_lock); solve(input); }