#P1862. 输油管道问题

输油管道问题

Description

An oil company plans to build a main pipeline running from east to west.

The pipeline must pass through an oil field that has nn wells. From each well, a feeder pipeline must connect to the main pipeline along the shortest path (either north or south).

Given the positions of the nn wells, with their xx coordinates (east–west) and yy coordinates (north–south), determine the optimal position of the main pipeline so that the total length of the feeder pipelines from the wells to the main pipeline is minimized. Prove that the optimal position of the main pipeline can be determined within the time limit.

Input Format

The first line contains the number of wells nn.

Each of the next nn lines contains the position of a well: two integers xx and yy.

Output Format

Output a single line with the minimum total length of the feeder pipelines from the wells to the main pipeline.

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

Hint

Constraints

For all testdata, 1n100001 \le n \le 10000, 104x,y104-10^4 \le x, y \le 10^4.

Translated by ChatGPT 5