The next N (1≤N10000) weeks, the cheese factory in the i-th sub C_i take weeks to a cheese production unit. York has a cheese factory
infinite warehouse, per week extra cheese production will be placed here. And each week a storage unit of cheese takes S minutes. Factory most
recently received customer orders for N weeks, the first week i want to provide customers with cheese Y_i units. Of course, these cheeses can be produced at the time of the i-th week
, you can also pick up from the warehouse. What kind of production strategy using plants York cheese minimum cost of it?
Input
The first line two integers: N and S; N subsequent lines, the two numbers represent the i-th row: C_i and Y_i.
Output
only one row, i.e. the minimum cost factory
the Sample the Input
. 4. 5
88 200 is
89 400
97 300
91 is 500
the Sample Output
126900
Sol: Analysis of the i-month price of cheese, it has two cases, one of the i-months of their own production, and second, by the 1 ~ i-1-month period of a month the price of cheese + storage costs retained. As in the following examples, set up i-month price now
First month 88,200 own production, now = c [1] = 88, the calculation is now updated now + s, i.e. now = 93, to see if the next month with the price of production.
89 400 now> c [2] , i.e., 93> 89, so the first February own production, now = 89, 94 is updated to the calculated now.
97 300 now <c [3] , i.e., 94 <97, so the first of March to spend months of storage production price, now = 94, 99 is updated to the calculated now.
91 500 now> c [4] , that is, 99> 91, so the first own production in April, now = 91.
From the above, according to the conditions greedy strategy selection, if last month's price + storage Price <month produce their own price, then choose a month earlier production storage, or their own production month.
. 1 #include <cstdio> 2 int n-, S, C, A; . 3 Long Long now = 10000000 , ANS = 0 ; . 4 int main () . 5 { . 6 Scanf ( " % D% D " , & n-, & S); / / n-month storage fees for the S . 7 for ( int I = . 1 ; I <= n-; I ++ ) . 8 { . 9 Scanf ( " % D% D " , & C, & a); // cost month is c, the demand an amount of A 10 IF (now> C) //If the [1, i-1] on the production cost of this section is higher than a month . 11 now = C; // month to produce its own cheese 12 is ANS = now * + A; 13 is now + = S; // accumulation reservoir cost 14 } 15 the printf ( " % LLD " , ANS); 16 return 0 ; . 17 }