Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n, k = map(int, input().split())
    h = tuple(map(int, input().split()))
    inf = float('inf')
    #dp = [inf] * n
    #dp[0] = 0
    dp = [0] * n

    for i1 in range(1, n):
        #print(i1, dp)
        start = max(0, i1 - k)
        dp[i1] = dp[start] + abs(h[start] - h[i1])
        for i2 in range(start, i1):
            dp[i1] = min(dp[i1], dp[i2] + abs(h[i1] - h[i2]))
    print(dp[n - 1])

if __name__ == '__main__':
    main()

Fixed submission:
n,k  = map(int, input().split())
h = list(map(int, input().split()))

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

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n, k = map(int, input().split())
    h = tuple(map(int, input().split()))
    inf = float('inf')
    dp = [inf] * n
    dp[0] = 0
    for i1 in range(1, n):
        #print(i1, dp)
        start = max(0, i1 - k)
        for i2 in range(start, i1 + 1):
            dp[i1] = min(dp[i1], dp[i2] + abs(h[i1] - h[i2]))
    print(dp[n - 1])

if __name__ == '__main__':
    main()

Fixed submission:
n,k  = map(int, input().split())
h = list(map(int, input().split()))

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

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n, k = map(int, input().split())
    h = tuple(map(int, input().split()))
    inf = float('inf')
    dp = [inf] * n
    dp[0] = 0
    for i1 in range(1, n):
        #print(i1, dp)
        start = max(0, i1 - k)
        for i2 in range(start, i1):
            dp[i1] = min(dp[i1], dp[i2] + abs(h[i1] - h[i2]))
    print(dp[n - 1])

if __name__ == '__main__':
    main()


Fixed submission:
n,k  = map(int, input().split())
h = list(map(int, input().split()))

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

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    n, k = map(int, input().split())
    h = tuple(map(int, input().split()))
    inf = float('inf')
    dp = [inf] * n
    r = min(n, k + 1)
    for i1 in range(1, r):
        dp[i1] = abs(h[i1] - h[0])

    for i1 in range(2, n):
        start = max(1, i1 - k)
        cost = dp[start] + abs(h[i1] - h[start])
        for i2 in range(start + 1, i1):
            cost = min(cost, dp[i2] + abs(h[i2] - h[i1]))
        dp[i1] = min(cost, dp[i1])
    print(dp[n - 1])

if __name__ == '__main__':
    main()

Fixed submission:
n,k  = map(int, input().split())
h = list(map(int, input().split()))

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

Buggy python submission (verdict: Time Limit Exceeded):

def main():
    n, k = map(int, input().split())
    h = tuple(map(int, input().split()))
    dp = [-1] * n
    r = min(n, k + 1)
    for i1 in range(1, r):
        dp[i1] = abs(h[i1] - h[0])

    for i1 in range(2, n):
        start = max(1, i1 - k)
        cost = dp[start] + abs(h[i1] - h[start])
        for i2 in range(start + 1, i1):
            cost = min(cost, dp[i2] + abs(h[i2] - h[i1]))
        if dp[i1] < 0:
            dp[i1] = cost
        else:
            dp[i1] = min(cost, dp[i1])
    print(dp[n - 1])

if __name__ == '__main__':
    main()


Fixed submission:
n,k  = map(int, input().split())
h = list(map(int, input().split()))

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

Buggy python submission (verdict: Time Limit Exceeded):
n,k  = map(int, input().split())
h = list(map(int, input().split()))

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

Fixed submission:
n,k  = map(int, input().split())
h = list(map(int, input().split()))

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

Buggy python submission (verdict: Time Limit Exceeded):
n,k  = map(int, input().split())
h = list(map(int, input().split()))

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

Fixed submission:
n,k  = map(int, input().split())
h = list(map(int, input().split()))

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