結果

問題 No.561 東京と京都
コンテスト
ユーザー elphe
提出日時 2026-02-09 15:46:45
言語 Rust
(1.93.0 + proconio + num + itertools)
結果
AC  
実行時間 1 ms / 2,000 ms
コード長 1,089 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,820 ms
コンパイル使用メモリ 203,600 KB
実行使用メモリ 7,848 KB
最終ジャッジ日時 2026-02-09 15:46:52
合計ジャッジ時間 4,310 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 17
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

fn main() {
    let stdin = std::io::read_to_string(std::io::stdin().lock()).unwrap();
    let mut stdin = stdin.split_ascii_whitespace();

    let n: usize = stdin.next().unwrap().parse().unwrap();
    let d: i32 = stdin.next().unwrap().parse().unwrap();
    let tasks: Vec<(i32, i32)> = (0..n)
        .map(|_| {
            (
                stdin.next().unwrap().parse().unwrap(),
                stdin.next().unwrap().parse().unwrap(),
            )
        })
        .collect();

    use std::io::Write;
    std::io::stdout()
        .lock()
        .write_all(output(solve(d, tasks)).as_bytes())
        .unwrap();
}

fn solve(d: i32, tasks: Vec<(i32, i32)>) -> i32 {
    let n = tasks.len();
    let mut dp = [[i32::MIN; 2]; 2];
    dp[0][0] = 0;

    tasks.into_iter().enumerate().for_each(|(i, (t, k))| {
        dp[(i & 1) ^ 1][0] = dp[i & 1][0].max(dp[i & 1][1].saturating_sub(d)) + t;
        dp[(i & 1) ^ 1][1] = dp[i & 1][1].max(dp[i & 1][0].saturating_sub(d)) + k;
    });
    dp[n & 1][0].max(dp[n & 1][1])
}

fn output(ans: i32) -> String {
    ans.to_string() + "\n"
}
0