(defn solve [n m x-org]
  (let [x (sort x-org)
        s (first x)
        e (last x)]
    (cond
      (= n 1) (- e s)
      (= n m) 0
      :else
      (->> x
           (partition 2 1)
           (map #(- (last %) (first %)))
           sort
           reverse
           (take (dec n))
           (apply +)
           (- (- e s))))))

(comment
  ;; 5
  (solve 2 5 [10 12 1 2 14])
  ;; 19
  (solve 3 7 [-10 -3 0 9 -100 2 17])
  ;; 0
  (solve 100 1 [-100000])
  )

(println
 (solve
  (Integer/parseInt (read))
  (Integer/parseInt (read))
  (map
   #(Integer/parseInt %)
   (clojure.string/split (read) #"\s+"))))
