Buggy python submission (verdict: Time Limit Exceeded):
# /usr/bin/python
# -*- coding: utf-8 -*-
import sys

n,k = map(int, sys.stdin.readline().rstrip().split())
hn = list(map(int, sys.stdin.readline().rstrip().split()))
dp = [-1] * n

dp[0] = 0
for i in range(1,n):
  for j in range(1,min(k+1,i+1)):
    ret = dp[i-j]+abs(hn[i-j]-hn[i])
    if (dp[i] > ret) or (dp[i] < 0):
      dp[i] = ret
print(dp[n-1])



Fixed submission:
# -*- coding: utf-8 -*-
import math
import sys
import numpy as np

n,k = map(int, sys.stdin.readline().rstrip().split())
h = np.array(list(map(int, sys.stdin.readline().rstrip().split())))

dp = np.zeros(n, dtype=np.int)

for i in range(1,n):
  if i < k:
    dp[i] = np.min(dp[:i] + np.abs(h[i]-h[:i]))
  else:
    dp[i] = np.min(dp[i-k:i] + np.abs(h[i]-h[i-k:i]))

print(dp[n-1])

Buggy python submission (verdict: Time Limit Exceeded):
# /usr/bin/python
# -*- coding: utf-8 -*-
import sys

n,k = map(int, sys.stdin.readline().rstrip().split())
hn = list(map(int, sys.stdin.readline().rstrip().split()))
dp = [-1] * n

dp[0] = 0
for i in range(1,n):
  dp[i] = sys.maxsize
  for j in range(1,min(k+1,i+1)):
    ret = dp[i-j]+abs(hn[i-j]-hn[i])
    if dp[i] > ret:
      dp[i] = ret
print(dp[n-1])



Fixed submission:
# -*- coding: utf-8 -*-
import math
import sys
import numpy as np

n,k = map(int, sys.stdin.readline().rstrip().split())
h = np.array(list(map(int, sys.stdin.readline().rstrip().split())))

dp = np.zeros(n, dtype=np.int)

for i in range(1,n):
  if i < k:
    dp[i] = np.min(dp[:i] + np.abs(h[i]-h[:i]))
  else:
    dp[i] = np.min(dp[i-k:i] + np.abs(h[i]-h[i-k:i]))

print(dp[n-1])

Buggy python submission (verdict: Time Limit Exceeded):
# /usr/bin/python
# -*- coding: utf-8 -*-
import sys
import numpy as np

n,k = map(int, sys.stdin.readline().rstrip().split())
hn = np.array(list(map(int, sys.stdin.readline().rstrip().split())))
dp = np.array([-1] * n)

dp[0] = 0
for i in range(1,n):
  ret = sys.maxsize
  for j in range(1,min(k+1,i+1)):
    ret = min(ret, dp[i-j]+abs(hn[i-j]-hn[i]))
  dp[i] = ret
print(dp[n-1])



Fixed submission:
# -*- coding: utf-8 -*-
import math
import sys
import numpy as np

n,k = map(int, sys.stdin.readline().rstrip().split())
h = np.array(list(map(int, sys.stdin.readline().rstrip().split())))

dp = np.zeros(n, dtype=np.int)

for i in range(1,n):
  if i < k:
    dp[i] = np.min(dp[:i] + np.abs(h[i]-h[:i]))
  else:
    dp[i] = np.min(dp[i-k:i] + np.abs(h[i]-h[i-k:i]))

print(dp[n-1])

Buggy python submission (verdict: Time Limit Exceeded):
# /usr/bin/python
# -*- coding: utf-8 -*-
import sys


n,k = map(int, input().split())
hn = list(map(int, input().split()))
dp = [-1] * n

dp[0] = 0
for i in range(1,n):
  ret = sys.maxsize
  for j in range(1,min(k+1,i+1)):
    ret = min(ret, dp[i-j]+abs(hn[i-j]-hn[i]))
  dp[i] = ret
print(dp[n-1])



Fixed submission:
# -*- coding: utf-8 -*-
import math
import sys
import numpy as np

n,k = map(int, sys.stdin.readline().rstrip().split())
h = np.array(list(map(int, sys.stdin.readline().rstrip().split())))

dp = np.zeros(n, dtype=np.int)

