Cod sursa(job #1331902)

Utilizator DrumeaVDrumea Vasile DrumeaV Data 1 februarie 2015 12:31:10
Problema Asmax Scor 90
Compilator fpc Status done
Runda Arhiva de probleme Marime 0.99 kb
const f1='asmax.in';
      f2='asmax.out';
      dim=17000;
type nod=^date;
     date=record
         num:longint;
         next:nod;
     end;
var a,s,v:array[1..dim] of longint;
    g:array[1..dim] of nod;
    n,i,j,sol,x,y:longint;
    p:nod;

function max(a,b:longint):longint;
begin
if a>b then max:=a else max:=b;
end;

procedure depth(k:longint);
begin
v[k]:=1;
s[k]:=s[k]+a[k];
while g[k]<>nil do begin
if v[g[k]^.num]=0 then begin
 depth(g[k]^.num);
 if s[g[k]^.num]>0 then s[k]:=s[k]+s[g[k]^.num];
end;
g[k]:=g[k]^.next;
end;
end;

begin
assign(input,f1);reset(input);
assign(output,f2);rewrite(output);
readln(n);sol:=-1000;
for i:=1 to n do begin
read(a[i]);
if sol<a[i] then begin sol:=a[i];j:=i;end;end;
for i:=1 to n-1 do begin
readln(x,y);
new(p);
p^.next:=g[x];
p^.num:=y;
g[x]:=p;
new(p);
p^.next:=g[y];
p^.num:=x;
g[y]:=p;
end;
close(input);
depth(j);sol:=-16000000;
for i:=1 to n-1 do
sol:=max(sol,s[i]);
write(sol);
close(output);
end.