結果

問題 No.1676 Coin Trade (Single)
ユーザー akakimidoriakakimidori
提出日時 2021-08-14 01:23:56
言語 Rust
(1.77.0)
結果
AC  
実行時間 15 ms / 2,000 ms
コード長 2,468 bytes
コンパイル時間 1,018 ms
コンパイル使用メモリ 139,340 KB
実行使用メモリ 5,492 KB
最終ジャッジ日時 2023-09-02 15:07:52
合計ジャッジ時間 2,532 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 12 ms
4,912 KB
testcase_04 AC 11 ms
4,948 KB
testcase_05 AC 10 ms
4,608 KB
testcase_06 AC 10 ms
4,680 KB
testcase_07 AC 8 ms
4,376 KB
testcase_08 AC 5 ms
4,376 KB
testcase_09 AC 8 ms
4,380 KB
testcase_10 AC 8 ms
4,376 KB
testcase_11 AC 12 ms
5,208 KB
testcase_12 AC 7 ms
4,380 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 1 ms
4,380 KB
testcase_15 AC 1 ms
4,376 KB
testcase_16 AC 1 ms
4,376 KB
testcase_17 AC 1 ms
4,376 KB
testcase_18 AC 1 ms
4,376 KB
testcase_19 AC 1 ms
4,376 KB
testcase_20 AC 1 ms
4,376 KB
testcase_21 AC 1 ms
4,380 KB
testcase_22 AC 1 ms
4,380 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 1 ms
4,376 KB
testcase_25 AC 1 ms
4,376 KB
testcase_26 AC 1 ms
4,380 KB
testcase_27 AC 1 ms
4,380 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 1 ms
4,376 KB
testcase_30 AC 1 ms
4,376 KB
testcase_31 AC 1 ms
4,380 KB
testcase_32 AC 1 ms
4,380 KB
testcase_33 AC 15 ms
5,472 KB
testcase_34 AC 15 ms
5,468 KB
testcase_35 AC 15 ms
5,476 KB
testcase_36 AC 15 ms
5,492 KB
testcase_37 AC 15 ms
5,420 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

// ---------- begin chmin, chmax ----------
pub trait ChangeMinMax {
    fn chmin(&mut self, x: Self) -> bool;
    fn chmax(&mut self, x: Self) -> bool;
}

impl<T: PartialOrd> ChangeMinMax for T {
    fn chmin(&mut self, x: Self) -> bool {
        *self > x && {
            *self = x;
            true
        }
    }
    fn chmax(&mut self, x: Self) -> bool {
        *self < x && {
            *self = x;
            true
        }
    }
}
// ---------- end chmin, chmax ----------
// ---------- begin scannner ----------
#[allow(dead_code)]
mod scanner {
    use std::str::FromStr;
    pub struct Scanner<'a> {
        it: std::str::SplitWhitespace<'a>,
    }
    impl<'a> Scanner<'a> {
        pub fn new(s: &'a String) -> Scanner<'a> {
            Scanner {
                it: s.split_whitespace(),
            }
        }
        pub fn next<T: FromStr>(&mut self) -> T {
            self.it.next().unwrap().parse::<T>().ok().unwrap()
        }
        pub fn next_bytes(&mut self) -> Vec<u8> {
            self.it.next().unwrap().bytes().collect()
        }
        pub fn next_chars(&mut self) -> Vec<char> {
            self.it.next().unwrap().chars().collect()
        }
        pub fn next_vec<T: FromStr>(&mut self, len: usize) -> Vec<T> {
            (0..len).map(|_| self.next()).collect()
        }
    }
}
// ---------- end scannner ----------

use std::io::Write;

fn main() {
    use std::io::Read;
    let mut s = String::new();
    std::io::stdin().read_to_string(&mut s).unwrap();
    let mut sc = scanner::Scanner::new(&s);
    let out = std::io::stdout();
    let mut out = std::io::BufWriter::new(out.lock());
    run(&mut sc, &mut out);
}

fn run<W: Write>(sc: &mut scanner::Scanner, out: &mut std::io::BufWriter<W>) {
    let n: usize = sc.next();
    let _k: usize = sc.next();
    // dp[v] = (x, y)
    // 国vにいる時
    // x: その国の硬貨を持ってない時の最大値
    // y: その国の硬貨を持ってる時の最大値
    let mut dp = vec![(0, 0); n];
    for i in 0..n {
        let a: i64 = sc.next();
        let mut x = 0;
        let mut y = -a;
        if i > 0 {
            x.chmax(dp[i - 1].0);
            y.chmax(dp[i - 1].0 - a);
        }
        let m: usize = sc.next();
        for _ in 0..m {
            let b = sc.next::<usize>() - 1;
            y.chmax(dp[b].1);
        }
        x.chmax(y + a);
        dp[i] = (x, y);
    }
    let ans = dp[n - 1].0;
    writeln!(out, "{}", ans).ok();
}
0