Problem:
Score : 600 points

Problem Statement

Given are an integer N and arrays S, T, U, and V, each of length N.
Construct an N×N matrix a that satisfy the following conditions:

a_{i,j} is an integer.

0 \leq a_{i,j} \lt 2^{64}.

If S_{i} = 0, the bitwise AND of the elements in the i-th row is U_{i}.

If S_{i} = 1, the bitwise OR of the elements in the i-th row is U_{i}.

If T_{i} = 0, the bitwise AND of the elements in the i-th column is V_{i}.

If T_{i} = 1, the bitwise OR of the elements in the i-th column is V_{i}.

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

Constraints

All values in input are integers.

1 \leq N \leq 500

0 \leq S_{i} \leq 1

0 \leq T_{i} \leq 1

0 \leq U_{i} \lt 2^{64}

0 \leq V_{i} \lt 2^{64}

Input

Input is given from Standard Input in the following format:

N
S_{1} S_{2} ... S_{N}
T_{1} T_{2} ... T_{N}
U_{1} U_{2} ... U_{N}
V_{1} V_{2} ... V_{N}

Output

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

a_{1,1} ... a_{1,N}
:
a_{N,1} ... a_{N,N}

Note that any matrix satisfying the conditions is accepted.

If no matrix satisfies the conditions, print -1.

Sample Input 1

2
0 1
1 0
1 1
1 0

Sample Output 1

1 1
1 0

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

the bitwise AND of the elements in the 1-st row is 1;

the bitwise OR of the elements in the 2-nd row is 1;

the bitwise OR of the elements in the 1-st column is 1;

the bitwise AND of the elements in the 2-nd column is 0.

Sample Input 2

2
1 1
1 0
15 15
15 11

Sample Output 2

15 11
15 11

Solution in C (GCC 9.2.1) (judge verdict: Wrong Answer)

#include <stdio.h>

