import java.io.*;
import java.util.*;
import static java.lang.System.out;

public class Main {
    static MyReader in = new MyReader();

    public static void main(String[] args) {
        int N = in.i();
        int K = in.i();
        int[] h = new int[N + 1];
        for (int i = 1; i <= N; i++) {
            h[i] = in.i();
        }
        int[] dp = new int[N + 1];
        h[0] = dp[0] = Integer.MAX_VALUE / 2;
        for (int i = 2; i <= N; i++) {
            int min = Integer.MAX_VALUE;
            for (int j = Math.max(1, i - K); j < i; j++) {
                min = Math.min(min, dp[j] + Math.abs(h[i] - h[j]));
            }
            dp[i] = min;
        }
        out.println(dp[N]);
    }
}

class MyReader extends BufferedReader {
    char[] cbuf = new char[1024];
    int head = 0;
    int tail = 0;

    MyReader() {
        super(new InputStreamReader(System.in));
    }

    char next() {
        if (head == tail) {
            try {
                tail = super.read(cbuf, 0, cbuf.length);
            } catch (IOException e) {
                e.printStackTrace();
            }
            head = 0;
        }
        return cbuf[head++];
    }

    void back() {
        head--;
    }

    boolean minus() {
        boolean minus;
        while (true) {
            char c = next();
            if (!isDelimiter(c)) {
                if (!(minus = c == '-')) back();
                return minus;
            }
        }
    }

    void skip() {
        while (isDelimiter(next()));
        back();
    }

    char[] s(int N) {
        char[] cbuf = new char[N];
        read(cbuf, 0, N);
        return cbuf;
    }

    public int read(char[] cbuf, int off, int len) {
        skip();
        int i;
        for (i = 0; i < cbuf.length; i++) {
            char c = next();
            if (isDelimiter(c)) {
                break;
            }
            cbuf[i] = c;
        }
        return i;
    }

    boolean isDelimiter(char c) {
        return c == ' ' || c == '\n' || c == '\r';
    }

    int i() {
        boolean minus = minus();
        int n = 0;
        while (true) {
            int k = next() - '0';
            if (k < 0 || 9 < k) break;
            n = 10 * n + k;
        }
        return minus ? -n : n;
    }

    int[] ii(final int N) {
        int[] a = new int[N];
        for (int j = 0; j < a.length; j++) a[j] = i();
        return a;
    }

