

All the integers in the ‘CUTS’ array are distinct.
Let ‘N’ be: 4
Let the ‘CUTS’ array be: [1, 3].
Let the order of doing the cut be [1, 3].
The first cut of 1 on length 4 results in a cost of 4, and chocolate is split into two parts of the length of 1 and 3.
The second cut of 3 on length 3 results in a cost of 3, and chocolate is split into two parts again of the length of 2 and 1. So the total cost is 7.
The cost will remain the same if we change the order of cutting. So the result is 7.
The first line of input contains an integer ‘T’, denoting the number of test cases.
The first line of each test case contains two space-separated integers, ‘N’ and ‘C’, representing the length of chocolate and size of the ‘CUTS’ array.
The second line of each test case contains ‘C’ space-separated integers, representing the array ‘CUTS’ elements.
For each test case, print the minimum cost to cut the chocolate.
Print output of each test case 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 <= 10
2 <= N <= 10^5
1 <= C <= 10^3
1 <= CUTS[i] <= N - 1
Time Limit: 1 sec
The basic idea is to find all the cuttings orders and select the minimum out of them. If we have chocolate of starting index (say, ‘startIdx’) and ending index (‘endIdx’), we divide it into two parts by cutting it at any given point. We check all the places we can cut, take the minimum of them, and add the current length of the chocolate. The base case will be when the size of chocolate is less than equal to 1 as it cannot be cut further into smaller pieces.
HELPER(‘N’, ‘CUTS’, ‘startIdx’, ‘endIdx’) (where ‘startIdx’ and ‘endIdx’ and the positions we do the cuts)
The approach is similar to the previous approach. In this approach, we just store the recursive calls of the function to avoid repeated recursive calls.
How do we know recursive calls are being repeated?
Let ‘N’ be 3 and the ‘CUTS’ array be [1, 2].
We call recursion to calculate the cost for, say, indices (0, 1).
Now let’s change the order of cuts to [2, 1]. We first call recursion on, say, indices (0, 2). We further divide this chocolate into (0, 1) and (1, 2).
We can see (0, 1) is being repeated again. For large inputs, there will be more extra recursive calls. To avoid this, we memoize our approach and save the results in an array (say, ‘DP’).
Here is the algorithm :
HELPER(‘N’, ‘CUTS’, ‘startIdx’, ‘endIdx’, ‘DP’) (where ‘startIdx’ and ‘endIdx’ and the positions we do the cuts)
The basic idea is to avoid recursion to reduce the space used in the recursive stack. We iteratively solve the problem. We calculate answers for cases where the number of cuts between the starting index and ending index of chocolate is 1, we calculate the answer for the cases where the gap between starting and ending is 2. We create an array (say, ‘DP’) initialized with 0 to store the results, where ‘DP[i][j]’ stores the minimum cost of cutting when we cut the chocolate starting from length ‘CUTS[i]’ and ‘CUTS[j]’.
Here is the algorithm :