Cod sursa(job #88636)

Utilizator mugurelionutMugurel-Ionut Andreica mugurelionut Data 3 octombrie 2007 11:47:31
Problema Asmax Scor 100
Compilator fpc Status done
Runda Arhiva de probleme Marime 1.57 kb
{ Mugurel Ionut Andreica - Bucuresti, ROMANIA }

{ Time Complexity: O(N) }

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.