    long l() {
        boolean minus = minus();
        long n = 0;
        while (true) {
            int k = next() - '0';
            if (k < 0 || 9 < k) break;
            n = 10 * n + k;
        }
        return minus ? -n : n;
    }
} import java.io.*; io.* java import java.util.*; util.* java import static java.lang.System.out; import static java.lang.System.out; import static java.lang.System.out; public class Main {
    static MyReader in = new MyReader();

    public static void main(String[] args) {
        int N = in.i();
        int K = in.i();
        int[] h = new int[N + 1];
        for (int i = 1; i <= N; i++) {
            h[i] = in.i();
        }
        int[] dp = new int[N + 1];
        h[0] = dp[0] = Integer.MAX_VALUE / 2;
        for (int i = 2; i <= N; i++) {
            int min = Integer.MAX_VALUE;
            for (int j = Math.max(1, i - K); j < i; j++) {
                min = Math.min(min, dp[j] + Math.abs(h[i] - h[j]));
            }
            dp[i] = min;
        }
        out.println(dp[N]);
    }
} Main static MyReader in = new MyReader(); in new MyReader() public static void main(String[] args) {
        int N = in.i();
        int K = in.i();
        int[] h = new int[N + 1];
        for (int i = 1; i <= N; i++) {
            h[i] = in.i();
        }
        int[] dp = new int[N + 1];
        h[0] = dp[0] = Integer.MAX_VALUE / 2;
        for (int i = 2; i <= N; i++) {
            int min = Integer.MAX_VALUE;
            for (int j = Math.max(1, i - K); j < i; j++) {
                min = Math.min(min, dp[j] + Math.abs(h[i] - h[j]));
            }
            dp[i] = min;
        }
        out.println(dp[N]);
    } main {
        int N = in.i();
        int K = in.i();
        int[] h = new int[N + 1];
        for (int i = 1; i <= N; i++) {
            h[i] = in.i();
        }
        int[] dp = new int[N + 1];
        h[0] = dp[0] = Integer.MAX_VALUE / 2;
        for (int i = 2; i <= N; i++) {
            int min = Integer.MAX_VALUE;
            for (int j = Math.max(1, i - K); j < i; j++) {
                min = Math.min(min, dp[j] + Math.abs(h[i] - h[j]));
            }
            dp[i] = min;
        }
        out.println(dp[N]);
    } int N = in.i(); N in.i() in.i in int K = in.i(); K in.i() in.i in int[] h = new int[N + 1]; h new int[N + 1] N + 1 N 1 for (int i = 1; i <= N; i++) {
            h[i] = in.i();
        } int i = 1; int i = 1; i 1 i <= N i N i++ i++ i {
            h[i] = in.i();
        } {
            h[i] = in.i();
        } h[i] = in.i() h[i] h i in.i() in.i in int[] dp = new int[N + 1]; dp new int[N + 1] N + 1 N 1 h[0] = dp[0] = Integer.MAX_VALUE / 2 h[0] h 0 dp[0] = Integer.MAX_VALUE / 2 dp[0] dp 0 Integer.MAX_VALUE / 2 Integer.MAX_VALUE Integer Integer.MAX_VALUE 2 for (int i = 2; i <= N; i++) {
            int min = Integer.MAX_VALUE;
            for (int j = Math.max(1, i - K); j < i; j++) {
                min = Math.min(min, dp[j] + Math.abs(h[i] - h[j]));
            }
            dp[i] = min;
        } int i = 2; int i = 2; i 2 i <= N i N i++ i++ i {
            int min = Integer.MAX_VALUE;
            for (int j = Math.max(1, i - K); j < i; j++) {
                min = Math.min(min, dp[j] + Math.abs(h[i] - h[j]));
            }
            dp[i] = min;
        } {
            int min = Integer.MAX_VALUE;
            for (int j = Math.max(1, i - K); j < i; j++) {
                min = Math.min(min, dp[j] + Math.abs(h[i] - h[j]));
            }
            dp[i] = min;
        } int min = Integer.MAX_VALUE; min Integer.MAX_VALUE Integer Integer.MAX_VALUE for (int j = Math.max(1, i - K); j < i; j++) {
                min = Math.min(min, dp[j] + Math.abs(h[i] - h[j]));
            } int j = Math.max(1, i - K); int j = Math.max(1, i - K); j Math.max(1, i - K) Math.max Math 1 i - K i K j < i j i j++ j++ j {
                min = Math.min(min, dp[j] + Math.abs(h[i] - h[j]));
            } {
                min = Math.min(min, dp[j] + Math.abs(h[i] - h[j]));
            } min = Math.min(min, dp[j] + Math.abs(h[i] - h[j])) min Math.min(min, dp[j] + Math.abs(h[i] - h[j])) Math.min Math min dp[j] + Math.abs(h[i] - h[j]) dp[j] dp j Math.abs(h[i] - h[j]) Math.abs Math h[i] - h[j] h[i] h i h[j] h j dp[i] = min dp[i] dp i min out.println(dp[N]) out.println out dp[N] dp N String[] args args class MyReader extends BufferedReader {
    char[] cbuf = new char[1024];
    int head = 0;
    int tail = 0;

    MyReader() {
        super(new InputStreamReader(System.in));
    }

    char next() {
        if (head == tail) {
            try {
                tail = super.read(cbuf, 0, cbuf.length);
            } catch (IOException e) {
                e.printStackTrace();
            }
            head = 0;
        }
        return cbuf[head++];
    }

    void back() {
        head--;
    }

    boolean minus() {
        boolean minus;
        while (true) {
            char c = next();
            if (!isDelimiter(c)) {
                if (!(minus = c == '-')) back();
                return minus;
            }
        }
    }

    void skip() {
        while (isDelimiter(next()));
        back();
    }

    char[] s(int N) {
        char[] cbuf = new char[N];
        read(cbuf, 0, N);
        return cbuf;
    }

    public int read(char[] cbuf, int off, int len) {
        skip();
        int i;
        for (i = 0; i < cbuf.length; i++) {
            char c = next();
            if (isDelimiter(c)) {
                break;
            }
            cbuf[i] = c;
        }
        return i;
    }

    boolean isDelimiter(char c) {
        return c == ' ' || c == '\n' || c == '\r';
    }

    int i() {
        boolean minus = minus();
        int n = 0;
        while (true) {
            int k = next() - '0';
            if (k < 0 || 9 < k) break;
            n = 10 * n + k;
        }
        return minus ? -n : n;
    }

    int[] ii(final int N) {
        int[] a = new int[N];
        for (int j = 0; j < a.length; j++) a[j] = i();
        return a;
    }

    long l() {
        boolean minus = minus();
        long n = 0;
        while (true) {
            int k = next() - '0';
            if (k < 0 || 9 < k) break;
            n = 10 * n + k;
        }
        return minus ? -n : n;
    }
} MyReader char[] cbuf = new char[1024]; cbuf new char[1024] 1024 int head = 0; head 0 int tail = 0; tail 0 MyReader() {
        super(new InputStreamReader(System.in));
    } MyReader {
        super(new InputStreamReader(System.in));
    } super(new InputStreamReader(System.in)) super new InputStreamReader(System.in) char next() {
        if (head == tail) {
            try {
                tail = super.read(cbuf, 0, cbuf.length);
            } catch (IOException e) {
                e.printStackTrace();
            }
            head = 0;
        }
        return cbuf[head++];
    } next {
        if (head == tail) {
            try {
                tail = super.read(cbuf, 0, cbuf.length);
            } catch (IOException e) {
                e.printStackTrace();
            }
            head = 0;
        }
        return cbuf[head++];
    } if (head == tail) {
            try {
                tail = super.read(cbuf, 0, cbuf.length);
            } catch (IOException e) {
                e.printStackTrace();
            }
            head = 0;
        } head == tail head tail {
            try {
                tail = super.read(cbuf, 0, cbuf.length);
            } catch (IOException e) {
                e.printStackTrace();
            }
            head = 0;
        } try {
                tail = super.read(cbuf, 0, cbuf.length);
            } catch (IOException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } IOException e {
                e.printStackTrace();
            } e.printStackTrace() e.printStackTrace e {
                tail = super.read(cbuf, 0, cbuf.length);
            } tail = super.read(cbuf, 0, cbuf.length) tail super.read(cbuf, 0, cbuf.length) super.read super cbuf 0 cbuf.length cbuf cbuf.length head = 0 head 0 return cbuf[head++]; cbuf[head++] cbuf head++ head void back() {
        head--;
    } back {
        head--;
    } head-- head boolean minus() {
        boolean minus;
        while (true) {
            char c = next();
            if (!isDelimiter(c)) {
                if (!(minus = c == '-')) back();
                return minus;
            }
        }
    } minus {
        boolean minus;
        while (true) {
            char c = next();
            if (!isDelimiter(c)) {
                if (!(minus = c == '-')) back();
                return minus;
            }
        }
    } boolean minus; minus while (true) {
            char c = next();
            if (!isDelimiter(c)) {
                if (!(minus = c == '-')) back();
                return minus;
            }
        } true {
            char c = next();
            if (!isDelimiter(c)) {
                if (!(minus = c == '-')) back();
                return minus;
            }
        } char c = next(); c next() next if (!isDelimiter(c)) {
                if (!(minus = c == '-')) back();
                return minus;
            } !isDelimiter(c) isDelimiter(c) isDelimiter c {
                if (!(minus = c == '-')) back();
                return minus;
            } if (!(minus = c == '-')) back(); !(minus = c == '-') (minus = c == '-') minus c == '-' c '-' back() back return minus; minus void skip() {
        while (isDelimiter(next()));
        back();
    } skip {
        while (isDelimiter(next()));
        back();
    } while (isDelimiter(next())); isDelimiter(next()) isDelimiter next() next back() back char[] s(int N) {
        char[] cbuf = new char[N];
        read(cbuf, 0, N);
        return cbuf;
    } s {
        char[] cbuf = new char[N];
        read(cbuf, 0, N);
        return cbuf;
    } char[] cbuf = new char[N]; cbuf new char[N] N read(cbuf, 0, N) read cbuf 0 N return cbuf; cbuf int N N public int read(char[] cbuf, int off, int len) {
        skip();
        int i;
        for (i = 0; i < cbuf.length; i++) {
            char c = next();
            if (isDelimiter(c)) {
                break;
            }
            cbuf[i] = c;
        }
        return i;
    } read {
        skip();
        int i;
        for (i = 0; i < cbuf.length; i++) {
            char c = next();
            if (isDelimiter(c)) {
                break;
            }
            cbuf[i] = c;
        }
        return i;
    } skip() skip int i; i for (i = 0; i < cbuf.length; i++) {
            char c = next();
            if (isDelimiter(c)) {
                break;
            }
            cbuf[i] = c;
        } i = 0; i = 0 i 0 i < cbuf.length i cbuf.length cbuf cbuf.length i++ i++ i {
            char c = next();
            if (isDelimiter(c)) {
                break;
            }
            cbuf[i] = c;
        } {
            char c = next();
            if (isDelimiter(c)) {
                break;
            }
            cbuf[i] = c;
        } char c = next(); c next() next if (isDelimiter(c)) {
                break;
            } isDelimiter(c) isDelimiter c {
                break;
            } break; cbuf[i] = c cbuf[i] cbuf i c return i; i char[] cbuf cbuf int off off int len len boolean isDelimiter(char c) {
        return c == ' ' || c == '\n' || c == '\r';
    } isDelimiter {
        return c == ' ' || c == '\n' || c == '\r';
    } return c == ' ' || c == '\n' || c == '\r'; c == ' ' || c == '\n' || c == '\r' c == ' ' || c == '\n' || c == '\r' c == ' ' c ' ' c == '\n' c '\n' c == '\r' c '\r' char c c int i() {
        boolean minus = minus();
        int n = 0;
        while (true) {
            int k = next() - '0';
            if (k < 0 || 9 < k) break;
            n = 10 * n + k;
        }
        return minus ? -n : n;
    } i {
        boolean minus = minus();
        int n = 0;
        while (true) {
            int k = next() - '0';
            if (k < 0 || 9 < k) break;
            n = 10 * n + k;
        }
        return minus ? -n : n;
    } boolean minus = minus(); minus minus() minus int n = 0; n 0 while (true) {
            int k = next() - '0';
            if (k < 0 || 9 < k) break;
            n = 10 * n + k;
        } true {
            int k = next() - '0';
            if (k < 0 || 9 < k) break;
            n = 10 * n + k;
        } int k = next() - '0'; k next() - '0' next() next '0' if (k < 0 || 9 < k) break; k < 0 || 9 < k k < 0 k 0 9 < k 9 k break; n = 10 * n + k n 10 * n + k 10 * n 10 n k return minus ? -n : n; minus ? -n : n minus -n n n int[] ii(final int N) {
        int[] a = new int[N];
        for (int j = 0; j < a.length; j++) a[j] = i();
        return a;
    } ii {
        int[] a = new int[N];
        for (int j = 0; j < a.length; j++) a[j] = i();
        return a;
    } int[] a = new int[N]; a new int[N] N for (int j = 0; j < a.length; j++) a[j] = i(); int j = 0; int j = 0; j 0 j < a.length j a.length a a.length j++ j++ j a[j] = i(); a[j] = i() a[j] a j i() i return a; a final int N N long l() {
        boolean minus = minus();
        long n = 0;
        while (true) {
            int k = next() - '0';
            if (k < 0 || 9 < k) break;
            n = 10 * n + k;
        }
        return minus ? -n : n;
    } l {
        boolean minus = minus();
        long n = 0;
        while (true) {
            int k = next() - '0';
            if (k < 0 || 9 < k) break;
            n = 10 * n + k;
        }
        return minus ? -n : n;
    } boolean minus = minus(); minus minus() minus long n = 0; n 0 while (true) {
            int k = next() - '0';
            if (k < 0 || 9 < k) break;
            n = 10 * n + k;
        } true {
            int k = next() - '0';
            if (k < 0 || 9 < k) break;
            n = 10 * n + k;
        } int k = next() - '0'; k next() - '0' next() next '0' if (k < 0 || 9 < k) break; k < 0 || 9 < k k < 0 k 0 9 < k 9 k break; n = 10 * n + k n 10 * n + k 10 * n 10 n k return minus ? -n : n; minus ? -n : n minus -n n n BufferedReader public class Main {
    static MyReader in = new MyReader();

    public static void main(String[] args) {
        int N = in.i();
        int K = in.i();
        int[] h = new int[N + 1];
        for (int i = 1; i <= N; i++) {
            h[i] = in.i();
        }
        int[] dp = new int[N + 1];
        h[0] = dp[0] = Integer.MAX_VALUE / 2;
        for (int i = 2; i <= N; i++) {
            int min = Integer.MAX_VALUE;
            for (int j = Math.max(1, i - K); j < i; j++) {
                min = Math.min(min, dp[j] + Math.abs(h[i] - h[j]));
            }
            dp[i] = min;
        }
        out.println(dp[N]);
    }
} public class Main {
    static MyReader in = new MyReader();

    public static void main(String[] args) {
        int N = in.i();
        int K = in.i();
        int[] h = new int[N + 1];
        for (int i = 1; i <= N; i++) {
            h[i] = in.i();
        }
        int[] dp = new int[N + 1];
        h[0] = dp[0] = Integer.MAX_VALUE / 2;
        for (int i = 2; i <= N; i++) {
            int min = Integer.MAX_VALUE;
            for (int j = Math.max(1, i - K); j < i; j++) {
                min = Math.min(min, dp[j] + Math.abs(h[i] - h[j]));
            }
            dp[i] = min;
        }
        out.println(dp[N]);
    }
} Main
import java.io.*;
import java.util.*;
import static java.lang.System.out;

