[Original question link]
Http://acm.pku.edu.cn/JudgeOnline/problem? Id = 1260
[Topic]
I want to buy several kinds of valuable pearls, but I have to pay 10 more for a pearl, and if I want to buy 100 pearls worth 1, I have to pay 110 yuan. A pearl can be filled with pearls that are more expensive than it. Therefore, it may be more cost-effective to replace them cheaply when you buy a variety of pearls. For example, if you buy 100 products with a value of 2 and 1 value of 1, then the best solution is to buy 101 products with a value of 2. Enter several types of pearls you want to buy, and you can fill them with high-price pearls to ask how much it will cost at least.
[Solution ideas]
We can break down the problem into an optimal sub-problem. If there is a pearl whose value is m to N, the maximum value of the pearl is K (M <k <= N) the process of obtaining the optimal solution is to enumerate the amount of money required for the Pearl bought with the last value plus the optimal solution for the Pearl bought with the last value to obtain the minimum value, this method is used to find the state transition equation of Dynamic Planning: ANS [I] = min (ANS [I], (sum [I]-sum [J] + 10) * P [I] + ans [J])
[SourceProgram]
[Code]
# Include <iostream>
Using namespace STD;
Long a [1100], p [1100], sum [1100], ANS [1100];
Int main ()
{
Long t, n, I, j;
Cin> T;
While (t --)
{
Cin> N;
Memset (sum, 0, sizeof (SUM ));
For (I = 1; I <= N; I ++)
{
Cin> A [I]> P [I];
Sum [I] = sum [I-1] + A [I];
// The sum array is used to record the sum, which facilitates the sum of values of a pearl in the future.
}
For (I = 1; I <= N; I ++)
{
Ans [I] = (sum [I] + 10) * P [I]; // only use the last value to buy the expected number of pearls.
For (j = 1; j <I; j ++) // use the state transition equation
Ans [I] = min (ANS [I], (sum [I]-sum [J] + 10) * P [I] + ans [J]);
}
Cout <ans [N] <Endl; // solution obtained by the output
}
Return 0;
}
[/Code]