#Q191. 「一本通 5.6 练习 2」仓库建设

「一本通 5.6 练习 2」仓库建设

Description

Original source: ZJOI 2007

L Company has several factories on a mountain. Due to the mountain's location in an inland plateau area (dry with little rain), L Company usually stores its products directly outdoors to save costs. Suddenly one day, Mr. L, the president of L Company, received a call from the meteorological department informing him that a heavy rainstorm would arrive in three days. Thus, Mr. L decided to urgently build warehouses in some factories to prevent the products from being damaged by the rain.

L Company has NN factories on the mountain. As shown in the figure, Factory 11 is at the mountaintop, and Factory NN is at the mountain base.

Due to the varying terrain, the cost of building a warehouse may differ across factories. Factory ii currently has PiP_i finished products, and the cost of building a warehouse in this factory is CiC_i. For factories without a warehouse, their products must be transported to other warehouses for storage. Since L Company's product sales office is located at Factory NN at the mountain base, products can only be transported downhill (i.e., only to warehouses of factories with larger indices). Of course, transporting products also incurs costs—the cost of moving one product one unit distance is 11. It is assumed that the capacity of all warehouses is sufficiently large to accommodate all products.

The following are known:

  1. The distance of Factory ii from Factory 11, denoted as XiX_i (where X1=0X_1=0);
  2. The current number of finished products in Factory ii, denoted as PiP_i;
  3. The cost of building a warehouse in Factory ii, denoted as CiC_i.

Please help L Company find a warehouse construction plan that minimizes the total cost (construction cost + transportation cost).

Input Format

The first line contains an integer NN, the number of factories. Next, NN lines follow, each containing three integers Xi,X_i, Pi,P_i, and CiC_i, as described in the problem statement.

Output Format

Output a single integer, the minimum total cost achievable with the optimal plan.

Sample 1

Building warehouses in Factory 11 and Factory 33 incurs a construction cost of 10+10=2010+10=20 and a transportation cost of (95)×3=12(9-5)\times 3 = 12, resulting in a total cost of 3232. If a warehouse is built only in Factory 33, the construction cost is 1010, and the transportation cost is (90)×5(9-0)\times 5 +(95)×3+(9-5)\times 3 =57=57, resulting in a total cost of 6767, which is worse than the first plan.

3
0 5 10
5 3 100
9 6 10

32

Data Range and Hints

For all data, N106N \le 10^6. It is guaranteed that all Xi,X_i, Pi,P_i, and CiC_i are within the int range, and intermediate calculation results will not exceed the long long range.