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 <bits/stdc++.h>
#define MOD 1000000007LL
using namespace std;
typedef long long ll;
typedef pair<int,int> P;

typedef pair<int,int> P;
struct edge{
	int to,cap,cost,rev;
	edge(int t,int c,int co,int r){
		to=t;
		cap=c;
		cost=co;
		rev=r;
	}
};

struct mincostflow{
	vector<edge> G[1005];
	const int INF=1000000;
	int h[1005];
	int dist[1005];
	int prevv[1005],preve[1005];

	void add_edge(int from,int to,int cap,int cost){
		G[from].push_back(edge(to,cap,cost,G[to].size()));
		G[to].push_back(edge(from,0,-cost,G[from].size()-1));
	}

	int min_cost_flow(int s,int t,int f,int V){
		int res=0;
		fill(h,h+V,0);
		while(f>0){
			priority_queue<P,vector<P>,greater<P> > que;
			fill(dist,dist+V,INF);
			dist[s]=0;
			que.push(P(0,s));
			while(que.size()){
				P p=que.top();
				que.pop();
				int v=p.second;
				if(dist[v]<p.first)continue;
				for(int i=0;i<G[v].size();i++){
					edge &e=G[v][i];
					if(e.cap>0 && dist[e.to]>dist[v]+e.cost+h[v]-h[e.to]){
						dist[e.to]=dist[v]+e.cost+h[v]-h[e.to];
						prevv[e.to]=v;
						preve[e.to]=i;
						que.push(P(dist[e.to],e.to));
					}
				}
			}
			if(dist[t]==INF)return 1145141;
			for(int v=0;v<V;v++){
				h[v]+=dist[v];
			}
			int d=f;
			for(int v=t;v!=s;v=prevv[v]){
				d=min(d,G[prevv[v]][preve[v]].cap);
			}
			f-=d;
			res+=d*h[t];
			for(int v=t;v!=s;v=prevv[v]){
				edge &e=G[prevv[v]][preve[v]];
				e.cap-=d;
				G[v][e.rev].cap+=d;
			}
		}
		return res;
	}
};

int n;
int s[501],t[501];
unsigned long long u[501],v[501];
unsigned long long axs[501],axm[501];
unsigned long long ays[501],aym[501];
bool flagx[501],flagy[501];
unsigned long long res[501][501];
mincostflow mi;

int main(void){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		scanf("%d",&s[i]);
	}
	for(int i=0;i<n;i++){
		scanf("%d",&t[i]);
	}
	for(int i=0;i<n;i++){
		scanf("%llu",&u[i]);
	}
	for(int i=0;i<n;i++){
		scanf("%llu",&v[i]);
	}
	/*
	for(int i=0;i<n;i++){
		if(s[i]==0){
			for(int j=0;j<n;j++){
				res[i][j]=u[i];
				if(!flagx[j]){
					axs[j]=res[i][j];
					axm[j]=res[i][j];
				}else{
					axs[j]|=res[i][j];
					axm[j]^=res[i][j];
				}
				if(!flagy[i]){
					ays[i]=res[i][j];
					aym[i]=res[i][j];
				}else{
					ays[i]|=res[i][j];
					aym[i]^=res[i][j];
				}
			}
		}
	}
	for(int i=0;i<n;i++){
		if(t[i]==0){
			for(int j=0;j<n;j++){
				res[j][i]|=v[i];
				if(!flagx[i]){
					axs[i]=res[j][i];
					axm[i]=res[j][i];
				}else{
					axs[i]|=res[j][i];
					axm[i]^=res[j][i];
				}
				if(!flagy[j]){
					ays[j]=res[j][i];
					aym[j]=res[j][i];
				}else{
					ays[j]|=res[j][i];
					aym[j]^=res[j][i];
				}
			}
		}
	}
	*/
	int S=n*2,T=n*2+1;
	for(int i=0;i<64;i++){
		for(int j=0;j<=T;j++){
			mi.G[j].clear();
		}
		int xc=0;
		int yc=0;
		int cnt=0;
		for(int j=0;j<n;j++){
			if(s[j]==0){
				if((u[j]>>i) & 1LL){
					mi.add_edge(S,j,n,-1);
					xc+=n;
					cnt+=n;
				}else{
					mi.add_edge(S,j,n-1,0);
				}
			}else{
				if((u[j]>>i) & 1LL){
					mi.add_edge(S,j,1,-1);
					mi.add_edge(S,j,n-1,0);
					xc++;
					cnt++;
				}
			}
			if(t[j]==0){
				if((v[j]>>i) & 1LL){
					mi.add_edge(n+j,T,n,-1);
					yc+=n;
					cnt+=n;
				}else{
					mi.add_edge(n+j,T,n-1,0);
				}
			}else{
				if((v[j]>>i) & 1LL){
					mi.add_edge(n+j,T,1,-1);
					mi.add_edge(n+j,T,n-1,0);
					yc++;
					cnt++;
				}
			}
			for(int k=0;k<n;k++){
				mi.add_edge(j,n+k,1,0);
			}
		}
		while(cnt>0){
			int co=mi.min_cost_flow(S,T,1,T+1);
			if(co>0){
				printf("-1\n");
				return 0;
			}
			cnt+=co;
		}
		for(int j=0;j<n;j++){
			for(int k=0;k<mi.G[j].size();k++){
				edge e=mi.G[j][k];
				if(e.cap==0 && e.to!=S){
					//printf("%d %d\n",j,e.to);
					res[e.to-n][j]|=1LU<<i;
				}
			}
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			printf("%llu%c",res[i][j],j+1==n?'\n':' ');
		}
	}
	return 0;
}