D - Meeting Hall Editorial /

Time Limit: 2 sec / Memory Limit: 1024 MiB

配点 : 400 点

問題文

高橋君が住む街には一直線に伸びる道路があり、数直線とみなすことができます。

この道路沿いには N 軒の家があり、1 から N までの番号が付けられています。家 i は座標 X_i にあり、P_i 人の住民が住んでいます。なお、複数の家が同じ座標に存在することもあります(すなわち、i \neq j かつ X_i = X_j となることがあります)。

高橋君は、数直線上の任意の実数座標 x に集会所を 1 箇所建設することにしました(既存の家と同じ座標に建設しても構いません)。

集会所を座標 x に建設したとき、家 i の住民 1 人が集会所へ移動するコストは、家から集会所までの距離 |X_i - x| です。

すべての住民が集会所へ移動するコストの総和

\sum_{i=1}^{N} P_i \times |X_i - x|

の最小値を求めてください。

なお、本問題の制約のもとで、この最小値は必ず整数になることが証明できます。

制約

  • 1 \leq N \leq 10^5
  • 1 \leq X_i \leq 10^9
  • 1 \leq P_i \leq 10^4
  • 入力はすべて整数

入力

入力は以下の形式で与えられる。

N
X_1 P_1
X_2 P_2
\vdots
X_N P_N

出力

すべての住民の移動コストの総和の最小値を整数として 1 行に出力してください。


入力例 1

3
2 1
5 2
9 1

出力例 1

7

入力例 2

4
10 5
20 3
10 4
30 2

出力例 2

70

入力例 3

8
25 30
10 100
70 20
40 50
90 10
40 30
15 60
60 40

出力例 3

6250

入力例 4

15
123456789 5000
987654321 8000
456789012 3500
234567890 9000
876543210 1200
345678901 6700
765432109 4300
567890123 2100
678901234 7800
1000000000 10000
1 1
100000000 500
500000000 6000
200000000 4000
800000000 3000

出力例 4

19674392919522

入力例 5

1
1000000000 10000

出力例 5

0

Score : 400 pts

Problem Statement

The town where Takahashi lives has a road that extends in a straight line, which can be regarded as a number line.

Along this road, there are N houses numbered 1 to N. House i is located at coordinate X_i and has P_i residents. Note that multiple houses may be located at the same coordinate (that is, i \neq j and X_i = X_j may hold).

Takahashi has decided to build a single meeting place at an arbitrary real coordinate x on the number line (it may be built at the same coordinate as an existing house).

When the meeting place is built at coordinate x, the cost for one resident of House i to travel to the meeting place is the distance from the house to the meeting place, which is |X_i - x|.

Find the minimum possible value of the total cost for all residents to travel to the meeting place:

\sum_{i=1}^{N} P_i \times |X_i - x|

It can be proved that under the constraints of this problem, this minimum value is always an integer.

Constraints

  • 1 \leq N \leq 10^5
  • 1 \leq X_i \leq 10^9
  • 1 \leq P_i \leq 10^4
  • All input values are integers.

Input

The input is given in the following format:

N
X_1 P_1
X_2 P_2
\vdots
X_N P_N

Output

Print the minimum total travel cost of all residents as an integer on a single line.


Sample Input 1

3
2 1
5 2
9 1

Sample Output 1

7

Sample Input 2

4
10 5
20 3
10 4
30 2

Sample Output 2

70

Sample Input 3

8
25 30
10 100
70 20
40 50
90 10
40 30
15 60
60 40

Sample Output 3

6250

Sample Input 4

15
123456789 5000
987654321 8000
456789012 3500
234567890 9000
876543210 1200
345678901 6700
765432109 4300
567890123 2100
678901234 7800
1000000000 10000
1 1
100000000 500
500000000 6000
200000000 4000
800000000 3000

Sample Output 4

19674392919522

Sample Input 5

1
1000000000 10000

Sample Output 5

0