#!/usr/bin/env python3

import sys





def solve(N: int, K: int, h: "List[int]"):

    dp = [float("inf")] * N

    dp[0] = 0

    dp[1] = abs(h[0] - h[1])



    for i in range(2, N):

        for k in range(1, K+1):

            if i - k < 0:

                continue

            dp[i] = min(dp[i], dp[i-k] + abs(h[i] - h[i-k]))



    print((dp[-1]))





# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)

def main():

    def iterate_tokens():

        for line in sys.stdin:

            for word in line.split():

                yield word

    tokens = iterate_tokens()

    N = int(next(tokens))  # type: int

    K = int(next(tokens))  # type: int

    h = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"

    solve(N, K, h)





if __name__ == '__main__':

    main()

#!/usr/bin/env python3

import sys





def solve(N: int, K: int, h: "List[int]"):

    dp = [float("inf")] * N

    dp[0] = 0

    for i in range(1, N):

        for j in range(max(0, i-K), i):

            dp[i] = min(dp[i], dp[j] + abs(h[i] - h[j]))

    print((dp[-1]))





# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)

def main():

    def iterate_tokens():

        for line in sys.stdin:

            for word in line.split():

                yield word

    tokens = iterate_tokens()

    N = int(next(tokens))  # type: int

    K = int(next(tokens))  # type: int

    h = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"

    solve(N, K, h)





if __name__ == '__main__':

    main()

#!/usr/bin/env python3
import sys


def solve(N: int, K: int, h: "List[int]"):
    dp = [float("inf")] * N
    dp[0] = 0
    dp[1] = abs(h[0] - h[1])
    for i in range(2, N):
        for k in range(1, K + 1):
            if i - k < 0:
                continue
            dp[i] = min(dp[i], dp[i - k] + abs(h[i] - h[i - k]))
    print((dp[-1]))


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    h = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, h)


if __name__ == "__main__":
    main()

#!/usr/bin/env python3
import sys


def solve(N: int, K: int, h: "List[int]"):
    dp = [float("inf")] * N
    dp[0] = 0
    for i in range(1, N):
        for j in range(max(0, i - K), i):
            dp[i] = min(dp[i], dp[j] + abs(h[i] - h[j]))
    print((dp[-1]))


# Generated by 1.1.6 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word

    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    K = int(next(tokens))  # type: int
    h = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, K, h)


if __name__ == "__main__":
    main()

-    dp[1] = abs(h[0] - h[1]) -    for i in range(2, N): -        for k in range(1, K + 1): -            if i - k < 0: -                continue -            dp[i] = min(dp[i], dp[i - k] + abs(h[i] - h[i - k])) +    for i in range(1, N): +        for j in range(max(0, i - K), i): +            dp[i] = min(dp[i], dp[j] + abs(h[i] - h[j]))