#include #include "atcoder/all" #define debug cout << "OK" << endl; template inline bool chmax(T& a, const T b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(T& a, const T b) { if (a > b) { a = b; return true; } return false; } inline int Code(char c) { if ('A' <= c and c <= 'Z')return (int)(c - 'A'); if ('a' <= c and c <= 'z')return (int)(c - 'a'); if ('0' <= c and c <= '9')return (int)(c - '0'); assert(false); } using namespace std; using namespace atcoder; #define int long long constexpr int MOD = 998244353; constexpr int INF = (1LL << 62); using minit = modint998244353; template ostream& operator << (ostream& st, const vector &v) { for (const T value : v) { st << value << " "; } return st; } template istream& operator >> (istream& st, vector& v) { for (T &value : v) { st >> value; } return st; } // typedef struct { int to, cost; }edge; // void solve() { //#1 入力 int N, M; cin >> N >> M; vector U(N); cin >> U; vector> G(N); for (int i = 0; i < N - 1; i++) { int u, v, c; cin >> u >> v >> c; G[u].push_back({ v,c }); G[v].push_back({ u,c }); } //#2 再帰的に答えを求める function(int, int)> dfs = [&](int now, int par) { //頂点nowから探索を(子方向に)始めてちょうど時間iで徴収できる最大税 vector res(M + 1, -1); res[0] = U[now]; for (const edge e : G[now]) { if (e.to == par)continue; vector prev = dfs(e.to, now); //cout << "edge " << now << "to " << e.to << "cost is " << e.cost << endl; for (int i = M; i >= 0; i--) { if (res[i] == -1)continue; for (int j = 0; j <= M; j++) { if (prev[j] == -1)continue; if (M < i + j + e.cost * 2)continue; chmax(res[i + j + e.cost * 2], res[i] + prev[j]); } } } return res; }; //#3 出力 vector ans = dfs(0, -1); cout << *max_element(ans.begin(), ans.end()) << endl; } signed main() { int T = 1; // cin >> T; for (int i = 0; i < T; i++) solve(); return 0; }