



The first line of input contains ‘T’ number of test cases.
The first line of each test case contains, input ‘m’ and ‘n’ i.e. a number of rows and columns.
The second line of each test contains ‘m-1’ space-separated integer which denotes the horizontal cost.
The third line of each test contains ‘n-1’ space-separated integer which denotes the vertical cost.
For each test case return the minimum cost to breaking the chocolate into a single square.
You do not need to print anything.It has been already taken care of. Just implement the given function.
1 <= T <= 10
1 <= Xi <= 100
1 <= Yi <= 100
2 <= m,n <= 1000
Where ‘T’ represents the number of test cases, ‘x[i]’ represents the horizontal cost at ‘i-th’ index, ‘y[i]’ represents the vertical cost at ‘i-th’ index, ‘m’ is a number of rows and ‘n’ is a number of columns.
Time Limit: 1 second
We would use dynamic programming in this approach, i.e., using our present solution's previous values.
In this approach, we would take a 2d matrix and would make use of previously stored values.
The algorithm will be:
The number of cuts required is always the same.
Proof:
C(rows, cols) = rows + cols + rows * cols
C(0, 0) = 0 (a single box chocolate needs 0 cut)
Cut horizontally:
C(cols, rows) = 1 + C(a, rows) + C(b, rows) such that a + b + 1 = n.
C(cols, rows) = 1 + a + rows + a * rows + b + rows + b * rows.
= 1 + ( n - 1 ) + 2 * rows + ( cols - 1 ) * rows
= cols + rows + cols * rows.
Equally for the vertical cuts.
The problem is similar to the weight distribution problem, and each cut will happen sooner or later. The "weight" of a cut is increasing, and if a vertical and horizontal cost is equal, cutting order does not matter. So we greedily choose the largest cut-cost each time.
The algorithm will be: