var
f:array[0..6000,0..1]of longint;
l,r,a:array[0..6000]of longint;
v:array[0..6000]of boolean;
n,ans:longint;
function max(a,b:longint):longint;
begin if a>b then exit(a) else exit(b);end;
function dp(x,y:longint):longint;
begin
if x=0 then exit(0);
if f[x,y]<>maxlongint then exit(f[x,y]);
if y=1 then f[x,y]:=dp(l[x],0)+a[x]+max(dp(r[x],1),dp(r[x],0)) else
f[x,y]:=max(dp(l[x],1),dp(l[x],0))+dp(r[x],0);
exit(f[x,y]);
end;
procedure init;
var i,x,b:longint;
begin
readln(n);ans:=0;
for i:=1 to n do begin f[i,1]:=maxlongint;f[i,0]:=maxlongint;read(a[i]);end;
while 1<>0 do
begin
read(x,b);
if (x=0)and(b=0) then break;
if l[b]=0 then l[b]:=x else
begin
r[x]:=l[b];
l[b]:=x;
end;
v[x]:=true;
end;
for i:=1 to n do if not v[i] then inc(ans,max(dp(i,1),dp(i,0)));
end;
begin
init;
writeln(ans);
end.
TYVJ 1052 没有上司的舞会
最新推荐文章于 2019-05-09 21:04:08 发布