I am CRAZY of this problem!!!
Послано
SP2 17 сен 2007 20:44
I can't understand WHY i have WA1! On all tests my solution is good. And what do i have to do with branches without apples
Here is my solution:
program Project2;
{$APPTYPE CONSOLE}
var Sons:array[1..200,1..2] of byte;
val:array[1..200,1..2] of longint;
Rez:array[1..200,1..200] of int64;
Was:array[1..200,1..200] of boolean;
x,y,c,n,q,j:longint;
function max(a,b:longint):longint;
begin
if a>=b then max:=a else max:=b;
end;
function R(m,k:longint):int64;
var i,maxim:longint;
begin
if k=0 then R:=0 else
if k=1 then R:=max(val[m,1],val[m,2]) else
begin
maxim:=0;
if Sons[m,1]<>0 then
begin
if Was[Sons[m,1],k-1] then maxim:=max(Rez[Sons[m,1],k-1],maxim) else
begin
Rez[Sons[m,1],k-1]:=R(Sons[m,1],k-1)+val[m,1];
Was[Sons[m,1],k-1]:=true;
maxim:=max(Rez[Sons[m,1],k-1],maxim);
end;
end;
if Sons[m,2]<>0 then
begin
if Was[Sons[m,2],k-1] then maxim:=max(Rez[Sons[m,2],k-1],maxim) else
begin
Rez[Sons[m,2],k-1]:=R(Sons[m,2],k-1)+val[m,2];
Was[Sons[m,2],k-1]:=true;
maxim:=max(Rez[Sons[m,2],k-1],maxim);
end;
end;
if (Sons[m,1]<>0)and(Sons[m,2]<>0) then
begin
for i:=0 to k-2 do
begin
if not Was[Sons[m,1],i] then begin Rez[Sons[m,1],i]:=R(Sons[m,1],i); Was[Sons[m,1],i]:=true; end;
if not Was[Sons[m,2],k-2-i] then begin Rez[Sons[m,2],k-2-i]:=R(Sons[m,2],k-2-i); Was[Sons[m,2],k-2-i]:=true; end;
maxim:=max(maxim,Rez[Sons[m,1],i]+Rez[Sons[m,2],k-2-i]+val[m,1]+val[m,2]);
end;
end;
R:=maxim;
end;
end;
begin
read(n,q);
for j:=1 to n-1 do
begin
read(x,y);
read(c);
if Sons[x,1]=0 then
begin Sons[x,1]:=y; val[x,1]:=c end else
begin Sons[x,2]:=y; val[x,2]:=c; end;
end;
write(R(1,q));
Help me!!!
end.