#P3819. 松江 1843 路

松江 1843 路

Description

Laifang Road is a road of length LL meters, with coordinates ranging from 00 to LL. There are NN houses on the road. The ii-th house is located at coordinate xix_i and has rir_i residents.

The Songjiang 1843 Road bus line will build a bus stop on this road. The city government wants to serve as many people as possible, so it hopes to minimize the sum of distances from every resident’s home to the stop.

Where should the bus stop be built?

Input Format

The first line contains LL, NN.

The next NN lines each contain two integers xix_i and rir_i.

Output Format

Output one integer: the minimal sum of distances from all residents’ homes to the bus stop.

100 3
20 3
50 2
70 1

110

100 2
0 1
100 10

100

10000000000 5
3282894320 391
4394338332 929
6932893249 181
7823822843 440
9322388365 623

5473201404068

Hint

Sample Explanation 1

When the stop is built at coordinate 4040, the sum of distances is $|20-40| \times 3+|50-40| \times 2+|70-40| \times 1=110$.

Constraints

  • For 10%10\% of the testdata, 1N501 \le N \le 50, ri=1r_i=1.
  • For 30%30\% of the testdata, 1N1001 \le N \le 100, ri10r_i \le 10, 1L10001 \le L \le 1000.
  • For 70%70\% of the testdata, 1N10001 \le N \le 1000, ri100r_i \le 100, 1L1061 \le L \le 10^6.
  • For all testdata, 1L10101 \le L \le 10^{10}, 1N1051 \le N \le 10^5, 0xiL0 \le x_i \le L, 1ri10001 \le r_i \le 1000.

Translated by ChatGPT 5