for i in range(1,n):
  if i < k:
    dp[i] = np.min(dp[:i] + np.abs(h[i]-h[:i]))
  else:
    dp[i] = np.min(dp[i-k:i] + np.abs(h[i]-h[i-k:i]))

print(dp[n-1])

Buggy python submission (verdict: Time Limit Exceeded):
# /usr/bin/python
# -*- coding: utf-8 -*-
import sys


n,k = map(int, sys.stdin.readline().rstrip().split())
hn = list(map(int, sys.stdin.readline().rstrip().split()))
dp = [-1] * n

dp[0] = 0
for i in range(1,n):
  ret = sys.maxsize
  for j in range(1,min(k+1,i+1)):
    ret = min(ret, dp[i-j]+abs(hn[i-j]-hn[i]))
  dp[i] = ret
print(dp[n-1])



Fixed submission:
# -*- coding: utf-8 -*-
import math
import sys
import numpy as np

n,k = map(int, sys.stdin.readline().rstrip().split())
h = np.array(list(map(int, sys.stdin.readline().rstrip().split())))

dp = np.zeros(n, dtype=np.int)

for i in range(1,n):
  if i < k:
    dp[i] = np.min(dp[:i] + np.abs(h[i]-h[:i]))
  else:
    dp[i] = np.min(dp[i-k:i] + np.abs(h[i]-h[i-k:i]))

print(dp[n-1])

Buggy python submission (verdict: Time Limit Exceeded):
# /usr/bin/python
# -*- coding: utf-8 -*-
import sys


n,k = map(int, sys.stdin.readline().rstrip().split())
hn = list(map(int, sys.stdin.readline().rstrip().split())) + [0]*k
dp = [-1] * (n+k)

dp[0] = 0
for i in range(n):
  for j in range(1,k+1):
    if dp[i+j] < 0:
      dp[i+j] = dp[i] + abs(hn[i+j]-hn[i])
    else:
      dp[i+j] = min(dp[i+j], dp[i] + abs(hn[i+j]-hn[i]))

print(dp[n-1])

Fixed submission:
# -*- coding: utf-8 -*-
import math
import sys
import numpy as np

n,k = map(int, sys.stdin.readline().rstrip().split())
h = np.array(list(map(int, sys.stdin.readline().rstrip().split())))

dp = np.zeros(n, dtype=np.int)

for i in range(1,n):
  if i < k:
    dp[i] = np.min(dp[:i] + np.abs(h[i]-h[:i]))
  else:
    dp[i] = np.min(dp[i-k:i] + np.abs(h[i]-h[i-k:i]))

print(dp[n-1])

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


n,k = map(int, sys.stdin.readline().rstrip().split())
h = [0] + list(map(int, sys.stdin.readline().rstrip().split()))

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

print(dp[n])


Fixed submission:
# -*- coding: utf-8 -*-
import math
import sys
import numpy as np

n,k = map(int, sys.stdin.readline().rstrip().split())
h = np.array(list(map(int, sys.stdin.readline().rstrip().split())))

dp = np.zeros(n, dtype=np.int)

for i in range(1,n):
  if i < k:
    dp[i] = np.min(dp[:i] + np.abs(h[i]-h[:i]))
  else:
    dp[i] = np.min(dp[i-k:i] + np.abs(h[i]-h[i-k:i]))

print(dp[n-1])

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


n,k = map(int, sys.stdin.readline().rstrip().split())
h = [0] + list(map(int, sys.stdin.readline().rstrip().split()))

dp = [sys.maxsize] * (n+k+1)
dp[0], dp[1] = [0, 0]
for i in range(1,n+1):
  for j in range(1,k+1):
    if i+j < len(h):
      dp[i+j] = min(dp[i+j], dp[i] + abs(h[i] - h[i+j]))

print(dp[n])

Fixed submission:
# -*- coding: utf-8 -*-
import math
import sys
import numpy as np

n,k = map(int, sys.stdin.readline().rstrip().split())
h = np.array(list(map(int, sys.stdin.readline().rstrip().split())))

dp = np.zeros(n, dtype=np.int)

for i in range(1,n):
  if i < k:
    dp[i] = np.min(dp[:i] + np.abs(h[i]-h[:i]))
  else:
    dp[i] = np.min(dp[i-k:i] + np.abs(h[i]-h[i-k:i]))

print(dp[n-1])