(import '(java.util HashSet PriorityQueue))
(defrecord StateABC123D
  [candidates queue queued emitter])

(defn sort-desc [coll] (sort (comparator >) coll))
(defn init-state [emitter as bs cs]
  (let [as (sort-desc as)
        bs (sort-desc bs)
        cs (sort-desc cs)
        candidates {:A (vec (sort-desc as))
                    :B (vec (sort-desc bs))
                    :C (vec (sort-desc cs))}]
    (->StateABC123D
      candidates
      (PriorityQueue. (fn [a b] ((comparator >) (:score a) (:score b))))
      (HashSet.)
      emitter)))
(defn calc-score [state v]
  (let [[Ia Ib Ic] v
        {:keys [A B C]} (:candidates state)]
    (+ (nth A Ia) (nth B Ib) (nth C Ic))))
(defn within-bound? [state [Ia Ib Ic]]
  (let [{:keys [A B C]} (:candidates state)]
    (and (< Ia (count A)) (< Ib (count B)) (< Ic (count C)))))
(defn visit [state vertex]
  (let [{:keys [queue queued emitter]} state
        with-score (fn [v] {:vertex v :score (calc-score state v)})]
    (emitter state (with-score vertex))
    (doseq [tr [[1 0 0] [0 1 0] [0 0 1]]
            :let [stretched (mapv + vertex tr)]
            :when (within-bound? state stretched)]
      (when (not (.contains queued stretched))
        (.add queued stretched)
        (.add queue (with-score stretched))))))

(defn abc123-D [as bs cs K]
  (let [collector (transient [])
        emitter (fn [state v] (conj! collector (:score v)))
        state (init-state emitter as bs cs)]
    (visit state [0 0 0])
    (dotimes [_ (dec K)] (->> (.poll (:queue state)) :vertex (visit state)))
    (persistent! collector)))
(defn main-abc123-D []
  (let [X (read) Y (read) Z (read) K (read)
        as (doall (for [_ (range X)] (read)))
        bs (doall (for [_ (range Y)] (read)))
        cs (doall (for [_ (range Z)] (read)))]
    (run! println (abc123-D as bs cs K))))
(defn well-sorted? [comp coll]
  (= coll (sort comp coll)))
(main-abc123-D)
