Table

Time limit: 2.5s Memory limit: 160MB Input: Output:

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 NN rows (numbered from 00 to N1N − 1) and MM columns (numbered from 00 to M1M − 1).

Elements on the last column are the sums of their row (more precisely, the element on row ii and column M1M − 1 is the sum of elements on row ii situated on the columns 0,1,2,...,M20, 1, 2,... , M − 2), and the elements on the last line are the sums of their column (more precisely, the element on column ii and row N1N − 1 is the sum of elements on column ii situated on the rows 0,1,2,...,N20, 1, 2,... , N − 2). An example of such a table is shown below.

2 2\ 5 5\ 7 7\ 14 \bold{14\ }
11 11\ 6 6\ 6 6\ 23 \bold{23\ }
13 \bold{13\ } 11 \bold{11\ } 13 \bold{13\ } 37 \bold{37\ }

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 (i,j)(i, j) he will need to pay a cost of BijB_{ij}.

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 NN and MM represent the size of the table. The arrays AA and BB (whose rows are indexed from 00 to N1N-1 and columns from 00 to M1M-1) 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

1N,M3 0001 ≤ N, M ≤ 3 \ 000

1Aij1 ≤ A_{ij} if the element is readable or Aij=1A_{ij} = −1 if the element is unreadable

Aij100A_{ij} ≤ 100 for iN1i \neq N - 1 or jM1j \neq M - 1

1Bij1061 ≤ B_{ij} ≤ 10^6

It is guaranteed there is no further information to be extracted from the (readable) values themselves

For tests worth 1010 points, (1N,M4)(1 ≤ N, M ≤ 4).

For tests worth 2525 more points, (1N,M15)(1 ≤ N, M ≤ 15).

For tests worth 4040 more points, (1N,M750)(1 ≤ N, M ≤ 750).

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 (0,0)(0, 0), paying 33 we will have enough information to reconstruct the entire table.

Log in or sign up to be able to send submissions!