eolymp
bolt
Try our new interface for solving problems
Problems

Road network

Road network

In a small, but very proud country there are \textbf{n} military bases. King decided to connect them with roads so that one can move from any base to any other along one or more roads. An interesting feature of this country is that engineers can build only the roads moving either from north to south or from east to west. King instructed the Minister of Transport to make a plan, and he, in turn, gave this assignment to you - the High Programmer of the state. You need to determine the minimum total length of the roads in the constructed road network. The country is represented with Cartesian coordinate system. The axis \textbf{Ox} goes from west to east, and the axis \textbf{Oy} goes from south to north. The base sizes are quite small, so they can be considered as points, and the road as lines in the plane. \InputFile The first line contains the number of bases \textbf{n} (\textbf{1} ≤ \textbf{n} ≤ \textbf{10}). Each of the next \textbf{n} lines contains the coordinates of one base in format "\textbf{x y}", where \textbf{x} is the abscissa, \textbf{y} is an ordinate, \textbf{x} and \textbf{y} are integers from \textbf{-1000} to \textbf{1000} inclusive. \OutputFile Print one number - the total length of all built roads with \textbf{3} digits after the digital point. \Note Possible locations for the best road networks for the above examples are provided below: \includegraphics{https://static.e-olymp.com/content/3f/3f5ea69412fa425214648d310b5ebec7b646b322.jpg}
Time limit 2 seconds
Memory limit 64 MiB
Input example #1
3
0 1
2 0
3 2
Output example #1
5.000
Author Ilya Razenshteyn