#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); Graph t(N); function haineken=[&](int pre,int cur){ for(auto &e:g[cur])if(pre!=e){ t.add_edge(e.from,e.to,e.w*leaves[e]); haineken(cur,e); } }; haineken(-1,0); ll res=0; rep(st,N){ vector dis(N),add(N); dis[st]=0; add[st]=0; fill(-1,st,g,dis); fill(-1,st,t,add); rep(gl,N){ if(dis[gl]<=K) chmax(res,add[gl]); } } ll ans=0; rep(i,N){ for(auto &e:t[i]) ans+=e.w; } ans/=2; ans+=res; cout<