/** * @FileName a.cpp * @Author kanpurin * @Created 2021.04.23 22:40:53 **/ #include "bits/stdc++.h" using namespace std; typedef long long ll; vector>> g; vector> edge; ll ans; int dfs(int v,int p = -1) { int sum = 0; for(auto [u,c,idx] : g[v]) { if (u == p) continue; int d = dfs(u,v); sum += d; edge[idx][1] = c*d; ans += c*d; } if (sum == 0) sum++; return sum; } int main() { int n,k;cin >> n >> k; g.resize(n); edge.resize(n-1); for (int i = 0; i < n-1; i++) { int a,b,c;cin >> a >> b >> c; a--;b--; g[a].push_back({b,c,i}); g[b].push_back({a,c,i}); edge[i][0] = c; edge[i][1] = 0; } dfs(0); vector> dp(n,vector(k+1)); for (int i = 0; i < n-1; i++) { for (int j = 0; j <= k; j++) { dp[i+1][j] = dp[i][j]; if (edge[i][0] <= j) dp[i+1][j] = max(dp[i+1][j],dp[i][j-edge[i][0]]+edge[i][1]); } } cout << ans + dp[n-1][k] << endl; return 0; }