結果
| 問題 | 
                            No.143 豆
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2020-08-13 15:52:52 | 
| 言語 | Rust  (1.83.0 + proconio)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 1 ms / 1,000 ms | 
| コード長 | 1,706 bytes | 
| コンパイル時間 | 12,477 ms | 
| コンパイル使用メモリ | 405,812 KB | 
| 実行使用メモリ | 6,944 KB | 
| 最終ジャッジ日時 | 2024-07-23 11:35:13 | 
| 合計ジャッジ時間 | 13,333 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge2 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 17 | 
ソースコード
use std::io::{stdin, Read, StdinLock};
use std::str::FromStr;
struct Input {
    k: usize,
    n: usize,
    f: usize,
    a: Vec<i32>,
}
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<i32>, 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<T: FromStr>(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::<String>()
        .parse::<T>()
        .ok()
        .unwrap()
}
fn next_vector_token<T: FromStr>(cin_lock: &mut StdinLock, n: usize) -> Vec<T> {
    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::<String>()
                .parse::<T>()
                .ok()
                .unwrap()
        })
        .collect()
}
fn main() {
    let cin = stdin();
    let mut cin_lock = cin.lock();
    let input = read_input(&mut cin_lock);
    solve(input);
}