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: Wrong Answer)

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 = false;
            bool flagV = false;
            // 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 = true;
                    for (int j = 0; j < N; j++)
                    {
                        ans[i][j] |= bit;
                    }
                }
                if (T[i] == 0 && vv)
                {
                    flagV = true;
                    for (int j = 0; j < N; j++)
                    {
                        ans[j][i] |= bit;
                    }
                }
            }
            // 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)
                {
                    for (int j = 0; j < N; j++)
                    {
                        if ((ans[i][j] & bit) == bit)
                        {
                            Console.WriteLine("-1");
                            return;
                        }
                    }
                }
                if (T[i] == 1 && !vv)
                {
                    for (int j = 0; j < N; j++)
                    {
                        if ((ans[j][i] & bit) == bit)
                        {
                            Console.WriteLine("-1");
                            return;
                        }
                    }
                }
            }
            // のこり
            // 1かつbitが立ってる まだ立ててない
            // 両方0 終わり
            // 行、列 両方1or2以上ある 市松模様
            // 片方1 
            // 0かつbitが立ってないところ or 1かつbitが立ってるとこ1つ含めて市松模様
            List<int> row = new List<int>();
            List<int> col = new List<int>();
            for (int i = 0; i < N; i++)
            {

                bool uu = (U[i] & bit) == bit;
                bool vv = (V[i] & bit) == bit;
                if (S[i] == 1 && uu && !flagV) row.Add(i);
                if (T[i] == 1 && vv && !flagU) col.Add(i);
            }

            if (row.Count < 2 && col.Count >= 2)
            {
                for (int i = 0; i < N && row.Count < 2; i++)
                {
                    bool uu = (U[i] & bit) == bit;
                    if (uu ^ (S[i] == 0)) row.Add(i);
                }
                if (row.Count < 2)
                {
                    Console.WriteLine("-1");
                    return;
                }
            }
            else if (col.Count < 2 && row.Count >= 2)
            {
                for (int i = 0; i < N && col.Count < 2; i++)
                {
                    bool vv = (V[i] & bit) == bit;
                    if (vv ^ (T[i] == 0)) col.Add(i);
                }
                if (col.Count < 2)
                {
                    Console.WriteLine("-1");
                    return;
                }
            }
            else if (row.Count == 0 && col.Count == 1)
            {
                for (int i = 0; i < N && row.Count < 1; i++)
                {
                    bool uu = (U[i] & bit) == bit;
                    if (uu ^ (S[i] == 0)) row.Add(i);
                }
                if (row.Count < 1)
                {
                    Console.WriteLine("-1");
                    return;
                }
            }
            else if (col.Count == 0 && row.Count == 1)
            {
                for (int i = 0; i < N && col.Count < 1; i++)
                {
                    bool vv = (V[i] & bit) == bit;
                    if (vv ^ (T[i] == 0)) col.Add(i);
                }
                if (col.Count < 1)
                {
                    Console.WriteLine("-1");
                    return;
                }
            }
            row.Sort();
            col.Sort();

            for (int i = 0; i < Math.Max(row.Count, col.Count); i++)
            {
                ans[row[i % row.Count]][col[i % col.Count]] |= bit;
            }
        }
        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();
    }
}