#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int N=2010;
inline ll read(){
ll x=0;int f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
return x*f;
}
struct shaha{
int w,to,next;
}e[N<<1];
int n,k,tot=0,head[N];
int sz[N];
ll f[N][N];
inline void add(int x,int y,int z){
e[++tot].to = y,e[tot].w =z;
e[tot].next = head[x],head[x]=tot;
}
void dp(int u,int fa){
sz[u]=1;
memset(f[u],-1,sizeof(f[u]));
f[u][0]=f[u][1]=0;
for(int i=head[u];~i;i=e[i].next ){
int v=e[i].to ;
if(v==fa)continue;
dp(v,u);
sz[u]+=sz[v];
}
for(int i=head[u];i;i=e[i].next ){
int v=e[i].to ;
if(v==fa)continue;
for(int j=min(sz[u],k);j>=0;j--)
for(int p=0;p<=min(j,sz[v]);p++){
if(f[u][j-p]>=0){
ll val=(ll)e[i].w * p *(k-p)+(ll)e[i].w * (sz[v]-p) * (n-k-(sz[v]-p));
f[u][j]=max(f[u][j],f[u][j-p]+f[v][p]+val);
}
}
}
}
int main(){
memset(head,-1,sizeof(head));
n=read(),k=read();
if(n-k<k)k=n-k;
for(int i=1;i<n;i++){
int fr=read(),to=read(),dis=read();
add(fr,to,dis);
add(to,fr,dis);
}
dp(1,0);
cout<<f[1][k];
return 0;
}