#![allow(unused_imports)] // use itertools::Itertools; // use proconio::input; // use proconio::marker::*; use std::cmp::Reverse; use std::collections::*; 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]; f(&g, &u, m, 0, n, &mut dp); let ans = dp[0][m]; println!("{}", ans); } fn f( g: &Vec>, u: &Vec, m: usize, crr: usize, prv: usize, dp: &mut Vec>, ) { for &(nxt, _) in g[crr].iter() { if prv == nxt { continue; } f(g, u, m, nxt, crr, dp); } for &(nxt, cost) in g[crr].iter() { for s in (0..=m).rev() { for t in (0..=m).take_while(|&t| s >= t + 2 * cost) { dp[crr][s] = dp[crr][s].max(dp[nxt][t] + dp[crr][s - (t + 2 * cost)]) } } } for t in 0..=m { dp[crr][t] += 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() }