Stačí body seřadit podle jedné souřadnice (při shodnosti podle druhé) a sečíst vzdálenosti 1-2, 3-4, 5-6, atd. Potom totéž s výměnou souřadnic.
Function Solve : Integer; Var I: Integer; R: Integer; X, Y: Integer; Begin QuickSort (1, NumPts); R := 0; Y := NOLINE; For I := 1 to NumPts do If (Y = NOLINE) then Begin X := Pts[I].X; Y := Pts[I].Y; End else Begin If (X <> Pts[I].X) then Bad := true else R := R + (Pts[I].Y - Y); Y := NOLINE; End; Solve := R; End;
For I := 1 to NumPts do ReadLn (Pts[I].X, Pts[I].Y); Res := Solve; For I := 1 to NumPts do Begin Tmp := Pts[I].X; Pts[I].X := Pts[I].Y; Pts[I].Y := Tmp; End; Res := Res + Solve;