As you may already know, accountants keep their data in form of tables and they calculate all sorts of sums on lines and columns. Atnoc, our accountant has organized his values in the for of a table with rows (numbered from to ) and columns (numbered from to ).
Elements on the last column are the sums of their row (more precisely, the element on row and column is the sum of elements on row situated on the columns ), and the elements on the last line are the sums of their column (more precisely, the element on column and row is the sum of elements on column situated on the rows ). An example of such a table is shown below.
Unfortunately Atnoc spilled water on his beloved table and in so doing some of the table's elements became unreadable. In order to recover the value on cell he will need to pay a cost of .
Determine the minimum cost Atnoc has to pay in order to be able to recover the table.
Interaction
To solve this problem you will have to implement the following function:
long long solve(int N, int M, int** A, int** B);
The parameters and represent the size of the table. The arrays and (whose rows are indexed from to and columns from to ) represent the elements of the table and the cost to recover the values respectively.
The function should return the minimum cost to recover the table.
Constraints
if the element is readable or if the element is unreadable
for or
It is guaranteed there is no further information to be extracted from the (readable) values themselves
For tests worth points, .
For tests worth more points, .
For tests worth more points, .
Note: The test data is not the same as the one used during the original contest and the statement has been slightly modified.
Example
The sample grader reads the number N
and M
from the first line and on the next N
lines M
values representing the values of A
and then on the final N
lines M
values representing the values of B
.
stdin
3 3
-1 -1 -1
-1 2 -1
3 4 7
3 3 18
4 1 4
2 2 2
stdout
3
Explanation
By finding the cell , paying we will have enough information to reconstruct the entire table.