一开始读错题导致各种不会做,无奈
其实是一道水题,缩点反向建图树形dp即可
type link=^point;
point=record
po:longint;
next:link;
end; var dfn,low,st,w,be,v,c:array[..] of longint;
e:array[..] of link;
fa,vis,f:array[..] of boolean;
dp:array[..,..,..] of longint;
y,s,h,t,ans,i,j,n,m,x:longint;
ch:boolean; function min(a,b:longint):longint;
begin
if a>b then exit(b) else exit(a);
end; function max(a,b:longint):longint;
begin
if a>b then exit(a) else exit(b);
end; procedure add(x,y:longint);
var p:link;
begin
new(p);
p^.po:=y;
p^.next:=e[x];
e[x]:=p;
end; procedure tarjan(x:longint);
var y,r:longint;
p:link;
begin
inc(h);
dfn[x]:=h;
low[x]:=h;
vis[x]:=true;
f[x]:=true;
inc(t);
st[t]:=x;
p:=e[x];
while p<>nil do
begin
y:=p^.po;
if not vis[y] then
begin
tarjan(y);
low[x]:=min(low[x],low[y]);
end
else if f[y] then low[x]:=min(low[x],low[y]);
p:=p^.next;
end;
if dfn[x]=low[x] then
begin
while st[t+]<>x do
begin
r:=st[t];
be[r]:=x;
f[r]:=false;
dec(t);
end;
end;
end; procedure treedp(x:longint);
var y,i,j,k:longint;
p:link;
begin
p:=e[x];
dp[x,w[x],]:=v[x];
k:=;
while p<>nil do
begin
y:=p^.po;
treedp(y);
k:=-k;
for i:= to m do
dp[x,i,k]:=dp[x,i,-k];
for i:= to m-w[x] do
if dp[y,i,]> then
begin
for j:=w[x] to m-i do
dp[x,j+i,k]:=max(dp[x,j+i,k],dp[x,j,-k]+dp[y,i,]);
end;
p:=p^.next;
end;
if k= then
for i:= to m do
dp[x,i,]:=dp[x,i,];
end; begin
readln(n,m);
for i:= to n do
read(w[i]);
for i:= to n do
read(v[i]);
for i:= to n do
begin
read(c[i]);
if c[i]<> then add(i,c[i]);
end;
for i:= to n do
if not vis[i] then
begin
h:=;
t:=;
tarjan(i);
end; for i:= to n do
begin
if be[i]<>i then
begin
inc(w[be[i]],w[i]);
inc(v[be[i]],v[i]);
end;
e[i]:=nil;
end;
for i:= to n do
if be[i]=i then
begin
if (be[c[i]]=i) or (c[i]=) then add(,i)
else add(be[c[i]],i);
end;
ans:=;
v[]:=;
w[]:=;
treedp();
for i:= to m do
ans:=max(ans,dp[,i,]);
writeln(ans);
end.