atcoder#ABC164F. [ABC164F] I hate Matrix Construction

[ABC164F] I hate Matrix Construction

Score : 600600 points

Problem Statement

Given are an integer NN and arrays SS, TT, UU, and VV, each of length NN. Construct an N×NN \times N matrix aa that satisfy the following conditions:

  • ai,ja_{i,j} is an integer.
  • 0ai,j<2640 \leq a_{i,j} \lt 2^{64}.
  • If Si=0S_{i} = 0, the bitwise AND of the elements in the ii-th row is UiU_{i}.
  • If Si=1S_{i} = 1, the bitwise OR of the elements in the ii-th row is UiU_{i}.
  • If Ti=0T_{i} = 0, the bitwise AND of the elements in the ii-th column is ViV_{i}.
  • If Ti=1T_{i} = 1, the bitwise OR of the elements in the ii-th column is ViV_{i}.

However, there may be cases where no matrix satisfies the conditions.

Constraints

  • All values in input are integers.
  • 1N5001 \leq N \leq 500
  • 0Si10 \leq S_{i} \leq 1
  • 0Ti10 \leq T_{i} \leq 1
  • 0Ui<2640 \leq U_{i} \lt 2^{64}
  • 0Vi<2640 \leq V_{i} \lt 2^{64}

Input

Input is given from Standard Input in the following format:

NN

S1S_{1} S2S_{2} ...... SNS_{N}

T1T_{1} T2T_{2} ...... TNT_{N}

U1U_{1} U2U_{2} ...... UNU_{N}

V1V_{1} V2V_{2} ...... VNV_{N}

Output

If there exists a matrix that satisfies the conditions, print one such matrix in the following format:

a1,1a_{1,1} ...... a1,Na_{1,N}

::

aN,1a_{N,1} ...... aN,Na_{N,N}

Note that any matrix satisfying the conditions is accepted.

If no matrix satisfies the conditions, print 1-1.

2
0 1
1 0
1 1
1 0
1 1
1 0

In Sample Input 11, we need to find a matrix such that:

  • the bitwise AND of the elements in the 11-st row is 11;
  • the bitwise OR of the elements in the 22-nd row is 11;
  • the bitwise OR of the elements in the 11-st column is 11;
  • the bitwise AND of the elements in the 22-nd column is 00.
2
1 1
1 0
15 15
15 11
15 11
15 11