import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop; alias Tuple!(int, "to", int, "cost") Edge; void main() { auto s = readln.split.map!(to!int); auto N = s[0]; auto M = s[1]; auto U = N.iota.map!(_ => readln.chomp.to!int).array; auto edges = new Edge[][](N); foreach (i; 0..N-1) { s = readln.split.map!(to!int); edges[s[0]] ~= Edge(s[1], s[2]); edges[s[1]] ~= Edge(s[0], s[2]); } auto dp = new int[][](N, M + 1); void dfs(int n, int prev) { foreach (m; edges[n]) if (m.to != prev) dfs(m.to, n); auto tmp = new int[][](2, M + 1); int cur = 0; int tar = 1; foreach (m; edges[n]) { if (m.to == prev) continue; foreach (i; 0..M+1) { int ii = i + m.cost * 2; foreach (j; 0..M+1) { if (ii + j > M) break; tmp[tar][ii+j] = max(tmp[cur][ii+j], dp[m.to][i] + tmp[cur][j]); } } fill(tmp[cur], 0); cur ^= 1; tar ^= 1; } //if (n == 0) tmp[cur].writeln; foreach (i; 0..M+1) dp[n][i] = tmp[cur][i] + U[n]; } dfs(0, -1); dp[0].reduce!max.writeln; }