#ifndef LOCAL #include using namespace std; #define debug(...) (void(0)) #else #include "algo/debug.h" #endif namespace std { template class y_combinator_result { Fun fun_; public: template explicit y_combinator_result(T &&fun) : fun_(std::forward(fun)) {} template decltype(auto) operator()(Args &&...args) { return fun_(std::ref(*this), std::forward(args)...); } }; template decltype(auto) y_combinator(Fun &&fun) { return y_combinator_result>(std::forward(fun)); } } // namespace std void solve() { int N, M; cin >> N >> M; vector U(N); for (int i = 0; i < N; i++) cin >> U[i]; vector>> G(N); for (int i = 1; i < N; i++) { int a, b, c; cin >> a >> b >> c; G[a].push_back({b, c}); G[b].push_back({a, c}); } const int64_t linf = 1e18; vector> dp(N, vector(M + 1, -linf)); y_combinator([&](auto self, int v, int p) -> void { dp[v][0] = U[v]; for (const auto &[to, cst] : G[v]) if(to != p) { self(to, v); for(int i = M; i >= 0; i--) if(dp[v][i] != -linf) { for(int j = 0; j <= M; j++) if(dp[to][j] != -linf){ int t = 2 * cst + j + i; if(t <= M) dp[v][t] = max(dp[v][t], dp[to][j] + dp[v][i]); } } } })(0, -1); int64_t ans = -linf; for(int i = 0; i <= M; i++) ans = max(ans, dp[0][i]); cout << ans << endl; } int main() { std::ios::sync_with_stdio(false); std::cin.tie(nullptr); int tt = 1; // std::cin >> tt; while (tt--) { solve(); } }