(defn input []
  (->> (repeatedly 3 read-line)
    (map
      (comp
        (partial map #(Integer/parseInt %))
        #(clojure.string/split % #"\s")))))

(defn CollectingBalls [[[_] [K] Xs]]
  (bit-shift-left (reduce #(+ %1 (min %2 (- K %2))) 0 Xs) 1))

(-> (input)
  CollectingBalls
  println)