80分求调QAQ
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<LL,LL> PII;
LL n,k,a[105],dp[105][105][50][2],stk[105],tot,siz[105];
vector<PII>g[105];
void dfs(LL u){
stk[++tot]=u;
for(LL i=0;i<g[u].size();i++){
LL v=g[u][i].second,w=g[u][i].first;
siz[v]=siz[u]+w;
dfs(v);
for(LL t=1;t<=tot;t++){
for(LL j=k;j>=0;j--){
dp[u][stk[t]][j][0]+=dp[v][stk[t]][0][0];
dp[u][stk[t]][j][1]+=dp[v][u][0][0];
for(LL l=1;l<=j;l++){
dp[u][stk[t]][j][0]=min(dp[u][stk[t]][j][0],dp[v][stk[t]][l][0]+dp[u][stk[t]][j-l][0]);
dp[u][stk[t]][j][1]=min(dp[u][stk[t]][j][1],dp[v][u][l][0]+dp[u][stk[t]][j-l][1]);
}
}
}
}
for(LL t=1;t<=tot;t++){
for(LL j=0;j<=k;j++){
if(j) dp[u][stk[t]][j][0]=min(dp[u][stk[t]][j][0]+a[u]*(siz[u]-siz[stk[t]]),dp[u][stk[t]][j-1][1]);
else dp[u][stk[t]][j][0]+=a[u]*(siz[u]-siz[stk[t]]);
}
}
tot--;
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);cout.tie(0);
cin>>n>>k;
for(LL u=1,v,d;u<=n;u++){
cin>>a[u]>>v>>d;
g[v].push_back({d,u});
}
dfs(0);
cout<<dp[0][0][k][0];
return 0;
}
/*
1 1 10 1
1 0 1
2 1 10
3 2 5
4 2 3
*/