import numpy as np

import sys

imput = sys.stdin.readline

def main():

    n, k = list(map(int, input().split()))

    h = list(map(int, input().split()))

    h = np.array(h)

    cost = np.empty(n)

    cost[0] = 0

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

    for i in range(2, n):

        j = min(i, k)

        cost[i] = np.min(cost[i-j:i] + np.abs(h[i-j:i] - h[i]))

    print((int(cost[-1])))

main()

def main():

    n, k = list(map(int, input().split()))

    h = tuple(map(int, input().split()))



    dp = [0]*(n)



    for i in range(1, n):

        temp = 10**10

        for j in range(1, min(i, k)+1):

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

        dp[i] = temp



    print((dp[-1]))



if __name__ == '__main__':

    main()

import numpy as np
import sys

imput = sys.stdin.readline


def main():
    n, k = list(map(int, input().split()))
    h = list(map(int, input().split()))
    h = np.array(h)
    cost = np.empty(n)
    cost[0] = 0
    cost[1] = abs(h[1] - h[0])
    for i in range(2, n):
        j = min(i, k)
        cost[i] = np.min(cost[i - j : i] + np.abs(h[i - j : i] - h[i]))
    print((int(cost[-1])))


main()

def main():
    n, k = list(map(int, input().split()))
    h = tuple(map(int, input().split()))
    dp = [0] * (n)
    for i in range(1, n):
        temp = 10**10
        for j in range(1, min(i, k) + 1):
            temp = min(temp, dp[i - j] + abs(h[i] - h[i - j]))
        dp[i] = temp
    print((dp[-1]))


if __name__ == "__main__":
    main()

-import numpy as np -import sys - -imput = sys.stdin.readline +def main(): +    n, k = list(map(int, input().split())) +    h = tuple(map(int, input().split())) +    dp = [0] * (n) +    for i in range(1, n): +        temp = 10**10 +        for j in range(1, min(i, k) + 1): +            temp = min(temp, dp[i - j] + abs(h[i] - h[i - j])) +        dp[i] = temp +    print((dp[-1])) -def main(): -    n, k = list(map(int, input().split())) -    h = list(map(int, input().split())) -    h = np.array(h) -    cost = np.empty(n) -    cost[0] = 0 -    cost[1] = abs(h[1] - h[0]) -    for i in range(2, n): -        j = min(i, k) -        cost[i] = np.min(cost[i - j : i] + np.abs(h[i - j : i] - h[i])) -    print((int(cost[-1]))) - - -main() +if __name__ == "__main__": +    main()