传送门
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <cstdlib>
using namespace std;
#define ll long long
#define re register
inline void read(int &a)
{
a=0;
int d=1;
char ch;
while(ch=getchar(),ch>'9'||ch<'0')
if(ch=='-')
d=-1;
a=ch^48;
while(ch=getchar(),ch>='0'&&ch<='9')
a=(a<<3)+(a<<1)+(ch^48);
a*=d;
}
struct note
{
int v,next,dis;
}edge[250];
int head[105],num,f[105][105],m;
inline void add(int u,int v,int w)
{
edge[++num].next=head[u];
edge[num].v=v;
edge[num].dis=w;
head[u]=num;
}
inline void dfs(int u,int fa)
{
for(re int i=head[u];i;i=edge[i].next)
{
if(edge[i].v!=fa)
{
dfs(edge[i].v,u);
for(re int j=m;j>0;j--)
for(re int k=0;k<j;k++)
f[u][j]=max(f[u][j],f[u][j-k-1]+edge[i].dis+f[edge[i].v][k]);
}
}
}
int main()
{
int n;
read(n);
read(m);
for(re int i=1;i<n;i++)
{
int u,v,w;
read(u);
read(v);
read(w);
add(u,v,w);
add(v,u,w);
}
dfs(1,0);
int ans=f[1][m];
printf("%d",ans);
return 0;
}