fn main() { let (n, m): (usize, usize) = io::input(); let m = m / 2; let a = (0..n).map(|_| io::input::()).collect::>(); 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![None; m + 1]; n]; for &x in sorted.iter().rev() { dp[x][0] = Some(a[x]); for (y, c) in g[x].clone() { if m < c { continue; } for from in (0..=m - c).rev() { let Some(from_value) = dp[x][from] else { continue }; for i in 0..=m - c - from { let Some(add_value) = dp[y][i] else { continue }; let to = from + c + i; dp[x][to] = dp[x][to].max(Some(from_value + add_value)); } } } } let ans = dp[0].iter().filter_map(|x| *x).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::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() } } } // }}} // lg {{{ // https://ngtkana.github.io/ac-adapter-rs/lg/index.html #[allow(dead_code)] mod lg { use std::borrow::Borrow; use std::fmt; use std::iter::once; #[macro_export] macro_rules! lg { (@contents $head:expr $(, $tail:expr)*) => {{ $crate::__lg_internal!($head); $( eprint!(","); $crate::__lg_internal!($tail); )* eprintln!(); }}; ($($expr:expr),* $(,)?) => {{ eprint!("{}\u{276f}", line!()); $crate::lg!(@contents $($expr),*) }}; } #[doc(hidden)] #[macro_export] macro_rules! __lg_internal { ($value:expr) => {{ match $value { head => { eprint!( " {} = {}", stringify!($value), $crate::lg::__quiet(format!("{:?}", &head)) ); } } }}; } #[macro_export] macro_rules! dict { ($($values:expr),* $(,)?) => {{ #![allow(unused_assignments)] let mut dict = $crate::lg::Dict::default(); $( { let mut name = stringify!($values).to_string(); if name.starts_with("&") { name = name[1..].to_string(); } dict.add_row(name, $values); } )* eprintln!("{}", dict); }}; } #[macro_export] macro_rules! rows { { $index_label:literal, $(@offset $offset:expr,)? $(@verticalbar $verticalbar:expr,)* $($(@$label:literal =>)? $values:expr),* $(,)? } => {{ #![allow(unused_assignments)] let mut rows = $crate::lg::Rows::default(); rows.line_number(line!()); $(rows.offset($offset);)? $(rows.verticalbar($verticalbar);)* rows.index_label($index_label); $({ let mut label = stringify!($values).to_string(); if label.starts_with("&") { label = label[1..].to_string(); } $({ let label_: &'static str = $label; label = label_.to_string(); })? rows.row(label, $values); })* eprintln!("{}", rows.to_string_table()); }}; } #[macro_export] macro_rules! table { { $(@$name:literal => )? $values:expr $(,)? } => {{ #![allow(unused_assignments)] let mut name = stringify!($values).to_string(); if name.starts_with("&") { name = name[1..].to_string(); } $({ let name_: &'static str = $name; name = name_.to_string(); })? let mut rows = $crate::lg::Rows::default(); rows.line_number(line!()); rows.table_name(name); #[allow(array_into_iter)] for (i, row) in $values.into_iter().enumerate() { rows.row(i.to_string(), row); } eprintln!("{}", rows.to_string_table()); }}; } #[doc(hidden)] pub fn __quiet(s: impl AsRef) -> String { s.as_ref() .replace("340282366920938463463374607431768211455", "*") // u128 .replace("170141183460469231731687303715884105727", "*") // i128 .replace("18446744073709551615", "*") // u64 .replace("9223372036854775807", "*") // i64 .replace("-9223372036854775808", "*") // i64 .replace("4294967295", "*") // u32 .replace("2147483647", "*") // i32 .replace("-2147483648", "*") // i32 .replace("None", "*") .replace("Some", "") .replace("true", "#") .replace("false", ".") .replace(['"', '\''], "") } #[doc(hidden)] #[derive(Default)] pub struct Rows { line_number: String, index_label: String, offset: usize, verticalbars: Vec, table_name: String, rows: Vec, } impl Rows { pub fn line_number(&mut self, line_number: u32) -> &mut Self { self.line_number = format!("{}", line_number); self } pub fn index_label(&mut self, index_label: impl Into) -> &mut Self { self.index_label = index_label.into(); self } pub fn offset(&mut self, offset: usize) -> &mut Self { self.offset = offset; self } pub fn verticalbar(&mut self, verticalbar: impl IntoIterator) -> &mut Self { self.verticalbars.extend(verticalbar); self } pub fn table_name(&mut self, table_name: impl Into) -> &mut Self { self.table_name = table_name.into(); self } pub fn row( &mut self, label: impl Into, values: impl IntoIterator, ) -> &mut Self { self.rows.push(Row { label: label.into(), values: values .into_iter() .map(|value| __quiet(format!("{:?}", value))) .collect(), }); self } pub fn to_string_table(self) -> StringTable { let Self { line_number, index_label, offset, verticalbars, table_name, rows, } = self; let w = rows .iter() .map(|row| row.values.len()) .max() .unwrap_or_default(); let mut verticalbar_count = vec![0; w + 1]; for &v in &verticalbars { if (offset..=offset + w).contains(&v) { verticalbar_count[v - offset] += 1; } } StringTable { head: StringRow { label: format!( "{line_number}❯ {table_name}{index_label}", index_label = if index_label.is_empty() { String::new() } else { format!("[{}]", index_label) } ), values: (offset..offset + w) .map(|index| index.to_string()) .collect(), }, body: rows .iter() .map(|row| StringRow { label: row.label.clone(), values: row.values.clone(), }) .collect(), verticalbar_count, } } } struct Row { label: String, values: Vec, } #[doc(hidden)] pub struct StringTable { head: StringRow, body: Vec, verticalbar_count: Vec, } impl fmt::Display for StringTable { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let Self { head, body, verticalbar_count, } = self; let w = body .iter() .map(|row| row.values.len()) .max() .unwrap_or_default(); let label_width = once(head.label.chars().count()) .chain(body.iter().map(|row| row.label.chars().count())) .max() .unwrap(); let value_width = (0..w) .map(|j| { once(j.to_string().len()) .chain( body.iter() .map(|row| row.values.get(j).map_or(0, |s| s.chars().count())), ) .max() .unwrap() }) .collect::>(); // Heading gray(f)?; write!( f, "{}", head.to_string(label_width, &value_width, verticalbar_count, true) )?; resetln(f)?; // Body for row in body { write!( f, "{}", row.to_string(label_width, &value_width, verticalbar_count, false) )?; writeln!(f)?; } Ok(()) } } struct StringRow { label: String, values: Vec, } impl StringRow { fn to_string( &self, label_width: usize, value_width: &[usize], varticalbars_count: &[usize], label_align_left: bool, ) -> String { let Self { label, values } = self; let w = value_width.len(); let mut s = String::new(); s.push_str(&if label_align_left { format!("{label: { s.push_str(&format!(" {value:>value_width$}",)); } None => { s.push_str(" ".repeat(value_width + 1).as_str()); } } } s } } const GRAY: &str = "\x1b[48;2;127;127;127;37m"; const RESET: &str = "\x1b[0m"; fn gray(f: &mut fmt::Formatter<'_>) -> fmt::Result { write!(f, "{GRAY}") } fn resetln(f: &mut fmt::Formatter<'_>) -> fmt::Result { writeln!(f, "{RESET}") } pub fn bools(iter: I) -> String where B: Borrow, I: IntoIterator, { format!( "[{}]", iter.into_iter() .map(|b| ['.', '#'][usize::from(*(b.borrow()))]) .collect::(), ) } #[derive(Default)] pub struct Dict { dict: Vec<(String, Vec)>, } impl Dict { pub fn add_row( &mut self, key: impl AsRef, values: impl IntoIterator, ) { self.dict.push(( key.as_ref().to_string(), values .into_iter() .map(|value| format!("{:?}", value)) .collect(), )); } } impl std::fmt::Display for Dict { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let max_nvalues = self .dict .iter() .map(|(_, values)| values.len()) .max() .unwrap_or(0); let key_width = self .dict .iter() .map(|(key, _)| key.len()) .max() .unwrap_or(0); let mut value_widths = vec![0; max_nvalues]; for (_, values) in &self.dict { for (i, value) in values.iter().enumerate() { value_widths[i] = value_widths[i].max(value.len()); } } write!(f, "{GRAY} {key:key_width$} |", key = "")?; for (i, &value_width) in value_widths.iter().enumerate() { write!(f, " {i:value_width$} ")?; } writeln!(f, "{RESET}")?; for (key, values) in &self.dict { write!(f, " {key:key_width$} |")?; for (value, &value_width) in values.iter().zip(&value_widths) { write!(f, " {value:value_width$} ",)?; } writeln!(f)?; } Ok(()) } } } // }}}