#P16840. [GKS 2021 #A] Checksum

[GKS 2021 #A] Checksum

Problem Description

Grace and Edsger are constructing a N×NN \times N boolean matrix AA. The element in the ii-th row and jj-th column is represented by Ai,jA_{i,j}. They decide to note down the checksum (defined as bitwise XOR of given list of elements) along each row and column. Checksum of the ii-th row is represented as RiR_i. Checksum of the jj-th column is represented as CjC_j.

For example, if N=2N = 2, A=[1011]A = \begin{bmatrix} 1 & 0 \\ 1 & 1 \end{bmatrix}, then R=[10]R = [1 \quad 0] and C=[01]C = [0 \quad 1].

Once they finished the matrix, Edsger stores the matrix in his computer. However, due to a virus, some of the elements in matrix AA are replaced with 1-1 in Edsger's computer. Luckily, Edsger still remembers the checksum values. He would like to restore the matrix, and reaches out to Grace for help. After some investigation, it will take Bi,jB_{i,j} hours for Grace to recover the original value of Ai,jA_{i,j} from the disk. Given the final matrix AA, cost matrix BB, and checksums along each row (RR) and column (CC), can you help Grace decide on the minimum total number of hours needed in order to restore the original matrix AA?

Input Format

The first line of the input gives the number of test cases, TT. TT test cases follow.

The first line of each test case contains a single integer NN.

The next NN lines each contain NN integers representing the matrix AA. The jj-th element on the ii-th line represents Ai,jA_{i,j}.

The next NN lines each contain NN integers representing the matrix BB. The jj-th element on the ii-th line represents Bi,jB_{i,j}.

The next line contains NN integers representing the checksum of the rows. The ii-th element represents RiR_i.

The next line contains NN integers representing the checksum of the columns. The jj-th element represents CjC_j.

Output Format

For each test case, output one line containing Case #x: y, where xx is the test case number (starting from 11) and yy is the minimum number of hours to restore matrix AA.

3
3
1 -1 0
0 1 0
1 1 1
0 1 0
0 0 0
0 0 0
1 1 1
0 0 1
2
-1 -1
-1 -1
1 10
100 1000
1 0
0 1
3
-1 -1 -1
-1 -1 -1
0 0 0
1 1 3
5 1 4
0 0 0
0 0 0
0 0 0
Case #1: 0
Case #2: 1
Case #3: 2

Hint

In Sample Case #11, A1,2A_{1,2} can be restored using the checksum of either 11-st row or 22-nd column. Hence, Grace can restore the matrix without spending any time to recover the data.

In Sample Case #22, Grace spends 11 hour to recover A1,1A_{1,1}. After that, she can use checksums of 11-st row and 11-st column to restore A1,2A_{1,2} and A2,1A_{2,1} respectively. And then she can use checksum of 22-nd row to restore A2,2A_{2,2}. Hence, Grace can restore the matrix by spending 11 hour.

In Sample Case #33, Grace can spend 11 hour to recover A1,1A_{1,1} and another hour to recover A2,2A_{2,2}. After that, she can use checksum to restore the rest of the matrix. Hence, Grace can restore the matrix by spending 22 hours in total.

Limits

1T1001 \le T \le 100.

1Ai,j1-1 \le A_{i,j} \le 1, for all ii, jj.

1Bi,j10001 \le B_{i,j} \le 1000, for ii, jj where Ai,j=1A_{i,j} = -1, otherwise Bi,j=0B_{i,j} = 0.

0Ri10 \le R_i \le 1, for all ii.

0Cj10 \le C_j \le 1, for all jj.

It is guaranteed that there exist at least one way to replace 1-1 in AA with 00 or 11 such that RR and CC are satisfied.

Test Set 11

1N41 \le N \le 4.

Test Set 22

1N401 \le N \le 40.

Test Set 33

1N5001 \le N \le 500.