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# (.NET Core 3.1.201) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using CompLib.Util;

public class Program
{
    int N;
    int[] S;
    int[] T;
    ulong[] U;
    ulong[] V;
    public void Solve()
    {
        var sc = new Scanner();
        N = sc.NextInt();
        S = sc.IntArray();
        T = sc.IntArray();
        U = sc.Array().Select(s => ulong.Parse(s)).ToArray();
        V = sc.Array().Select(s => ulong.Parse(s)).ToArray();
        ulong[][] ans = new ulong[N][];
        for (int i = 0; i < N; i++)
        {
            ans[i] = new ulong[N];
        }
        for (int t = 0; t < 64; t++)
        {
            ulong bit = 1UL << t;
            bool[] flagU = new bool[N];
            bool[] flagV = new bool[N];
            // 0かつbitが立ってるなら全部1
            for (int i = 0; i < N; i++)
            {
                bool uu = (U[i] & bit) == bit;
                bool vv = (V[i] & bit) == bit;
                if (S[i] == 0 && uu)
                {
                    flagU[i] = true;
                    for (int j = 0; j < N; j++)
                    {
                        ans[i][j] |= bit;
                        flagV[j] = true;
                    }
                }
                if (T[i] == 0 && vv)
                {
                    flagV[i] = true;
                    for (int j = 0; j < N; j++)
                    {
                        ans[j][i] |= bit;
                        flagU[j] = true;
                    }
                }
            }
            // U,V両方立ってる　立てる
            for (int i = 0; i < N; i++)
            {
                bool uu = (U[i] & bit) == bit;
                for (int j = 0; j < N; j++)
                {

                    bool vv = (V[j] & bit) == bit;
                    if (uu && vv)
                    {
                        ans[i][j] |= bit;
                        flagU[i] = true;
                        flagV[j] = true;
                    }
                }
            }
            // 1かつbitが立ってないなら全部0 矛盾するなら -1
            for (int i = 0; i < N; i++)
            {
                bool uu = (U[i] & bit) == bit;
                bool vv = (V[i] & bit) == bit;

                if (S[i] == 1 && !uu && flagU[i])
                {
                    Console.WriteLine("-1");
                    return;
                }
                if (T[i] == 1 && !vv && flagV[i])
                {
                    Console.WriteLine("-1");
                    return;
                }
            }
            // のこり
            // 1かつbitが立ってる まだ立ててない

            // 行
            // 列 どの列も 立ってる列がない
            // 1かつ立ってる行がある 0かつ立ってないがない or
            // 全部1かつ立ってる 列 0かつ立ってないが1以下 
            // -1
            List<int> row = new List<int>();
            List<int> col = new List<int>();
            bool andR = true;
            bool andC = true;
            bool orR = false;
            bool orC = false;
            for (int i = 0; i < N; i++)
            {
                bool uu = (U[i] & bit) == bit;
                bool vv = (V[i] & bit) == bit;
                if (S[i] == 0 && !uu) row.Add(i);
                andR &= S[i] == 1 && uu && !flagU[i];
                orR |= S[i] == 1 && uu && !flagU[i];

                if (T[i] == 0 && !vv) col.Add(i);
                andC &= T[i] == 1 && vv && !flagV[i];
                orC |= T[i] == 1 && vv && !flagV[i];
            }

            if ((orR && col.Count == 0) || (orC && row.Count == 0) || (andR && col.Count <= 1) || (andC && row.Count <= 1))
            {
                Console.WriteLine("-1");
                return;
            }

            if (orR)
            {
                int cnt = 0;
                for (int i = 0; i < N; i++)
                {
                    bool uu = (U[i] & bit) == bit;
                    if (S[i] == 1 && uu && !flagU[i])
                    {
                        ans[i][col[(cnt++) % col.Count]] |= bit;
                    }
                }
            }
            else if (orC)
            {
                int cnt = 0;
                for (int i = 0; i < N; i++)
                {
                    bool vv = (V[i] & bit) == bit;
                    if (T[i] == 1 && vv && !flagV[i])
                    {
                        ans[row[(cnt++) % row.Count]][i] |= bit;
                    }
                }
            }
        }

        for (int i = 0; i < N; i++)
        {
            if (S[i] == 0)
            {
                ulong u = ulong.MaxValue;
                for (int j = 0; j < N; j++)
                {
                    u &= ans[i][j];
                }
                if (U[i] != u)
                {
                    Console.WriteLine("-1");
                    return;
                }
            }
            else
            {
                ulong u = 0;
                for (int j = 0; j < N; j++)
                {
                    u |= ans[i][j];
                }
                if (U[i] != u)
                {
                    Console.WriteLine("-1");
                    return;
                }
            }
            if (T[i] == 0)
            {
                ulong v = ulong.MaxValue;
                for (int j = 0; j < N; j++)
                {
                    v &= ans[j][i];
                }
                if (V[i] != v)
                {
                    Console.WriteLine("-1");
                    return;
                }
            }
            else
            {
                ulong v = 0;
                for (int j = 0; j < N; j++)
                {
                    v |= ans[j][i];
                }
                if (V[i] != v)
                {
                    Console.WriteLine("-1");
                    return;
                }
            }
        }


        Console.WriteLine(string.Join("\n", ans.Select(ln => string.Join(" ", ln))));
    }

    public static void Main(string[] args) => new Program().Solve();
}

namespace CompLib.Util
{
    using System;
    using System.Linq;

    class Scanner
    {
        private string[] _line;
        private int _index;
        private const char Separator = ' ';

        public Scanner()
        {
            _line = new string[0];
            _index = 0;
        }

        public string Next()
        {
            while (_index >= _line.Length)
            {
                _line = Console.ReadLine().Split(Separator);
                _index = 0;
            }

            return _line[_index++];
        }

        public int NextInt() => int.Parse(Next());
        public long NextLong() => long.Parse(Next());
        public double NextDouble() => double.Parse(Next());
        public decimal NextDecimal() => decimal.Parse(Next());
        public char NextChar() => Next()[0];
        public char[] NextCharArray() => Next().ToCharArray();

        public string[] Array()
        {
            _line = Console.ReadLine().Split(Separator);
            _index = _line.Length;
            return _line;
        }

        public int[] IntArray() => Array().Select(int.Parse).ToArray();
        public long[] LongArray() => Array().Select(long.Parse).ToArray();
        public double[] DoubleArray() => Array().Select(double.Parse).ToArray();
        public decimal[] DecimalArray() => Array().Select(decimal.Parse).ToArray();
    }
}