


1) Yuki can buy 1 more blade with cost 'A.’ He now has ‘K+1’ Ninja blades.
2) Yuki could buy a ‘K’ number of blades with cost 'B.’ He now has ‘2*K’ blades.
where 'A' and 'B' are predefined and constant.
There can be two or more ways with the exact cost. You can consider any one of them, but the overall cost to reach from 0 to 'N' must be minimized.
Consider Yuki need to buy 5 blades, the cost of adding 1 blade is 2, and the cost of doubling the blades is 1 then you have to perform the following operations:
1) Doubling 0 will result in 0 only, so add 1 blade to 0 blades with cost 2. Total cost becomes 2.
2) Next, you can either double 1 to reach 2 or add 1 blade. But since the cost of doubling is less than that of adding, so double 1 with cost 1. Total cost becomes 3.
3) Doubling 2 will result in 4 with a cost of 1. Total becomes 4.
4) Adding 1 in 4 will result in 5 (which is the desired number) with a cost of 2. The total cost to reach 5 becomes 6.
The first line of input contains an integer 'T' representing the number of the test cases. Then the test case follows.
The first line of each test case contains three space-separated integers, 'N', 'A', and 'B'. where 'N' is the number of blades Yuki wants to buy, 'A' is the price to increase the number of blades by 1 and 'B' is the price to double the number of blades he has.
For each test case, return the minimum price needed to buy the ‘N’ blades following the method described above.
You do not need to print anything; it has already been taken care of. Just implement the given function.
1 <= ‘T’ <= 100
1 <= 'N' <= 10^4
0 <= 'A','B' <= 10^3
Time limit: 1 second
One observation we need to make is that Ninja buys the blades step by step i.e first he buys 1 blade then maybe buys another blade to now have net 2 blades, then again maybe doubles it to buy 4 blades, and so on. That is, we need to compute the answer step by step. For the final price to be minimum we also need the pre-final prices to be minimum.
Following that we can conclude that to get N blades at the minimum price we need to optimally buy either N-1 blade or N/2 blades(assuming N is even). Therefore we can divide the problem into subproblems and can compute them with the same function using recursion.
Return ANS.
In the previous approach, we are calculating the answer for the same values of N again and again. This leads to Overlapping subproblems and we have already seen the Optimal substructure property(How the recurrence relation was found).
Therefore to avoid computing the same values again and again we need to store the answer for each N into an array and later retrieve it.
In the previous approach we used recursion and top-down memoization to find the answer. This approach introduces a iterative bottom-up Dynamic Programming solution.