int main()
{
	int i, N, S[501], T[501];
	unsigned long long U[501], V[501];
	scanf("%d", &N);
	for (i = 1; i <= N; i++) scanf("%d", &(S[i]));
	for (i = 1; i <= N; i++) scanf("%d", &(T[i]));
	for (i = 1; i <= N; i++) scanf("%llu", &(U[i]));
	for (i = 1; i <= N; i++) scanf("%llu", &(V[i]));
	if (N == 1) {
		if (U[1] == V[1]) printf("%llu\n", U[1]);
		else printf("-1\n");
		fflush(stdout);
		return 0;
	}
	
	int j, k, flag[2][501], check, count[2];
	unsigned long long ans[501][501] = {}, b = 1;
	for (k = 0; k < 64; k++, b <<= 1) {
		for (i = 1; i <= N; i++) {
			if (S[i] == 0 && (U[i] & 1) == 1) flag[0][i] = 1;
			else if (S[i] == 1 && (U[i] & 1) == 0) flag[0][i] = 0;
			else flag[0][i] = -1;
			if (T[i] == 0 && (V[i] & 1) == 1) flag[1][i] = 1;
			else if (T[i] == 1 && (V[i] & 1) == 0) flag[1][i] = 0;
			else flag[1][i] = -1;
		}
		for (i = 1, check = -1; i <= N; i++) {
			if (check == -1 && flag[0][i] != check) check = flag[0][i];
			if (check == -1 && flag[1][i] != check) check = flag[1][i];
			if (check != -1 && flag[0][i] != -1 && flag[0][i] != check) break;
			if (check != -1 && flag[1][i] != -1 && flag[1][i] != check) break;
		}
		if (i <= N) break;
		
		if (check == -1) {
			for (i = 1; i <= N; i++) ans[i][i] += b;
		} else if (check == 0) {
			for (i = 1, count[0] = 0, count[1] = 0; i <= N; i++) {
				if (flag[0][i] == 0) count[0]++;
				else if (S[i] == 1 && (U[i] & 1) == 1) flag[0][i] = 1;
				if (flag[1][i] == 0) count[1]++;
				else if (T[i] == 1 && (V[i] & 1) == 1) flag[1][i] = 1;
			}
			if (count[0] == N - 1 && count[1] == 0) {
				for (i = 1; i <= N; i++) if (flag[0][i] == 1 || flag[1][i] == -1) break;
				if (i > N) break;
			} else if (count[1] == N - 1 && count[0] == 0) {
				for (i = 1; i <= N; i++) if (flag[1][i] == 1 || flag[0][i] == -1) break;
				if (i > N) break;
			}
			for (i = 1, count[0] = 0, count[1] = 0; i <= N; i++) {
				if (flag[0][i] == 1) count[0]++;
				if (flag[1][i] == 1) count[1]++;
			}
			if (count[0] + count[1] > 0) {
				for (i = 1, j = 1, check = 0; check < 3; ) {
					for (; ((check & 1) == 0 && flag[0][i] != 1) || ((check & 1) == 1 && flag[0][i] == 0); i++) {
						if (i == N) {
							check |= 1;
							if (check == 3) break;
							i = 0;
						}
					}
					for (; ((check & 2) == 0 && flag[1][j] != 1) || ((check & 2) == 2 && flag[1][j] == 0); j++) {
						if (j == N) {
							check |= 2;
							if (check == 3) break;
							j = 0;
						}
					}
					ans[i++][j++] += b;
					if (i > N) {
						check |= 1;
						i = 1;
					}
					if (j > N) {
						check |= 2;
						j = 1;
					}
				}
			}
		} else {
			for (i = 1; i <= N; i++) {
				for (j = 1; j <= N; j++) ans[i][j] += b;
			}
			for (i = 1, count[0] = 0, count[1] = 0; i <= N; i++) {
				if (flag[0][i] == 1) count[0]++;
				else if (S[i] == 0 && (U[i] & 1) == 0) flag[0][i] = 0;
				if (flag[1][i] == 1) count[1]++;
				else if (T[i] == 0 && (V[i] & 1) == 0) flag[1][i] = 0;
			}
			
			if (count[0] == N - 1 && count[1] == 0) {
				for (i = 1; i <= N; i++) if (flag[0][i] == 0 || flag[1][i] == -1) break;
				if (i > N) break;
			} else if (count[1] == N - 1 && count[0] == 0) {
				for (i = 1; i <= N; i++) if (flag[1][i] == 0 || flag[0][i] == -1) break;
				if (i > N) break;
			}
			for (i = 1, count[0] = 0, count[1] = 0; i <= N; i++) {
				if (flag[0][i] == 0) count[0]++;
				if (flag[1][i] == 0) count[1]++;
			}
			if (count[0] + count[1] > 0) {
				for (i = 1, j = 1, check = 0; check < 3; ) {
					for (; ((check & 1) == 0 && flag[0][i] != 0) || ((check & 1) == 1 && flag[0][i] == 1); i++) {
						if (i == N) {
							check |= 1;
							if (check == 3) break;
							i = 0;
						}
					}
					for (; ((check & 2) == 0 && flag[1][j] != 0) || ((check & 2) == 2 && flag[1][j] == 1); j++) {
						if (j == N) {
							check |= 2;
							if (check == 3) break;
							j = 0;
						}
					}
					printf("%d %d %d\n", k, i, j);
					ans[i++][j++] -= b;
					if (i > N) {
						check |= 1;
						i = 1;
					}
					if (j > N) {
						check |= 2;
						j = 1;
					}
				}
			}
		}
		
		for (i = 1; i <= N; i++) {
			U[i] >>= 1;
			V[i] >>= 1;
		}
	}

	if (k == 64) {
		for (i = 1; i <= N; i++) {
			for (j = 1; j < N; j++) printf("%llu ", ans[i][j]);
			printf("%llu\n", ans[i][N]);
		}
	} else printf("-1\n");
	fflush(stdout);
	return 0;
}