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 Java (OpenJDK 1.8.0) (judge verdict: Time Limit Exceeded)

import java.io.*;
import java.math.BigInteger;
import java.util.*;

public class Main{
	static void main() throws Exception{
		int n=sc.nextInt();
		int[][]op=new int[][] {sc.intArr(n),sc.intArr(n)};
		BigInteger[]masks=new BigInteger[64];
		masks[0]=new BigInteger("1");
		for(int i=1;i<64;i++) {
			masks[i]=masks[i-1].multiply(new BigInteger("2"));
		}
		long[][][]vals=new long[2][n][64];
		for(int i=0;i<2;i++) {
			for(int j=0;j<n;j++) {
				BigInteger cur=new BigInteger(sc.next());
				BigInteger zero=new BigInteger("0");
				for(int bit=0;bit<64;bit++) {
					if(!((cur.and(masks[bit])).equals(zero))) {
						vals[i][j][bit]=1;
					}
				}
			}
		}
		long[][][]ans=new long[n][n][64];
		for(int bit=0;bit<64;bit++) {
			for(int row=0;row<n;row++) {
				//and of row is 1
				if(op[0][row]==0 && vals[0][row][bit]!=0) {
					for(int j=0;j<n;j++) {
						ans[row][j][bit]=1;
					}
				}
			}
			for(int col=0;col<n;col++) {
				//and of col is 1
				if(op[1][col]==0 && (vals[1][col][bit])!=0) {
					for(int j=0;j<n;j++) {
						ans[j][col][bit]=1;
					}
				}
			}
		}
		
		for(int bit=0;bit<64;bit++) {
			for(int row=0;row<n;row++) {
				//or of row is 0
				if(op[0][row]==1 && (vals[0][row][bit])==0) {
					for(int j=0;j<n;j++) {
						if((ans[row][j][bit])!=0){
							pw.println(-1);
							return;
						}
					}
				}
			}
			for(int col=0;col<n;col++) {
				//or of col is 0
				if(op[1][col]==1 && (vals[1][col][bit])==0) {
					for(int j=0;j<n;j++) {
						if((ans[j][col][bit])!=0){
							pw.println(-1);
							return;
						}
					}
				}
			}
		}
		int[][][]cntZeros=new int[2][n][64];
		for(int i=0;i<n;i++) {
			for(int j=0;j<n;j++) {
				for(int bit=0;bit<64;bit++) {
					cntZeros[0][i][bit]+=(ans[i][j][bit])==0?1:0;
					cntZeros[1][j][bit]+=(ans[i][j][bit])==0?1:0;
				}
			}
		}
		//fill cells with row with or=1 and col with or=1 with ones
		for(int bit=0;bit<64;bit++) {
			for(int i=0;i<n;i++) {
				for(int j=0;j<n;j++) {
					if(op[0][i]==1 && (vals[0][i][bit])!=0 && op[1][j]==1 && (vals[1][j][bit])!=0) {
						ans[i][j][bit]=1;
					}
				}
			}
		}
		
		
		//or is 1
		for(int bit=0;bit<64;bit++) {
			o:for(int row=0;row<n;row++) {
				if(op[0][row]==1 && (vals[0][row][bit])!=0) {
					for(int j=0;j<n;j++) {
						if((ans[row][j][bit])!=0) {
							continue o;
						}
					}
					
					
					//find column with and = 0 and has more than one zero
					int col=-1;
					for(int j=0;j<n;j++) {
						if(op[1][j]==0 && (vals[1][j][bit])==0 && cntZeros[1][j][bit]>1) {
							col=j;
							break;
						}
					}
					if(col==-1) {
						pw.println(-1);
						return;
					}
					cntZeros[1][col][bit]--;
					ans[row][col][bit]=1;
				}
			}
			o:for(int col=0;col<n;col++) {
				if(op[1][col]==1 && (vals[1][col][bit])!=0) {
					for(int j=0;j<n;j++) {
						if((ans[j][col][bit])!=0) {
							continue o;
						}
					}
					
					
					//find row with and = 0 and has more than one zero 
					int row=-1;
					for(int j=0;j<n;j++) {
						if(op[0][j]==0 && (vals[0][j][bit])==0 && cntZeros[0][j][bit]>1) {
							row=j;
							break;
						}
					}
					if(row==-1) {
						pw.println(-1);
						return;
					}
					cntZeros[0][row][bit]--;
					ans[row][col][bit]=1;
				}
			}
		}
		for(int bit=0;bit<64;bit++) {
			for(int row=0;row<n;row++) {
				int[]ops=new int[2];
				ops[0]=1;
				for(int j=0;j<n;j++) {
					ops[0]&=ans[row][j][bit];
					ops[1]|=ans[row][j][bit];
				}
				if(ops[op[0][row]]!=vals[0][row][bit]) {
					pw.println(-1);
					return;
				}
			}
		}
		for(int bit=0;bit<64;bit++) {
			for(int col=0;col<n;col++) {
				int[]ops=new int[2];
				ops[0]=1;
				for(int j=0;j<n;j++) {
					ops[0]&=ans[j][col][bit];
					ops[1]|=ans[j][col][bit];
				}
				if(ops[op[1][col]]!=vals[1][col][bit]) {
					pw.println(-1);
					return;
				}
			}
		}
		
		for(int i=0;i<n;i++) {
			for(int j=0;j<n;j++){
				BigInteger cur=new BigInteger("0");
				for(int bit=0;bit<64;bit++) {
					if(ans[i][j][bit]==0)continue;
					cur=cur.or(masks[bit]);
				}
				pw.print(cur+" ");
			}
			pw.println();
		}
	}
	public static void main(String[] args) throws Exception{
		sc=new MScanner(System.in);
		pw = new PrintWriter(System.out);
		int tc=1;
//		tc=sc.nextInt();
		while(tc-->0)
			main();
		pw.flush();
	}
	static PrintWriter pw;
	static MScanner  sc;
	static class MScanner {
		StringTokenizer st;
		BufferedReader br;
		public MScanner(InputStream system) {
			br = new BufferedReader(new InputStreamReader(system));
		}
 
