#include using namespace std; #define ALL(x) begin(x),end(x) #define rep(i,n) for(int i=0;i<(n);i++) #define debug(v) cout<<#v<<":";for(auto x:v){cout<bool chmax(T &a,const T &b){if(abool chmin(T &a,const T &b){if(b ostream &operator<<(ostream &os,const vector&v){ for(int i=0;i<(int)v.size();i++) os< istream &operator>>(istream &is,vector&v){ for(T &x:v)is>>x; return is; } // graph template // ref : https://ei1333.github.io/library/graph/graph-template.cpp template struct Edge{ int from,to; T w; int idx; Edge()=default; Edge(int from,int to,T w=1,int idx=-1):from(from),to(to),w(w),idx(idx){} operator int() const{return to;} }; template struct Graph{ vector>> g; int V,E; Graph()=default; Graph(int n):g(n),V(n),E(0){} size_t size(){ return g.size(); } void resize(int k){ g.resize(k); } inline const vector> &operator[](int k)const{ return (g.at(k)); } inline vector> &operator[](int k){ return (g.at(k)); } void add_directed_edge(int from,int to,T cost=1){ g[from].emplace_back(from,to,cost,E++); } void add_edge(int from,int to,T cost=1){ g[from].emplace_back(from,to,cost,E); g[to].emplace_back(to,from,cost,E++); } void read(int m,int pad=-1,bool weighted=false,bool directed=false){ for(int i=0;i>u>>v; u+=pad,v+=pad; T w=T(1); if(weighted) cin>>w; if(directed) add_directed_edge(u,v,w); else add_edge(u,v,w); } } }; void fill(int pre,int cur,Graph &G,vector &dis){ for(auto &e:G[cur])if(pre!=e){ dis[e]=dis[cur]+e.w; fill(cur,e,G,dis); } } signed main(){ int N,K;cin>>N>>K; Graph g(N); rep(i,N-1){ int u,v;ll c;cin>>u>>v>>c;u--,v--; g.add_edge(u,v,c); } vector leaves(N,0); function budweiser=[&](int pre,int cur){ if(pre!=-1 and g[cur].size()==1) leaves[cur]++; for(auto &e:g[cur])if(pre!=e){ budweiser(cur,e); leaves[cur]+=leaves[e]; } }; budweiser(-1,0); vector> VW; function haineken=[&](int pre,int cur){ for(auto &e:g[cur])if(pre!=e){ VW.emplace_back(e.w*leaves[e],e.w); haineken(cur,e); } }; haineken(-1,0); vector dp(K+1,0); for(auto [v,w]:VW){ for(int i=K;i>=0;i--){ if(i+w<=K) chmax(dp[i+w],dp[i]+v); } } ll ans=0; for(auto [v,w]:VW) ans+=v; cout<