#include <stdio.h> #include <atcoder/all> #include <bits/stdc++.h> using namespace std; using namespace atcoder; using mint = modint998244353; #define rep(i,n) for (int i = 0; i < (n); ++i) #define Inf 1000000000 void dfs(int cur,int p,vector<vector<int>> &E,vector<int> &sz){ sz[cur] = 0; rep(i,E[cur].size()){ int to = E[cur][i]; if(to==p)continue; dfs(to,cur,E,sz); sz[cur] += sz[to]; } if(sz[cur]==0)sz[cur] = 1; } int main(){ int N,K; cin>>N>>K; map<pair<int,int>,long long> mp; vector<vector<int>> E(N); rep(i,N-1){ int a,b,c; cin>>a>>b>>c; a--;b--; E[a].push_back(b); E[b].push_back(a); mp[make_pair(a,b)] = c; mp[make_pair(b,a)] = c; } vector<int> sz(N,-1); dfs(0,-1,E,sz); vector<long long> w,v; for(auto a:mp){ int x = a.first.first,y = a.first.second; if(x>y)continue; w.push_back(a.second); v.push_back((long long)a.second * min(sz[x],sz[y])); } vector<long long> dp(K+1,0LL); rep(i,w.size()){ vector<long long> ndp(K+1,0LL); rep(j,K+1){ ndp[j] = max(ndp[j],dp[j]); if(j+w[i]<=K){ ndp[j+w[i]] = max(ndp[j+w[i]],dp[j] + v[i]); } } swap(dp,ndp); } long long ans = 0LL; rep(i,K+1){ ans = max(ans,dp[i]); } for(auto a:mp){ int x = a.first.first,y = a.first.second; if(x>y)continue; long long X = a.second; ans += (long long)a.second * min(sz[x],sz[y]); } cout<<ans<<endl; return 0; }