最大的bug就是这里
for(int jj=1;jj<=num[rt];jj++){
tem[jj]=dp[rt][jj];
}
for(int j=1;j<=num[rt];j++){
for(int k=0;k<=j;k++){
//dp[i][j]=max(dp[i][j],dp[i][j-k]+dp[son][k]-edge[i].w );
tem[j]=max(tem[j],dp[rt][j-k]+dp[son][k]-edge[i].w );
}
}
for(int jj=1;jj<=num[rt];jj++){
dp[rt][jj]=tem[jj];
}
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
#include<cmath>
#include<iostream>
using namespace std;
typedef int ll;
const ll inf=0x3f3f3f3f;
const long long maxn=3010;
int n,m;
int head[maxn];
int tot;
void init(){
memset(head,-1,sizeof(head));
tot=0;
}
struct Edge{
int to,next,w;
}edge[maxn];
void addedge(int u,int v,int w){
edge[tot].to=v; edge[tot].w=w; edge[tot].next=head[u]; head[u]=tot++;
}
ll v[maxn];
ll dp[maxn][maxn];
ll num[maxn];
ll tem[maxn];
void dfs(int rt){
num[rt]=0;
for(int i=head[rt];i!=-1;i=edge[i].next){
int son=edge[i].to;
dfs(son);
num[rt]+=num[son];
for(int jj=1;jj<=num[rt];jj++){
tem[jj]=dp[rt][jj];
}
for(int j=1;j<=num[rt];j++){
for(int k=0;k<=j;k++){
//dp[i][j]=max(dp[i][j],dp[i][j-k]+dp[son][k]-edge[i].w );
tem[j]=max(tem[j],dp[rt][j-k]+dp[son][k]-edge[i].w );
}
}
for(int jj=1;jj<=num[rt];jj++){
dp[rt][jj]=tem[jj];
}
}
if(num[rt]==0){
dp[rt][1]=v[rt];
}
num[rt]=num[rt]+1;
}
int main()
{
// freopen("D://in.txt","r",stdin);
while(scanf("%d%d",&n,&m)!=EOF){
init();
for(int i=0;i<n-m;i++){
int k,to,w;
scanf("%d",&k);
for(int j=0;j<k;j++){
scanf("%d%d",&to,&w);
addedge(i+1,to,w);
}
}
memset(v,0,sizeof(v));
for(int i=0;i<m;i++){
scanf("%d",&v[n-m+i+1]);
}
for(int i=0;i<maxn;i++){
for(int j=0;j<maxn;j++)
dp[i][j]=-inf;
dp[i][0]=0;
}
memset(num,0,sizeof(num));
dfs(1);
int x=0;
for(int i=m;i>=0;i--){
if(dp[1][i]>=0){
x=i;break;
}
}
printf("%d\n",x);
}
return 0;
}