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>
using namespace std;
#define FOR(i, a, b) for(long long i=(a);i<(b);++i)
#define rep(i, n) FOR(i, 0, n)
#define whole(x) (x).begin(),(x).end()
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end())
using ll = long long;
using P = pair<int, int>;
const int mod = 1000000007;

ll s[505];
ll t[505];
ll u[505];
ll v[505];
ll su[505];
ll sv[505];
ll ans[505][505][64];

int main(){
    ll n;
    cin >> n;
    rep(i, n) cin >> s[i];
    rep(i, n) cin >> t[i];
    rep(i, n) cin >> u[i];
    rep(i, n) cin >> v[i];

    rep(k, 64) {
        
        rep(i, n) {
            rep(j, n) {
                ans[i][j][k] = -1;
            }
        }
        
        vector<ll> iu(n);
        vector<ll> iv(n);
        vector<ll> ru;
        vector<ll> rv;
        rep(i, n) {
            iu[i] = (u[i]>>k)&1;
            iv[i] = (v[i]>>k)&1;
            if (s[i]==0 && iu[i]==1) {
                rep(j, n) {
                    if (ans[i][j][k]==0) {
                        cout << "-1" << endl;
                        return 0;
                    }
                    ans[i][j][k] = 1;
                }
            }
            else if (s[i]==1 && iu[i]==0) {
                rep(j, n) {
                    if (ans[i][j][k]==1) {
                        cout << "-1" << endl;
                        return 0;
                    }
                    ans[i][j][k] = 0;
                }
            }
            else ru.push_back(i);

            if (t[i]==0 && iv[i]==1) {
                rep(j, n) {
                    if (ans[j][i][k]==0) {
                        cout << "-1" << endl;
                        return 0;
                    }
                    ans[j][i][k] = 1;
                }
            }
            else if (t[i]==1 && iv[i]==0) {
                rep(j, n) {
                    if (ans[j][i][k]==1) {
                        cout << "-1" << endl;
                        return 0;
                    }
                    ans[j][i][k] = 0;
                }
            }
            else rv.push_back(i);
        }
        if (ru.size()==0 || rv.size()==0) continue;
        if (ru.size()>=2 && rv.size()>=2) {
            rep(i, ru.size()) rep(j, rv.size()) {
                if ((abs(i-j)%min(ru.size(), rv.size()))==0) ans[ru[i]][rv[j]][k] = 1;
                else ans[ru[i]][rv[j]][k] = 0;
            }
            continue;
        }
        vector<ll> remain;
        if (ru.size()==1) {
            rep(j, rv.size()) {
                ll sum = 0;
                rep(i, n) {
                    if (i==ru[0]) continue;
                    sum += ans[i][rv[j]][k];
                }
                if (t[rv[j]]==0 && sum==n-1) ans[ru[0]][rv[j]][k] = 0;
                if (t[rv[j]]==1 && sum==0) ans[ru[0]][rv[j]][k] = 1; 
            }
            ll sum = 0;
            rep(j, n) {
                if (ans[ru[0]][j][k]==-1) remain.push_back(j);
                else sum += ans[ru[0]][j][k];
            }
            if (remain.size()==0) {
                if (s[ru[0]]==0 && sum==n) {
                    cout << "-1" << endl;
                    return 0;
                }
                if (s[ru[0]]==1 && sum==0) {
                    cout << "-1" << endl;
                    return 0;
                }
                continue;
            }
            rep(j, remain.size()) {
                ans[ru[0]][remain[j]][k] = s[ru[0]];
            }
            continue;
        }
        if (rv.size()==1) {
            rep(i, ru.size()) {
                ll sum = 0;
                rep(j, n) {
                    if (j==rv[0]) continue;
                    sum += ans[ru[i]][j][k];
                }
                if (s[ru[i]]==0 && sum==n-1) ans[ru[i]][rv[0]][k] = 0;
                if (s[ru[i]]==1 && sum==0) ans[ru[i]][rv[0]][k] = 1; 
            }
            ll sum = 0;
            rep(i, n) {
                if (ans[i][rv[0]][k]==-1) remain.push_back(i);
                else sum += ans[i][rv[0]][k];
            }
            if (remain.size()==0) {
                if (t[rv[0]]==0 && sum==n) {
                    cout << "-1" << endl;
                    return 0;
                }
                if (t[rv[0]]==1 && sum==0) {
                    cout << "-1" << endl;
                    return 0;
                }
                continue;
            }
            rep(i, remain.size()) {
                ans[remain[i]][rv[0]][k] = t[rv[0]];
            }
        }
    }
    
    //cout << "aaa " << ans[0][0][0] << " " << ans[0][1][0] << " " << ans[1][0][0] << " " << ans[1][1][0] << endl;
    
    
    rep(i, n) {
        rep(j, n) {
            ll v = 0;
            rep(k, 64) {
                v |= ans[i][j][k]<<k;
            }
            cout << v << " ";
        }
        cout << endl;
    }
    return 0;
}