		public MScanner(String file) throws Exception {
			br = new BufferedReader(new FileReader(file));
		}
 
		public String next() throws IOException {
			while (st == null || !st.hasMoreTokens())
				st = new StringTokenizer(br.readLine());
			return st.nextToken();
		}
		public int[] intArr(int n) throws IOException {
	        int[]inArr=new int[n];for(int i=0;i<n;i++)inArr[i]=nextInt();
	        return inArr;
		}
		public long[] longArr(int n) throws IOException {
	        long[]inArr=new long[n];for(int i=0;i<n;i++)inArr[i]=nextLong();
	        return inArr;
		}
		public int[] intSortedArr(int n) throws IOException {
	        int[]inArr=new int[n];for(int i=0;i<n;i++)inArr[i]=nextInt();
	        shuffle(inArr);
	        Arrays.sort(inArr);
	        return inArr;
		}
		public long[] longSortedArr(int n) throws IOException {
	        long[]inArr=new long[n];for(int i=0;i<n;i++)inArr[i]=nextLong();
	        shuffle(inArr);
	        Arrays.sort(inArr);
	        return inArr;
		}
		public Integer[] IntegerArr(int n) throws IOException {
	        Integer[]inArr=new Integer[n];for(int i=0;i<n;i++)inArr[i]=nextInt();
	        return inArr;
		}
		public Long[] LongArr(int n) throws IOException {
	        Long[]inArr=new Long[n];for(int i=0;i<n;i++)inArr[i]=nextLong();
	        return inArr;
		}
		public String nextLine() throws IOException {
			return br.readLine();
		}
 
		public int nextInt() throws IOException {
			return Integer.parseInt(next());
		}
 
		public double nextDouble() throws IOException {
			return Double.parseDouble(next());
		}
 
		public char nextChar() throws IOException {
			return next().charAt(0);
		}
 
		public long nextLong() throws IOException {
			return Long.parseLong(next());
		}
 
		public boolean ready() throws IOException {
			return br.ready();
		}
 
		public void waitForInput() throws InterruptedException {
			Thread.sleep(3000);
		}
	}
	static void shuffle(int[]inArr) {
		for(int i=0;i<inArr.length;i++) {
			int idx=i+(int)(Math.random()*(inArr.length-i));
			int tmp=inArr[i];
			inArr[i]=inArr[idx];
			inArr[idx]=tmp;
		}
	}
	static void shuffle(long[]inArr) {
		for(int i=0;i<inArr.length;i++) {
			int idx=i+(int)(Math.random()*(inArr.length-i));
			long tmp=inArr[i];
			inArr[i]=inArr[idx];
			inArr[idx]=tmp;
		}
	}
	static void shuffle(String[]inArr) {
		for(int i=0;i<inArr.length;i++) {
			int idx=i+(int)(Math.random()*(inArr.length-i));
			String tmp=inArr[i];
			inArr[i]=inArr[idx];
			inArr[idx]=tmp;
		}
	}
}