cswork / OAOpen practice workspace

NutanixOA #3

Min Cost to Connect All Points

Estimated MediumJudge-ready

Topics (AI-labelled)Spanning Trees

The cost of connecting two points is their Manhattan distance. Find the minimum total edge weight that connects all points so that there is exactly one simple path between any two points.

The standard input/output format and any supplementary ranges on this site are as defined in this problem statement.

Input

n (1≤n≤1000), followed by n lines of integer coordinates x y (−10^6≤x,y≤10^6); all coordinates are distinct.

Output

Output the total weight of the minimum spanning tree.

Example 1

Input:

5
0 0
2 2
3 10
5 2
7 0

Output:

20

Example 2

Input:

3
3 12
-2 5
-4 1

Output:

18

Example 3

Input:

1
4 -2

Output:

0

Editorial

Loading solution…