fn main() { let nm = read_ints(); let n = nm[0]; let m = nm[1]; let mut u = vec![0; n]; for i in 0..n { let ui = read_int(); u[i] = ui; } let mut g = vec![vec![]; n]; for _ in 0..n - 1 { let abc = read_ints(); g[abc[0]].push((abc[1], abc[2])); g[abc[1]].push((abc[0], abc[2])); } let mut dp = vec![vec![0; m + 1]; n]; dfs(&g, m, &u, 0, n, &mut dp); let ans = dp[0][m]; println!("{}", ans); } fn dfs( g: &Vec>, m: usize, u: &Vec, crr: usize, prv: usize, dp: &mut Vec>, ) { for &(nxt, _) in g[crr].iter() { if nxt != prv { dfs(g, m, u, nxt, crr, dp); } } for &(nxt, t) in g[crr].iter() { for i in (0..=m).rev() { for j in (0..).take_while(|&j| i >= 2 * t + j) { dp[crr][i] = dp[crr][i].max(dp[crr][i - (2 * t + j)] + dp[nxt][j]); } } } for i in 0..=m { dp[crr][i] += u[crr]; } } use std::io; fn read_ints() -> Vec { let mut buf = String::new(); io::stdin().read_line(&mut buf).unwrap(); buf.trim() .split_whitespace() .map(|s| s.parse().unwrap()) .collect() } fn read_int() -> usize { let mut buffer = String::new(); io::stdin().read_line(&mut buffer).unwrap(); buffer.trim().parse().unwrap() }