Cod sursa(job #43366)

Utilizator info_arrandrei gigea info_arr Data 29 martie 2007 23:47:05
Problema Asmax Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.26 kb
Program asmax;
const MAXN=16000;
filein='asmax.in';
fileout='asmax.out';
type longarray=array[1..MAXN] of longint;
plongarray=^longarray;
plist=^list;
list=record
next:integer;
urm:plist;
end;
var edge:array[1..MAXN] of plist;
v,max,tata:plongarray;
n:integer;
procedure readinput;
var i,j,k:integer;
aux:plist;
begin
assign(input,filein);
reset(input);
read(n);
new(v);
for i:=1 to n do
begin
read(v^[i]);
edge[i]:=nil;
end;
for k:=1 to n-1 do
begin
read(i,j);
new(aux);
aux^.next:=j;
aux^.urm:=edge[i];
edge[i]:=aux;
new(aux);
aux^.next:=i;
aux^.urm:=edge[j];
edge[j]:=aux;
end;
close(input);
end;
procedure compute(nod:integer);
var aa:plist;
i:integer;
begin
max^[nod]:=v^[nod];
aa:=edge[nod];
while (aa<>nil) do
begin
i:=aa^.next;
if (i<>tata^[nod]) then
begin
tata^[i]:=nod;
compute(i);
if (max^[i]>0) then
max^[nod]:=max^[nod]+max^[i];
end;
aa:=aa^.urm;
end;
end;
procedure print;
var maxim:longint;
i:integer;
begin
maxim:=max^[1];
for i:=2 to n do
if (max^[i]>maxim) then
maxim:=max^[i];
assign(output,fileout);
rewrite(output);
writeln(maxim);
close(output);
end;
begin
readinput;
new(tata);
fillchar(tata^,sizeof(longarray),0);
new(max);
fillchar(max^,sizeof(longarray),0);
compute(1);
print;
end.