#t264. 输油管道问题

输油管道问题

Oil Pipeline Problem

Problem Description

An oil company plans to build a main east-to-west oil pipeline. The pipeline must pass through an oil field that has nn oil wells. From each well, a connecting pipeline must run along the shortest path (either south or north) to join the main pipeline.

Given the positions of the nn wells, with their xx coordinate (east-west) and yy coordinate (north-south), how should the optimal position of the main pipeline be determined, i.e. the position that minimizes the total length of the connecting pipelines from all wells to the main pipeline? Prove that the optimal position can be determined within the required time.

Input Format

The first line is the number of oil wells nn.

The following nn lines give the position of each well, with two integers xx and yy per line.

Output Format

A single line containing the minimum total length of the connecting pipelines from the wells to the main pipeline.

Sample Input

5
1 2
2 2
1 3
3 -2
3 3

Sample Output

6

Constraints

pipe diagram

medium diagram

Data Range and Notes

For all test cases, 1≤n≤100001 \le n \le 10000, −104≤x,y≤104-10^4 \le x, y \le 10^4.