Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

import numpy as np

EDPC = "https://atcoder.jp/contests/dp/"
TDPC = "https://atcoder.jp/contests/tdpc/tasks"

################ DANGER ################
test = ""
#test = \
"""
5 3
10 30 40 50 20
ans 30
0 20 30 40 30
"""

"""
3 1
10 20 10
ans 20
"""

"""
2 100
10 10
ans 0
"""

"""
10 4
40 10 20 70 80 10 20 70 80 60
ans 40
0 30 20 30 40 30 20 30 40 40
"""
########################################
test = list(reversed(test.strip().splitlines()))
if test:
    def input2():
        return test.pop()
else:
    def input2():
        return input()
########################################  

n, k = map(int, input2().split())
a = list(map(int, input2().split()))
k = min(n-1, k)

def main(n, k, a):
    aa = np.concatenate([a])
    dp = np.concatenate([np.abs(aa - aa[0])[:k+1], 
                                np.zeros(n-k-1, dtype="int")])
    for i in range(k+1, n):
        dp[i] = min(dp[j] + abs(a[i] - a[j]) for j in range(i-k, i))
        dp[i] = np.min(dp[i-k:i+1] + np.abs(aa[i-k:i+1] - aa[i]))
    
    return dp[-1]


if __name__ == "__main__":
    print(main(n, k, a))


Fixed submission:
# -*- coding: utf-8 -*-

import numpy as np

EDPC = "https://atcoder.jp/contests/dp/"
TDPC = "https://atcoder.jp/contests/tdpc/tasks"

################ DANGER ################
test = ""
#test = \
"""
5 3
10 30 40 50 20
ans 30
0 20 30 40 30
"""

"""
3 1
10 20 10
ans 20
"""

"""
2 100
10 10
ans 0
"""

"""
10 4
40 10 20 70 80 10 20 70 80 60
ans 40
0 30 20 30 40 30 20 30 40 40
"""
########################################
test = list(reversed(test.strip().splitlines()))
if test:
    def input2():
        return test.pop()
else:
    def input2():
        return input()
########################################  

n, k = map(int, input2().split())
a = list(map(int, input2().split()))
k = min(n-1, k)

def main(n, k, a):
    aa = np.concatenate([a])
    dp = np.concatenate([np.abs(aa - aa[0])[:k+1], 
                                np.zeros(n-k-1, dtype="int")])
    for i in range(k+1, n):
        dp[i] = np.min(dp[i-k:i] + np.abs(aa[i-k:i] - aa[i]))
    
    return dp[-1]


if __name__ == "__main__":
    print(main(n, k, a))

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

################ DANGER ################
test = ""
#test = \
"""
5 3
10 30 40 50 20
ans 30
0 20 30 40 30
"""

"""
3 1
10 20 10
ans 20
"""

"""
2 100
10 10
ans 0
"""

"""
10 4
40 10 20 70 80 10 20 70 80 60
ans 40
0 30 20 30 40 30 20 30 40 40
"""
########################################
test = list(reversed(test.strip().splitlines()))
if test:
    def input2():
        return test.pop()
else:
    def input2():
        return input()
########################################  

# B_Frog2
n, k = map(int, input2().split())
a = list(map(int, input2().split()))

k = min(n-1, k)

dp = [0] * n
for i in range(1, k+1):
    dp[i] = abs(a[i] - a[0])
    
for i in range(k+1, n):
    dp[i] = min(dp[j] + abs(a[i] - a[j]) for j in range(i-k, i))

print(dp[-1])



Fixed submission:
# -*- coding: utf-8 -*-

import numpy as np

EDPC = "https://atcoder.jp/contests/dp/"
TDPC = "https://atcoder.jp/contests/tdpc/tasks"

################ DANGER ################
test = ""
#test = \
"""
5 3
10 30 40 50 20
ans 30
0 20 30 40 30
"""

"""
3 1
10 20 10
ans 20
"""

"""
2 100
10 10
ans 0
"""

"""
10 4
40 10 20 70 80 10 20 70 80 60
ans 40
0 30 20 30 40 30 20 30 40 40
"""
########################################
test = list(reversed(test.strip().splitlines()))
if test:
    def input2():
        return test.pop()
