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 Rust (1.42.0) (judge verdict: Wrong Answer)

use proconio::*;

#[fastout]
fn run() {
    input! {
        n: usize,
        s: [usize; n],
        t: [usize; n],
        u: [usize; n],
        v: [usize; n],
    }
    let mut ans = vec![vec![0usize; n]; n];
    for k in 0..64 {
        let mut tmp = vec![vec![None; n]; n];
        let mut row = vec![];
        for (i, (&s, u)) in s.iter().zip(u.iter().cloned().map(|u| (u >> k) & 1)).enumerate() {
            match (s, u) {
                (0, 0) => {
                    row.push((i, 0));
                },
                (0, 1) => {
                    let tmp = &mut tmp[i];
                    for t in tmp.iter_mut() {
                        *t = Some(1);
                    }
                },
                (1, 0) => {
                    let tmp = &mut tmp[i];
                    for t in tmp.iter_mut() {
                        *t = Some(0);
                    }
                },
                (1, 1) => {
                    row.push((i, 1));
                },
                _ => unreachable!(),
            }
        }
        let mut col = vec![];
        for j in 0..n {
            let t = t[j];
            let v = (v[j] >> k) & 1;
            match (t, v) {
                (0, 0) => {
                    col.push((j, 0));
                },
                (0, 1) => {
                    for i in 0..n {
                        if let Some(v) = tmp[i][j] {
                            if v == 0 {
                                println!("-1");
                                return;
                            }
                        }
                        tmp[i][j] = Some(1);
                    }
                },
                (1, 0) => {
                    for i in 0..n {
                        if let Some(v) = tmp[i][j] {
                            if v == 1 {
                                println!("-1");
                                return;
                            }
                        }
                        tmp[i][j] = Some(0);
                    }
                },
                (1, 1) => {
                    col.push((j, 1));
                },
                _ => unreachable!(),
            }
        }
        let x = row;
        let y = col;
        if x.len() == 0 {
            for &(c, b) in y.iter() {
                let mut find = false;
                for i in 0..n {
                    if tmp[i][c].unwrap() == b {
                        find = true;
                    }
                }
                if !find {
                    println!("-1");
                    return;
                }
            }
        } else if y.len() == 0 {
            for &(r, b) in x.iter() {
                let mut find = false;
                for j in 0..n {
                    if tmp[r][j].unwrap() == b {
                        find = true;
                    }
                }
                if !find {
                    println!("-1");
                    return;
                }
            }
        } else if x.len() == 1 {
            let (r, b) = x[0];
            let mut ok = false;
            for y in y.iter() {
                if y.1 == b {
                    ok = true;
                }
            }
            for j in 0..n {
                if tmp[r][j].map_or(false, |p| p == b) {
                    ok = true;
                }
            }
            if !ok {
                println!("-1");
                return;
            }
            for &(c, b) in y.iter() {
                tmp[r][c] = Some(b);
            }
        } else if y.len() == 1 {
            let (c, b) = y[0];
            let mut ok = false;
            for x in x.iter() {
                if x.1 == b {
                    ok = true;
                }
            }
            for i in 0..n {
                if tmp[i][c].map_or(false, |p| p == b) {
                    ok = true;
                }
            }
            if !ok {
                println!("-1");
                return;
            }
            for &(r, b) in x.iter() {
                tmp[r][c] = Some(b);
            }
        } else if x.len() >= y.len() {
            for i in 0..x.len() {
                let (r, b) = x[i];
                let c = y[i % y.len()].0;
                tmp[r][c] = Some(b);
            }
            for (c, b) in y {
                for &(r, _) in x.iter() {
                    if tmp[r][c].is_none() {
                        tmp[r][c] = Some(b);
                        break;
                    }
                }
            }
        } else {
            for j in 0..y.len() {
                let (c, b) = y[j];
                let r = x[j % x.len()].0;
                tmp[r][c] = Some(b);
            }
            for (r, b) in x {
                for &(c, _) in y.iter() {
                    if tmp[r][c].is_none() {
                        tmp[r][c] = Some(b);
                        break;
                    }
                }
            }
        }
        for (a, t) in ans.iter_mut().zip(tmp.iter()) {
            for (a, t) in a.iter_mut().zip(t.iter()) {
                if let Some(b) = *t {
                    *a |= b << k;
                }
            }
        }
    }
    for a in ans {
        for a in a {
            print!("{} ", a);
        }
        println!();
    }
}

fn main() {
    run();
}