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 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
| #include<algorithm> #include<iostream> #include<iomanip> #include<cstring> #include<cstdlib> #include<vector> #include<cstdio> #include<cmath> #include<queue> using namespace std; typedef long long LL; inline const int Get_Int() { int num=0,bj=1; char x=getchar(); while(x<'0'||x>'9') { if(x=='-')bj=-1; x=getchar(); } while(x>='0'&&x<='9') { num=num*10+x-'0'; x=getchar(); } return num*bj; } const int maxn=100005; struct LeftSideTree { int n,lson[maxn],rson[maxn],dist[maxn]; LL a[maxn]; inline void init(int n,LL a[]) { this->n=n; for(int i=1; i<=n; i++)this->a[i]=a[i]; } inline int merge(int x,int y) { if(x==0||y==0)return x+y; if(a[x]<a[y])swap(x,y); rson[x]=merge(rson[x],y); if(dist[rson[x]]>dist[lson[x]])swap(lson[x],rson[x]); if(!rson[x])dist[x]=0; else dist[x]=dist[rson[x]]+1; return x; } inline int del(int x) { int tmp=merge(lson[x],rson[x]); lson[x]=rson[x]=0; return tmp; } } heap; int n,root[maxn]; LL m,sum[maxn],Cost[maxn],Size[maxn],b[maxn],ans=0; vector<int>edges[maxn]; void AddEdge(int x,int y) { edges[x].push_back(y); } void Dfs(int Now) { sum[Now]=Cost[Now]; Size[Now]=1; root[Now]=Now; for(auto& Next:edges[Now]) { Dfs(Next); Size[Now]+=Size[Next]; sum[Now]+=sum[Next]; root[Now]=heap.merge(root[Now],root[Next]); } while(sum[Now]>m) { sum[Now]-=Cost[root[Now]]; Size[Now]--; root[Now]=heap.del(root[Now]); } ans=max(ans,b[Now]*Size[Now]); } int main() { n=Get_Int(); m=Get_Int(); for(int i=1; i<=n; i++) { int f=Get_Int(); Cost[i]=Get_Int(); b[i]=Get_Int(); if(f)AddEdge(f,i); } heap.init(n,Cost); Dfs(1); printf("%lld\n",ans); return 0; }
|