#include using namespace std; const int mod=1e9+7; int main(){ int N,K; cin>>N>>K; vector>>g(N); for(int i=0,a,b,c;i>a>>b>>c; a--,b--; g[a].push_back({b,c}); g[b].push_back({a,c}); } queuebfs; vector>ki(N); bfs.push(0); vectordist(N,1e9),leaf; dist[0]=0; while(bfs.size()){ int t=bfs.front(); bfs.pop(); bool ha=true; for(auto i:g[t]){ if(dist[i.first]>dist[t]+i.second){ dist[i.first]=dist[t]+i.second; ki[i.first]={t,i.second,0}; ha=false; bfs.push(i.first); } } if(ha)leaf.push_back(t); } for(int i:leaf){ int t=i; while(t!=0){ get<2>(ki[t])++; t=get<0>(ki[t]); } } vectordp(K+1); for(int i=1;i=0;j--){ if(get<1>(ki[i])<=j){ dp[j]=max(dp[j],dp[j-get<1>(ki[i])]+get<1>(ki[i])*get<2>(ki[i])); } } } int ans=dp.back(); for(int i:leaf){ ans+=dist[i]; } cout<