import sys

#import numpy as np

#from collections import defaultdict

#import math

#from collections import deque



input = sys.stdin.readline







def main():





    N ,K= list(map(int,input().split()))

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

    H = [0] + H

    dp= [0] *(N+1)

    dp = [10**5] * (N + 1)

    dp[1] = 0



    for i in range(2, N + 1):

        way =[10**10]*(K+2)

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

            if j<i:

                way[j]=(dp[i-j]+abs(H[i]-H[i-j]))

        dp[i] = min(way)





    print((dp[N]))











if __name__ == "__main__":

    main()
import sys

from collections import deque

from operator import itemgetter





def input():

    return sys.stdin.readline().rstrip()





def main():

    N, K = list(map(int, input().split()))

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

    h.insert(0,0)

    dp = [10 ** 9 + 7] * (N + 1)

    dp[1] = 0

    for i in range(1, N + 1):

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

            if k <= i:

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

    print((dp[-1]))



if __name__ == "__main__":

    main()

import sys

# import numpy as np
# from collections import defaultdict
# import math
# from collections import deque
input = sys.stdin.readline


def main():
    N, K = list(map(int, input().split()))
    H = list(map(int, input().split()))
    H = [0] + H
    dp = [0] * (N + 1)
    dp = [10**5] * (N + 1)
    dp[1] = 0
    for i in range(2, N + 1):
        way = [10**10] * (K + 2)
        for j in range(1, K + 1):
            if j < i:
                way[j] = dp[i - j] + abs(H[i] - H[i - j])
        dp[i] = min(way)
    print((dp[N]))


if __name__ == "__main__":
    main()

import sys
from collections import deque
from operator import itemgetter


def input():
    return sys.stdin.readline().rstrip()


def main():
    N, K = list(map(int, input().split()))
    h = list(map(int, input().split()))
    h.insert(0, 0)
    dp = [10**9 + 7] * (N + 1)
    dp[1] = 0
    for i in range(1, N + 1):
        for k in range(1, K + 1):
            if k <= i:
                dp[i] = min(dp[i], dp[i - k] + abs(h[i - k] - h[i]))
    print((dp[-1]))


if __name__ == "__main__":
    main()

+from collections import deque +from operator import itemgetter -# import numpy as np -# from collections import defaultdict -# import math -# from collections import deque -input = sys.stdin.readline + +def input(): +    return sys.stdin.readline().rstrip() -    H = list(map(int, input().split())) -    H = [0] + H -    dp = [0] * (N + 1) -    dp = [10**5] * (N + 1) +    h = list(map(int, input().split())) +    h.insert(0, 0) +    dp = [10**9 + 7] * (N + 1) -    for i in range(2, N + 1): -        way = [10**10] * (K + 2) -        for j in range(1, K + 1): -            if j < i: -                way[j] = dp[i - j] + abs(H[i] - H[i - j]) -        dp[i] = min(way) -    print((dp[N])) +    for i in range(1, N + 1): +        for k in range(1, K + 1): +            if k <= i: +                dp[i] = min(dp[i], dp[i - k] + abs(h[i - k] - h[i])) +    print((dp[-1]))