Steve has ’N’ slimes. Each slime has a size between 0 and 99. He wants to merge all these slimes into one by repeatedly mixing two adjacent slimes. The cost of mixing two slimes of size a and b is a * b, and the resulting slime has size (a + b) % 100. Help Steve compute the minimum cost to combine all slimes into one.
Help Steve to find the minimum cost in mixing for mixing all these slimes.
For Example :Slimes = {10, 7, 3}
On mixing slime 3 and 7 remaining slimes are: {10, 10} and the cost is 3 * 7 = 21.
On mixing slime 10 and 10 remaining slimes are: {10} and the cost of mixing is 10 * 10 = 100.
So the final cost of mixing the slime is 121.
The first line contains a single integer ‘T’ denoting the number of test cases, then each test case follows:
The first line of each test case contains a single integer ‘N’ denoting the total number of slimes.
The next line contains 'n' integers denoting the size of the slimes.
Output Format :
For each test case, print a single integer “ans” denoting the minimum cost of converting all these slime into one single slime.
Output for each test case will be printed in a separate line.
Note :
You are not required to print anything; it has already been taken care of. Just implement the function.
1 <= T <= 10
1 <= N <= 100
0 <= slime[i] <= 99
Time limit: 1 sec
2
2
11 13
3
40 60 20
143
2400
In the first test case, there are two slimes, Hence the answer is 11 * 13 = 143
In the second test case, we will first combine slime 1 and slime 2 which will result in slime 100 and 20, now the slime with size 100 will lose its size by 100. Now the slimes are 0 and 20 which will give the final answer 2400.
2
3
13 57 43
4
1 2 3 4
2451
35
Try to break the problem into subproblems starting with two slimes to n slimes.
In this approach, we will start combining every adjacent slime and check for the minimum possible cost.
The steps are as follows:
O( 2 ^ N ), where N is the total number of slimes.
As we are checking for every possible case of combining the slimes.
Hence the time complexity is O( 2 ^ N ).
O( 1 )
No extra space is used.
Hence the space complexity is O(1).