#include #include #include #include #include #include #include #include #include static const int MOD = 1000000007; using ll = long long; using u32 = unsigned; using u64 = unsigned long long; using namespace std; template constexpr T INF = ::numeric_limits::max() / 32 * 15 + 208; template struct REC { F f; REC(F &&f_) : f(std::forward(f_)) {} template auto operator()(Args &&... args) const { return f(*this, std::forward(args)...); } }; int main() { int n, k; cin >> n >> k; vector>> G(n); for (int i = 0; i < n-1; ++i) { int a, b, c; scanf("%d %d %d", &a, &b, &c); a--; b--; G[a].emplace_back(b, c); G[b].emplace_back(a, c); } vector cnt(n, 0), val(n); int sum = 0; REC([&](auto &&f, int x, int p, int d) -> void { int leaf = 1; for (auto &&[y, c] : G[x]) { if(y == p) continue; val[y] = c; f(y, x, d+c); cnt[x] += cnt[y]; leaf = 0; } if(leaf) cnt[x]++, sum += d; })(0, -1, 0); vector dp(k+1, -INF); dp[0] = 0; for (int i = 1; i < n; ++i) { ll c = val[i], v = cnt[i]*val[i]; for (ll j = k-c; j >= 0; --j) { dp[j+c] = max(dp[j+c], dp[j]+v); } } cout << sum+*max_element(dp.begin(),dp.end()) << "\n"; return 0; }