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 Lua (LuaJIT 2.1.0) (judge verdict: Accepted)

local function lltonumber(str)
  local ret = 0ULL
  for i = 1, #str do
    ret = ret * 10ULL + (1ULL * str:sub(i, i):byte() - 48ULL)
  end
  return ret
end

local n = io.read("*n")
local s = {}
for i = 1, n do
  s[i] = io.read("*n") == 1
end
local t = {}
for i = 1, n do
  t[i] = io.read("*n") == 1
end
io.read()
local u = {}
do
  local str = io.read()
  local i = 1
  for w in str:gmatch("%d+") do
    u[i] = lltonumber(w)
    i = i + 1
  end
end
local v = {}
do
  local str = io.read()
  local i = 1
  for w in str:gmatch("%d+") do
    v[i] = lltonumber(w)
    i = i + 1
  end
end
local ret = {}
for i = 1, n do
  ret[i] = {}
  for j = 1, n do
    ret[i][j] = 0ULL
  end
end
local function solveStage(mul)
  local z = {}
  for i = 1, n do
    z[i] = {}
    -- -1: unknown
    for j = 1, n do
      z[i][j] = -1
    end
  end
  local need_0_row = {}
  local need_1_row = {}
  local need_0_col = {}
  local need_1_col = {}
  for i = 1, n do
    -- ROW
    local flag = u[i] % 2ULL == 1ULL
    if s[i] then
      -- OR
      if not flag then
        for j = 1, n do
          if z[i][j] == 1 then
            return false
          end
          z[i][j] = 0
        end
      else
        table.insert(need_1_row, i)
      end
    else
      -- AND
      if flag then
        for j = 1, n do
          if z[i][j] == 0 then
            return false
          end
          z[i][j] = 1
        end
      else
        table.insert(need_0_row, i)
      end
    end
    -- COLUMN
    local flag = v[i] % 2ULL == 1ULL
    if t[i] then
      -- OR
      if not flag then
        for j = 1, n do
          if z[j][i] == 1 then
            return false
          end
          z[j][i] = 0
        end
      else
        table.insert(need_1_col, i)
      end
    else
      -- AND
      if flag then
        for j = 1, n do
          if z[j][i] == 0 then
            return false
          end
          z[j][i] = 1
        end
      else
        table.insert(need_0_col, i)
      end
    end
  end
  -- refresh
  local any = false
  local function flushcol(row)
    for k = #need_0_col, 1, -1 do
      local col = need_0_col[k]
      z[row][col] = 0
      table.remove(need_0_col)
    end
    for k = #need_1_col, 1, -1 do
      local col = need_1_col[k]
      z[row][col] = 1
      table.remove(need_1_col)
    end
    for j = 1, n do
      if z[row][j] == -1 then
        z[row][j] = 0
      end
    end
    any = true
  end
  local function flushrow(col)
    for k = #need_0_row, 1, -1 do
      local row = need_0_row[k]
      z[row][col] = 0
      table.remove(need_0_row)
    end
    for k = #need_1_row, 1, -1 do
      local row = need_1_row[k]
      z[row][col] = 1
      table.remove(need_1_row)
    end
    for j = 1, n do
      if z[j][col] == -1 then
        z[j][col] = 0
      end
    end
    any = true
  end
  for i = #need_0_row, 1, -1 do
    local row = need_0_row[i]
    local found = false
    for j = 1, n do
      if z[row][j] == 0 then
        table.remove(need_0_row, i)
        found = true
        break
      end
    end
    if found then
      flushcol(row)
    end
  end
  for i = #need_1_row, 1, -1 do
    local row = need_1_row[i]
    local found = false
    for j = 1, n do
      if z[row][j] == 1 then
        table.remove(need_1_row, i)
        found = true
        break
      end
    end
    if found then
      flushcol(row)
    end
  end
  for i = #need_0_col, 1, -1 do
    local col = need_0_col[i]
    local found = false
    for j = 1, n do
      if z[j][col] == 0 then
        table.remove(need_0_col, i)
        found = true
        break
      end
    end
    if found then
      flushrow(col)
    end
  end
  for i = #need_1_col, 1, -1 do
    local col = need_1_col[i]
    local found = false
    for j = 1, n do
      if z[j][col] == 1 then
        table.remove(need_1_col, i)
        found = true
        break
      end
    end
    if found then
      flushrow(col)
    end
  end
  local remrow = #need_0_row + #need_1_row
  local remcol = #need_0_col + #need_1_col
  if any then
    for i = 1, #need_0_row do
      local row = need_0_row[i]
      for j = 1, n do
        if z[row][j] == -1 then z[row][j] = 0 end
      end
    end
    for i = 1, #need_1_row do
      local row = need_1_row[i]
      for j = 1, n do
        if z[row][j] == -1 then z[row][j] = 1 end
      end
    end
    for i = 1, #need_0_col do
      local col = need_0_col[i]
      for j = 1, n do
        if z[j][col] == -1 then z[j][col] = 0 end
      end
    end
    for i = 1, #need_1_col do
      local col = need_1_col[i]
      for j = 1, n do
        if z[j][col] == -1 then z[j][col] = 1 end
      end
    end
  elseif remrow == 0 and 0 < remcol then
    return false
  elseif remcol == 0 and 0 < remrow then
    return false
  elseif 0 < remrow and 0 < remcol then
    if #need_0_row == 0 and #need_1_row == 1 then
      if #need_1_col == 0 then
        return false
      end
      local row = need_1_row[1]
      for i = 1, #need_1_col do
        z[row][need_1_col[i]] = 1
      end
      for i = 1, #need_0_col do
        z[row][need_0_col[i]] = 0
      end
    elseif #need_0_row == 1 and #need_1_row == 0 then
      if #need_0_col == 0 then
        return false
      end
      local row = need_0_row[1]
      for i = 1, #need_1_col do
        z[row][need_1_col[i]] = 1
      end
      for i = 1, #need_0_col do
        z[row][need_0_col[i]] = 0
      end
    elseif #need_0_col == 0 and #need_1_col == 1 then
      if #need_1_row == 0 then
        return false
      end
      local col = need_1_col[1]
      for i = 1, #need_1_row do
        z[need_1_row[i]][col] = 1
      end
      for i = 1, #need_0_row do
        z[need_0_row[i]][col] = 0
      end
    elseif #need_0_col == 1 and #need_1_col == 0 then
      if #need_0_row == 0 then
        return false
      end
      local col = need_0_col[1]
      for i = 1, #need_1_row do
        z[need_1_row[i]][col] = 1
      end
      for i = 1, #need_0_row do
        z[need_0_row[i]][col] = 0
      end
    else
      local rows = {}
      local cols = {}
      for i = 1, #need_0_row do
        table.insert(rows, need_0_row[i])
      end
      for i = 1, #need_1_row do
        table.insert(rows, need_1_row[i])
      end
      for i = 1, #need_0_col do
        table.insert(cols, need_0_col[i])
      end
      for i = 1, #need_1_col do
        table.insert(cols, need_1_col[i])
      end
      for i = 1, #rows do
        for j = 1, #cols do
          local r, c = rows[i], cols[j]
          if i * j == 1 then
            z[r][c] = 1
          elseif i == 1 or j == 1 then
            z[r][c] = 0
          else
            z[r][c] = 1
          end
        end
      end
    end
  end
  for i = 1, n do
    for j = 1, n do
      assert(z[i][j] ~= -1)
      if z[i][j] == 1 then
        ret[i][j] = ret[i][j] + mul
      end
    end
  end
  return true
end
local function reduce()
  for i = 1, n do
    u[i] = u[i] / 2ULL
    v[i] = v[i] / 2ULL
  end
end
local add = 1ULL
for i = 0, 63 do
  local ret = solveStage(add)
  if not ret then
    print(-1)
    os.exit()
  end
  reduce()
  add = add * 2LL
end
for i = 1, n do
  for j = 1, n do
    local str = tostring(ret[i][j]):gsub("ULL", "")
    io.write(str)
    io.write(j == n and "\n" or " ")
  end
end