Why do i get W.A.? Can someone post some input and output ?
program timus_p_1018;
const
maxn=100;
type
art=record
left:byte;
right:byte;
father:byte;
apples:word;
rent:real;
nodes:byte;
totalcost:longint;
end;
ttree=array[1..maxn]of art;
tnod=record
c:word;
i:byte;
end;
tm=array[1..maxn,1..3]of tnod;
nlinks=array[1..maxn]of 1..3;
tlevels=array[0..maxn,0..maxn]of byte;
tindex=array[1..maxn]of byte;
ttooken=array[1..maxn]of boolean;
var
n,q,i:byte;
tree:ttree;
x,y:byte;
c:word;
m:tm;
l:nlinks;
levels:tlevels;
ni:tindex;
nlevels:byte;
allapples:longint;
takenapples:longint;
tooken:ttooken;
procedure init_data;
begin
fillchar(tree,sizeof(tree),0);
fillchar(m,sizeof(m),0);
fillchar(l,sizeof(l),0);
fillchar(levels,sizeof(levels),0);
fillchar(ni,sizeof(ni),0);
fillchar(tooken,sizeof(tooken),0);
tooken[1]:=true;
allapples:=0;
takenapples:=0;
end;
procedure read_data;
begin
readln(n,q);
for i:=1 to n - 1 do
begin
readln(x,y,c);
if c<>0 then
begin
inc(l[x]);
m[x,l[x]].c:=c;
m[x,l[x]].i:=y;
inc(l[y]);
m[y,l[y]].c:=c;
m[y,l[y]].i:=x;
inc(allapples,c);
end;
end;
end;
procedure DFS(k,father:byte;cost:word;level:byte);
var
ck:byte;
begin
if nlevels<level then
nlevels:=level;
inc(ni[level]);
levels[level,ni[level]]:=k;
tree[k].father:=father;
tree[k].apples:=cost;
if l[k]=1 then {contains only one link to its father}
exit;
ck:=1;
if m[k,ck].i=father then {if link1 is the father}
inc(ck); {then link ck=link2}
tree[k].left:=m[k,ck].i;
DFS(m[k,ck].i,k,m[k,ck].c,level+1);
if l[k]=2 then
exit;
inc(ck);
{just in case link1 isn't the father}
if m[k,ck].i=father then {if link2 is the father}
inc(ck); {then link ck=link3}
tree[k].right:=m[k,ck].i;
DFS(m[k,ck].i,k,m[k,ck].c,level+1);
end;
procedure make_tree;
var
k:byte;
begin
nlevels:=1;
tree[1].left:=m[1,1].i;
levels[1,1]:=1;
ni[1]:=1;
DFS(m[1,1].i,1,m[1,1].c,2);
if l[1]>1 then
begin
tree[1].right:=m[1,2].i;
DFS(m[1,2].i,1,m[1,2].c,2);
end;
end;
procedure DownUp;
var
cl:byte; { current level }
cn:byte; { current node }
i:byte; { index }
begin
for i:=1 to ni[nlevels] do { working out the last level }
begin
cn:=levels[nlevels,i];
tree[cn].nodes:=1;
tree[cn].totalcost:=tree[cn].apples;
tree[cn].rent:=tree[cn].apples;
end;
for cl:=nlevels-1 downto 2 do { working out each upper level except root }
begin
for i:=1 to ni[cl] do
begin
cn:=levels[cl,i];
tree[cn].nodes:=1;
tree[cn].totalcost:=tree[cn].apples;
if tree[cn].left<>0 then
begin
inc(tree[cn].nodes);
inc(tree[cn].totalcost,tree[tree[cn].left].totalcost);
end;
if tree[cn].right<>0 then
begin
inc(tree[cn].nodes);
inc(tree[cn].totalcost,tree[tree[cn].right].totalcost);
end;
tree[cn].rent:=tree[cn].totalcost/tree[cn].nodes;
end;
end;
end;
procedure FindMinRent(lefttotake:byte;var node:byte);
var
i:byte;
min:real;
k:byte;
begin
min:=maxlongint;
k:=0;
for i:=2 to n do
if (not tooken[i]) and (tree[i].nodes<=lefttotake) and
(tree[i].rent<min) then
begin
k:=i;
min:=tree[i].rent;
end;
node:=k;
end;
procedure TakeMinApples(var lefttotake:byte);
var
node:byte;
begin
FindMinRent(lefttotake,node);
dec(lefttotake,tree[node].nodes);
inc(takenapples,tree[node].totalcost);
tooken[node]:=true;
end;
procedure TakeOutBranches;
var
lefttotake:byte;
begin
lefttotake:=n-1-q;
while lefttotake<>0 do
TakeMinApples(lefttotake);
end;