type link=^node;
node=record
data:integer;
left,right:link;
end;
var n,m:integer;
c,v:array [1..1024] of integer;
tree:link;
procedure insert(x:link;y:integer);
var p,q:link;
begin
new(q);
q^.data:=y;
q^.left:=nil;
q^.right:=nil;
if x^.left=nil then x^.left:=q
else if x^.left^.right=nil then x^.left^.right:=q
else
begin
p:=x^.left;
while p^.right<>nil do
p:=p^.right;
p^.right:=q;
end;
end;
function find(p:link;x:integer):link;
var q:link;
begin
if p=nil then exit(nil);
if p^.data=x then exit(p);
q:=find(p^.left,x);
if q=nil then find:=find(p^.right,x)
else find:=q;
end;
procedure init;
var i,x:integer;
p:link;
begin
new(p);
p^.data:=1;
p^.left:=nil;
p^.right:=nil;
tree:=p;
for i:=2 to n do
begin
read(x);
insert(find(tree,x),i);
end;
for i:=1 to n do
read(c[i]);
for i:=1 to n do
read(v[i]);
end;
function max(x,y:longint):longint;
begin
if x>y then exit(x);
exit(y);
end;
function digit(r:link;money:integer):longint;
var p:link;
ans:longint;
i:integer;
begin
if (money<0) or (r=nil) then exit(0);
p:=r;
if money>=c[r^.data] then ans:=max(0,v[r^.data])
else ans:=0;
while p<>nil do
begin
if p^.right<>nil then
for i:=0 to money do
ans:=max(ans,digit(p^.left,i)+digit(p^.right,money-i))
else ans:=max(ans,digit(p^.left,money));
p:=p^.right;
end;
digit:=ans;
end;
begin
readln(n,m);
init;
writeln(digit(tree,m));
end.
前几个点还只是小10几,但后面的为何我的输出答案都是1516??