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 85 86 87 88
| #include <cstdio> #include <cctype> #include <cstring> #include <vector> #include <algorithm> using namespace std; typedef long long LL; const int Mn(100500);
template<typename T> void qrd(T& x) { x = 0; char c = getchar(); while(!isdigit(c)) { c = getchar(); } while(isdigit(c)) { x = x*10 + c-'0'; c = getchar(); } }
int n,m;
vector<int> tsn[Mn]; int c[Mn], l[Mn];
int hrt[Mn], sz[Mn], sn[Mn][2], fa[Mn]; int d[Mn]; LL sum[Mn]; int getr(int x) { return fa[x]==0 ? x : fa[x] = getr(fa[x]); } int merge(int x,int y) { if(!y) { return x; } if(!x) { return y; } if(c[x]<c[y]) { swap(x,y); } sn[x][1] = merge(sn[x][1],y); fa[sn[x][1]] = x; if(d[sn[x][0]]<d[sn[x][1]]) { swap(sn[x][0],sn[x][1]); } d[x] = d[sn[x][1]] + 1; return x; } int pop(int x) { fa[sn[x][0]] = fa[sn[x][1]] = 0; int nrt = merge(sn[x][0],sn[x][1]); fa[x] = nrt; return nrt; }
LL ans(0); void dfs(int x) { for(int a: tsn[x]) { dfs(a); sz[x] += sz[a]; sum[x] += sum[a]; hrt[x] = merge(hrt[x],hrt[a]); } while(sum[x]>m) { --sz[x]; sum[x] -= c[hrt[x]]; hrt[x] = pop(hrt[x]); } ans = max(ans,1ll * sz[x] * l[x]); }
int main() { qrd(n), qrd(m); for(int i(1);i<=n;++i) { int b; qrd(b), qrd(c[i]), qrd(l[i]); tsn[b].push_back(i); hrt[i] = i, sum[i] = c[i], sz[i] = 1; } dfs(1); printf("%lld",ans); return 0; }
|