Buggy python submission (verdict: Time Limit Exceeded):
# edpc b

import itertools
from math import factorial
import sys
import heapq
from collections import Counter
from collections import deque
from fractions import gcd
from functools import reduce

sys.setrecursionlimit(4100000)
INF = 1 << 60
MOD = 10 ** 9 + 7

# ここから書き始める
n, k = map(int, input().split())
h = list(map(int, input().split()))
dp = [INF] * n
dp[0] = 0
for i in range(1, n):
    for j in range(1, k + 1):
        if 0 <= i - j < n:
            dp[i] = min(dp[i], dp[i - j] + abs(h[i] - h[i - j]))
print(dp[n - 1])




Fixed submission:
import itertools
import math
import sys
from collections import Counter
from fractions import gcd
from functools import reduce
import sys
sys.setrecursionlimit(4100000)

INF = 1 << 60

# ここから書き始める
n, k = map(int, input().split())
h = list(map(int, input().split()))
dp = [INF] * n
dp[0] = 0
for i in range(n):
    for j in range(1, k + 1):
        if i + j == n:
            break
        dp[i + j] = min(dp[i + j], dp[i] + abs(h[i] - h[i + j]))
print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
import math
import sys
from collections import Counter
from fractions import gcd
from functools import reduce
import sys
sys.setrecursionlimit(4100000)

INF = 1 << 60

# ここから書き始める
n, k = map(int, input().split())
h = list(map(int, input().split()))
dp = [INF] * n
dp[0] = 0
for i in range(n):
    for j in range(1, k + 1):
        if i + j == n:
            break
        dp[i + j] = min(dp[i + j], dp[i] + abs(h[i] - h[i + j]))
print(dp[-1])


Fixed submission:
import itertools
import math
import sys
from collections import Counter
from fractions import gcd
from functools import reduce
import sys
sys.setrecursionlimit(4100000)

INF = 1 << 60

# ここから書き始める
n, k = map(int, input().split())
h = list(map(int, input().split()))
dp = [INF] * n
dp[0] = 0
for i in range(n):
    for j in range(1, k + 1):
        if i + j == n:
            break
        dp[i + j] = min(dp[i + j], dp[i] + abs(h[i] - h[i + j]))
print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
import math
import sys
from collections import Counter
from fractions import gcd
from functools import reduce
import sys
sys.setrecursionlimit(4100000)

INF = 1 << 60

# ここから書き始める
n, k = map(int, input().split())
h = list(map(int, input().split()))
dp = [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[j] - h[i]))
print(dp[-1])


Fixed submission:
import itertools
import math
import sys
from collections import Counter
from fractions import gcd
from functools import reduce
import sys
sys.setrecursionlimit(4100000)

INF = 1 << 60

# ここから書き始める
n, k = map(int, input().split())
h = list(map(int, input().split()))
dp = [INF] * n
dp[0] = 0
for i in range(n):
    for j in range(1, k + 1):
        if i + j == n:
            break
        dp[i + j] = min(dp[i + j], dp[i] + abs(h[i] - h[i + j]))
print(dp[-1])

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
import math
import sys
from collections import Counter
from fractions import gcd
from functools import reduce
import sys
sys.setrecursionlimit(4100000)

INF = 1 << 60

# ここから書き始める
n, k = map(int, input().split())
h = list(map(int, input().split()))
dp = [INF] * n
dp[0] = 0
for i in range(1, n):
    for j in range(i - k, i):
        if j < 0:
            continue
        dp[i] = min(dp[i], dp[j] + abs(h[j] - h[i]))
print(dp[-1])


Fixed submission:
import itertools
import math
import sys
from collections import Counter
from fractions import gcd
from functools import reduce
import sys
sys.setrecursionlimit(4100000)

INF = 1 << 60

# ここから書き始める
n, k = map(int, input().split())
h = list(map(int, input().split()))
dp = [INF] * n
dp[0] = 0
for i in range(n):
    for j in range(1, k + 1):
        if i + j == n:
            break
        dp[i + j] = min(dp[i + j], dp[i] + abs(h[i] - h[i + j]))
print(dp[-1])