#include<bits/stdc++.h>
#define Max(x,y) (x>y?x:y)
#define Min(x,y) (x<y?x:y)
using namespace std;
const int N=100010,M=1000010,INF=0x3f3f3f3f;
int head[N],ver[M],edge[M],Next[M],tot;
int n,m,st,et,ans=INF,d1[N],d2[N],d_e[N],v_c[N],v_e[N],Max_ans[M],cnt;
void add(int x,int y,int z){
ver[++tot]=y,edge[tot]=z,Next[tot]=head[x],head[x]=tot;
}
void dfs1(int x,int fa){
for(int i=head[x];i;i=Next[i]){
int y=ver[i],z=edge[i];
if(y==fa||v_e[y])continue;
d1[y]=d1[x]+z;
if(d1[y]>=d1[st])st=y;
dfs1(y,x);
}
}
void dfs2(int x,int fa){
for(int i=head[x];i;i=Next[i]){
int y=ver[i],z=edge[i];
if(y==fa)continue;
d2[y]=d2[x]+z;
if(d2[y]>=d2[et])et=y;
dfs2(y,x);
}
}
bool dfs_c(int x){
v_c[x]=1;
if(x==et)return 1;
for(int i=head[x];i;i=Next[i]){
int y=ver[i];
if(v_c[y])continue;
if(dfs_c(y))return 1;
}
v_c[x]=0;
return 0;
}
void dfs_e(int x,int fa){
// cout<<x<<" ";
for(int i=head[x];i;i=Next[i]){
int y=ver[i],z=edge[i];
if(!v_c[y]||y==fa)continue;
d_e[y]=d_e[x]+z;
if(d_e[y]>=d_e[et]&&d_e[y]<=m)et=y;
dfs_e(y,x);
}
}
bool dfs_ec(int x){
v_e[x]=1;
if(x==et)return 1;
for(int i=head[x];i;i=Next[i]){
int y=ver[i];
if(v_e[y])continue;
if(dfs_ec(y))return 1;
}
v_e[x]=0;
return 0;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<n;i++){
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
add(x,y,z),add(y,x,z);
}
st=et=1;
dfs1(1,0);
dfs2(st,0);
dfs_c(st);
// cout<<st<<" "<<et<<"%\n";
// for(int i=1;i<=n;i++)
// if(v_c[i])cout<<i<<" ";
// puts("");
for(int i=1;i<=n;i++){
if(!v_c[i])continue;
memset(d_e,0,sizeof(d_e));
memset(v_e,0,sizeof(v_e));
// cout<<"\n"<<i<<":\n";
et=i;++cnt;
dfs_e(i,0);
dfs_ec(i);
// cout<<"#"<<i<<"#"<<et<<"#"<<d_e[et]<<"#\n";
for(int j=1;j<=n;j++){
if(!v_e[j])continue;
memset(d1,0,sizeof(d1));
st=j;dfs1(j,0);
// cout<<j<<" "<<st<<" "<<d1[st]<<"@\n";
Max_ans[cnt]=Max(Max_ans[cnt],d1[st]);
}
// cout<<Max_ans[cnt]<<"M\n";
}
for(int i=1;i<=cnt;i++)
ans=Min(ans,Max_ans[i]);//cout<<Max_ans[i]<<endl;
printf("%d\n",ans);
return 0;
}
求助…调了一天了都没找出问题