You are given chocolate of ‘N’ length. The chocolate is labeled from 0 to ‘N’. You are also given an array ‘CUTS’ of size ‘C’, denoting the positions at which you can do a cut. The order of cuts can be changed. The cost of one cut is the length of the chocolate to be cut. Therefore, the total cost is the sum of all the cuts. Print the minimum cost to cut the chocolate.
Note:
All the integers in the ‘CUTS’ array are distinct.
For example:
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.
Output Format :
For each test case, print the minimum cost to cut the chocolate.
Print output of each test case in a separate line.
Note :
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
2
4 2
1 3
5 3
1 3 4
7
10
For test case 1:
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.
For test case 2:
Let the order of doing the cut be [1, 3, 4].
The first cut of 1 on length 5 results in a cost of 5, and chocolate is split into two parts of the length of 1 and 4.
The second cut of 3 on length 4 results in a cost of 4, and chocolate is split into two parts again of the length of 2 and 2. The total cost is 9.
The third cut of 4 on length 2 results in a cost of 2, and chocolate is split into two parts again of the length of 1 and 1. The total cost is 11.
The minimum cost will be 10 when the order of doing cuts will be: [3, 1, 4].
2
10 4
1 3 4 7
10 2
1 3
23
13
Try to check all orders to do the cuts.
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.
Here is the algorithm :
HELPER(‘N’, ‘CUTS’, ‘startIdx’, ‘endIdx’) (where ‘startIdx’ and ‘endIdx’ and the positions we do the cuts)
O(C!), where ‘C’ is the number of cuts.
We check all the permutations of cutting, which takes O(C!) time. We also sort the ‘CUTS’ array, which takes O(C*log(C)) time. Therefore, the overall time complexity will be O(C!).
O(C), where ‘C’ is the number of cuts.
The recursion stack of the HELPER function can be maximum O(C). Therefore, the overall space complexity will be O(C).