#include<iostream> #include<cstdio> #include<algorithm> using namespace std; #define LL long long LL n,m,ans,tot; LL h[300030],ls[300030],rs[300030],dist[300030]; LL root[100030],s[100030],ss[100030],a[100030],c[100030],l[100030],head[100030]; struct edge{ LL v,next; }e[100030]; void add(LL x,LL y){ tot++; e[tot].v=y; e[tot].next=head[x]; head[x]=tot; } int merge(LL x,LL y){ if (!x || !y) return x|y; if (h[x]<h[y]) swap(x,y); rs[x]=merge(rs[x],y); if (dist[ls[x]]<dist[rs[x]]) swap(ls[x],rs[x]); dist[x]=dist[rs[x]]==0?dist[rs[x]]+1:0; return x; } void search(int x){ for (int i=head[x];i;i=e[i].next){ search(e[i].v); } LL jd=root[x]; while (s[x]>m){ ss[x]--; s[x]-=h[jd]; jd=merge(ls[jd],rs[jd]); } root[x]=jd; root[a[x]]=merge(root[a[x]],jd); s[a[x]]+=s[x]; ss[a[x]]+=ss[x]; if (ss[x]*l[x]>ans) ans=ss[x]*l[x]; } int main(){ scanf("%lld%lld",&n,&m); for (int i=1;i<=n;i++){ scanf("%lld%lld%lld",&a[i],&c[i],&l[i]); add(a[i],i); } for (int i=0;i<=n;i++){ h[i]=c[i]; ss[i]++; s[i]+=c[i]; root[i]=i; } search(0); printf("%lld\n",ans); return 0; }
|