(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0)))
  #+swank (progn (ql:quickload '(:cl-debug-print :fiveam))
                 (shadow :run)
                 (use-package :fiveam)))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)

;; BEGIN_INSERTED_CONTENTS
(defmacro define-binary-heap (name &key (test '#'>) (element-type 'fixnum))
  (check-type name symbol)
  (let* ((string-name (string name))
         (fname-push (intern (format nil "~A-PUSH" string-name)))
         (fname-pop (intern (format nil "~A-POP" string-name)))
         (fname-reinitialize (intern (format nil "~A-REINITIALIZE" string-name)))
         (fname-peak (intern (format nil "~A-PEAK" string-name)))
         (fname-make (intern (format nil "MAKE-~A" string-name)))
         (acc-next-position (intern (format nil "~A-NEXT-POSITION" string-name)))
         (acc-data (intern (format nil "~A-DATA" string-name))))
    `(progn
       (defstruct (,name
                   (:constructor ,fname-make
                       (size
                        &aux (data ,(if (eql element-type '*)
                                        `(make-array (1+ size))
                                        `(make-array (1+ size) :element-type ',element-type))))))
         (data #() :type (simple-array ,element-type (*)) :read-only t)
         (next-position 1 :type (integer 1 #.most-positive-fixnum)))

       (declaim (inline ,fname-push))
       (defun ,fname-push (obj heap)
         (symbol-macrolet ((next-position (,acc-next-position heap)))
           (let ((data (,acc-data heap)))
             (declare ((simple-array ,element-type (*)) data))
             (labels ((update (pos)
                        (unless (= pos 1)
                          (let ((parent-pos (ash pos -1)))
                            (when (funcall ,test (aref data pos) (aref data parent-pos))
                              (rotatef (aref data pos) (aref data parent-pos))
                              (update parent-pos))))))
               (setf (aref data next-position) obj)
               (update next-position)
               (incf next-position)
               heap))))

       (declaim (inline ,fname-pop))
       (defun ,fname-pop (heap &optional (error t) null-value)
         (symbol-macrolet ((next-position (,acc-next-position heap)))
           (let ((data (,acc-data heap)))
             (declare ((simple-array ,element-type (*)) data))
             (labels ((update (pos)
                        (declare ((integer 1 #.most-positive-fixnum) pos))
                        (let* ((child-pos1 (+ pos pos))
                               (child-pos2 (1+ child-pos1)))
                          (when (<= child-pos1 next-position)
                            (if (<= child-pos2 next-position)
                                (if (funcall ,test (aref data child-pos1) (aref data child-pos2))
                                    (unless (funcall ,test (aref data pos) (aref data child-pos1))
                                      (rotatef (aref data pos) (aref data child-pos1))
                                      (update child-pos1))
                                    (unless (funcall ,test (aref data pos) (aref data child-pos2))
                                      (rotatef (aref data pos) (aref data child-pos2))
                                      (update child-pos2)))
                                (unless (funcall ,test (aref data pos) (aref data child-pos1))
                                  (rotatef (aref data pos) (aref data child-pos1))))))))
               (if (= next-position 1)
                   (if error
                       (error "No element in heap.")
                       null-value)
                   (prog1 (aref data 1)
                     (decf next-position)
                     (setf (aref data 1) (aref data next-position))
                     (update 1)))))))

       (declaim (inline ,fname-reinitialize))
       (defun ,fname-reinitialize (heap)
         (setf (,acc-next-position heap) 1)
         heap)

       (declaim (inline ,fname-peak))
       (defun ,fname-peak (heap &optional (error t) null-value)
         (if (= 1 (,acc-next-position heap))
             (if error
                 (error "No element in heap")
                 null-value)
             (aref (,acc-data heap) 1))))))

(defmacro with-output-buffer (&body body)
  "Buffers all outputs to *STANDARD-OUTPUT* and flushes them to
*STANDARD-OUTPUT* at the end. Note that only BASE-CHAR is allowed."
  (let ((out (gensym)))
    `(let ((,out (make-string-output-stream :element-type 'base-char)))
       (let ((*standard-output* ,out))
         ,@body)
       (write-string (get-output-stream-string ,out)))))

(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  (declare #.OPT)
  (macrolet ((%read-byte ()
               `(the (unsigned-byte 8)
                     #+swank (char-code (read-char in nil #\Nul))
                     #-swank (sb-impl::ansi-stream-read-byte in nil #.(char-code #\Nul) nil))))
    (let* ((minus nil)
           (result (loop (let ((byte (%read-byte)))
                           (cond ((<= 48 byte 57)
                                  (return (- byte 48)))
                                 ((zerop byte) ; #\Nul
                                  ;; (return-from read-fixnum 0)
                                  (error "Read EOF or #\Nul."))
                                 ((= byte #.(char-code #\-))
                                  (setf minus t)))))))
      (declare ((integer 0 #.most-positive-fixnum) result))
      (loop
        (let* ((byte (%read-byte)))
          (if (<= 48 byte 57)
              (setq result (+ (- byte 48) (the fixnum (* result 10))))
              (return (if minus (- result) result))))))))

(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)

(defmacro println (obj &optional (stream '*standard-output*))
  `(let ((*read-default-float-format* 'double-float))
     (prog1 (princ ,obj ,stream) (terpri ,stream))))

(defconstant +mod+ 1000000007)

;; Body

(defstruct (node (:constructor %make-node (sum a b c)))
  (sum nil :type uint62)
  (a nil :type uint62)
  (b nil :type uint62)
  (c nil :type uint62))

(define-binary-heap heap
  :test (lambda (node1 node2)
          (declare (node node1 node2))
          (> (node-sum node1) (node-sum node2)))
  :element-type node)

(defun main ()
  (declare #.OPT
           (inline sort))
  (let* ((x (read))
         (y (read))
         (z (read))
         (k (read))
         (as (make-array x :element-type 'uint62))
         (bs (make-array y :element-type 'uint62))
         (cs (make-array z :element-type 'uint62))
         (checked (make-array (list x y z) :element-type 'bit :initial-element 0))
         (pqueue (make-heap (* 3 k)))
         (res (make-array k :element-type 'uint62)))
    (declare ((simple-array uint62 (*)) as bs cs))
    (labels ((make-node (a b c)
               (%make-node (the uint62 (+ (aref as a) (aref bs b) (aref cs c)))
                           a b c)))
      (declare (inline make-node) (uint16 x y z k))
      (dotimes (i x) (setf (aref as i) (read-fixnum)))
      (dotimes (i y) (setf (aref bs i) (read-fixnum)))
      (dotimes (i z) (setf (aref cs i) (read-fixnum)))
      (setf as (sort as #'>)
            bs (sort bs #'>)
            cs (sort cs #'>))
      (heap-push (make-node 0 0 0) pqueue)
      (setf (aref checked 0 0 0) 1)
      (dotimes (i k)
        (let* ((node (heap-pop pqueue))
               (a (node-a node))
               (b (node-b node))
               (c (node-c node)))
          (setf (aref res i) (node-sum node))
          (when (and (< (1+ a) x)
                     (zerop (aref checked (1+ a) b c)))
            (heap-push (make-node (1+ a) b c) pqueue)
            (setf (aref checked (1+ a) b c) 1))
          (when (and (< (1+ b) y)
                     (zerop (aref checked a (1+ b) c)))
            (heap-push (make-node a (1+ b) c) pqueue)
            (setf (aref checked a (1+ b) c) 1))
          (when (and (< (1+ c) z)
                     (zerop (aref checked a b (1+ c))))
            (heap-push (make-node a b (1+ c)) pqueue)
            (setf (aref checked a b (1+ c)) 1))))
      (with-output-buffer
        (dotimes (i k)
          (println (aref res i)))))))

#-swank(main)


Measured memory_bytes: 187236