#Q194. 「一本通 5.6 练习 5」锯木厂选址

「一本通 5.6 练习 5」锯木厂选址

Description

Original source: CEOI 2004

From the top of the mountain to the bottom along a straight line, there are nn old trees planted. The local government has decided to cut them down. To avoid wasting any timber, the trees must be transported to sawmills after being felled.
The timber can only be transported downhill. There is a sawmill at the foot of the mountain. Two additional sawmills will be newly built along the mountain path. You must decide where to build these two sawmills to minimize the total transportation cost. It is assumed that transporting one kilogram of timber per meter costs one cent.
Your task is to write a program that reads the number of trees, their weights, and positions, and calculates the minimum transportation cost.

Input Format

The first line of input is a positive integer nn, representing the number of trees. The trees are labeled from 11 to nn from the mountain top to the foot.
The next nn lines each contain two integers wiw_i and did_i. These represent the weight (in kilograms) of the ii-th tree and the distance between the ii-th tree and the (i+1)(i+1)-th tree, respectively. The last number dnd_n represents the distance from the nn-th tree to the sawmill at the foot of the mountain.

Output Format

Output only one number, representing the minimum transportation cost.

Sample 1

The figure below illustrates the optimal sawmill locations for the sample input. Trees are represented by circles, and sawmills are marked in black. The result is:

two.png

9
1 2
2 1
3 3
1 1
3 2
1 6
2 1
1 2
1 1

26

Data Range and Hints

For 9797 points of the data, $2\le n\le 2\times 10^4, 1\le w_i\le 10^4, 0\le d_i\le 10^4$. It is guaranteed that the cost to transport all trees to the sawmill at the foot of the mountain is less than 2×1092\times 10^9 cents. (This part of the data is the original dataset.)

For the remaining 33 points of the data, 2n2×1052\le n\le 2\times 10^5, and all calculations can be performed within 64-bit signed integers.