(def connection-profile-samples
  [{:time-to-first-delivery       3
    :first-delivery-size          4
    :continuous-delivery-size     6
    :count-of-continuous-delivery 12
    :last-delivery-size           3}])
(defn count-total-size [profile]
  (let [{Ccd :count-of-continuous-delivery
         ir  :continuous-delivery-size
         fd  :first-delivery-size
         ld  :last-delivery-size} profile]
    (+ fd ld (* Ccd ir))))
(defn next-connection-profile [prev outlet-rate]
  (let [{Ccd :count-of-continuous-delivery
         ir  :continuous-delivery-size
         fd  :first-delivery-size
         ld  :last-delivery-size} prev
        or outlet-rate
        m {:time-to-first-delivery (inc (:time-to-first-delivery prev))}]
    (cond (<= ir or)
          (merge m
            (select-keys prev
              [:first-delivery-size :last-delivery-size
               :continuous-delivery-size
               :count-of-continuous-delivery]))

          (> ir or)
          (let [amortizing (cond
                             (<= fd or) (+ ld (* ir Ccd))
                             (>  fd or) (+ ld (* ir Ccd) (- fd or)))]
            (assoc m
             :first-delivery-size (cond (<= fd or) fd
                                        (>  fd or) or)
             :continuous-delivery-size or
             :count-of-continuous-delivery (quot amortizing or)
             :last-delivery-size (rem amortizing or))))))
(defn total-delivery-time [profile]
  (let [{Tfd :time-to-first-delivery
         Ccd :count-of-continuous-delivery
         ld  :last-delivery-size} profile
        except-last-delivery (+ Tfd 1 Ccd)]
    (cond (> ld 0) (inc except-last-delivery)
          :else except-last-delivery)))
(defn abc123-C [[r0 & outlet-rates] N]
  (let [amortizing0 (- N r0)
        profile0
        {:time-to-first-delivery 0
         :first-delivery-size r0
         :continuous-delivery-size r0
         :count-of-continuous-delivery (quot amortizing0 r0)
         :last-delivery-size (rem amortizing0 r0)}
        profiles (->> outlet-rates
                   (reductions
                     next-connection-profile
                     profile0))]
    #_(doseq [[prof rate] (zipmap profiles (concat outlet-rates [-1]))]
      (pprint prof) (println rate))
    (total-delivery-time (last profiles))))
(defn main-abc123-C []
  (let [N (read)
        rates (doall (for [_ (range 5)] (read)))]
    (println (abc123-C rates N))))
(main-abc123-C)
