比赛 20111109 评测结果 WWWWWWWWWW
题目名称 火车站饭店 最终得分 0
用户昵称 lizhe 运行时间 0.000 s
代码语言 Pascal 内存使用 0.00 MiB
提交时间 2011-11-09 11:18:34
显示代码纯文本
program profitz;
var
  i,j,k1,k2,n,x,y,max:longint;
  map:array[1..100000,-1..50]of longint;
  f,g,w,l,a:array[0..100000]of longint;
procedure topo;
var
  k,t,num:longint;
begin
  num:=0;
  for i:=1 to n do
    if map[i,-1]=1 then
    begin
      inc(l[0]);
      l[l[0]]:=i
    end;
  t:=0;
  while l[0]<>t do
  begin
    inc(t); inc(num);
    a[num]:=l[t];
    k:=l[t];
    for i:=1 to map[k,0] do
    begin
      dec(map[map[k,i],-1]);
      if map[map[k,i],-1]=1 then
      begin
        inc(l[0]);
        l[l[0]]:=map[k,i]
      end
    end
  end
end;

procedure init;
begin
  assign(input,'profitz.in');
  reset(input);
  assign(output,'profitz.out');
  rewrite(output);
  read(n);
  for i:=1 to n do
    read(w[i]);
  for i:=1 to n-1 do
  begin
    read(x,y);
    inc(map[x,0]);
    map[x,map[x,0]]:=y;
    inc(map[y,-1]);
    inc(map[y,0]);
    map[y,map[y,0]]:=x;
    inc(map[x,-1]);
  end;
  topo;
  fillchar(l,sizeof(l),0);
  for i:=1 to n do
    l[a[i]]:=i
end;

procedure dp;
begin
  for i:=1 to n do
  begin
    k1:=a[i];
    for j:=1 to map[k1,0] do
    begin
      k2:=l[map[k1,j]];
      if g[i]<f[k2] then g[i]:=f[k2];
      if g[i]<g[k2] then g[i]:=g[k2];
      if f[i]<f[i]+g[k2]+w[k1] then
        f[i]:=f[i]+g[k2]+w[k1]
    end
  end
end;

procedure print;
begin
  if g[n]>f[n] then max:=g[n]
  else max:=f[n];
  writeln(max);
  close(input);
  close(output)
end;

begin
  init;
  dp;
  print
end.