結果

問題 No.1861 Required Number
ユーザー phspls
提出日時 2022-10-23 13:06:58
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 138 ms / 2,500 ms
コード長 1,053 bytes
コンパイル時間 13,700 ms
コンパイル使用メモリ 379,612 KB
実行使用メモリ 22,272 KB
最終ジャッジ日時 2024-07-02 06:11:39
合計ジャッジ時間 23,711 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
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 {
                dp[pat][i+1][j] |= dp[pat][i][j];
                if j+a[i] <= k {
                    dp[pat][i+1][j+a[i]] |= dp[pat][i][j];
                }
            }
        }
        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