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 <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#include <list>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;

int main()
{
    using ull=unsigned long long;
    int n;
    cin>>n;
    int s[505], t[505];
    ull u[505], v[505];
    for(int i=0; i<n; i++) cin>>s[i];
    for(int i=0; i<n; i++) cin>>t[i];
    for(int i=0; i<n; i++) cin>>u[i];
    for(int i=0; i<n; i++) cin>>v[i];
    ull a[505][505]={};
    for(int k=0; k<64; k++){
        int x[505][505];
        for(int i=0; i<n; i++) for(int j=0; j<n; j++) x[i][j]=-1;
        auto myon=[&](){
            for(int i=0; i<n; i++){
                for(int j=0; j<n; j++){
                    if(x[i][j]) a[i][j]^=(1ull<<k);
                }
            }
        };
        int x0=0, x1=0, y0=0, y1=0;
        for(int i=0; i<n; i++){
            if(s[i]==0 && ((u[i]>>k)&1ull)){
                x1=1;
                for(int j=0; j<n; j++) x[i][j]=1;
            }
            if(s[i]==1 && ((u[i]>>k)&1ull)==0){
                x0=1;
                for(int j=0; j<n; j++) x[i][j]=0;
            }
        }
        for(int j=0; j<n; j++){
            if(t[j]==0 && ((v[j]>>k)&1ull)){
                y1=1;
                for(int i=0; i<n; i++){
                    if(x[i][j]==0){
                        cout<<-1<<endl;
                        return 0;
                    }
                    x[i][j]=1;
                }
            }
            if(t[j]==1 && ((v[j]>>k)&1ull)==0){
                y0=1;
                for(int i=0; i<n; i++){
                    if(x[i][j]==1){
                        cout<<-1<<endl;
                        return 0;
                    }
                    x[i][j]=0;
                }
            }
        }
        if((x0 && x1) || (y0 && y1)){
            for(int i=0; i<n; i++){
				for(int j=0; j<n; j++){
					if(x[i][j]==-1){
						if(x0 && x1){
							x[i][j]=s[i];
						}else{
							x[i][j]=t[j];
						}
					}
				}
			}
            myon(); continue;
        }
        if(x0 && y0){
            for(int i=0; i<n; i++) for(int j=0; j<n; j++) if(x[i][j]==-1) x[i][j]=1;
            myon(); continue;
        }
        if(x1 && y1){
            for(int i=0; i<n; i++) for(int j=0; j<n; j++) if(x[i][j]==-1) x[i][j]=0;
            myon(); continue;
        }
        int z0=0, z1=0, w0=0, w1=0;
        for(int i=0; i<n; i++){
            if(s[i]==0 && ((u[i]>>k)&1ull)==0){
                z0=1;
            }
            if(s[i]==1 && ((u[i]>>k)&1ull)){
                z1=1;
            }
        }
        for(int j=0; j<n; j++){
            if(t[j]==0 && ((v[j]>>k)&1ull)==0){
                w0=1;
            }
            if(t[j]==1 && ((v[j]>>k)&1ull)){
                w1=1;
            }
        }
        if(!x0 && !x1 && !y0 && !y1){
            if(z0!=z1 && w0!=w1 && z0!=w0){
                for(int i=0; i<n; i++){
                    for(int j=0; j<n; j++){
                        if(i==j) x[i][j]=1;
                        else x[i][j]=0;
                    }
                }
            }else if(z0 && z1){
                for(int i=0; i<n; i++){
                    for(int j=0; j<n; j++){
                        if(s[i]==0) x[i][j]=0;
                        else x[i][j]=1;
                    }
                }
            }else if(w0 && w1){
                for(int i=0; i<n; i++){
                    for(int j=0; j<n; j++){
                        if(t[j]==0) x[i][j]=0;
                        else x[i][j]=1;
                    }
                }
            }else if(z0){
                for(int i=0; i<n; i++){
                    for(int j=0; j<n; j++){
                        x[i][j]=0;
                    }
                }
            }else{
                for(int i=0; i<n; i++){
                    for(int j=0; j<n; j++){
                        x[i][j]=1;
                    }
                }
            }
            myon(); continue;
        }
        if((z0 && x1) || (w0 && y1)){
            for(int i=0; i<n; i++){
                for(int j=0; j<n; j++) if(x[i][j]!=1) x[i][j]=0;
            }
            myon(); continue;
        }
        if((z1 && x0) || (w1 && y0)){
            for(int i=0; i<n; i++){
                for(int j=0; j<n; j++) if(x[i][j]!=0) x[i][j]=1;
            }
            myon(); continue;
        }
        if(z0 && z1){
            for(int i=0; i<n; i++){
                for(int j=0; j<n; j++){
                    if(x[i][j]==-1 && s[i]==0) x[i][j]=0;
                    else if(x[i][j]==-1 && s[i]==1) x[i][j]=1;
                }
            }
            myon(); continue;
        }
        if(w0 && w1){
            for(int i=0; i<n; i++){
                for(int j=0; j<n; j++){
                    if(x[i][j]==-1 && t[j]==0) x[i][j]=0;
                    else if(x[i][j]==-1 && t[j]==1) x[i][j]=1;
                }
            }
            myon(); continue;
        }
        if((w1 && x1) || (w0 && x0)){
            for(int i=0; i<n; i++){
                for(int j=0; j<n; j++){
                    if(x[i][j]==-1 && s[i]==1) x[i][j]=1;
                    else if(x[i][j]==-1 && s[i]==0) x[i][j]=0;
                }
            }
            myon(); continue;
        }
        if((z1 && y1) || (z0 && y0)){
            for(int i=0; i<n; i++){
                for(int j=0; j<n; j++){
                    if(x[i][j]==-1 && t[j]==1) x[i][j]=1;
                    else if(x[i][j]==-1 && t[j]==0) x[i][j]=0;
                }
            }
            myon(); continue;
        }
        int z=0;
        if(x1 || y1) z=1;
        for(int i=0; i<n; i++){
            for(int j=0; j<n; j++){
                if(x[i][j]==-1){
                    if(i==j) x[i][j]=z;
                    else x[i][j]=(z^1);
                }
            }
        }
        myon();
    }
    for(int i=0; i<n; i++){
        ull c=a[i][0];
        for(int j=1; j<n; j++){
            if(s[i]==0) c&=a[i][j];
            else c|=a[i][j];
        }
        if(c!=u[i]){
            cout<<-1<<endl;
            return 0;
        }
    }
    for(int j=0; j<n; j++){
        ull c=a[0][j];
        for(int i=1; i<n; i++){
            if(t[j]==0) c&=a[i][j];
            else c|=a[i][j];
        }
        if(c!=v[j]){
            cout<<-1<<endl;
            return 0;
        }
    }
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++) cout<<a[i][j]<<" ";
        cout<<endl;
    }
	return 0;
}