結果

問題 No.2741 Balanced Choice
ユーザー nautnaut
提出日時 2024-04-24 01:13:15
言語 Rust
(1.77.0)
結果
TLE  
実行時間 -
コード長 2,370 bytes
コンパイル時間 1,535 ms
コンパイル使用メモリ 157,312 KB
実行使用メモリ 110,208 KB
最終ジャッジ日時 2024-04-24 01:13:23
合計ジャッジ時間 7,622 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 TLE -
testcase_01 -- -
testcase_02 -- -
testcase_03 -- -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(non_snake_case, unused_imports, unused_must_use)]
use std::io::{self, prelude::*};
use std::str;

fn main() {
    let (stdin, stdout) = (io::stdin(), io::stdout());
    let mut scan = Scanner::new(stdin.lock());
    let mut out = io::BufWriter::new(stdout.lock());

    macro_rules! input {
        ($T: ty) => {
            scan.token::<$T>()
        };
        ($T: ty, $N: expr) => {
            (0..$N).map(|_| scan.token::<$T>()).collect::<Vec<_>>()
        };
    }

    let N = input!(usize);
    let W = input!(usize);
    let D = input!(i32);

    let mut dp = [[i32::MIN; 5001]; 5001];
    dp[0][0] = 0;

    for _ in 0..N {
        let (t, w, v) = (input!(u8), input!(usize), input!(i32));

        for i in (0..=W - w).rev() {
            for j in (0..=i).rev() {
                if dp[i][j] == i32::MIN {
                    continue;
                }
                
                if t == 0 {
                    dp[i + w][j + w] = std::cmp::max(dp[i + w][j + w], dp[i][j] + v);
                } else {
                    dp[i + w][j] = std::cmp::max(dp[i + w][j], dp[i][j] + v);
                }
            }
        }
    }

    let mut ans = 0;

    for i in 0..=W {
        for j in 0..=i {
            let s0 = j;
            let s1 = i - j;

            let d = (s0 as i32 - s1 as i32).abs();

            if d <= D {
                ans = std::cmp::max(ans, dp[i][j]);
                eprintln!("{} {} {}", s0, s1, dp[i][j]);
            }
        }
    }

    writeln!(out, "{}", ans);
}

struct Scanner<R> {
    reader: R,
    buf_str: Vec<u8>,
    buf_iter: str::SplitWhitespace<'static>,
}
impl<R: BufRead> Scanner<R> {
    fn new(reader: R) -> Self {
        Self {
            reader,
            buf_str: vec![],
            buf_iter: "".split_whitespace(),
        }
    }
    fn token<T: str::FromStr>(&mut self) -> T {
        loop {
            if let Some(token) = self.buf_iter.next() {
                return token.parse().ok().expect("Failed parse");
            }
            self.buf_str.clear();
            self.reader
                .read_until(b'\n', &mut self.buf_str)
                .expect("Failed read");
            self.buf_iter = unsafe {
                let slice = str::from_utf8_unchecked(&self.buf_str);
                std::mem::transmute(slice.split_whitespace())
            }
        }
    }
}
0