#P7253. [BalticOI 2012] 城市烟花 (Day2)

[BalticOI 2012] 城市烟花 (Day2)

Description

This is a city with infinitely many grid-like streets. Some citizens live at the intersection points of the grid. (It is possible that two citizens live at the same place.)

Now you need to choose an intersection point between some vertical street and horizontal street 00 as the place to set off fireworks. Citizens must come to either the horizontal street or the vertical street that contains this point to watch. At the same time, their distance to the fireworks location must not be less than SS. You need to choose a fireworks location to minimize the total moving distance of all citizens.

Input Format

The first line contains two integers NN, SS, representing the total number of citizens and the minimum distance, respectively.

The next NN lines each contain two integers HiH_i, ViV_i, meaning that this citizen lives at the intersection of horizontal street HiH_i and vertical street ViV_i.

Output Format

Output the minimum possible sum of moving distances.

7 2
3 -2
0 8
-4 8
-1 4
-2 13
-4 8
1 5
9

Hint

Sample Explanation

Note that there are two people living at the intersection of horizontal street 4-4 and vertical street 88.

The optimal solution is to choose vertical street 88, and then the minimum total distance is 99.

Constraints

  • For 20%20\% of the testdata, 0Vi50000 \leq V_i \leq 5000.
  • For 40%40\% of the testdata, N5000N \leq 5000.
  • For 100%100\% of the testdata, 1N1051 \leq N \leq 10^5, 1S1061 \leq S \leq 10^6, 109Hi,Vi109-10^9 \leq H_i, V_i \leq 10^9.

Notes

Translated from BalticOI 2012 Day2 T1. Fireworks in RightAngleles.

Translated by ChatGPT 5