結果

問題 No.1861 Required Number
ユーザー phspls
提出日時 2022-10-09 12:05:22
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 100 ms / 2,500 ms
コード長 1,083 bytes
コンパイル時間 30,209 ms
コンパイル使用メモリ 384,380 KB
実行使用メモリ 22,528 KB
最終ジャッジ日時 2024-06-23 12:18:53
合計ジャッジ時間 21,762 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 42 MLE * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

fn main() {
    let mut nk = String::new();
    std::io::stdin().read_line(&mut nk).ok();
    let nk: Vec<usize> = nk.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();
    let n = nk[0];
    let k = nk[1];
    let mut a = String::new();
    std::io::stdin().read_line(&mut a).ok();
    let mut a: Vec<usize> = a.trim().split_whitespace().map(|s| s.parse().unwrap()).collect();

    let mut dp = vec![vec![vec![false; k+1]; n+2]; 2];
    dp[0][0][0] = true;
    dp[1][0][0] = true;
    for pat in 0..2 {
        for i in 0..n {
            for j in 0..=k {
                if !dp[pat][i][j] { continue; }
                dp[pat][i+1][j] = true;
                if j + a[i] <= k {
                    dp[pat][i+1][j+a[i]] = true;
                }
            }
        }
        a.reverse();
    }
    dp[1].reverse();
    if !dp[0][n][k] {
        println!("-1");
        return;
    }
    let mut result = 0usize;
    for i in 0..n {
        if !(0..=k).any(|j| dp[0][i][j] && dp[1][i+2][k-j]) {
            result += 1;
        }
    }
    println!("{}", result);
}
0