1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 const int maxn=107; 6 int map[maxn][maxn],l[maxn],r[maxn],a[maxn],f[maxn][maxn]; 7 int n,q; 8 void buildtree(int u){ 9 for(int i=1;i<=n;i++){ 10 if(map[u][i]!=-1){ 11 l[u]=i;a[i]=map[u][i]; 12 map[u][i]=-1;map[i][u]=-1; 13 buildtree(i); 14 break; 15 } 16 } 17 for(int i=1;i<=n;i++){ 18 if(map[u][i]!=-1){ 19 r[u]=i;a[i]=map[u][i]; 20 map[u][i]=-1;map[i][u]=-1; 21 buildtree(i); 22 break; 23 } 24 } 25 } 26 int DP(int i,int j){ 27 if(j==0) return f[i][j]=0; 28 if(l[i]==0&&r[i]==0) return f[i][j]=a[i]; 29 if(f[i][j]>0) return f[i][j]; 30 for(int k=0;k<=j-1;k++) 31 f[i][j]=max(f[i][j],DP(l[i],k)+DP(r[i],j-k-1)+a[i]); 32 return f[i][j]; 33 } 34 int main(){ 35 cin>>n>>q; 36 memset(map,-1,sizeof(map)); 37 for(int i=1;i<n;i++){ 38 int u,v,w;cin>>u>>v>>w; 39 map[u][v]=w;map[v][u]=w; 40 } 41 buildtree(1); 42 int ans=DP(1,q+1); 43 cout<<ans<<endl; 44 return 0; 45 }