

1. The sizes will range from 1 to ‘N’ and will be integers.
2. The sum of the pieces cut should be equal to ‘N’.
3. Consider 1-based indexing.
The first line of input contains an integer ‘T’ denoting the number of test cases.
The next 2 * T lines represent the ‘T’ test cases.
The first line of each test case contains an integer ‘N’ denoting the length of the rod.
The second line of each test case contains a vector ’A’, of size ‘N’ representing the cost of different lengths, where each index of the array is the sub-length and the element at that index is the cost for that sub-length.
Since 1-based indexing is considered, the 0th index of the vector will represent sub-length 1 of the rod. Hence the (N - 1)th index would represent the cost for the length ‘N’.
For each test case, print a single line that contains a single integer which is the maximum cost obtained by selling the pieces.
The output of each test case will be printed in a separate line.
You do not need to print anything; it has already been taken care of. Just implement the given function.
1 <= T <= 50
1 <= N <= 100
1 <= A[i] <= 100
Where ‘T’ is the total number of test cases, ‘N’ denotes the length of the rod, and A[i] is the cost of sub-length.
Time limit: 1 sec.
The approach is similar to the Unbounded knapsack problem. The idea is to select a sub-length of rod out of all possible sub-lengths, as one option and ignore this sub-length, as another option.
In this way, we will get all possible configurations and we calculate the ‘MAXCOST’ for all the possible configurations and return the maximum of them all.
Following is a recursion tree for a rod of length 3.
We can observe that the problem has optimal substructure and overlapping subproblems and hence can be solved using dynamic programming. The idea is to store the results of subproblems so that we do not have to re-compute them when they are needed.
Below is a bottom-up approach, in which smaller subproblems are stored first which are used to solve the larger sub-problems. The below approach computes ‘COST[i]’, which stores maximum profit achieved from the rod of length i for each 1 <= i <= 'N'. It uses the 'COST' of smaller values of 'I' already computed.