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 Common Lisp (SBCL 2.0.3) (judge verdict: Wrong Answer)

(eval-when (:compile-toplevel :load-toplevel :execute)
  (sb-int:defconstant-eqx opt
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0))
    #'equal)
  #+swank (ql:quickload '(:cl-debug-print :fiveam) :silent t)
  #-swank (set-dispatch-macro-character
           #\# #\> (lambda (s c p) (declare (ignore c p)) `(values ,(read s nil nil t)))))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)
#-swank (disable-debugger) ; for CS Academy

;; BEGIN_INSERTED_CONTENTS
(declaim (inline println-matrix))
(defun println-matrix (array &key (separator #\ ) (key #'identity) (writer #'write) (row-start 0) row-end (col-start 0) col-end)
  "Prints a 2-dimensional array."
  (declare ((array * (* *)) array)
           ((integer 0 #.most-positive-fixnum) row-start col-start))
  (let ((row-end (or row-end (array-dimension array 0)))
        (col-end (or col-end (array-dimension array 1))))
    (declare ((integer 0 #.most-positive-fixnum) row-end col-end))
    (loop for i from row-start below row-end
          do (loop for j from col-start below col-end
                   unless (= j col-start)
                   do (princ separator)
                   do (funcall writer (funcall key (aref array i j))))
             (terpri))))

(in-package :cl-user)

(defmacro dbg (&rest forms)
  #+swank
  (if (= (length forms) 1)
      `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms))
      `(format *error-output* "~A => ~A~%" ',forms `(,,@forms)))
  #-swank (declare (ignore forms)))

(defmacro define-int-types (&rest bits)
  `(progn
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
(define-int-types 2 4 7 8 15 16 31 32 62 63 64)

(declaim (inline println))
(defun println (obj &optional (stream *standard-output*))
  (let ((*read-default-float-format* 'double-float))
    (prog1 (princ obj stream) (terpri stream))))

(defconstant +mod+ 1000000007)

;;;
;;; Body
;;;

(define-condition infeasible () ())

(defun solve (n rows cols mat)
  (declare ((simple-array bit (* *)) mat))
  (setq rows (sort rows #'> :key #'second))
  (setq cols (sort cols #'> :key #'second))
  (loop for row-node in rows
        for col-node in cols
        for (row row-lo row-hi) = row-node
        for (col col-lo col-hi) = col-node
        while (and (> row-lo 0) (> col-lo 0))
        do (setf (aref mat row col) 1)
           (decf (second row-node))
           (decf (second col-node))
           (decf (third row-node))
           (decf (third col-node)))
  (let ((cols cols)
        (pos (position-if (lambda (node) (> (second node) 0)) rows)))
    (when pos
      (loop for row-node in (nthcdr pos rows)
            for (row row-lo row-hi) = row-node
            when (> row-lo 0)
            do (assert (<= row-lo 1))
               (loop (when (<= row-lo 0)
                       (return))
                     (unless (car cols)
                       (error 'infeasible))
                     (destructuring-bind (col col-lo col-hi) (car cols)
                       (if (<= col-hi 0)
                           (pop cols)
                           (progn
                             (decf row-lo)
                             (decf row-hi)
                             (decf (second row-node))
                             (decf (third row-node))
                             (decf (second (car cols)))
                             (decf (third (car cols)))
                             (setf (aref mat row col) 1)
                             (return))))))))
  (let ((rows rows)
        (pos (position-if (lambda (node) (> (second node) 0)) cols)))
    (when pos
      (loop for col-node in (nthcdr pos cols)
            for (col col-lo col-hi) = col-node
            when (> col-lo 0)
            do (assert (<= col-lo 1))
               (loop (when (<= col-lo 0)
                       (return))
                     (unless (car rows)
                       (error 'infeasible))
                     (destructuring-bind (row row-lo row-hi) (car rows)
                       (if (<= row-hi 0)
                           (pop rows)
                           (progn
                             (decf col-lo)
                             (decf col-hi)
                             (decf (second col-node))
                             (decf (third col-node))
                             (decf (second (car rows)))
                             (decf (third (car rows)))
                             (setf (aref mat row col) 1)
                             (return))))))))
  mat)

(defun main ()
  (let* ((n (read))
         (ss (make-array n :element-type 'bit :initial-element 0))
         (ts (make-array n :element-type 'bit :initial-element 0))
         (us (make-array n :element-type 'uint64 :initial-element 0))
         (vs (make-array n :element-type 'uint64 :initial-element 0))
         (res (make-array (list n n) :element-type 'uint64  :initial-element 0)))
    (dotimes (i n)
      (setf (aref ss i) (read)))
    (dotimes (i n)
      (setf (aref ts i) (read)))
    (dotimes (i n)
      (setf (aref us i) (read)))
    (dotimes (i n)
      (setf (aref vs i) (read)))
    (handler-bind ((infeasible (lambda (c) (println -1) (return-from main))))
      (dotimes (pos 64)
        (let (rows ; row lo hi
              cols
              (row-offset 0)
              (col-offset 0)
              (mat (make-array (list n n) :element-type 'bit :initial-element 0)))
          (declare ((simple-array bit (* *)) mat))
          (dotimes (row n)
            (let ((s (aref ss row))
                  (u (ldb (byte 1 pos) (aref us row))))
              (when (and (= s 0) (= u 1))
                (incf col-offset)
                (dotimes (j n)
                  (setf (aref mat row j) 1)))))
          (dotimes (col n)
            (let ((tt (aref ts col))
                  (v (ldb (byte 1 pos) (aref vs col))))
              (when (and (= tt 0) (= v 1))
                (incf row-offset)
                (dotimes (i n)
                  (setf (aref mat i col) 1)))))
          (dotimes (row n)
            (let ((s (aref ss row))
                  (u (ldb (byte 1 pos) (aref us row))))
              (cond ((and (= s 0) (= u 0))
                     (when (< (- n 1 row-offset) 0)
                       (error 'infeasible))
                     (push (list row 0 (- n 1 row-offset)) rows))
                    ((and (= s 1) (= u 1))
                     (when (< (- 1 row-offset) 0)
                       (error 'infeasible))
                     (push (list row (- 1 row-offset) (- n row-offset)) rows)))))
          (dotimes (col n)
            (let ((tt (aref ts col))
                  (v (ldb (byte 1 pos) (aref vs col))))
              (cond ((and (= tt 0) (= v 0))
                     (when (< (- n 1 col-offset) 0)
                       (error 'infeasible))
                     (push (list col 0 (- n 1 col-offset)) cols))
                    ((and (= tt 1) (= v 1))
                     (when (< (- 1 col-offset) 0)
                       (error 'infeasible))
                     (push (list col (- 1 col-offset) (- n col-offset)) cols)))))
          (solve n rows cols mat)
          (dotimes (i n)
            (dotimes (j n)
              (setf (ldb (byte 1 pos) (aref res i j))
                    (aref mat i j)))))))
    (println-matrix res)))

#-swank (main)

;;;
;;; Test and benchmark
;;;

#+swank
(defun io-equal (in-string out-string &key (function #'main) (test #'equal))
  "Passes IN-STRING to *STANDARD-INPUT*, executes FUNCTION, and returns true if
the string output to *STANDARD-OUTPUT* is equal to OUT-STRING."
  (labels ((ensure-last-lf (s)
             (if (eql (uiop:last-char s) #\Linefeed)
                 s
                 (uiop:strcat s uiop:+lf+))))
    (funcall test
             (ensure-last-lf out-string)
             (with-output-to-string (out)
               (let ((*standard-output* out))
                 (with-input-from-string (*standard-input* (ensure-last-lf in-string))
                   (funcall function)))))))

#+swank
(defun get-clipbrd ()
  (with-output-to-string (out)
    (run-program "powershell.exe" '("-Command" "Get-Clipboard") :output out :search t)))

#+swank (defparameter *this-pathname* (uiop:current-lisp-file-pathname))
#+swank (defparameter *dat-pathname* (uiop:merge-pathnames* "test.dat" *this-pathname*))

#+swank
(defun run (&optional thing (out *standard-output*))
  "THING := null | string | symbol | pathname

null: run #'MAIN using the text on clipboard as input.
string: run #'MAIN using the string as input.
symbol: alias of FIVEAM:RUN!.
pathname: run #'MAIN using the text file as input."
  (let ((*standard-output* out))
    (etypecase thing
      (null
       (with-input-from-string (*standard-input* (delete #\Return (get-clipbrd)))
         (main)))
      (string
       (with-input-from-string (*standard-input* (delete #\Return thing))
         (main)))
      (symbol (5am:run! thing))
      (pathname
       (with-open-file (*standard-input* thing)
         (main))))))

#+swank
(defun gen-dat ()
  (uiop:with-output-file (out *dat-pathname* :if-exists :supersede)
    (format out "")))

#+swank
(defun bench (&optional (out (make-broadcast-stream)))
  (time (run *dat-pathname* out)))

;; To run: (5am:run! :sample)
#+swank
(it.bese.fiveam:test :sample
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "2
0 1
1 0
1 1
1 0
"
    "1 1
1 0
"))
  (it.bese.fiveam:is
   (common-lisp-user::io-equal "2
1 1
1 0
15 15
15 11
"
    "15 11
15 11
")))