#include using namespace std; using int128 = __int128_t; #define rep(i, n) for (int i = 0; i < n; ++i) #define reps(i, n, s) for (int i = 0; i < n; i += s) #define repl(i, l, r) for (int i = l; i < r; ++i) #define repsl(i, l, r, s) for (int i = l; i < r; i += s) #define all(iterable) (iterable).begin(), (iterable).end() constexpr int dx4[4] = {1, 0, -1, 0}; constexpr int dy4[4] = {0, 1, 0, -1}; constexpr int dx8[8] = {1, 1, 0, -1, -1, -1, 0, 1}; constexpr int dy8[8] = {0, 1, 1, 1, 0, -1, -1, -1}; template void print(const vector& vec, const string sep = " ", const string end = "\n") { int n = vec.size(); rep(i, n) { cout << vec[i]; if (i < n - 1) cout << sep; else cout << end; } } template void read(vector& a, int begin_index, int length) { if (a.size() < begin_index + length) a.resize(begin_index + length); while (length --> 0) cin >> a[begin_index++]; } template void read(vector& a) { read(a, 0, a.size()); } using tree = vector>>; int main() { int n, k; cin >> n >> k; tree t(n); rep(i, n - 1) { int u, v, c; cin >> u >> v >> c; --u, --v; t[u].push_back({v, c}); t[v].push_back({u, c}); } long long ans = 0; vector val(n, 0), prd(n, 0); auto dfs = [&](auto self, int u, int p, int c) -> int { val[u] = c; int leaves = 0; for (auto &[v, c] : t[u]) { if (v == p) continue; leaves += self(self, v, u, c); } if (leaves == 0) ++leaves; ans += prd[u] = (long long) val[u] * leaves; return leaves; }; dfs(dfs, 0, -1, 0); vector dp(k + 1, 0); repl(i, 1, n) repl(j, val[i], k + 1) { dp[j] = max(dp[j], dp[j - val[i]] + prd[i]); } ans += *max_element(all(dp)); cout << ans << '\n'; return 0; }