#include using namespace std; using ll = long long; ll N, K; vector>> E; vector subtree; vector> dp; vector> D; ll subtree_size(ll from, ll p){ ll cnt = 0; for (auto [to, C] : E[from]){ if (to == p) continue; cnt += subtree_size(to, from); D.push_back({C, C*subtree[to]}); } if (cnt == 0) cnt = 1; return subtree[from] = cnt; } int main(){ ll A, B, C, x, y, ans=0; cin >> N >> K; E.resize(N); subtree.resize(N); for (int i=0; i < N-1; i++){ cin >> A >> B >> C; A--; B--; E[A].push_back({B, C}); E[B].push_back({A, C}); } subtree_size(0, -1); vector> dp(N, vector(K+1)); for (int i=1; i<=N-1; i++){ tie(x, y) = D[i-1]; ans += y; for (int j=0; j<=K; j++){ if (j-x>=0) dp[i][j] = max(dp[i][j], dp[i-1][j-x]+y); dp[i][j] = max(dp[i][j], dp[i-1][j]); } } cout << ans + dp[N-1][K] << endl; return 0; }