public class Main {
    static MyReader in = new MyReader();

    public static void main(String[] args) {
        int N = in.i();
        int K = in.i();
        int[] h = new int[N + 1];
        for (int i = 1; i <= N; i++) {
            h[i] = in.i();
        }
        int[] dp = new int[N + 1];
        h[0] = dp[0] = Integer.MAX_VALUE / 2;
        for (int i = 2; i <= N; i++) {
            int min = Integer.MAX_VALUE;
            for (int j = Math.max(1, i - K); j < i; j++) {
                min = Math.min(min, dp[j] + Math.abs(h[i] - h[j]));
            }
            dp[i] = min;
        }
        out.println(dp[N]);
    }
}

class MyReader extends BufferedReader {
    char[] cbuf = new char[1024];
    int head = 0;
    int tail = 0;

    MyReader() {
        super(new InputStreamReader(System.in));
    }

    char next() {
        if (head == tail) {
            try {
                tail = super.read(cbuf, 0, cbuf.length);
            } catch (IOException e) {
                e.printStackTrace();
            }
            head = 0;
        }
        return cbuf[head++];
    }

    void back() {
        head--;
    }

    boolean minus() {
        boolean minus;
        while (true) {
            char c = next();
            if (!isDelimiter(c)) {
                if (!(minus = c == '-')) back();
                return minus;
            }
        }
    }

    void skip() {
        while (isDelimiter(next()));
        back();
    }

    char[] s(int N) {
        char[] cbuf = new char[N];
        read(cbuf, 0, N);
        return cbuf;
    }

    public int read(char[] cbuf, int off, int len) {
        skip();
        int i;
        for (i = 0; i < cbuf.length; i++) {
            char c = next();
            if (isDelimiter(c)) {
                break;
            }
            cbuf[i] = c;
        }
        return i;
    }

    boolean isDelimiter(char c) {
        return c == ' ' || c == '\n' || c == '\r';
    }

    int i() {
        boolean minus = minus();
        int n = 0;
        while (true) {
            int k = next() - '0';
            if (k < 0 || 9 < k) break;
            n = 10 * n + k;
        }
        return minus ? -n : n;
    }

    int[] ii(final int N) {
        int[] a = new int[N];
        for (int j = 0; j < a.length; j++) a[j] = i();
        return a;
    }

    long l() {
        boolean minus = minus();
        long n = 0;
        while (true) {
            int k = next() - '0';
            if (k < 0 || 9 < k) break;
            n = 10 * n + k;
        }
        return minus ? -n : n;
    }
}
