# 【GDOI2018Day1模拟4.17】药香沁鼻

## 分析

（这个合并是直接合并，即Ci= max(Ai,Bi))。

## code

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
#include<vector>
#include<ctime>

using namespace std;

const int N=3003;
const int inf=0X7fffffff;
const int mod=1e9+7;
typedef long long LL;

struct arr{
int x,y;
int next;
}edge[N*4];
int edge_m;
int ls[N];
int a[N],b[N];
int f[N][2004];
int n,p;

int num[N];
int l[N],r[N];
int cnt=0;

int dfs(int x,int R)
{
num[++cnt]=x;
l[x]=cnt; r[x]=cnt;
for (int i=ls[x];i;i=edge[i].next)
{
if (edge[i].y==R) continue;
dfs(edge[i].y,x);
r[x]=max(r[x],r[edge[i].y]);
}
}

int add(int x,int y)
{
edge[++edge_m]=(arr){x,y,ls[x]},ls[x]=edge_m;
edge[++edge_m]=(arr){y,x,ls[y]},ls[y]=edge_m;
}

int main()
{
//freopen("medicine.in","r",stdin);
//freopen("medicine.out","w",stdout);
memset(f,0,sizeof(f));
scanf("%d%d",&n,&p);
for (int i=1;i<=n;i++)
{
int fa;
scanf("%d%d%d",&a[i],&fa,&b[i]);
if (fa==i) continue;
}
dfs(1,0);
for (int i=cnt;i>0;i--)
{
int k=num[i];
for (int j=p;j>=0;j--)
f[i][j]=max(f[r[k]+1][j],0);
for (int j=p;j>=a[k];j--)
f[i][j]=max(f[i][j],f[i+1][j-a[k]]+b[k]);
}
int ans=0;
for (int j=p;j>=0;j--)
ans=max(ans,f[1][j]);
printf("%d",ans);
}