fn solve() { let (a, b, c): (usize, usize, usize) = (read(), read(), read()); println!("{:?}", a + b + c); let s: String = read(); println!("{:?}", s); } fn dfs( u: usize, p: usize, time_to_come: usize, adjl: &Vec>, dp: &mut Vec>, m: usize, zei: &Vec, ) { for &(v, c) in &adjl[u] { if v == p { continue; } if (time_to_come + c) * 2 <= m { dfs(v, u, time_to_come + c, adjl, dp, m, zei); // 部分木の最適解が求まったとする. // その子に行く場合 // b時間uで使う時間の最適値は求まってなさそう. for b in (0..m - c * 2 + 1).rev() { for t in 0..m - c * 2 - b + 1 { dp[u][b + 2 * c + t] = max(dp[u][b + 2 * c + t], dp[v][t] + dp[u][b]); } } } } } fn main() { let (n, m): (usize, usize) = (read::(), read::()); let zei: Vec = (0..n).map(|_| read::()).collect(); let mut adjl: Vec> = vec![vec![]; n]; for _ in 0..n - 1 { let (a, b, c): (usize, usize, usize) = (read::(), read::(), read::()); adjl[a].push((b, c)); adjl[b].push((a, c)); } // dp[i][t] ... iでt時間使って集められる税の最大値. let mut dp = vec![vec![0; m + 1]; n]; for i in 0..n { for j in 0..m + 1 { dp[i][j] = zei[i]; } } dfs(0, 0, 0, &adjl, &mut dp, m, &zei); let mut res = 0; for i in 0..m + 1 { res = max(res, dp[0][i]); } println!("{}", res); } // ========= #[allow(unused_imports)] use std::cmp::{max, min, Reverse}; #[allow(unused_imports)] use std::collections::{BinaryHeap, HashMap, HashSet}; #[allow(unused_imports)] use std::process::exit; #[allow(dead_code)] const MOD: usize = 1000000007; fn read() -> T { use std::io::Read; let stdin = std::io::stdin(); let stdin = stdin.lock(); let token: String = stdin .bytes() .map(|c| c.expect("failed to read char") as char) .skip_while(|c| c.is_whitespace()) .take_while(|c| !c.is_whitespace()) .collect(); token.parse().ok().expect("failed to parse token") } // =========