j:=1; i:=1; while (j<=max) do begin j:=j+1; i:=i+1; while i<=n do begin if P[i] = j then begin Wt[i]:=w; w:= w+B[i]; end; end; end;