else:
    def input2():
        return input()
########################################  

n, k = map(int, input2().split())
a = list(map(int, input2().split()))
k = min(n-1, k)

def main(n, k, a):
    aa = np.concatenate([a])
    dp = np.concatenate([np.abs(aa - aa[0])[:k+1], 
                                np.zeros(n-k-1, dtype="int")])
    for i in range(k+1, n):
        dp[i] = np.min(dp[i-k:i] + np.abs(aa[i-k:i] - aa[i]))
    
    return dp[-1]


if __name__ == "__main__":
    print(main(n, k, a))

Buggy python submission (verdict: Time Limit Exceeded):
# -*- coding: utf-8 -*-

EDPC = "https://atcoder.jp/contests/dp/"
TDPC = "https://atcoder.jp/contests/tdpc/tasks"


################ DANGER ################
test = ""
#test = \

########################################
test = list(reversed(test.strip().splitlines()))
if test:
    def input2():
        return test.pop()
else:
    def input2():
        return input()
########################################  


def maximized(a):
    """
    1 -2 3 -4 5
    全探索より速い 
    """
    """
    a = list(map(int, input2().split()))
    print(A_Frog1(a))
    """
    dp = [0] * (len(a) + 1)
    for i in range(len(a)):
        dp[i+1] = max(dp[i], dp[i] + a[i])
    return dp[-1]


def A_Frog1(a):
    """
    4
    10 30 40 20
    ans 30
    """
    """
    2
    10 10
    ans 0
    """
    """
    6
    30 10 60 10 60 50
    ans 40
    """
    """
    input2()
    a = list(map(int, input2().split()))
    print(A_Frog1(a))
    """
    n, h = len(a), a
    dp = [0] * n
    dp[0] = 0
    dp[1] = abs(h[1] - h[0])
    for i in range(1, n - 1):
        dp[i+1] = min(
                dp[i-1] + abs(h[i+1] - h[i-1]), 
                dp[i] + abs(h[i+1] - h[i]))
    return dp[-1]
    
    
def B_Frog2(K, a):
    """
    5 3
    10 30 40 50 20
    ans 30
    """
    """
    3 1
    10 20 10
    ans 20
    """
    """
    2 100
    10 10
    ans 0
    """
    """
    10 4
    40 10 20 70 80 10 20 70 80 60
    ans 40
    """
    """
    n, k = map(int, input2().split())
    a = list(map(int, input2().split()))
    print(B_Frog2(k, a))
    """
    n, h = len(a), a
    dp = [0] + [float('inf')] * (n-1)
    for j in range(1, n):
        for i in range(max(0, j - K), j):
            dp[j] = min(dp[j], dp[i] + abs(h[j] - h[i]))
    return dp[-1]
    

if __name__ == "__main__":
    ################## IO ##################
    n, k = map(int, input2().split())
    a = list(map(int, input2().split()))
    print(B_Frog2(k, a))
    
    
    
    

Fixed submission:
# -*- coding: utf-8 -*-

import numpy as np

EDPC = "https://atcoder.jp/contests/dp/"
TDPC = "https://atcoder.jp/contests/tdpc/tasks"

################ DANGER ################
test = ""
#test = \
"""
5 3
10 30 40 50 20
ans 30
0 20 30 40 30
"""

"""
3 1
10 20 10
ans 20
"""

"""
2 100
10 10
ans 0
"""

"""
10 4
40 10 20 70 80 10 20 70 80 60
ans 40
0 30 20 30 40 30 20 30 40 40
"""
########################################
test = list(reversed(test.strip().splitlines()))
if test:
    def input2():
        return test.pop()
else:
    def input2():
        return input()
########################################  

n, k = map(int, input2().split())
a = list(map(int, input2().split()))
k = min(n-1, k)

def main(n, k, a):
    aa = np.concatenate([a])
    dp = np.concatenate([np.abs(aa - aa[0])[:k+1], 
                                np.zeros(n-k-1, dtype="int")])
    for i in range(k+1, n):
        dp[i] = np.min(dp[i-k:i] + np.abs(aa[i-k:i] - aa[i]))
    
    return dp[-1]


if __name__ == "__main__":
    print(main(n, k, a))