use std::collections::HashMap; fn main() { let (n, m): (usize, usize) = io::input(); let m = m / 2; let v = (0..n).map(|_| io::input::()).collect::>(); let mut g = vec![Vec::new(); n]; let mut map = HashMap::new(); for _ in 0..n - 1 { let (i, j, c): (usize, usize, usize) = io::input(); g[i].push(j); g[j].push(i); map.insert((i.min(j), i.max(j)), c); } let mut w = vec![0; n]; let mut stack = vec![0]; let mut parent = vec![usize::MAX; n]; while let Some(i) = stack.pop() { g[i].retain(|&j| parent[i] != j); for &j in &g[i] { parent[j] = i; w[j] = map[&(i.min(j), i.max(j))]; stack.push(j); } } let mut dp = vec![None; m + 1]; dp[0] = Some(0); let dp = dfs(0, &g, &w, &v, dp); let ans = dp.iter().max().unwrap().unwrap(); println!("{}", ans); } fn dfs( x: usize, g: &[Vec], w: &[usize], v: &[u64], mut dp: Vec>, ) -> Vec> { let mut next = vec![None; dp.len()]; if w[x] < dp.len() { for i in (0..dp.len() - w[x]).rev() { if let Some(d) = dp[i] { next[i + w[x]] = Some(d + v[x]); } } } for &y in &g[x] { next = dfs(y, g, w, v, next); } for (dp, next) in dp.iter_mut().zip(next.iter()) { *dp = (*dp).max(*next); } dp } // 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::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 ParseLine for Vec { fn parse_line(s: &str) -> Self { s.split_whitespace().map(T::parse_line).collect() } } static ONCE: Once = Once::new(); type Server = Mutex>>; struct Lazy(Cell>); 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() } } } // }}}