Help me!!!I've got WA(0.03),but I don't know why.(pascal)
program t1039;
type
arr=array[1..2]of integer;
var
max:longint;
cc,x,n:integer;
t:arr;
Q,happy:array[1..6000]of arr;
procedure Qsort(l,r:integer);
var
i,j:integer;
begin
i:=l; j:=r; x:=Q[(l+r)div 2,1];
repeat
while Q[i,1]<x do i:=i+1;
while Q[j,1]>x do j:=j-1;
if i<=j then begin
t:=Q[i]; Q[i]:=Q[j]; Q[j]:=t;
i:=i+1; j:=j-1;
end;
until i>j;
if i<r then Qsort(i,r);
if l<j then Qsort(l,j);
end;
procedure read_data;
var
i:integer;
begin
fillchar(happy,sizeof(happy),-1);
readln(n);
for i:=1 to n do readln(happy[i,1]);
cc:=0;
repeat
cc:=cc+1;
readln(Q[cc,2],Q[cc,1]);
until (Q[cc,1]=0)and(Q[cc,2]=0);
cc:=cc-1;
Qsort(1,cc);
end;
function search(now:integer):integer;
var
l,r,x:integer;
begin
l:=1; r:=cc; x:=0;
while l<=r do
if Q[(l+r)div 2,1]=now then begin
x:=(l+r)div 2;
break;
end else if Q[(l+r)div 2,1]>now then r:=(l+r) div 2-1
else l:=(l+r) div 2+1;
while (Q[x-1,1]=now)and(x-1>0) do x:=x-1;
search:=x;
end;
procedure recursion(now:integer);
var
x:integer;
begin
x:=search(now); happy[now,2]:=0;
while (x>0)and(Q[x,1]=now)and(x<=n) do begin
if happy[Q[x,2],2]<0 then recursion(Q[x,2]);
happy[now,1]:=happy[now,1]+happy[Q[x,2],2];
happy[now,2]:=happy[now,2]+happy[Q[x,2],1];
x:=x+1;
end;
if happy[now,1]<happy[now,2] then happy[now,1]:=happy[now,2];
if happy[now,1]>max then max:=happy[now,1];
end;
procedure doit;
var
i:integer;
begin
max:=0;
for i:=1 to n do if happy[i,2]<0 then recursion(i);
end;
procedure write_data;
begin
write(max);
end;
begin
read_data;
doit;
write_data;
end.