可以先穷举那个是管理者,然后就发现
其实就是求每个子树选尽可能多的人,使薪水和小于m
这显然是从小往大选,可以用启发式合并
但是用主席树写的更简单一点吧,dfs序之后
每课线段树不仅维护出现出现个数,然后在维护一个区间和(未离散化之前的)
然后类似查找第k大就可以解决了

 type node=record
po,next:longint;
end;
link=record
l,r,s:longint;
sum:int64;
end; var tree:array[..*] of link;
v,a,b,c,e,sa,rank,h,p:array[..] of longint;
w:array[..] of node;
n,m,t,tot,len,x,root,i,s:longint;
ans:int64; function max(a,b:int64):int64;
begin
if a>b then exit(a) else exit(b);
end; procedure swap(var a,b:longint);
var c:longint;
begin
c:=a;
a:=b;
b:=c;
end; procedure sort(l,r: longint);
var i,j,x:longint;
begin
i:=l;
j:=r;
x:=a[(l+r) div ];
repeat
while (a[i]<x) do inc(i);
while (x<a[j]) do dec(j);
if not(i>j) then
begin
swap(a[i],a[j]);
swap(c[i],c[j]);
inc(i);
j:=j-;
end;
until i>j;
if l<j then sort(l,j);
if i<r then sort(i,r);
end; procedure add(x,y:longint);
begin
inc(len);
w[len].po:=y;
w[len].next:=p[x];
p[x]:=len;
end; procedure dfs(x:longint);
var i,y:longint;
begin
i:=p[x];
inc(len);
a[len]:=x;
c[x]:=len;
while i<> do
begin
dfs(w[i].po);
i:=w[i].next;
end;
e[x]:=len;
end; function build(l,r:longint):longint;
var m,q:longint;
begin
inc(t);
if l=r then exit(t)
else begin
q:=t;
m:=(l+r) shr ;
tree[q].l:=build(l,m);
tree[q].r:=build(m+,r);
exit(q);
end;
end; function add(l,r,last,x,y:longint):longint;
var m,q:longint;
begin
inc(t);
if l=r then
begin
tree[t].s:=tree[last].s+;
tree[t].sum:=tree[last].sum+y;
exit(t);
end
else begin
q:=t;
m:=(l+r) shr ;
if x<=m then
begin
tree[q].r:=tree[last].r;
tree[q].l:=add(l,m,tree[last].l,x,y);
end
else begin
tree[q].l:=tree[last].l;
tree[q].r:=add(m+,r,tree[last].r,x,y);
end;
tree[q].sum:=tree[tree[q].l].sum+tree[tree[q].r].sum;
tree[q].s:=tree[tree[q].l].s+tree[tree[q].r].s;
exit(q);
end;
end; function ask(l,r,a,b,k:longint):longint;
var m,s:longint;
p:int64; begin
if l=r then
begin
p:=tree[b].sum-tree[a].sum;
if k>=p then exit(tree[b].s-tree[a].s) //这里要注意下
else exit(k div sa[l]);
end
else begin
m:=(l+r) shr ;
p:=tree[tree[b].l].sum-tree[tree[a].l].sum;
if p>k then
exit(ask(l,m,tree[a].l,tree[b].l,k))
else begin
s:=tree[tree[b].l].s-tree[tree[a].l].s;
exit(s+ask(m+,r,tree[a].r,tree[b].r,k-p));
end;
end;
end; begin
readln(n,m);
for i:= to n do
begin
readln(x,b[i],v[i]);
if x= then root:=i;
add(x,i);
end;
for i:= to n do
begin
a[i]:=b[i];
c[i]:=i;
end;
sort(,n);
tot:=;
rank[c[]]:=;
sa[]:=a[];
for i:= to n do
begin
if a[i]<>a[i-] then
begin
inc(tot);
sa[tot]:=a[i];
end;
rank[c[i]]:=tot;
end;
len:=;
dfs(root);
h[]:=build(,tot);
for i:= to n do
h[i]:=add(,tot,h[i-],rank[a[i]],b[a[i]]);
for i:= to n do
begin
s:=ask(,tot,h[c[i]-],h[e[i]],m);
ans:=max(ans,int64(v[i])*int64(s));
end;
writeln(ans);
end.
05-08 15:24