(defparameter mod-number 1000000007)
(defparameter inf 10000000000)

(defun unfold (p f g seed &optional (tail-gen (lambda () '())))
  (if (p seed)
      (tail-gen seed)
	  (cons (f seed)
	        (unfold p f g (g seed) tail-gen))))

(defun iota (c &optional (s 0) (step 1) (acc nil))
  (if (zerop c)
      (reverse acc)
      (iota (1- c) (+ s step) step (cons s acc))))

(defun sum (list &optional (init 0))
  (reduce #'+ list :initial-value init))

(defun chomp (str)
    (string-right-trim '(#\Return #\Linefeed) str))

(defun split (x str &optional (acc nil))
  (let ((pos (search x str))
        (size (length x)))
    (if pos
        (split x (subseq str (+ pos size)) (cons (subseq str 0 pos) acc))
        (reverse (cons str acc)))))

(defmacro debug-print (x)
  `(let ((y ,x))
    (format t "~A: ~A~%" ',x y)
	y))

(defun memorized-solve (stairs)
  (let ((dp (make-hash-table :test #'eq)))
    (labels ((solve (pos)
               (let ((memo (gethash pos dp)))
                 (if memo
                     memo
                     (let ((ret (if (zerop pos)
                                    0
                                    (min (+ (solve (max (1- pos) 0))
                                            (abs (- (aref stairs pos) (aref stairs (max (1- pos) 0)))))
                                         (+ (solve (max (- pos 2) 0))
                                            (abs (- (aref stairs pos) (aref stairs (max (- pos 2) 0)))))))))
                       (setf (gethash pos dp) ret)
                       ret)))))
      #'solve)))

(let* ((n (read))
       (stairs (make-array n :initial-contents (mapcar #'parse-integer (split " " (read-line))))))
  (format t "~A~%" (funcall (memorized-solve stairs) (1- n))))


(defparameter mod-number 1000000007)
(defparameter inf 10000000000)

(defun unfold (p f g seed &optional (tail-gen (lambda () '())))
  (if (p seed)
      (tail-gen seed)
	  (cons (f seed)
	        (unfold p f g (g seed) tail-gen))))

(defun iota (c &optional (s 0) (step 1) (acc nil))
  (if (zerop c)
      (reverse acc)
      (iota (1- c) (+ s step) step (cons s acc))))

(defun sum (list &optional (init 0))
  (reduce #'+ list :initial-value init))

(defun chomp (str)
    (string-right-trim '(#\Return #\Linefeed) str))

(defun split (x str &optional (acc nil))
  (let ((pos (search x str))
        (size (length x)))
    (if pos
        (split x (subseq str (+ pos size)) (cons (subseq str 0 pos) acc))
        (reverse (cons str acc)))))

(defmacro debug-print (x)
  `(let ((y ,x))
    (format t "~A: ~A~%" ',x y)
	y))

(defun solve (pos stairs dp)
  (let ((memo (aref dp pos)))
    (if memo
        memo
        (let ((ret (if (zerop pos)
                       0
                       (apply #'min
                          (mapcar (lambda (prev-pos)
                                    (if (< prev-pos 0)
                                        inf
                                        (+ (solve prev-pos stairs dp)
                                           (abs (- (aref stairs prev-pos) (aref stairs pos))))))
                                  (list (1- pos) (- pos 2)))))))
          (setf (aref dp pos) ret)
          ret))))

(let* ((n (read))
       (stairs (make-array n :initial-contents (mapcar #'parse-integer (split " " (read-line)))))
       (dp (make-array n :initial-element nil)))
  (format t "~A~%" (solve (1- n) stairs dp)))

Global OOD problem IDs are disjoint from train, validation, and in-distribution test problem IDs.

Score : 100 points

Problem Statement

There are N stones, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), the height of Stone i is h_i.

There is a frog who is initially on Stone 1.
He will repeat the following action some number of times to reach Stone N:

If the frog is currently on Stone i, jump to Stone i + 1 or Stone i + 2. Here, a cost of |h_i - h_j| is incurred, where j is the stone to land on.

Find the minimum possible total cost incurred before the frog reaches Stone N.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq h_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N
h_1 h_2 \ldots h_N

Output

Print the minimum possible total cost incurred.

Sample Input 1

4
10 30 40 20

Sample Output 1

30

If we follow the path 1 → 2 → 4, the total cost incurred would be |10 - 30| + |30 - 20| = 30.

Sample Input 2

2
10 10

Sample Output 2

0

If we follow the path 1 → 2, the total cost incurred would be |10 - 10| = 0.

Sample Input 3

6
30 10 60 10 60 50

Sample Output 3

40

If we follow the path 1 → 3 → 5 → 6, the total cost incurred would be |30 - 60| + |60 - 60| + |60 - 50| = 40.

Submitted source-code file size, in bytes.

Execution CPU time reported by official Project CodeNet metadata, in milliseconds.

Peak memory usage reported by official Project CodeNet metadata, in kilobytes.


(defparameter mod-number 1000000007)
(defparameter inf 10000000000)

(defun unfold (p f g seed &optional (tail-gen (lambda () '())))
  (if (p seed)
      (tail-gen seed)
	  (cons (f seed)
	        (unfold p f g (g seed) tail-gen))))

(defun iota (c &optional (s 0) (step 1) (acc nil))
  (if (zerop c)
      (reverse acc)
      (iota (1- c) (+ s step) step (cons s acc))))

(defun sum (list &optional (init 0))
  (reduce #'+ list :initial-value init))

(defun chomp (str)
    (string-right-trim '(#\Return #\Linefeed) str))

(defun split (x str &optional (acc nil))
  (let ((pos (search x str))
        (size (length x)))
    (if pos
        (split x (subseq str (+ pos size)) (cons (subseq str 0 pos) acc))
        (reverse (cons str acc)))))

(defmacro debug-print (x)
  `(let ((y ,x))
    (format t "~A: ~A~%" ',x y)
	y))

(defun solve (pos stairs dp)
  (let ((memo (aref dp pos)))
    (if memo
        memo
        (let ((ret (apply #'min
                          (mapcar (lambda (prev-pos)
                                    (if (< prev-pos 0)
                                        inf
                                        (+ (if (= prev-pos 0)
                                             0
                                             (solve prev-pos stairs dp))
                                           (abs (- (aref stairs prev-pos) (aref stairs pos))))))
                                  (list (1- pos) (- pos 2))))))
          (setf (aref dp pos) ret)
          ret))))

(let* ((n (read))
       (stairs (make-array n :initial-contents (mapcar #'parse-integer (split " " (read-line)))))
       (dp (make-array n :initial-element nil)))
  (format t "~A~%" (solve (1- n) stairs dp)))


(defparameter mod-number 1000000007)
(defparameter inf 10000000000)

(defun unfold (p f g seed &optional (tail-gen (lambda () '())))
  (if (p seed)
      (tail-gen seed)
	  (cons (f seed)
	        (unfold p f g (g seed) tail-gen))))

(defun iota (c &optional (s 0) (step 1) (acc nil))
  (if (zerop c)
      (reverse acc)
      (iota (1- c) (+ s step) step (cons s acc))))

(defun sum (list &optional (init 0))
  (reduce #'+ list :initial-value init))

(defun chomp (str)
    (string-right-trim '(#\Return #\Linefeed) str))

(defun split (x str &optional (acc nil))
  (let ((pos (search x str))
        (size (length x)))
    (if pos
        (split x (subseq str (+ pos size)) (cons (subseq str 0 pos) acc))
        (reverse (cons str acc)))))

(defmacro debug-print (x)
  `(let ((y ,x))
    (format t "~A: ~A~%" ',x y)
	y))

(defun solve (pos stairs dp)
  (let ((memo (aref dp pos)))
    (if memo
        memo
        (let ((ret (apply #'min
                          (mapcar (lambda (prev-pos)
                                    (if (< prev-pos 0)
                                        inf
                                        (+ (if (= prev-pos 0)
                                             0
                                             (solve prev-pos stairs dp))
                                           (abs (- (aref stairs prev-pos) (aref stairs pos))))))
                                  (list (1- pos) (- pos 2))))))
          (setf (aref dp pos) ret)
          ret))))

(let* ((n (read))
       (stairs (make-array n :initial-contents (mapcar #'parse-integer (split " " (read-line)))))
       (dp (make-array n :initial-element nil)))
  (format t "~A~%" stairs)
  (format t "~A~%" (solve (1- n) stairs dp)))


(defvar N (read))
(defvar infty (expt 10 10))

(loop repeat (1- N) with a = infty and b = 0
      and ha and hb = 0 and hc = (read) do
      (shiftf ha hb hc (read))
      (shiftf a b (min (+ a (abs (- ha hc)))
                       (+ b (abs (- hb hc)))))
      finally (princ b))


(defun Frog-1 (n)
  (let
   ((h
     (make-array 0 :fill-pointer 0 :adjustable t))
    (dp
     (make-array 0 :fill-pointer 0 :adjustable t))
     )
   (loop :repeat n
         :do (vector-push-extend (read) h))
   (vector-push-extend 0 dp)
   (vector-push-extend (abs (- (aref h 0) (aref h 1))) dp)
   (if (> n 2)
       (loop :for i :from 2 :to (- n 1)
           	 :do (let ((tmp1
                        (+ (aref dp (- i 1)) (abs (- (aref h i) (aref h (- i 1))))))
                   	   (tmp2
                        (+ (aref dp (- i 2)) (abs (- (aref h i) (aref h (- i 2)))))))
                  	  (if (< tmp1 tmp2)
                          (vector-push-extend tmp1 dp)
                      	  (vector-push-extend tmp2 dp)))))
   (aref dp (- n 1))))

(print (Frog-1 (read)))


(defun chmin (a b)
  (if (> a b)
      b
      a))

(defun main ()
  (let* ((n (read))
         (h (make-array n))
         (dp (make-array 100005 :initial-element 1000000000)))
    (dotimes (i n)
      (setf (aref h i) (read)))
    (setf (aref dp 0) 0)

    (dotimes (i (1- n))
      (setf (aref dp (1+ i)) (chmin (aref dp (1+ i)) (+ (aref dp i) (abs (- (aref h (1+ i)) (aref h i))))))
      (if (> i 0)
          (setf (aref dp (1+ i)) (chmin (aref dp (1+ i)) (+ (aref dp (1- i)) (abs (- (aref h (1+ i)) (aref h (1- i)))))))))
    (format t "~d~%" (aref dp (1- n)))))

(main)


#-swank
(unless (member :child-sbcl *features*)
  (quit
   :unix-status
   (process-exit-code
    (run-program *runtime-pathname*
                 `("--control-stack-size" "128MB"
                   "--noinform" "--disable-ldb" "--lose-on-corruption" "--end-runtime-options"
                   "--eval" "(push :child-sbcl *features*)"
                   "--script" ,(namestring *load-pathname*))
                 :output t :error t :input t))))
(defmacro dp (name args memo-size &body body)
  (let ((memo (gensym)))
    `(let ((,memo (make-array ,memo-size :initial-element nil)))
       (defun ,name ,args
         (or (aref ,memo ,@args)
             (setf (aref ,memo ,@args)
                   ,@body))))))

(defun split (x str)
  (let ((pos (search x str))
        (size (length x)))
    (if pos
      (cons (subseq str 0 pos)
            (split x (subseq str (+ pos size))))
      (list str))))

(defvar n (read))
(defvar h (coerce (loop repeat n collect (read)) 'vector))

(defun cul-cost (from to)
  (labels ((height (x)
             (if (>= x n) (expt 10 10)
                 (aref h x))))
    (abs (- (height from) (height to)))))

(dp func (x) '(100010)
    (if (>= x (1- n)) 0
        (apply #'min
               (loop for i from 1 to 2
                     collect (+ (cul-cost x (+ x i)) (func (+ x i)))))))

(princ (func 0))


#-swank
(unless (member :child-sbcl *features*)
  (quit
   :unix-status
   (process-exit-code
    (run-program *runtime-pathname*
                 `("--control-stack-size" "128MB"
                   "--noinform" "--disable-ldb" "--lose-on-corruption" "--end-runtime-options"
                   "--eval" "(push :child-sbcl *features*)"
                   "--script" ,(namestring *load-pathname*))
                 :output t :error t :input t))))
(defmacro dp (name args memo-size &body body)
  (let ((memo (gensym)))
    `(let ((,memo (make-array ,memo-size :initial-element nil)))
       (defun ,name ,args
         (or (aref ,memo ,@args)
             (setf (aref ,memo ,@args)
                   ,@body))))))

(defun split (x str)
  (let ((pos (search x str))
        (size (length x)))
    (if pos
      (cons (subseq str 0 pos)
            (split x (subseq str (+ pos size))))
      (list str))))

(defvar n (read))
(defvar h (coerce (loop repeat n collect (read)) 'vector))

(defun cul-cost (from to)
  (labels ((height (x)
             (if (>= x n) 0
                 (aref h x))))
    (abs (- (height from) (height to)))))

(dp func (x) '(100010)
    (if (>= x (1- n)) 0
        (apply #'min
               (loop for i from 1 to 2
                     collect (+ (cul-cost x (+ x i)) (func (+ x i)))))))

(princ (func 0))


#-swank
(unless (member :child-sbcl *features*)
  (quit
   :unix-status
   (process-exit-code
    (run-program *runtime-pathname*
                 `("--control-stack-size" "128MB"
                   "--noinform" "--disable-ldb" "--lose-on-corruption" "--end-runtime-options"
                   "--eval" "(push :child-sbcl *features*)"
                   "--script" ,(namestring *load-pathname*))
                 :output t :error t :input t))))

(defparameter *hash-memo* (make-hash-table :test #'equal))

(defmacro dp (func &body args)
  `(let* ((h (list (quote func) ,@args))
          (g (gethash h *hash-memo*)))
     (if g
       g
       (setf (gethash h *hash-memo*) (eval h)))))

(defparameter n (read))
(defparameter line (concatenate 'vector (loop repeat n collect (read))))

(defun num (x)
  (if (>= x n)
    (aref line (1- n))
    (aref line x)))

(defun get-value (a b)
  (abs (- (num a) (num b))))

(defun func (place)
  (if (>= place (1- n))
    0
    (min (+ (get-value place (1+ place)) (dp func (1+ place)))
         (+ (get-value place (+ place 2)) (dp func (+ place 2))))))

(princ (dp func 0))


(defparameter *hash-memo* (make-hash-table :test #'equal))

(defmacro dp (func &body args)
  `(let* ((h (list (quote func) ,@args))
          (g (gethash h *hash-memo*)))
     (if g
       g
       (setf (gethash h *hash-memo*) (eval h)))))

(defparameter n (read))
(defparameter line (concatenate 'vector (loop repeat n collect (read))))

(defun num (x)
  (if (>= x n)
    (aref line (1- n))
    (aref line x)))

(defun get-value (a b)
  (abs (- (num a) (num b))))

(defun func (place)
  (if (>= place (1- n))
    0
    (min (+ (get-value place (1+ place)) (dp func (1+ place)))
         (+ (get-value place (+ place 2)) (dp func (+ place 2))))))

(princ (dp func 0))


(defun solve (n h)
  (let ((costs (make-array n :element-type 'fixnum)))
    (loop for i from 0 below n
          do (setf (aref costs i)
                   (min (+ (aref costs (max (- i 1) 0))
                           (abs (- (aref h (max (- i 1) 0))
                                   (aref h i))))
                        (+ (aref costs (max (- i 2) 0))
                           (abs (- (aref h (max (- i 2) 0))
                                   (aref h i)))))))
    (aref costs (1- n))))

#-swank
(let* ((n (read))
       (h (make-array n :element-type 'fixnum)))
  (loop for i from 0 below n
        do (setf (aref h i) (read)))
  (format t "~A~%" (solve n h)))


(defun solve (n h)
  (let ((costs (make-array n :element-type 'fixnum)))
    (loop for i from 0 below n
          do (setf (aref costs i)
                   (min (+ (aref costs (max (- i 1) 0))
                           (abs (- (nth (max (- i 1) 0) h)
                                   (nth i h))))
                        (+ (aref costs (max (- i 2) 0))
                           (abs (- (nth (max (- i 2) 0) h)
                                   (nth i h)))))))
    (aref costs (1- n))))

#-swank
(let* ((n (read))
       (h (loop repeat n collect (read))))
  (format t "~A~%" (solve n h)))


(let* ((n (read))
       (arr (make-array n :initial-element 0 :element-type 'fixnum))
       (dparr (make-array (1+ n) :initial-element most-positive-fixnum :element-type 'fixnum)))
  (loop :for k :from 0 :upto (1- n) :do(setf (aref arr k) (read)))
  (setf (aref dparr 0) 0)
  (setf (aref dparr 1) (abs (- (aref arr 0) (aref arr 1))))
  (loop :for k :from 2 :upto (1- n)
        :do (setf (aref dparr k)
                  (min (+ (aref dparr (- k 2))
                          (abs (- (aref arr (- k 2)) (aref arr k))))
                       (+ (aref dparr (- k 1))
                          (abs (- (aref arr (- k 1)) (aref arr k)))))))
  (princ (aref dparr (1- n))))


(let* ((n (read))
       (lst (loop :repeat n :collect (read)))
       (dparr (make-array (1+ n) :initial-element most-positive-fixnum :element-type 'fixnum)))
  (setf (aref dparr 0) 0)
  (setf (aref dparr 1) (abs (- (car lst) (cadr lst))))
  (loop :for k :from 2 :upto (1- n)
        :do (setf (aref dparr k)
                  (min (+ (aref dparr (- k 2))
                          (abs (- (elt lst (- k 2)) (elt lst k))))
                       (+ (aref dparr (- k 1))
                          (abs (- (elt lst (- k 1)) (elt lst k)))))))
  (princ (aref dparr (1- n))))


(unless (member :child-sbcl *features*)
  (quit
   :unix-status
   (process-exit-code
    (run-program *runtime-pathname*
                 `("--control-stack-size" "128MB"
                   "--noinform" "--disable-ldb" "--lose-on-corruption" "--end-runtime-options"
                   "--eval" "(push :child-sbcl *features*)"
                   "--script" ,(namestring *load-pathname*))
                 :output t :error t :input t))))

(defvar *n* (read))
(defvar *k* (read))
(defvar *hi* (make-array *n*))

;dp[i]→i番目にたどり着くための最小コスト
(defvar *dp* (make-array *n* :initial-element nil))

(dotimes (i *n*)
         (setf (aref *hi* i) (read)))

(defun cost (now delta)
   (abs (- (aref *hi* now) (aref *hi* (- now delta)))))

;dpテーブル初期化
(setf (aref *dp* 0) 0)

(defun cand (i delta)
  (if (< *k* delta)
    nil
    (cons (+ (ans (- i delta)) (cost i delta)) (cand i (+ delta 1)))))

(defun ans (i)
  (cond
    ((aref *dp* i) (aref *dp* i))
    (t
       (setf (aref *dp* i) (apply #'min (cand i 1))))))

(print (ans (1- *n*)))


(unless (member :child-sbcl *features*)
  (quit
   :unix-status
   (process-exit-code
    (run-program *runtime-pathname*
                 `("--control-stack-size" "128MB"
                   "--noinform" "--disable-ldb" "--lose-on-corruption" "--end-runtime-options"
                   "--eval" "(push :child-sbcl *features*)"
                   "--script" ,(namestring *load-pathname*))
                 :output t :error t :input t))))

(defvar *n* (read))
(defvar *hi* (make-array *n*))

;dp[i]→i番目にたどり着くための最小コスト
(defvar *dp* (make-array *n* :initial-element nil))

(dotimes (i *n*)
         (setf (aref *hi* i) (read)))

(defun cost (now delta)
   (abs (- (aref *hi* now) (aref *hi* (- now delta)))))

;dpテーブル初期化
(setf (aref *dp* 0) 0)
(setf (aref *dp* 1) (cost 1 1))

(defun ans (i)
  (cond
    ((aref *dp* i) (aref *dp* i))
    (t (setf (aref *dp* i) (min
                            (+ (ans (- i 1)) (cost i 1))
                            (+ (ans (- i 2)) (cost i 2))
                            )))))

(print (ans (1- *n*)))


(unless (member :child-sbcl *features*)
  (quit
   :unix-status
   (process-exit-code
    (run-program *runtime-pathname*
                 `("--control-stack-size" "128MB"
                   "--noinform" "--disable-ldb" "--lose-on-corruption" "--end-runtime-options"
                   "--eval" "(push :child-sbcl *features*)"
                   "--script" ,(namestring *load-pathname*))
                 :output t :error t :input t))))

(defconstant inf 1000000)
(defvar *n* (read))
(defvar *hi* (make-array *n*))

;dp[i]→i番目にたどり着くための最小コスト
(defvar *dp* (make-array *n* :initial-element inf))

(dotimes (i *n*)
         (setf (aref *hi* i) (read)))

(defun cost (now delta)
   (abs (- (aref *hi* now) (aref *hi* (- now delta)))))

;dpテーブル初期化
(setf (aref *dp* 0) 0)
(setf (aref *dp* 1) (cost 1 1))

(defun ans (i)
  (cond
    ((< (aref *dp* i) inf) (aref *dp* i))
    (t (setf (aref *dp* i) (min
                            (+ (ans (- i 1)) (cost i 1))
                            (+ (ans (- i 2)) (cost i 2))
                            )))))

(print (ans (1- *n*)))


(unless (member :child-sbcl *features*)
  (quit
   :unix-status
   (process-exit-code
    (run-program *runtime-pathname*
                 `("--control-stack-size" "128MB"
                   "--noinform" "--disable-ldb" "--lose-on-corruption" "--end-runtime-options"
                   "--eval" "(push :child-sbcl *features*)"
                   "--script" ,(namestring *load-pathname*))
                 :output t :error t :input t))))

(defconstant inf 1000000)
(defvar *n* (read))
(defvar *hi* (make-array *n*))

;dp[i]→i番目にたどり着くための最小コスト
(defvar *dp* (make-array *n* :initial-element inf))

(dotimes (i *n*)
         (setf (aref *hi* i) (read)))

(defun cost (now delta)
   (abs (- (aref *hi* now) (aref *hi* (- now delta)))))

;dpテーブル初期化
(setf (aref *dp* 0) 0)
(setf (aref *dp* 1) (cost 1 1))

(defun ans (i)
  (cond
    ((< (aref *dp* i) inf) (aref *dp* i))
    (t (setf (aref *dp* i) (min
                            (+ (ans (- i 1)) (cost i 1))
                            (+ (ans (- i 2)) (cost i ))
                            )))))

(print (ans (1- *n*)))


(unless (member :child-sbcl *features*)
  (quit
   :unix-status
   (process-exit-code
    (run-program *runtime-pathname*
                 `("--control-stack-size" "128MB"
                   "--noinform" "--disable-ldb" "--lose-on-corruption" "--end-runtime-options"
                   "--eval" "(push :child-sbcl *features*)"
                   "--script" ,(namestring *load-pathname*))
                 :output t :error t :input t))))

(defconstant inf 1000000)
(defvar *n* (read))
(defvar *hi* (make-array *n*))

;dp[i]→i番目にたどり着くための最小コスト
(defvar *dp* (make-array *n* :initial-element inf))

(dotimes (i *n*)
         (setf (aref *hi* i) (read)))

(defun cost (now delta)
   (abs (- (aref *hi* now) (aref *hi* (- now delta)))))

;dpテーブル初期化
(setf (aref *dp* 0) 0)
(setf (aref *dp* 1) (cost 1 1))

(defun ans (i)
  (cond
    ((< (aref *dp* i) inf) (aref *dp* i))
    (t
     (let ((m (min
               (+ (ans (- i 1)) (cost i 1))
               (+ (ans (- i 2)) (cost i 2)))))
       (setf (aref *dp* i) m)))))

(print (ans (1- *n*)))


(defconstant inf 1000000)
(defvar *n* (read))
(defvar *hi* (make-array *n*))

;dp[i]→i番目にたどり着くための最小コスト
(defvar *dp* (make-array *n* :initial-element inf))

(dotimes (i *n*)
         (setf (aref *hi* i) (read)))

(defun cost (now delta)
   (abs (- (aref *hi* now) (aref *hi* (- now delta)))))

;dpテーブル初期化
(setf (aref *dp* 0) 0)
(setf (aref *dp* 1) (cost 1 1))

(defun ans (i)
  (cond
    ((< (aref *dp* i) inf) (aref *dp* i))
    (t
     (let ((m (min
               (+ (ans (- i 1)) (cost i 1))
               (+ (ans (- i 2)) (cost i 2)))))
       (setf (aref *dp* i) m)))))

(print (ans (1- *n*)))


(defvar *n* (read))
(defvar *hi* (make-array *n*))

;dp[i]→i番目にたどり着くための最小コスト
(defvar *dp* (make-array *n*))

(dotimes (i *n*)
         (setf (aref *hi* i) (read)))

(defun cost (now delta)
   (abs (- (aref *hi* now) (aref *hi* (- now delta)))))

;dpテーブル初期化
(setf (aref *dp* 0) 0)
(setf (aref *dp* 1) (cost 1 1))

(defun ans (i)
  (cond
    ((equal i 0) 0)
    ((equal i 1) (cost 1 1))
    (t
     (if (not (equal (aref *dp* i) nil)) (aref *dp* i)
      (setf (aref *dp* i)
       (min
        (+ (ans (- i 1)) (cost i 1))
        (+ (ans (- i 2)) (cost i 2))))))))

(ans (1- *n*))
(print (aref *dp* (1- *n*)))


(defvar *n* (read))
(defvar *hi* (make-array *n*))

;dp[i]→i番目にたどり着くための最小コスト
(defvar *dp* (make-array *n*))

(dotimes (i *n*)
         (setf (aref *hi* i) (read)))

(defun cost (now delta)
   (abs (- (aref *hi* now) (aref *hi* (- now delta)))))

;dpテーブル初期化
(setf (aref *dp* 0) 0)
(setf (aref *dp* 1) (cost 1 1))

(defun ans (i)
  (cond
    ((equal i 0) 0)
    ((equal i 1) (cost 1 1))
    (t
     (if (not (equal (aref *dp* i) nil)) (aref *dp* i)
      (setf (aref *dp* i)
       (min
        (+ (ans (- i 1)) (cost i 1))
        (+ (ans (- i 2)) (cost i 2))))))))

(ans (1- *n*))
(print *dp*)
(print (aref *dp* (1- *n*)))


(unless (member :child-sbcl *features*)
  (quit
   :unix-status
   (process-exit-code
    (run-program *runtime-pathname*
                 `("--control-stack-size" "128MB"
                   "--noinform" "--disable-ldb" "--lose-on-corruption" "--end-runtime-options"
                   "--eval" "(push :child-sbcl *features*)"
                   "--script" ,(namestring *load-pathname*))
                 :output t :error t :input t))))

(defvar *n* (read))
(defvar *hi* (make-array *n*))

;dp[i]→i番目にたどり着くための最小コスト
(defvar *dp* (make-array *n*))

(dotimes (i *n*)
         (setf (aref *hi* i) (read)))

(defun cost (now delta)
   (abs (- (aref *hi* now) (aref *hi* (- now delta)))))

;dpテーブル初期化
(setf (aref *dp* 0) 0)
(setf (aref *dp* 1) (cost 1 1))

(defun ans (i)
  (cond
    ((equal i 0) 0)
    ((equal i 1) (cost 1 1))
    (t
     (if (not (equal (aref *dp* i) nil)) (aref *dp* i)
      (setf (aref *dp* i)
       (min
        (+ (ans (- i 1)) (cost i 1))
        (+ (ans (- i 2)) (cost i 2))))))))

(ans (1- *n*))
(print (aref *dp* (1- *n*)))


(unless (member :child-sbcl *features*)
  (quit
   :unix-status
   (process-exit-code
    (run-program *runtime-pathname*
                 `("--control-stack-size" "128MB"
                   "--noinform" "--disable-ldb" "--lose-on-corruption" "--end-runtime-options"
                   "--eval" "(push :child-sbcl *features*)"
                   "--script" ,(namestring *load-pathname*))
                 :output t :error t :input t))))

(defvar *n* (read))
(defvar *hi* (make-array *n*))

;dp[i]→i番目にたどり着くための最小コスト
(defvar *dp* (make-array *n*))

(dotimes (i *n*)
         (setf (aref *hi* i) (read)))

(defun cost (now delta)
   (abs (- (aref *hi* now) (aref *hi* (- now delta)))))

;dpテーブル初期化
(setf (aref *dp* 0) 0)
(setf (aref *dp* 1) (cost 1 1))

(defun ans (i)
  (cond
    ((equal i 0) 0)
    ((equal i 1) (cost 1 1))
    (t (setf (aref *dp* i)
        (min
         (+ (ans (- i 1)) (cost i 1))
         (+ (ans (- i 2)) (cost i 2)))))))

(ans (1- *n*))
(print (aref *dp* (1- *n*)))


(unless (member :child-sbcl *features*)
  (quit
   :unix-status
   (process-exit-code
    (run-program *runtime-pathname*
                 `("--control-stack-size" "128MB"
                   "--noinform" "--disable-ldb" "--lose-on-corruption" "--end-runtime-options"
                   "--eval" "(push :child-sbcl *features*)"
                   "--script" ,(namestring *load-pathname*))
                 :output t :error t :input t))))

(defvar *n* (read))
(defvar *hi* (make-array *n*))

;dp[i]→i番目にたどり着くための最小コスト
(defvar *dp* (make-array *n*))

;*hi*の初期化
(dotimes (i *n*)
         (setf (aref *hi* i) (read)))

(defun cost (now delta)
   (abs (- (aref *hi* now) (aref *hi* (- now delta)))))

(defun ans (i)
  (cond
    ((equal i 0) 0)
    ((equal i 1) (cost 1 1))
    (t (setf (aref *dp* i)
        (min
         (+ (ans (- i 1)) (cost i 1))
         (+ (ans (- i 2)) (cost i 2)))))))

(ans (1- *n*))
(print (aref *dp* (1- *n*)))


(defvar *n* (read))
(defvar *hi* (make-array *n*))

(dotimes (i *n*)
         (setf (aref *hi* i) (read)))

(defun cost (now delta)
   (abs (- (aref *hi* now) (aref *hi* (- now delta)))))

(defun ans (i)
  (cond
    ((equal i 0) 0)
    ((equal i 1) (cost 1 1))
    (t (min
        (+ (ans (- i 1)) (cost i 1))
        (+ (ans (- i 2)) (cost i 2))))))

(print (ans (1- *n*)))


(defun carmincost (rest done mincosts)
  "To compute minimum cost to car of rest."
  (let ((dest (car rest)))
    (apply #'min (remove nil (mapcar (lambda (depth)
				       (let ((from (nth depth done))
					     (mincost (nth depth mincosts)))
					 (when from
					   (+ mincost (abs (- dest from))))
					 ))
				     '(0 1))))))
(defun solve (initial-list)
  (labels ((aux (rest done mincosts)
	     (if rest
		 (let ((mincost (carmincost rest done mincosts)))
		   (aux (cdr rest) (cons (car rest) done) (cons mincost mincosts)))
		 (car mincosts))
	     ))
    (aux (cdr initial-list) (list (car initial-list)) '(0))))
(let* ((n (read))
       (initial-list (loop for i from 1 to n
			collect (read))))
  (princ (solve initial-list))
  (princ #\newline))


;; #!/usr/bin/env sbcl --script

(declaim (optimize (speed 3) (safety 0) (debug 0)))

(deftype int32 () '(signed-byte 32))

(defun main ()
  (let* ((n (the int32 (read)))
         (k (the int32 (read)))
         (a (make-array n :element-type 'int32))
         (dp (make-array n :element-type 'int32 :initial-element (+ (expt 10 9) 100))))
    (dotimes (i n)
      (setf (aref a i) (the int32 (read))))
    (setf (aref dp 0) 0)
    (loop for i of-type int32 from 0 below n do
      (loop with from-val of-type int32 = (aref dp i)
         for j of-type int32 from (1+ i) to (+ i k)
         for nval of-type int32 = (+ from-val (abs (- (aref a i) (aref a j))))
         while (< j n)
         do
           (setf (aref dp j) (min (aref dp j) nval))))
    (format t "~a~%" (aref dp (- n 1)))))

;; (defun main2 ()
;;   (let* ((n (the int32 (read)))
;;          (k (the int32 (read)))
;;          (a (make-array n :element-type 'int32))
;;          (dp (make-array n :element-type 'int32 :initial-element (+ (expt 10 9) 100))))
;;     (let ((stream (make-string-input-stream (read-line))))
;;       (loop for i from 0
;;          for j = (read stream nil) until (null j)
;;          do (setf (aref a i) (the int32 j)))
;;     (setf (aref dp 0) 0)
;;     (loop for i of-type int32 from 0 below n do
;;       (loop with from-val of-type int32 = (aref dp i)
;;          for j of-type int32 from (1+ i) to (+ i k)
;;          while (< j n)
;;          do
;;            (let ((nval (the int32 (+ from-val (abs (- (aref a i) (aref a j)))))))
;;              (setf (aref dp j) (min (aref dp j) nval)))
;;     (format t "~a~%" (aref dp (- n 1)))))

(main)


(defun main ()
  (let* ((n (read))
         (a (make-array n :element-type 'fixnum))
         (dp (make-array n :element-type 'fixnum :initial-element (floor most-positive-fixnum 2))))
    (dotimes (i n)
      (setf (aref a i) (read)))
    (flet ((relax (from to)
             (let* ((from-val (aref dp from))
                    (to-val (aref dp to))
                    (diff (abs (- (aref a to) (aref a from)))))
               (setf (aref dp to) (min to-val (+ from-val diff))))))
      (setf (aref dp 0) 0)
      (dotimes (i n)
        (when (< (+ i 1) n) (relax i (+ i 1)))
        (when (< (+ i 2) n) (relax i (+ i 2)))))
    (format t "~a~%" (aref dp (- n 1)))))

(main)


#-(or child-sbcl swank)
(quit
 :unix-status
 (process-exit-code
  (run-program *runtime-pathname*
               '("--control-stack-size" "32MB"
                 "--noinform" "--disable-ldb" "--lose-on-corruption" "--end-runtime-options"
                 "--eval" "(push :child-sbcl *features*)"
                 "--script" #.(namestring *load-pathname*))
               :output t :error t :input t)))

(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 nlet (name args &body body)
  (labels ((ensure-list (x) (if (listp x) x (list x))))
    (let ((args (mapcar #'ensure-list args)))
      `(labels ((,name ,(mapcar #'car args) ,@body))
         (,name ,@(mapcar #'cadr args))))))

(declaim (inline split-ints-into-vector))
(defun split-ints-into-vector (str dest-arr &key (offset 0))
  (declare ((simple-array * (*)) dest-arr)
           ((integer 0 #.most-positive-fixnum) offset))
  (loop for idx from offset below (length dest-arr)
        for pos1 = 0 then (1+ pos2)
        for pos2 = (position #\space str :start pos1 :test #'char=)
        do (setf (aref dest-arr idx)
                 (parse-integer str :start pos1 :end pos2))
        finally (return dest-arr)))

;; (with-memoizing (:hash-table :test #'equal :key #'cons)
;;   (defun ...))
;; (with-memoizing (:array (10 10 * 10) :initial-element -1 :element-type 'fixnum)
;;   (defun ...))
(defmacro with-memoizing (cache-attribs def-form)
  (let* ((cache-attribs (if (atom cache-attribs) (list cache-attribs) cache-attribs))
         (cache-type (first cache-attribs))
         (dimensions-with-* (when (eql cache-type :array) (second cache-attribs)))
         (dimensions (remove '* dimensions-with-*))
         (rank (length dimensions))
         (rest-attribs (ecase cache-type
                         (:hash-table (cdr cache-attribs))
                         (:array (cddr cache-attribs))))
         (key (prog1 (getf rest-attribs :key) (remf rest-attribs :key)))
         (cache-form (case cache-type
                       (:hash-table `(make-hash-table ,@rest-attribs))
                       (:array `(make-array ',dimensions ,@rest-attribs))))
         (initial-element (when (eql cache-type :array)
                            (assert (member :initial-element rest-attribs))
                            (getf rest-attribs :initial-element))))
    (let ((cache (gensym))
          (value (gensym))
	  (present-p (gensym))
          (name-alias (gensym))
	  (args-lst (gensym))
          (indices (loop repeat rank collect (gensym))))
      (labels ((make-cache-check-form (cache-type args)
                 (case cache-type
                   (:hash-table
                    `(let ((,args-lst (funcall ,(or key #'list) ,@args)))
                       (multiple-value-bind (,value ,present-p)
                           (gethash ,args-lst ,cache)
                         (if ,present-p
                             ,value
                             (setf (gethash ,args-lst ,cache)
                                   (,name-alias ,@args))))))
                   (:array
                    (let ((memoized-args (loop for dimension in dimensions-with-*
                                               for arg in args
                                               unless (eql dimension '*)
                                               collect arg)))
                      (if key
                          `(multiple-value-bind ,indices
                               (funcall ,key ,@memoized-args)
                             (let ((,value (aref ,cache ,@indices)))
                               (if (eql ,initial-element ,value)
                                   (setf (aref ,cache ,@indices)
                                         (,name-alias ,@args))
                                   ,value)))
                          `(let ((,value (aref ,cache ,@memoized-args)))
                             (if (eql ,initial-element ,value)
                                 (setf (aref ,cache ,@memoized-args)
                                       (,name-alias ,@args))
                                 ,value)))))))
               (make-reset-form (cache-type)
                 (case cache-type
                   (:hash-table `(setf ,cache (make-hash-table ,@rest-attribs)))
                   (:array `(prog1 nil
                              (fill (array-storage-vector ,cache) ,initial-element)))))
               (make-reset-name (name)
                 (intern (format nil "RESET-~A" (symbol-name name))))
               (extract-declarations (body)
                 (remove-if-not (lambda (form) (eql 'declare (car form))) body)))
        (ecase (car def-form)
          ((defun)
           (destructuring-bind (_ name args &body body) def-form
             (declare (ignore _))
             `(let ((,cache ,cache-form))
                (defun ,(make-reset-name name) () ,(make-reset-form cache-type))
                (defun ,name ,args
                  (labels ((,name-alias ,args ,@body))
                    (declare (inline ,name-alias))
                    ,@(extract-declarations body)
                    ,(make-cache-check-form cache-type args))))))
          ((nlet)
           (destructuring-bind (_ name bindings &body body) def-form
             (declare (ignore _))
             `(let ((,cache ,cache-form))
                (nlet ,name ,bindings
                      ,(let ((args (mapcar (lambda (x) (if (atom x) x (car x))) bindings)))
                         `(labels ((,name-alias ,args ,@body))
                            (declare (inline ,name-alias))
                            ,@(extract-declarations body)
                            ,(make-cache-check-form cache-type args)))))))
          ((labels flet)
           (destructuring-bind (_ definitions &body labels-body) def-form
             (declare (ignore _))
             (destructuring-bind (name args &body body) (car definitions)
               `(let ((,cache ,cache-form))
                  (,(car def-form)
                   ((,(make-reset-name name) () ,(make-reset-form cache-type))
                    (,name ,args
                           (labels ((,name-alias ,args ,@body))
                             (declare (inline ,name-alias))
                             ,@(extract-declarations body)
                             ,(make-cache-check-form cache-type args)))
                    ,@(cdr definitions))
                   (declare (ignorable #',(make-reset-name name)))
                   ,@labels-body))))))))))

;; (test with-memoizing
;;   (finishes (macroexpand `(with-memoizing (:hash-table :test #'equal)
;;                             (defun add (x y) (+ x y)))))
;;   (finishes (macroexpand `(with-memoizing (:array '(10 10)
;;                                            :element-type 'fixnum
;;                                            :initial-element -1)
;;                             (defun add (x y) (+ x y)))))
;;   (finishes (macroexpand `(with-memoizing (:array '(10 10)
;;                                            :element-type 'fixnum
;;                                            :initial-element -1)
;;                             (labels ((add (x y) (+ x y))
;; 		                     (my-print (x) (print x)))
;; 	                      (add 1 2))))))

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

;; Hauptteil

(defun solve (hs)
  (declare #.OPT
           ((simple-array uint32 (*)) hs))
  (let ((dest (- (length hs) 1)))
    (with-memoizing (:array (100001) :element-type 'uint32
                     :initial-element #.(- (expt 2 32) 1))
      (nlet recurse ((i 0))
        (cond ((= i dest) 0)
              ((= i (- dest 1))
               (+ (abs (- (aref hs i) (aref hs (+ i 1))))
                  (recurse (+ i 1))))
              (t (min (+ (abs (- (aref hs i) (aref hs (+ i 1))))
                         (recurse (+ i 1)))
                      (+ (abs (- (aref hs i) (aref hs (+ i 2))))
                         (recurse (+ i 2))))))))))
(defun main ()
  (let* ((n (read))
         (hs (make-array n :element-type 'uint32)))
    (split-ints-into-vector (read-line) hs)
    (println (solve hs))))

#-swank(main)


#|
------------------------------------
                Utils
------------------------------------
|#

(in-package :cl-user)

(defconstant +mod+ 1000000007)
;(defconstant +mod+ 998244353)

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

#-swank
(unless (member :child-sbcl *features*)
  (quit
   :unix-status
   (process-exit-code
    (run-program *runtime-pathname*
                 `("--control-stack-size" "128MB"
                   "--noinform" "--disable-ldb" "--lose-on-corruption" "--end-runtime-options"
                   "--eval" "(push :child-sbcl *features*)"
                   "--script" ,(namestring *load-pathname*))
                 :output t :error t :input t))))

(defmacro define-int-types (&rest bits)
  `(progn
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~a" b)) () '(signed-byte ,b))) bits)
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~a" b)) () '(unsigned-byte ,b))) bits)))

(define-int-types 2 4 8 16 32 64)

(defmacro buffered-read-line (&optional (buffer-size 30) (in '*standard-input*) (term-char #\Space))
  (let ((buffer (gensym))
        (character (gensym))
        (idx (gensym)))
    `(let* ((,buffer (load-time-value (make-string ,buffer-size :element-type 'base-char))))
       (declare (simple-base-string ,buffer)
                (inline read-byte))
       (loop for ,character of-type base-char =
                ,(if (member :swank *features*)
                     `(read-char ,in nil #\Newline) ; on SLIME
                     `(code-char (read-byte ,in nil #.(char-code #\Newline))))
             for ,idx from 0
             until (char= ,character #\Newline)
             do (setf (schar ,buffer ,idx) ,character)
             finally (when (< ,idx ,buffer-size)
                       (setf (schar ,buffer ,idx) ,term-char))
                     (return (values ,buffer ,idx))))))

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

(declaim (inline println))
(defun println (obj &optional (stream *standard-output*))
  (let ((*read-default-float-format* 'double-float))
    (prog1
        (write obj :stream stream)
      (fresh-line stream))))

(defmacro safe-sort (list test &key (key #'identity))
  `(progn
    (declaim (inline sort sb-impl::stable-sort-list))
    (sort (copy-seq ,list) ,test :key ,key)))

(declaim (inline read-numbers-to-list))
(defun read-numbers-to-list (size)
  (loop repeat size collect (read-fixnum)))

(declaim (inline read-numbers-to-array))
(defun read-numbers-to-array (size)
  (let ((arr (make-array size
                         :element-type 'fixnum
                         :adjustable nil)))
    (declare ((array fixnum 1) arr))
    (loop for i of-type fixnum below size do
         (setf (aref arr i) (read-fixnum))
       finally
         (return arr))))

(defmacro read-characters-to-board (row-size column-size)
  (let ((board (gensym))
        (r (gensym))
        (c (gensym))
        (tmp (gensym)))
    `(let ((,board (make-array '(,row-size ,column-size) :element-type 'character :adjustable nil)))
       (dotimes (,r ,row-size ,board)
         (let ((,tmp (buffered-read-line)))
           (dotimes (,c ,column-size)
             (setf (aref ,board ,r ,c) (char ,tmp ,c))))))))

(declaim (inline princ-for-each-line))
(defmethod princ-for-each-line ((sequence list))
  (format t "~{~a~&~}" sequence))

(defmethod princ-for-each-line ((sequence vector))
  (loop for i below (length sequence) do
       (princ (aref sequence i))
       (fresh-line)))

(declaim (inline unwrap))
(defun unwrap (list)
  (the string
       (format nil "~{~a~^ ~}" list)))

(defmacro with-buffered-stdout (&body body)
  (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)))))

(defmacro maxf (place cand)
  `(setf ,place (max ,place ,cand)))

(defmacro minf (place cand)
  `(setf ,place (min ,place ,cand)))

(defmacro modf (place &optional (m +mod+))
  `(setf ,place (mod ,place ,m)))

(defmacro alambda (parms &body body)
  `(labels ((self ,parms ,@body))
     #'self))

(declaim (inline iota))
(defun iota (count &optional (start 0) (step 1))
  (loop for i from 0 below count collect (+ start (* i step))))

(declaim (inline int->lst))
(defun int->lst (integer)
  (declare ((integer 0) integer))
  (labels ((sub (int &optional (acc nil))
             (declare ((integer 0) int)
                      (list acc))
             (if (zerop int)
                 acc
                 (sub (floor int 10) (cons (rem int 10) acc)))))
    (sub integer)))

(declaim (inline lst->int))
(defun lst->int (list)
  (declare (list list))
  (labels ((sub (xs &optional (acc 0))
             (declare (ftype (function (list &optional (integer 0)) (integer 0)) sub))
             (declare (list xs)
                      ((integer 0) acc))
             (if (null xs)
                 acc
                 (sub (rest xs) (+ (* acc 10)
                                   (rem (first xs) 10))))))
    (the fixnum
         (sub list))))

(defun int->str (integer)
  (format nil "~a" integer))

(defun str->int (str)
  (parse-integer str))

(defun char->int (char)
  (declare (character char))
  (- (char-code char) #.(char-code #\0)))

(declaim (inline prime-factorize-to-list))
(defun prime-factorize-to-list (integer)
  (declare ((integer 0) integer))
  (the list
       (if (<= integer 1)
           nil
           (loop
              while (<= (* f f) integer)
              with acc list = nil
              with f integer = 2
              do
                (if (zerop (rem integer f))
                    (progn
                      (push f acc)
                      (setq integer (floor integer f)))
                    (incf f))
              finally
                (when (/= integer 1)
                  (push integer acc))
                (return (reverse acc))))))

(declaim (inline prime-p))
(defun prime-p (integer)
  (declare ((integer 1) integer))
  (if (= integer 1)
      nil
      (loop
         with f = 2
         while (<= (* f f) integer)
         do
           (when (zerop (rem integer f))
             (return nil))
           (incf f)
         finally
           (return t))))

#|
------------------------------------
                Body
------------------------------------
|#

(defparameter *inf* 1000000001)

(declaim (inline solve main))
(defun solve (n k h)
  (declare (uint32 n)
           (uint8 k)
           ((array fixnum 1)))
  (let ((memo (make-array (+ n 110)
                          :element-type 'fixnum
                          :adjustable nil
                          :initial-element *inf*)))
    (declare ((array fixnum 1) memo))
    (setf (aref memo 0) 0)
    (dotimes (i n)
      (dotimes (j k)
        (minf (aref memo (+ i j 1))
              (+ (aref memo i)
                 (abs (- (aref h (min (+ i j 1) (1- n)))
                         (aref h i)))))))
    (aref memo (1- n))))

(defun main ()
  (declare #.OPT)
  (let ((n (read))
        (k (read)))
    (declare (uint32 n)
             (uint8 k))
    (let ((h (read-numbers-to-array n)))
      (declare ((array fixnum 1) h))
      (princ (solve n k h))
      (fresh-line))))

#-swank (main)

Score : 100 points

Problem Statement

There are N stones, numbered 1, 2, \ldots, N.
For each i (1 \leq i \leq N), the height of Stone i is h_i.

There is a frog who is initially on Stone 1.
He will repeat the following action some number of times to reach Stone N:

If the frog is currently on Stone i, jump to one of the following: Stone i + 1, i + 2, \ldots, i + K. Here, a cost of |h_i - h_j| is incurred, where j is the stone to land on.

Find the minimum possible total cost incurred before the frog reaches Stone N.

Constraints

All values in input are integers.

2 \leq N \leq 10^5

1 \leq K \leq 100

1 \leq h_i \leq 10^4

Input

Input is given from Standard Input in the following format:

N K
h_1 h_2 \ldots h_N

Output

Print the minimum possible total cost incurred.

Sample Input 1

5 3
10 30 40 50 20

Sample Output 1

30

If we follow the path 1 → 2 → 5, the total cost incurred would be |10 - 30| + |30 - 20| = 30.

Sample Input 2

3 1
10 20 10

Sample Output 2

20

If we follow the path 1 → 2 → 3, the total cost incurred would be |10 - 20| + |20 - 10| = 20.

Sample Input 3

2 100
10 10

Sample Output 3

0

If we follow the path 1 → 2, the total cost incurred would be |10 - 10| = 0.

Sample Input 4

10 4
40 10 20 70 80 10 20 70 80 60

Sample Output 4

40

If we follow the path 1 → 4 → 8 → 10, the total cost incurred would be |40 - 70| + |70 - 70| + |70 - 60| = 40.


#|
------------------------------------
                Utils
------------------------------------
|#

(in-package :cl-user)

(defconstant +mod+ 1000000007)
;(defconstant +mod+ 998244353)

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

#-swank
(unless (member :child-sbcl *features*)
  (quit
   :unix-status
   (process-exit-code
    (run-program *runtime-pathname*
                 `("--control-stack-size" "128MB"
                   "--noinform" "--disable-ldb" "--lose-on-corruption" "--end-runtime-options"
                   "--eval" "(push :child-sbcl *features*)"
                   "--script" ,(namestring *load-pathname*))
                 :output t :error t :input t))))

(defmacro define-int-types (&rest bits)
  `(progn
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~a" b)) () '(signed-byte ,b))) bits)
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~a" b)) () '(unsigned-byte ,b))) bits)))

(define-int-types 2 4 8 16 32 64)

(defmacro buffered-read-line (&optional (buffer-size 30) (in '*standard-input*) (term-char #\Space))
  (let ((buffer (gensym))
        (character (gensym))
        (idx (gensym)))
    `(let* ((,buffer (load-time-value (make-string ,buffer-size :element-type 'base-char))))
       (declare (simple-base-string ,buffer)
                (inline read-byte))
       (loop for ,character of-type base-char =
                ,(if (member :swank *features*)
                     `(read-char ,in nil #\Newline) ; on SLIME
                     `(code-char (read-byte ,in nil #.(char-code #\Newline))))
             for ,idx from 0
             until (char= ,character #\Newline)
             do (setf (schar ,buffer ,idx) ,character)
             finally (when (< ,idx ,buffer-size)
                       (setf (schar ,buffer ,idx) ,term-char))
                     (return (values ,buffer ,idx))))))

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

(declaim (inline println))
(defun println (obj &optional (stream *standard-output*))
  (let ((*read-default-float-format* 'double-float))
    (prog1
        (write obj :stream stream)
      (fresh-line stream))))

(defmacro safe-sort (list test &key (key #'identity))
  `(progn
    (declaim (inline sort sb-impl::stable-sort-list))
    (sort (copy-seq ,list) ,test :key ,key)))

(declaim (inline read-numbers-to-list))
(defun read-numbers-to-list (size)
  (loop repeat size collect (read-fixnum)))

(declaim (inline read-numbers-to-array))
(defun read-numbers-to-array (size)
  (let ((arr (make-array size
                         :element-type 'fixnum
                         :adjustable nil)))
    (declare ((array fixnum 1) arr))
    (loop for i of-type fixnum below size do
         (setf (aref arr i) (read-fixnum))
       finally
         (return arr))))

(defmacro read-characters-to-board (row-size column-size)
  (let ((board (gensym))
        (r (gensym))
        (c (gensym))
        (tmp (gensym)))
    `(let ((,board (make-array '(,row-size ,column-size) :element-type 'character :adjustable nil)))
       (dotimes (,r ,row-size ,board)
         (let ((,tmp (buffered-read-line)))
           (dotimes (,c ,column-size)
             (setf (aref ,board ,r ,c) (char ,tmp ,c))))))))

(declaim (inline princ-for-each-line))
(defmethod princ-for-each-line ((sequence list))
  (format t "~{~a~&~}" sequence))

(defmethod princ-for-each-line ((sequence vector))
  (loop for i below (length sequence) do
       (princ (aref sequence i))
       (fresh-line)))

(declaim (inline unwrap))
(defun unwrap (list)
  (the string
       (format nil "~{~a~^ ~}" list)))

(defmacro with-buffered-stdout (&body body)
  (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)))))

(defmacro maxf (place cand)
  `(setf ,place (max ,place ,cand)))

(defmacro minf (place cand)
  `(setf ,place (min ,place ,cand)))

(defmacro modf (place &optional (m +mod+))
  `(setf ,place (mod ,place ,m)))

(defmacro alambda (parms &body body)
  `(labels ((self ,parms ,@body))
     #'self))

(declaim (inline iota))
(defun iota (count &optional (start 0) (step 1))
  (loop for i from 0 below count collect (+ start (* i step))))

(declaim (inline int->lst))
(defun int->lst (integer)
  (declare ((integer 0) integer))
  (labels ((sub (int &optional (acc nil))
             (declare ((integer 0) int)
                      (list acc))
             (if (zerop int)
                 acc
                 (sub (floor int 10) (cons (rem int 10) acc)))))
    (sub integer)))

(declaim (inline lst->int))
(defun lst->int (list)
  (declare (list list))
  (labels ((sub (xs &optional (acc 0))
             (declare (ftype (function (list &optional (integer 0)) (integer 0)) sub))
             (declare (list xs)
                      ((integer 0) acc))
             (if (null xs)
                 acc
                 (sub (rest xs) (+ (* acc 10)
                                   (rem (first xs) 10))))))
    (the fixnum
         (sub list))))

(defun int->str (integer)
  (format nil "~a" integer))

(defun str->int (str)
  (parse-integer str))

(defun char->int (char)
  (declare (character char))
  (- (char-code char) #.(char-code #\0)))

(declaim (inline prime-factorize-to-list))
(defun prime-factorize-to-list (integer)
  (declare ((integer 0) integer))
  (the list
       (if (<= integer 1)
           nil
           (loop
              while (<= (* f f) integer)
              with acc list = nil
              with f integer = 2
              do
                (if (zerop (rem integer f))
                    (progn
                      (push f acc)
                      (setq integer (floor integer f)))
                    (incf f))
              finally
                (when (/= integer 1)
                  (push integer acc))
                (return (reverse acc))))))

(declaim (inline prime-p))
(defun prime-p (integer)
  (declare ((integer 1) integer))
  (if (= integer 1)
      nil
      (loop
         with f = 2
         while (<= (* f f) integer)
         do
           (when (zerop (rem integer f))
             (return nil))
           (incf f)
         finally
           (return t))))

#|
------------------------------------
                Body
------------------------------------
|#

(defparameter *inf* 1000000001)

(declaim (inline solve main))
(defun solve (n k h)
  (let ((memo (make-array (+ n 110)
                          :element-type 'fixnum
                          :adjustable nil
                          :initial-element *inf*)))
    (declare ((array fixnum 1) memo))
    (setf (aref memo 0) 0)
    (dotimes (i n)
      (dotimes (j k)
        (when (< (+ i j 1) n)
          (minf (aref memo (+ i j 1))
                (+ (aref memo i)
                   (abs (- (aref h (+ i j 1))
                           (aref h i))))))))
    (aref memo (1- n))))

(defun main ()
  (declare #.OPT)
  (let ((n (read))
        (k (read)))
    (declare (uint32 n)
             (uint8 k))
    (let ((h (read-numbers-to-array n)))
      (declare ((array fixnum 1) h))
      (princ (solve n k h))
      (fresh-line))))

#-swank (main)


(defparameter *inf* (expt 10 9))

(defun solve (n k h)
  (let ((memo (make-array (+ n k 1) :initial-element *inf*)))
    (setf (aref memo 0) 0)
    (dotimes (x n)
      (dotimes (i k)
        (setf (aref memo (+ x (1+ i))) (min (aref memo (+ x (1+ i)))
                                       (+ (aref memo x)
                                          (abs (- (aref h (+ x (1+ i)))
                                                  (aref h x))))))))
    (aref memo (1- n))))

(defun main ()
  (let* ((n (read))
         (k (read))
         (h (make-array 104000 :adjustable t :initial-element 0)))
    (dotimes (i n)
      (setf (aref h i) (read)))
    (princ (solve n k h))
    (fresh-line)))

(main)


(unless (member :child-sbcl *features*)
  #-swank (quit
            :unix-status
            (process-exit-code
              (run-program *runtime-pathname*
                           `("--control-stack-size" "128MB"
                             "--noinform"
                             "--disable-ldb"
                             "--lose-on-corruption"
                             "--end-runtime-options"
                             "--eval" "(push :child-sbcl *features*)"
                             "--script" ,(namestring *load-pathname*))
                           :output t :error t :input t))))

(eval-when (:compile-toplevel)
  #-swank (proclaim '(optimize (speed 3) (debug 0) (safety 0))))

(defconstant mod-number 1000000007)

(defun unfold (p f g seed &optional (tail-gen (lambda () '())))
  (if (p seed)
      (tail-gen seed)
      (cons (f seed)
            (unfold p f g (g seed) tail-gen))))

(defun iota (c &optional (s 0) (step 1) (acc nil))
  (if (zerop c)
      (reverse acc)
      (iota (1- c) (+ s step) step (cons s acc))))

(defun sum (list &optional (init 0))
  (reduce #'+ list :initial-value init))

(defun chomp (str)
  (string-right-trim '(#\Return #\Linefeed) str))

(defun read-integer (&optional (in *standard-input*))
  (declare (inline read-byte))
  (labels ((number-char-p (b)
             (<= #.(char-code #\0) b #.(char-code #\9)))
           (minus-char-p (b)
             (= b #.(char-code #\-)))
           (to-number (b)
             (- b #.(char-code #\0))))
    (declare (inline number-char-p minus-char-p to-number))
    (macrolet ((%read-byte ()
                 '(the (unsigned-byte 8)
                       #+swank (char-code (read-char in nil #\Nul))
                       #-swank (read-byte in nil 0))))
      (let* ((minus-p nil)
             (x (loop for b = (%read-byte)
                      if (number-char-p b)
                        return (to-number b)
                        end
                      if (minus-char-p b)
                        do (setf minus-p t))))
        (declare (boolean minus-p) (fixnum x))
        (loop for b = (%read-byte)
              and y = x then (+ (* y 10) (to-number b))
              unless (number-char-p b)
                return (the fixnum (funcall (if minus-p #'- #'+) y)))))))

(defun split (x str &optional (acc nil))
  (let ((pos (search x str))
        (size (length x)))
    (if pos
        (split x (subseq str (+ pos size)) (cons (subseq str 0 pos) acc))
        (nreverse (cons str acc)))))

(defmacro minf (field &rest x)
  `(setf ,field (min ,field ,@x)))

(defmacro maxf (field &rest x)
  `(setf ,field (max ,field ,@x)))

(defmacro debug-print (x)
  `(let ((y ,x))
    (format t "~A: ~A~%" ',x y)
    y))

(defun make-solve (n k h)
  (let ((dp (make-array n :initial-element nil)))
    (setf (aref dp 0) 0)
    (labels ((solve (pos)
               (let ((memo (aref dp pos)))
                 (if memo
                     memo
                     (let ((ret (loop for i from 1 to k
                                      if (>= (- pos i) 0)
                                        minimize (+ (solve (- pos i))
                                                    (abs (- (aref h pos) (aref h (- pos i))))))))
                       (setf (aref dp pos) ret)
                       ret)))))
      #'solve)))

(let* ((n (read))
       (k (read))
       (h (make-array n :adjustable t :fill-pointer 0)))
  (dotimes (i n)
    (vector-push (read-integer) h))
  (format t "~A~%" (funcall (make-solve n k h) (1- n))))


(unless (member :child-sbcl *features*)
  #-swank (quit
            :unix-status
            (process-exit-code
              name*
              `("--control-stack-size" "128MB"
                "--noinform" "--disable-ldb" "--lose-on-corruption" "--end-runtime-options"
                "--eval" "(push :child-sbcl *features*)"
                "--script" ,(namestring *load-pathname*))
              :output t :error t :input t)))

(eval-when (:compile-toplevel)
  #-swank (proclaim '(optimize (speed 3) (debug 0) (safety 0))))

(defconstant mod-number 1000000007)

(defun unfold (p f g seed &optional (tail-gen (lambda () '())))
  (if (p seed)
      (tail-gen seed)
      (cons (f seed)
            (unfold p f g (g seed) tail-gen))))

(defun iota (c &optional (s 0) (step 1) (acc nil))
  (if (zerop c)
      (reverse acc)
      (iota (1- c) (+ s step) step (cons s acc))))

(defun sum (list &optional (init 0))
  (reduce #'+ list :initial-value init))

(defun chomp (str)
  (string-right-trim '(#\Return #\Linefeed) str))

(defun read-integer (&optional (in *standard-input*))
  (declare (inline read-byte))
  (labels ((number-char-p (b)
             (<= #.(char-code #\0) b #.(char-code #\9)))
           (minus-char-p (b)
             (= b #.(char-code #\-)))
           (to-number (b)
             (- b #.(char-code #\0))))
    (declare (inline number-char-p minus-char-p to-number))
    (macrolet ((%read-byte ()
                 '(the (unsigned-byte 8)
                       #+swank (char-code (read-char in nil #\Nul))
                       #-swank (read-byte in nil 0))))
      (let* ((minus-p nil)
             (x (loop for b = (%read-byte)
                      if (number-char-p b)
                        return (to-number b)
                        end
                      if (minus-char-p b)
                        do (setf minus-p t))))
        (declare (boolean minus-p) (fixnum x))
        (loop for b = (%read-byte)
              and y = x then (+ (* y 10) (to-number b))
              unless (number-char-p b)
                return (the fixnum (funcall (if minus-p #'- #'+) y)))))))

(defun split (x str &optional (acc nil))
  (let ((pos (search x str))
        (size (length x)))
    (if pos
        (split x (subseq str (+ pos size)) (cons (subseq str 0 pos) acc))
        (nreverse (cons str acc)))))

(defmacro minf (field &rest x)
  `(setf ,field (min ,field ,@x)))

(defmacro maxf (field &rest x)
  `(setf ,field (max ,field ,@x)))

(defmacro debug-print (x)
  `(let ((y ,x))
    (format t "~A: ~A~%" ',x y)
    y))

(defun make-solve (n k h)
  (let ((dp (make-array n :initial-element nil)))
    (setf (aref dp 0) 0)
    (labels ((solve (pos)
               (let ((memo (aref dp pos)))
                 (if memo
                     memo
                     (let ((ret (loop for i from 1 to k
                                      if (>= (- pos i) 0)
                                        minimize (+ (solve (- pos i))
                                                    (abs (- (aref h pos) (aref h (- pos i))))))))
                       (setf (aref dp pos) ret)
                       ret)))))
      #'solve)))

(let* ((n (read))
       (k (read))
       (h (make-array n :adjustable t :fill-pointer 0)))
  (dotimes (i n)
    (vector-push (read-integer) h))
  (format t "~A~%" (funcall (make-solve n k h) (1- n))))


#-swank
(unless (member :child-sbcl *features*)
  (quit
   :unix-status
   (process-exit-code
    (run-program *runtime-pathname*
                 `("--control-stack-size" "128MB"
                   "--noinform" "--disable-ldb" "--lose-on-corruption" "--end-runtime-options"
                   "--eval" "(push :child-sbcl *features*)"
                   "--script" ,(namestring *load-pathname*))
                 :output t :error t :input t))))

(eval-when (:compile-toplevel)
  #-swank (proclaim '(optimize (speed 3) (debug 0) (safety 0))))

(defconstant mod-number 1000000007)

(defun unfold (p f g seed &optional (tail-gen (lambda () '())))
  (if (p seed)
      (tail-gen seed)
      (cons (f seed)
            (unfold p f g (g seed) tail-gen))))

(defun iota (c &optional (s 0) (step 1) (acc nil))
  (if (zerop c)
      (reverse acc)
      (iota (1- c) (+ s step) step (cons s acc))))

(defun sum (list &optional (init 0))
  (reduce #'+ list :initial-value init))

(defun chomp (str)
  (string-right-trim '(#\Return #\Linefeed) str))

(defun read-integer (&optional (in *standard-input*))
  (declare (inline read-byte))
  (labels ((number-char-p (b)
             (<= #.(char-code #\0) b #.(char-code #\9)))
           (minus-char-p (b)
             (= b #.(char-code #\-)))
           (to-number (b)
             (- b #.(char-code #\0))))
    (declare (inline number-char-p minus-char-p to-number))
    (macrolet ((%read-byte ()
                 '(the (unsigned-byte 8)
                       #+swank (char-code (read-char in nil #\Nul))
                       #-swank (read-byte in nil 0))))
      (let* ((minus-p nil)
             (x (loop for b = (%read-byte)
                      if (number-char-p b)
                        return (to-number b)
                        end
                      if (minus-char-p b)
                        do (setf minus-p t))))
        (declare (boolean minus-p) (fixnum x))
        (loop for b = (%read-byte)
              and y = x then (+ (* y 10) (to-number b))
              unless (number-char-p b)
                return (the fixnum (funcall (if minus-p #'- #'+) y)))))))

(defun split (x str &optional (acc nil))
  (let ((pos (search x str))
        (size (length x)))
    (if pos
        (split x (subseq str (+ pos size)) (cons (subseq str 0 pos) acc))
        (nreverse (cons str acc)))))

(defmacro minf (field &rest x)
  `(setf ,field (min ,field ,@x)))

(defmacro maxf (field &rest x)
  `(setf ,field (max ,field ,@x)))

(defmacro debug-print (x)
  `(let ((y ,x))
    (format t "~A: ~A~%" ',x y)
    y))

(defun make-solve (n k h)
  (let ((dp (make-array n :initial-element nil)))
    (setf (aref dp 0) 0)
    (labels ((solve (pos)
               (let ((memo (aref dp pos)))
                 (if memo
                     memo
                     (let ((ret (loop for i from 1 to k
                                      if (>= (- pos i) 0)
                                        minimize (+ (solve (- pos i))
                                                    (abs (- (aref h pos) (aref h (- pos i))))))))
                       (setf (aref dp pos) ret)
                       ret)))))
      #'solve)))

(let* ((n (read))
       (k (read))
       (h (make-array n :adjustable t :fill-pointer 0)))
  (dotimes (i n)
    (vector-push (read-integer) h))
  (format t "~A~%" (funcall (make-solve n k h) (1- n))))


(eval-when (:compile-toplevel)
  #-swank (proclaim '(optimize (speed 0) (debug 3) (safety 3))))

(defconstant mod-number 1000000007)

(defun unfold (p f g seed &optional (tail-gen (lambda () '())))
  (if (p seed)
      (tail-gen seed)
      (cons (f seed)
            (unfold p f g (g seed) tail-gen))))

(defun iota (c &optional (s 0) (step 1) (acc nil))
  (if (zerop c)
      (reverse acc)
      (iota (1- c) (+ s step) step (cons s acc))))

(defun sum (list &optional (init 0))
  (reduce #'+ list :initial-value init))

(defun chomp (str)
  (string-right-trim '(#\Return #\Linefeed) str))

(defun read-integer (&optional (in *standard-input*))
  (declare (inline read-byte))
  (labels ((number-char-p (b)
             (<= #.(char-code #\0) b #.(char-code #\9)))
           (minus-char-p (b)
             (= b #.(char-code #\-)))
           (to-number (b)
             (- b #.(char-code #\0))))
    (declare (inline number-char-p minus-char-p to-number))
    (macrolet ((%read-byte ()
                 '(the (unsigned-byte 8)
                       #+swank (char-code (read-char in nil #\Nul))
                       #-swank (read-byte in nil 0))))
      (let* ((minus-p nil)
             (x (loop for b = (%read-byte)
                      if (number-char-p b)
                        return (to-number b)
                        end
                      if (minus-char-p b)
                        do (setf minus-p t))))
        (declare (boolean minus-p) (fixnum x))
        (the fixnum (loop for b = (%read-byte)
                          and y = x then (+ (* y 10) (to-number b))
                          unless (number-char-p b)
                            return (funcall (if minus-p #'- #'+) y)))))))

(defun split (x str &optional (acc nil))
  (let ((pos (search x str))
        (size (length x)))
    (if pos
        (split x (subseq str (+ pos size)) (cons (subseq str 0 pos) acc))
        (nreverse (cons str acc)))))

(defmacro minf (field &rest x)
  `(setf ,field (min ,field ,@x)))

(defmacro maxf (field &rest x)
  `(setf ,field (max ,field ,@x)))

(defmacro debug-print (x)
  `(let ((y ,x))
    (format t "~A: ~A~%" ',x y)
    y))

(defun make-solve (n k h)
  (let ((dp (make-array n :initial-element nil)))
    (setf (aref dp 0) 0)
    (labels ((solve (pos)
               (let ((memo (aref dp pos)))
                 (if memo
                     memo
                     (let ((ret (loop for i from 1 to k
                                      if (>= (- pos i) 0)
                                        minimize (+ (solve (- pos i))
                                                    (abs (- (aref h pos) (aref h (- pos i))))))))
                       (setf (aref dp pos) ret)
                       ret)))))
      #'solve)))

(let* ((n (read))
       (k (read))
       (h (make-array n :adjustable t :fill-pointer 0)))
  (dotimes (i n)
    (vector-push (read-integer) h))
  (funcall (make-solve n k h) (1- n)))


#-swank
(unless (member :child-sbcl *features*)
  (quit
   :unix-status
   (process-exit-code
    (run-program *runtime-pathname*
                 `("--control-stack-size" "128MB"
                   "--noinform" "--disable-ldb" "--lose-on-corruption" "--end-runtime-options"
                   "--eval" "(push :child-sbcl *features*)"
                   "--script" ,(namestring *load-pathname*))
                 :output t :error t :input t))))
(defmacro dp (name args memo-size &body body)
  (let ((memo (gensym)))
    `(let ((,memo (make-array ,memo-size :initial-element nil)))
       (defun ,name ,args
         (or (aref ,memo ,@args)
             (setf (aref ,memo ,@args)
                   ,@body))))))

(defun split (x str)
  (let ((pos (search x str))
        (size (length x)))
    (if pos
      (cons (subseq str 0 pos)
            (split x (subseq str (+ pos size))))
      (list str))))

(defvar n (read))
(defvar k (read))
(defvar h (coerce (loop repeat n collect (read)) 'vector))

(defun cul-cost (from to)
  (labels ((height (x)
             (if (>= x n) (expt 10 10)
                 (aref h x))))
    (abs (- (height from) (height to)))))

(dp func (x) '(100010)
    (if (>= x (1- n)) 0
        (apply #'min
               (loop for i from 1 to k
                     collect (+ (cul-cost x (+ x i)) (func (+ x i)))))))

(princ (func 0))


(deftype uint32 () '(unsigned-byte 32))

(defun solve (n k h)
  (let ((costs (make-array n :element-type 'uint32)))
    (loop for i from 0 below n
          do (setf (aref costs i)
                   (loop for d from 1 to k
                         minimize (+ (aref costs (max 0 (- i d)))
                                     (abs (- (aref h (max 0 (- i d)))
                                             (aref h i)))))))
    (aref costs (1- n))))

#-swank
(let* ((n (read))
       (k (read))
       (h (make-array n :element-type 'uint32)))
  (loop for i from 0 below n
        do (setf (aref h i) (read)))
  (format t "~A~%" (solve n k h)))


;; -*- coding: utf-8 -*-
(eval-when (:compile-toplevel :load-toplevel :execute)
  (sb-int:defconstant-eqx OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0))
    #'equal)
  #+swank (ql:quickload '(:cl-debug-print :fiveam) :silent t))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)
#-swank (disable-debugger) ; for CS Academy

;; BEGIN_INSERTED_CONTENTS
(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  (declare #.OPT
           #-swank (sb-kernel:ansi-stream in))
  (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* ((result (loop (let ((byte (%read-byte)))
                           (when (<= 48 byte)
                             (return (- byte 48)))))))
      (declare ((integer 0 #.most-positive-fixnum) result))
      (loop
        (let* ((byte (%read-byte)))
          (if (<= 48 byte)
              (setq result (+ (- byte 48) (* 10 (the (integer 0 #.(floor most-positive-fixnum 10)) result))))
              (return result)))))))

(declaim (inline println))
(defun println (obj &optional (stream *standard-output*))
  (let ((*read-default-float-format* 'double-float))
    (prog1 (princ obj stream) (terpri stream))))

;; Body

(define-modify-macro minf (new-value) min)

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (k (read))
         (hs (make-array 100000 :element-type '(unsigned-byte 31)))
         (dp (make-array 100000 :element-type '(unsigned-byte 31) :initial-element #x7fffffff)))
    (declare ((unsigned-byte 31) n k))
    (setf (aref dp 0) 0)
    (dotimes (i n)
      (setf (aref hs i) (read-fixnum)))
    (dotimes (i n)
      (let ((h (aref hs i))
            (d (aref dp i)))
        (loop for dest from (+ i 1) to (min (+ i k) (- n 1))
              do (minf (aref dp dest) (+ d (abs (- h (aref hs dest))))))))
    (println (aref dp (- n 1)))))

#-swank (main)


(defparameter *safe-aref-error* most-positive-fixnum)
(defun safe-aref (array discript)
  (if (not (minusp discript))
      (aref array discript)
      *safe-aref-error*))
(let* ((n (read))
       (m (read))
       (arr (make-array n :initial-element 0 :element-type 'fixnum))
       (dparr (make-array (1+ n) :initial-element most-positive-fixnum :element-type 'fixnum)))
  (setf *safe-aref-error* 10000000)
  (loop :for k :from 0 :upto (1- n) :do(setf (aref arr k) (read)))
  (setf (aref dparr 0) 0)
  (loop :for k :from 1 :upto (1- n)
        :do (setf (aref dparr k)
                  (loop :for j :from 1 :upto m
                        :minimize (+ (safe-aref dparr (- k j))
                                     (abs (- (safe-aref arr (- k j)) (aref arr k)))))))
  (princ (aref dparr (1- n))))


(defparameter *safe-aref-error* most-positive-fixnum)
(defun safe-aref (array discript)
  (if (not (minusp discript))
      (aref array discript)
      *safe-aref-error*))
(let* ((n (read))
       (m (read))
       (arr (make-array n :initial-element 0 :element-type 'fixnum))
       (dparr (make-array (1+ n) :initial-element most-positive-fixnum :element-type 'fixnum)))
  (setf *safe-aref-error* 10000000)
  (loop :for k :from 0 :upto (1- n) :do(setf (aref arr k) (read)))
  (setf (aref dparr 0) 0)
  (loop :for k :from 1 :upto (1- n)
        :do (setf (aref dparr k)
                  (loop :for j :from 1 :upto m
                        :minimize (+ (aref dparr k)
                                     (abs (- (safe-aref arr (- k j)) (aref arr k)))))))
  (princ (aref dparr (1- n))))


(defparameter *safe-aref-error* most-positive-fixnum)
(defun safe-aref (array discript)
  (if (not (minusp discript))
      (aref array discript)
      *safe-aref-error*))
(let* ((n (read))
       (m (read))
       (arr (make-array n :initial-element 0 :element-type 'fixnum))
       (dparr (make-array (1+ n) :initial-element most-positive-fixnum :element-type 'fixnum)))
  (setf *safe-aref-error* 10000000)
  (loop :for k :from 0 :upto (1- n) :do(setf (aref arr k) (read)))
  (setf (aref dparr 0) 0)
  (loop :for k :from 1 :upto (1- n)
        :do (setf (aref dparr k)
                  (loop :for j :from 1 :upto m
                        :minimize (+ (safe-aref dparr (- k j))
                                     (abs (- (aref arr (- k j)) (aref arr k)))))))
  (princ (aref dparr (1- n))))


(unless (member :child-sbcl *features*)
  (quit
   :unix-status
   (process-exit-code
    (run-program *runtime-pathname*
                 `("--control-stack-size" "128MB"
                   "--noinform" "--disable-ldb" "--lose-on-corruption" "--end-runtime-options"
                   "--eval" "(push :child-sbcl *features*)"
                   "--script" ,(namestring *load-pathname*))
                 :output t :error t :input t))))

(defvar *n* (read))
(defvar *k* (read))
(defvar *hi* (make-array *n*))

;dp[i]→i番目にたどり着くための最小コスト
(defvar *dp* (make-array *n* :initial-element nil))

(dotimes (i *n*)
         (setf (aref *hi* i) (read)))

(defun cost (now delta)
   (abs (- (aref *hi* now) (aref *hi* (- now delta)))))

;dpテーブル初期化
(setf (aref *dp* 0) 0)

(defun cand (i delta)
  (if (or (< *k* delta) (< (- i delta) 0))
    nil
    (cons (+ (ans (- i delta)) (cost i delta)) (cand i (+ delta 1)))))

(defun ans (i)
  (cond
    ((aref *dp* i) (aref *dp* i))
    (t
       (setf (aref *dp* i) (apply #'min (cand i 1))))))

(print (ans (1- *n*)))


(defvar *n* (read))
(defvar *k* (read))
(defvar *hi* (make-array *n*))

;dp[i]→i番目にたどり着くための最小コスト
(defvar *dp* (make-array *n* :initial-element nil))

(dotimes (i *n*)
         (setf (aref *hi* i) (read)))

(defun cost (now delta)
   (abs (- (aref *hi* now) (aref *hi* (- now delta)))))

;dpテーブル初期化
(setf (aref *dp* 0) 0)

(defun cand (i delta)
  (if (or (< *k* delta) (< (- i delta) 0))
    nil
    (cons (+ (ans (- i delta)) (cost i delta)) (cand i (+ delta 1)))))

(defun ans (i)
  (cond
    ((aref *dp* i) (aref *dp* i))
    (t
       (setf (aref *dp* i) (apply #'min (cand i 1))))))

(print (ans (1- *n*)))


;; -*- coding: utf-8 -*-
(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0)))
  #+swank (ql:quickload '(:cl-debug-print :fiveam)))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)
#-swank (disable-debugger) ; for CS Academy

;; BEGIN_INSERTED_CONTENTS
(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  (declare #.OPT
           #-swank (sb-kernel:ansi-stream in))
  (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* ((result (loop (let ((byte (%read-byte)))
                           (when (<= 48 byte)
                             (return (- byte 48)))))))
      (declare ((integer 0 #.most-positive-fixnum) result))
      (loop
        (let* ((byte (%read-byte)))
          (if (<= 48 byte)
              (setq result (+ (- byte 48) (* 10 (the (integer 0 #.(floor most-positive-fixnum 10)) result))))
              (return result)))))))

(declaim (inline println))
(defun println (obj &optional (stream *standard-output*))
  (let ((*read-default-float-format* 'double-float))
    (prog1 (princ obj stream) (terpri stream))))

;; Body

(define-modify-macro minf (new-value)
  (lambda (x y) (min x y)))

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (k (read))
         (hs (make-array 100000 :element-type '(unsigned-byte 32)))
         (dp (make-array 100000 :element-type '(unsigned-byte 32) :initial-element #xffffffff)))
    (declare ((unsigned-byte 31) n k))
    (setf (aref dp 0) 0)
    (dotimes (i n)
      (setf (aref hs i) (read-fixnum)))
    (dotimes (i n)
      (let ((h (aref hs i))
            (d (aref dp i)))
        (loop for dest from (+ i 1) to (min (+ i k) (- n 1))
              do (minf (aref dp dest) (+ d (abs (- h (aref hs dest))))))))
    (println (aref dp (- n 1)))))

#-swank (main)


;; -*- coding: utf-8 -*-
(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0)))
  #+swank (ql:quickload '(:cl-debug-print :fiveam))
  #-swank (set-dispatch-macro-character #\# #\> (lambda (s c p) (declare (ignore c p)) (read s nil (values) t))))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)
#-swank (disable-debugger) ; for CS Academy

;; BEGIN_INSERTED_CONTENTS
(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  (declare #.OPT
           #-swank (sb-kernel:ansi-stream in))
  (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* ((result (loop (let ((byte (%read-byte)))
                           (cond ((<= 48 byte 57)
                                  (return (- byte 48)))
                                 ((zerop byte) ; #\Nul
                                  (error "Read EOF or #\Nul.")))))))
      (declare ((integer 0 #.most-positive-fixnum) result))
      (loop
        (let* ((byte (%read-byte)))
          (if (<= 48 byte 57)
              (setq result (+ (- byte 48) (* 10 (the (integer 0 #.(floor most-positive-fixnum 10)) result))))
              (return 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)))

(declaim (inline println))
(defun println (obj &optional (stream *standard-output*))
  (let ((*read-default-float-format* 'double-float))
    (prog1 (princ obj stream) (terpri stream))))

(defconstant +mod+ 1000000007)

;; Body

(define-modify-macro minf (new-value)
  (lambda (x y) (min x y)))

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (k (read))
         (hs (make-array n :element-type '(unsigned-byte 32)))
         (dp (make-array n :element-type '(unsigned-byte 32) :initial-element #xffffffff)))
    (declare ((unsigned-byte 31) n k))
    (setf (aref dp 0) 0)
    (dotimes (i n)
      (setf (aref hs i) (read-fixnum)))
    (dotimes (i n)
      (loop for dest from (+ i 1) to (min (+ i k) (- n 1))
            do (minf (aref dp dest)
                     (+ (aref dp i) (abs (- (aref hs i) (aref hs dest)))))))
    (println (aref dp (- n 1)))))

#-swank (main)


;; -*- coding: utf-8 -*-
(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0)))
  #+swank (ql:quickload '(:cl-debug-print :fiveam))
  #-swank (set-dispatch-macro-character #\# #\> (lambda (s c p) (declare (ignore c p)) (read s nil (values) t))))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)
#-swank (disable-debugger) ; for CS Academy

;; BEGIN_INSERTED_CONTENTS
(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  (declare #.OPT
           #-swank (sb-kernel:ansi-stream in))
  (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
                                  (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) (* 10 (the (integer 0 #.(floor most-positive-fixnum 10)) result))))
              (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)

(declaim (inline println))
(defun println (obj &optional (stream *standard-output*))
  (let ((*read-default-float-format* 'double-float))
    (prog1 (princ obj stream) (terpri stream))))

(defconstant +mod+ 1000000007)

;; Body

(define-modify-macro minf (new-value)
  (lambda (x y) (min x y)))

(defun main ()
  (declare #.OPT)
  (let* ((n (read))
         (k (read))
         (hs (make-array n :element-type 'uint32))
         (dp (make-array n :element-type 'uint32 :initial-element #xffffffff)))
    (declare (uint31 n k))
    (setf (aref dp 0) 0)
    (dotimes (i n)
      (setf (aref hs i) (read-fixnum)))
    (dotimes (i n)
      (loop for dest from (+ i 1) to (min (+ i k) (- n 1))
            do (minf (aref dp dest)
                     (+ (aref dp i) (abs (- (aref hs i) (aref hs dest)))))))
    (println (aref dp (- n 1)))))

#-swank (main)


;; #!/usr/bin/env sbcl --script

(define-alien-routine getchar_unlocked char)

(declaim (optimize (speed 3) (safety 0) (debug 0))
         (inline getchar_unlocked))

(deftype int32 () '(signed-byte 32))

(declaim (ftype (function () int32) my-read-int))
(defun my-read-int ()
  (let ((is-negative nil)
        (result 0))
    (declare (type int32 result)
             (type boolean is-negative))
    (loop for cur-char = (getchar_unlocked) then (getchar_unlocked)
       while (and (/= cur-char 45)
                  (not (<= 48 cur-char 57)))
       finally (if (<= 48 cur-char 57)
                   (setf result (- cur-char 48))
                   (setf is-negative t)))
    (loop for cur-char = (getchar_unlocked) then (getchar_unlocked)
       while (<= 48 cur-char 57)
       do (setf result (+ (ash result 3) (ash result 1) (- cur-char 48))))
    (when is-negative (setf result (- result)))
    result))

(defun main ()
  (let* ((n (my-read-int))
         (k (my-read-int))
         (a (make-array n :element-type 'int32))
         (dp (make-array n :element-type 'int32 :initial-element (+ (expt 10 9) 100))))
    (dotimes (i n)
      (setf (aref a i) (my-read-int)))
    (setf (aref dp 0) 0)
    (loop for i of-type int32 from 0 below n do
      (loop with from-val of-type int32 = (aref dp i)
         for j of-type int32 from (1+ i) to (+ i k)
         while (< j n)
         for nval of-type int32 = (+ from-val (abs (- (aref a i) (aref a j))))
         if (< nval (aref dp j))
         do
           (setf (aref dp j) nval)))
    (format t "~a~%" (aref dp (- n 1)))))

(main)


;; #!/usr/bin/env sbcl --script

(define-alien-routine getchar_unlocked char)

(declaim (optimize (speed 3) (safety 0) (debug 0))
         (inline getchar_unlocked my-read-int))

(deftype int32 () '(signed-byte 32))

(declaim (ftype (function () int32) my-read-int))
(defun my-read-int ()
  (let ((is-negative nil)
        (result 0))
    (declare (type int32 result))
    (loop for cur-char = (getchar_unlocked) then (getchar_unlocked)
       while (and (/= cur-char (char-code #\-))
                  (/= 0)
                  (or (< cur-char 48)
                      (> cur-char 57)))
       finally (if (<= 48 cur-char 57)
                   (setf result (- cur-char 48))
                   (setf is-negative t)))
    (loop for cur-char = (getchar_unlocked) then (getchar_unlocked)
       while (<= 48 cur-char 57)
       do (setf result (+ (* result 10) (- cur-char 48))))
    (when is-negative (setf result (- result)))
    result))

(defun main ()
  (let* ((n (my-read-int))
         (k (my-read-int))
         (a (make-array n :element-type 'int32))
         (dp (make-array n :element-type 'int32 :initial-element (+ (expt 10 9) 100))))
    (dotimes (i n)
      (setf (aref a i) (my-read-int)))
    (setf (aref dp 0) 0)
    (loop for i of-type int32 from 0 below n do
      (loop with from-val of-type int32 = (aref dp i)
         for j of-type int32 from (1+ i) to (+ i k)
         while (< j n)
         for nval of-type int32 = (+ from-val (abs (- (aref a i) (aref a j))))
         if (< nval (aref dp j))
         do
           (setf (aref dp j) nval)))
    (format t "~a~%" (aref dp (- n 1)))))

(main)


;; #!/usr/bin/env sbcl --script

(define-alien-routine getchar_unlocked char)

(declaim (optimize (speed 3) (safety 0) (debug 0)))
(declaim (inline getchar_unlocked))

(deftype uint8 () '(unsigned-byte 8))
(deftype int32 () '(signed-byte 32))

;; (declaim (type fixnum *ptr* *len*)
;;          (type (vector uint8 4096) *buf*))

;; (defvar *buf* (make-array 4096 :element-type 'uint8 :initial-element 0))
;; (defvar *ptr* (the fixnum 0))
;; (defvar *len* (the fixnum -1))
;;
;; (defun refresh-buf ()
;;   (setf *len* (the fixnum (read-sequence *buf* *standard-input*)))
;;   (setf *ptr* (the fixnum 0)))
;;
;; (declaim (ftype (function () uint8) my-read-char))
;; (defun my-read-char ()
;;   (when (>= *ptr* *len*) (refresh-buf))
;;   (if (>= *ptr* *len*)
;;       0
;;       (progn (incf *ptr*)
;;              (aref *buf* (1- *ptr*))))

(declaim (ftype (function () int32) my-read-int))
(defun my-read-int ()
  (let ((is-negative nil)
        (result 0))
    (declare (type int32 result))
    (loop for cur-char = (getchar_unlocked) then (getchar_unlocked)
       while (and (/= cur-char (char-code #\-))
                  (/= 0)
                  (or (< cur-char 48)
                      (> cur-char 57)))
       finally (if (<= 48 cur-char 57)
                   (setf result (- cur-char 48))
                   (setf is-negative t)))
    (loop for cur-char = (getchar_unlocked) then (getchar_unlocked)
       while (<= 48 cur-char 57)
       do (setf result (+ (* result 10) (- cur-char 48))))
    (when is-negative (setf result (- result)))
    result))

(defun main ()
  (let* ((n (my-read-int))
         (k (my-read-int))
         (a (make-array n :element-type 'int32))
         (dp (make-array n :element-type 'int32 :initial-element (+ (expt 10 9) 100))))
    (dotimes (i n)
      (setf (aref a i) (my-read-int)))
    (setf (aref dp 0) 0)
    (loop for i of-type int32 from 0 below n do
      (loop with from-val of-type int32 = (aref dp i)
         for j of-type int32 from (1+ i) to (+ i k)
         while (< j n)
         for nval of-type int32 = (+ from-val (abs (- (aref a i) (aref a j))))
         if (< nval (aref dp j))
         do
           (setf (aref dp j) nval)))
    (format t "~a~%" (aref dp (- n 1)))))

(main)

;; (defun main2 ()
;;   (let* ((n (the int32 (read)))
;;          (k (the int32 (read)))
;;          (a (make-array n :element-type 'int32))
;;          (dp (make-array n :element-type 'int32 :initial-element (+ (expt 10 9) 100))))
;;     (let ((stream (make-string-input-stream (read-line))))
;;       (loop for i from 0
;;          for j = (read stream nil) until (null j)
;;          do (setf (aref a i) (the int32 j)))
;;     (setf (aref dp 0) 0)
;;     (loop for i of-type int32 from 0 below n do
;;       (loop with from-val of-type int32 = (aref dp i)
;;          for j of-type int32 from (1+ i) to (+ i k)
;;          while (< j n)
;;          do
;;            (let ((nval (the int32 (+ from-val (abs (- (aref a i) (aref a j)))))))
;;              (setf (aref dp j) (min (aref dp j) nval)))
;;     (format t "~a~%" (aref dp (- n 1)))))


;; #!/usr/bin/env sbcl --script

(define-alien-routine getchar_unlocked char)

(declaim (optimize (speed 3) (safety 0) (debug 0)))

(deftype uint8 () '(unsigned-byte 8))
(deftype int32 () '(signed-byte 32))

;; (declaim (type fixnum *ptr* *len*)
;;          (type (vector uint8 4096) *buf*))

;; (defvar *buf* (make-array 4096 :element-type 'uint8 :initial-element 0))
;; (defvar *ptr* (the fixnum 0))
;; (defvar *len* (the fixnum -1))
;;
;; (defun refresh-buf ()
;;   (setf *len* (the fixnum (read-sequence *buf* *standard-input*)))
;;   (setf *ptr* (the fixnum 0)))
;;
;; (declaim (ftype (function () uint8) my-read-char))
;; (defun my-read-char ()
;;   (when (>= *ptr* *len*) (refresh-buf))
;;   (if (>= *ptr* *len*)
;;       0
;;       (progn (incf *ptr*)
;;              (aref *buf* (1- *ptr*))))

(declaim (ftype (function () int32) my-read-int))
(defun my-read-int ()
  (let ((is-negative nil)
        (result 0))
    (declare (type int32 result))
    (loop for cur-char = (getchar_unlocked) then (getchar_unlocked)
       while (and (/= cur-char (char-code #\-))
                  (/= 0)
                  (or (< cur-char 48)
                      (> cur-char 57)))
       finally (if (<= 48 cur-char 57)
                   (setf result (- cur-char 48))
                   (setf is-negative t)))
    (loop for cur-char = (getchar_unlocked) then (getchar_unlocked)
       while (<= 48 cur-char 57)
       do (setf result (+ (* result 10) (- cur-char 48))))
    (when is-negative (setf result (- result)))
    result))

(defun main ()
  (let* ((n (my-read-int))
         (k (my-read-int))
         (a (make-array n :element-type 'int32))
         (dp (make-array n :element-type 'int32 :initial-element (+ (expt 10 9) 100))))
    (dotimes (i n)
      (setf (aref a i) (my-read-int)))
    (setf (aref dp 0) 0)
    (loop for i of-type int32 from 0 below n do
      (loop with from-val of-type int32 = (aref dp i)
         for j of-type int32 from (1+ i) to (+ i k)
         while (< j n)
         for nval of-type int32 = (+ from-val (abs (- (aref a i) (aref a j))))
         if (< nval (aref dp j))
         do
           (setf (aref dp j) nval)))
    (format t "~a~%" (aref dp (- n 1)))))

(main)

;; (defun main2 ()
;;   (let* ((n (the int32 (read)))
;;          (k (the int32 (read)))
;;          (a (make-array n :element-type 'int32))
;;          (dp (make-array n :element-type 'int32 :initial-element (+ (expt 10 9) 100))))
;;     (let ((stream (make-string-input-stream (read-line))))
;;       (loop for i from 0
;;          for j = (read stream nil) until (null j)
;;          do (setf (aref a i) (the int32 j)))
;;     (setf (aref dp 0) 0)
;;     (loop for i of-type int32 from 0 below n do
;;       (loop with from-val of-type int32 = (aref dp i)
;;          for j of-type int32 from (1+ i) to (+ i k)
;;          while (< j n)
;;          do
;;            (let ((nval (the int32 (+ from-val (abs (- (aref a i) (aref a j)))))))
;;              (setf (aref dp j) (min (aref dp j) nval)))
;;     (format t "~a~%" (aref dp (- n 1)))))