
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        final int N = sc.nextInt(), K = sc.nextInt();   //N_max = 100000, K_max = 100, h_max = 10000
        int[] h = new int[N];
        for(int i=0;i<N;i++){
            h[i] = sc.nextInt();
        }
        int[] minCost = new int[N];
        minCost[0] = 0;
        for(int i=1;i<N;i++){
            int costK = 999999999;
            for(int k=1;k<=K;k++) {
                if(k > i){
                    break;
                }else {
                    costK = Math.min(costK, minCost[i - k] + Math.max(h[i] - h[i - k], h[i - k] - h[i]));
                }
            }
            minCost[i] = costK;
        }
        System.out.println(minCost[N-1]);
    }
} import java.util.Scanner; Scanner java.util public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        final int N = sc.nextInt(), K = sc.nextInt();   //N_max = 100000, K_max = 100, h_max = 10000
        int[] h = new int[N];
        for(int i=0;i<N;i++){
            h[i] = sc.nextInt();
        }
        int[] minCost = new int[N];
        minCost[0] = 0;
        for(int i=1;i<N;i++){
            int costK = 999999999;
            for(int k=1;k<=K;k++) {
                if(k > i){
                    break;
                }else {
                    costK = Math.min(costK, minCost[i - k] + Math.max(h[i] - h[i - k], h[i - k] - h[i]));
                }
            }
            minCost[i] = costK;
        }
        System.out.println(minCost[N-1]);
    }
} Main public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        final int N = sc.nextInt(), K = sc.nextInt();   //N_max = 100000, K_max = 100, h_max = 10000
        int[] h = new int[N];
        for(int i=0;i<N;i++){
            h[i] = sc.nextInt();
        }
        int[] minCost = new int[N];
        minCost[0] = 0;
        for(int i=1;i<N;i++){
            int costK = 999999999;
            for(int k=1;k<=K;k++) {
                if(k > i){
                    break;
                }else {
                    costK = Math.min(costK, minCost[i - k] + Math.max(h[i] - h[i - k], h[i - k] - h[i]));
                }
            }
            minCost[i] = costK;
        }
        System.out.println(minCost[N-1]);
    } main {
        Scanner sc = new Scanner(System.in);
        final int N = sc.nextInt(), K = sc.nextInt();   //N_max = 100000, K_max = 100, h_max = 10000
        int[] h = new int[N];
        for(int i=0;i<N;i++){
            h[i] = sc.nextInt();
        }
        int[] minCost = new int[N];
        minCost[0] = 0;
        for(int i=1;i<N;i++){
            int costK = 999999999;
            for(int k=1;k<=K;k++) {
                if(k > i){
                    break;
                }else {
                    costK = Math.min(costK, minCost[i - k] + Math.max(h[i] - h[i - k], h[i - k] - h[i]));
                }
            }
            minCost[i] = costK;
        }
        System.out.println(minCost[N-1]);
    } Scanner sc = new Scanner(System.in); sc new Scanner(System.in) final int N = sc.nextInt() N sc.nextInt() sc.nextInt sc K = sc.nextInt(); K sc.nextInt() sc.nextInt sc int[] h = new int[N]; h new int[N] N for(int i=0;i<N;i++){
            h[i] = sc.nextInt();
        } int i=0; int i=0; i 0 i<N i N i++ i++ i {
            h[i] = sc.nextInt();
        } {
            h[i] = sc.nextInt();
        } h[i] = sc.nextInt() h[i] h i sc.nextInt() sc.nextInt sc int[] minCost = new int[N]; minCost new int[N] N minCost[0] = 0 minCost[0] minCost 0 0 for(int i=1;i<N;i++){
            int costK = 999999999;
            for(int k=1;k<=K;k++) {
                if(k > i){
                    break;
                }else {
                    costK = Math.min(costK, minCost[i - k] + Math.max(h[i] - h[i - k], h[i - k] - h[i]));
                }
            }
            minCost[i] = costK;
        } int i=1; int i=1; i 1 i<N i N i++ i++ i {
            int costK = 999999999;
            for(int k=1;k<=K;k++) {
                if(k > i){
                    break;
                }else {
                    costK = Math.min(costK, minCost[i - k] + Math.max(h[i] - h[i - k], h[i - k] - h[i]));
                }
            }
            minCost[i] = costK;
        } {
            int costK = 999999999;
            for(int k=1;k<=K;k++) {
                if(k > i){
                    break;
                }else {
                    costK = Math.min(costK, minCost[i - k] + Math.max(h[i] - h[i - k], h[i - k] - h[i]));
                }
            }
            minCost[i] = costK;
        } int costK = 999999999; costK 999999999 for(int k=1;k<=K;k++) {
                if(k > i){
                    break;
                }else {
                    costK = Math.min(costK, minCost[i - k] + Math.max(h[i] - h[i - k], h[i - k] - h[i]));
                }
            } int k=1; int k=1; k 1 k<=K k K k++ k++ k {
                if(k > i){
                    break;
                }else {
                    costK = Math.min(costK, minCost[i - k] + Math.max(h[i] - h[i - k], h[i - k] - h[i]));
                }
            } {
                if(k > i){
                    break;
                }else {
                    costK = Math.min(costK, minCost[i - k] + Math.max(h[i] - h[i - k], h[i - k] - h[i]));
                }
            } if(k > i){
                    break;
                }else {
                    costK = Math.min(costK, minCost[i - k] + Math.max(h[i] - h[i - k], h[i - k] - h[i]));
                } k > i k i {
                    break;
                } break; {
                    costK = Math.min(costK, minCost[i - k] + Math.max(h[i] - h[i - k], h[i - k] - h[i]));
                } costK = Math.min(costK, minCost[i - k] + Math.max(h[i] - h[i - k], h[i - k] - h[i])) costK Math.min(costK, minCost[i - k] + Math.max(h[i] - h[i - k], h[i - k] - h[i])) Math.min Math costK minCost[i - k] + Math.max(h[i] - h[i - k], h[i - k] - h[i]) minCost[i - k] minCost i - k i k Math.max(h[i] - h[i - k], h[i - k] - h[i]) Math.max Math h[i] - h[i - k] h[i] h i h[i - k] h i - k i k h[i - k] - h[i] h[i - k] h i - k i k h[i] h i minCost[i] = costK minCost[i] minCost i costK System.out.println(minCost[N-1]) System.out.println System.out System System.out minCost[N-1] minCost N-1 N 1 String[] args args public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        final int N = sc.nextInt(), K = sc.nextInt();   //N_max = 100000, K_max = 100, h_max = 10000
        int[] h = new int[N];
        for(int i=0;i<N;i++){
            h[i] = sc.nextInt();
        }
        int[] minCost = new int[N];
        minCost[0] = 0;
        for(int i=1;i<N;i++){
            int costK = 999999999;
            for(int k=1;k<=K;k++) {
                if(k > i){
                    break;
                }else {
                    costK = Math.min(costK, minCost[i - k] + Math.max(h[i] - h[i - k], h[i - k] - h[i]));
                }
            }
            minCost[i] = costK;
        }
        System.out.println(minCost[N-1]);
    }
} public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        final int N = sc.nextInt(), K = sc.nextInt();   //N_max = 100000, K_max = 100, h_max = 10000
        int[] h = new int[N];
        for(int i=0;i<N;i++){
            h[i] = sc.nextInt();
        }
        int[] minCost = new int[N];
        minCost[0] = 0;
        for(int i=1;i<N;i++){
            int costK = 999999999;
            for(int k=1;k<=K;k++) {
                if(k > i){
                    break;
                }else {
                    costK = Math.min(costK, minCost[i - k] + Math.max(h[i] - h[i - k], h[i - k] - h[i]));
                }
            }
            minCost[i] = costK;
        }
        System.out.println(minCost[N-1]);
    }
} Main

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        final int N = sc.nextInt(), K = sc.nextInt();   //N_max = 100000, K_max = 100, h_max = 10000
        int[] h = new int[N];
        for(int i=0;i<N;i++){
            h[i] = sc.nextInt();
        }
        int[] minCost = new int[N];
        minCost[0] = 0;
        for(int i=1;i<N;i++){
            int costK = 999999999;
            for(int k=1;k<=K;k++) {
                if(k > i){
                    break;
                }else {
                    costK = Math.min(costK, minCost[i - k] + Math.max(h[i] - h[i - k], h[i - k] - h[i]));
                }
            }
            minCost[i] = costK;
        }
        System.out.println(minCost[N-1]);
    }
}
