結果

問題 No.417 チューリップバブル
ユーザー ngtkana
提出日時 2024-05-24 04:49:08
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 312 ms / 2,000 ms
コード長 3,520 bytes
コンパイル時間 13,852 ms
コンパイル使用メモリ 380,564 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-12-20 19:13:59
合計ジャッジ時間 19,160 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 40
権限があれば一括ダウンロードができます

ソースコード

diff #

fn main() {
    let (n, m): (usize, usize) = io::input();
    let m = m / 2;
    let a = (0..n).map(|_| io::input::<i64>()).collect::<Vec<_>>();
    let mut g = vec![Vec::new(); n];
    for _ in 0..n - 1 {
        let (i, j, c): (usize, usize, usize) = io::input();
        g[i].push((j, c));
        g[j].push((i, c));
    }
    let mut sorted = Vec::new();
    let mut stack = vec![0];
    while let Some(i) = stack.pop() {
        sorted.push(i);
        for (j, _) in g[i].clone() {
            g[j].retain(|&(k, _)| k != i);
            stack.push(j);
        }
    }
    let mut dp = vec![vec![i64::MIN; m + 1]; n];
    for &x in sorted.iter().rev() {
        dp[x][0] = a[x];
        for (y, c) in g[x].clone() {
            if m < c {
                continue;
            }
            for from in (0..=m - c).rev() {
                for i in 0..=m - c - from {
                    let to = from + c + i;
                    dp[x][to] = dp[x][to].max(dp[x][from] + dp[y][i]);
                }
            }
        }
    }
    let ans = *dp[0].iter().max().unwrap();
    println!("{}", ans);
}

// io {{{
// https://ngtkana.github.io/ac-adapter-rs/io/index.html

#[allow(dead_code)]
mod io {
    use std::cell::Cell;
    use std::io::stdin;
    use std::io::BufRead;
    use std::io::BufReader;
    use std::io::Lines;
    use std::io::Stdin;
    use std::sync::Mutex;
    use std::sync::Once;
    pub fn input<T: ParseLine>() -> T {
        ParseLine::parse_line(&line())
    }
    pub trait ParseLine {
        fn parse_line(s: &str) -> Self;
    }
    macro_rules! impl_parse_line {
        ($($t:ty),*) => {
            $(impl ParseLine for $t {
                fn parse_line(s: &str) -> Self {
                    s.parse().unwrap()
                }
            })*
        };
    }
    impl_parse_line!(u8, u16, u32, u64, u128, usize, i8, i16, i32, i64, i128, isize, String, char);
    macro_rules! impl_parse_line_tuple {
        ($($t:ident),*) => {
            impl<$($t: ParseLine),*> ParseLine for ($($t,)*) {
                fn parse_line(s: &str) -> Self {
                    let mut s = s.split_whitespace();
                    ($($t::parse_line(s.next().unwrap()),)*)
                }
            }
        };
    }
    impl_parse_line_tuple!(T0, T1);
    impl_parse_line_tuple!(T0, T1, T2);
    impl_parse_line_tuple!(T0, T1, T2, T3);
    impl_parse_line_tuple!(T0, T1, T2, T3, T4);
    impl_parse_line_tuple!(T0, T1, T2, T3, T4, T5);
    impl_parse_line_tuple!(T0, T1, T2, T3, T4, T5, T6);
    impl_parse_line_tuple!(T0, T1, T2, T3, T4, T5, T6, T7);
    impl_parse_line_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8);
    impl_parse_line_tuple!(T0, T1, T2, T3, T4, T5, T6, T7, T8, T9);
    impl<T: ParseLine> ParseLine for Vec<T> {
        fn parse_line(s: &str) -> Self {
            s.split_whitespace().map(T::parse_line).collect()
        }
    }
    static ONCE: Once = Once::new();
    type Server = Mutex<Lines<BufReader<Stdin>>>;
    struct Lazy(Cell<Option<Server>>);
    unsafe impl Sync for Lazy {}
    fn line() -> String {
        static SYNCER: Lazy = Lazy(Cell::new(None));
        ONCE.call_once(|| {
            SYNCER
                .0
                .set(Some(Mutex::new(BufReader::new(stdin()).lines())));
        });
        unsafe {
            (*SYNCER.0.as_ptr())
                .as_ref()
                .unwrap()
                .lock()
                .unwrap()
                .next()
                .unwrap()
                .unwrap()
        }
    }
}
// }}}
0