Who can tell me why I got WA?
This is my program.
var long:array [1..500,1..3] of integer;
num:array [1..500,1..2] of integer;
max,i,j,l,n:integer;
begin
read(n);
for i:=1 to n do
begin
read(num[i,1],num[i,2]);
if num[i,1]>num[i,2] then
begin j:=num[i,1]; num[i,1]:=num[i,2]; num[i,2]:=j; end;
end;
for i:=1 to n-1 do
for j:=i+1 to n do
if (num[i,1]>num[j,1]) or
((num[i,1]=num[j,1]) and (num[i,2]>num[j,2])) then
begin
l:=num[i,1]; num[i,1]:=num[j,1]; num[j,1]:=l;
l:=num[i,2]; num[i,2]:=num[j,2]; num[j,2]:=l;
end;
for i:=1 to n do long[i,1]:=num[i,2]-num[i,1];
for i:=1 to n do long[i,3]:=1;
for i:=n-1 downto 1 do
for j:=n downto i+1 do
if (num[i,2]<=num[j,1]) then
if long[j,1]+num[i,2]-num[i,1]>long[i,1] then
begin long[i,1]:=long[j,1]+num[i,2]-num[i,1]; long[i,2]:=j;
long[i,3]:=long[j,3]+1; end;
max:=1;
for i:=2 to n do
if long[i,1]>long[max,1] then max:=i;
writeln(long[max,3]);
while long[max,2]<>0 do
begin
writeln(num[max,1],' ',num[max,2]);
max:=long[max,2];
end;
writeln(num[max,1],' ',num[max,2]);
end.