1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
| #include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define repd(i,a,b) for(int i=a;i>=b;i--)
#define clr(a,b) memset(a,b,sizeof(a))
const int N=3e2+7;
const int M=N;
int n,m;
int pre[N],val[N];
int f[N][M];
vector<int> son[N];
void dp(int x){
f[x][0]=0;
for(int i=0;i<son[x].size();i++){
int y=son[x][i];
dp(y);
repd(t,m,0){
if(x!=0)
rep(j,0,t-1)
f[x][t]=max(f[x][t],f[x][t-j]+f[y][j]);
else
rep(j,0,t)
f[x][t]=max(f[x][t],f[x][t-j]+f[y][j]);
}
}
if(x){
repd(i,m,1) f[x][i]=f[x][i]+val[x];
}
}
int main(){
scanf("%d%d",&n,&m);
rep(i,1,n){
scanf("%d%d",&pre[i],&val[i]);
son[pre[i]].push_back(i);
}
dp(0);
printf("%d\n",f[0][m]);
return 0;
}
|