Buggy python submission (verdict: Time Limit Exceeded):
import sys

def main():
	N, K = map(int, sys.stdin.readline().rstrip().split())
	h_i = list(map(int, sys.stdin.readline().rstrip().split()))
	dp = [0] * N
	for i in range(1, N):
		dp[i] = min([abs(h_i[i] - h_i[i-j]) + dp[i-j] for j in range(1, min(i, K)+1)])
		
	return dp[-1]


if __name__ == "__main__":
	print(main())

Fixed submission:
import sys
import numpy as np

def LI():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def main():
	N, K = LI()
	h_i = np.array(LI())
	dp = np.full(N, 10**10, dtype=int)
	dp[0] = 0
	for i in range(1, N):
		dp[i:i+K] = np.fmin(dp[i:i+K], dp[i-1] + np.abs(h_i[i:i+K] - h_i[i-1]))
		
	return dp[-1]


if __name__ == "__main__":
	print(main())

Buggy python submission (verdict: Time Limit Exceeded):
import sys

def IL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def main():
	N, K = IL()
	h_i = IL()
	dp = [0] * N
	for i in range(1, N):
		dp[i] = min([abs(h_i[i] - h_i[i-j]) + dp[i-j] for j in range(1, min(i, K)+1)])
		
	return dp[-1]


if __name__ == "__main__":
	print(main())

Fixed submission:
import sys
import numpy as np

def LI():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def main():
	N, K = LI()
	h_i = np.array(LI())
	dp = np.full(N, 10**10, dtype=int)
	dp[0] = 0
	for i in range(1, N):
		dp[i:i+K] = np.fmin(dp[i:i+K], dp[i-1] + np.abs(h_i[i:i+K] - h_i[i-1]))
		
	return dp[-1]


if __name__ == "__main__":
	print(main())

Buggy python submission (verdict: Time Limit Exceeded):
import sys

def IL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def main():
	N, K = IL()
	h_i = IL()
	dp = [0] * N
	dp[0] = 0
	for i in range(1, N):
		dp[i] = min([abs(h_i[i] - h_i[i-j]) + dp[i-j] for j in range(1, min(i, K)+1)])
		
	print(dp[-1])


if __name__ == "__main__":
	main()

Fixed submission:
import sys
import numpy as np

def LI():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def main():
	N, K = LI()
	h_i = np.array(LI())
	dp = np.full(N, 10**10, dtype=int)
	dp[0] = 0
	for i in range(1, N):
		dp[i:i+K] = np.fmin(dp[i:i+K], dp[i-1] + np.abs(h_i[i:i+K] - h_i[i-1]))
		
	return dp[-1]


if __name__ == "__main__":
	print(main())

Buggy python submission (verdict: Time Limit Exceeded):
import sys

def IL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def main():
	N, K = IL()
	h_i = IL()
	dp = [sys.maxsize for _ in range(N)]
	dp[0] = 0
	for i in range(1, N):
		dp[i] = min([abs(h_i[i] - h_i[i-j]) + dp[i-j] for j in range(1, min(i, K)+1)])
		# for j in range(1, K+1):
		# 	if i+j < N:
		# 		dp[i+j] = min(dp[i+j], abs(h_i[i] - h_i[i+j]) + dp[i])
		
	print(int(dp[-1]))


if __name__ == "__main__":
	main()

Fixed submission:
import sys
import numpy as np

def LI():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def main():
	N, K = LI()
	h_i = np.array(LI())
	dp = np.full(N, 10**10, dtype=int)
	dp[0] = 0
	for i in range(1, N):
		dp[i:i+K] = np.fmin(dp[i:i+K], dp[i-1] + np.abs(h_i[i:i+K] - h_i[i-1]))
		
	return dp[-1]


if __name__ == "__main__":
	print(main())

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np

def IL():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def main():
	N, K = IL()
	h_i = IL()
	dp = np.full((N,), np.inf)
	dp[0] = 0
	for i in range(N):
		for j in range(1, K+1):
			if i+j < N:
				dp[i+j] = np.min([dp[i+j], abs(h_i[i] - h_i[i+j]) + dp[i]])
		
	print(int(dp[-1]))


if __name__ == "__main__":
	main()

Fixed submission:
import sys
import numpy as np

def LI():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def main():
	N, K = LI()
	h_i = np.array(LI())
	dp = np.full(N, 10**10, dtype=int)
	dp[0] = 0
	for i in range(1, N):
		dp[i:i+K] = np.fmin(dp[i:i+K], dp[i-1] + np.abs(h_i[i:i+K] - h_i[i-1]))
		
	return dp[-1]


if __name__ == "__main__":
	print(main())

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import numpy as np

def IL():
	return list(map(int, sys.stdin.readline().rstrip().split()))


N, K = IL()
h_i = IL()
dp = np.full((N,), np.inf)
dp[0] = 0
for i in range(N):
	for j in range(1, K+1):
		if i+j < N:
			dp[i+j] = np.min([dp[i+j], abs(h_i[i] - h_i[i+j]) + dp[i]])
	
print(int(dp[-1]))

Fixed submission:
import sys
import numpy as np

def LI():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def main():
	N, K = LI()
	h_i = np.array(LI())
	dp = np.full(N, 10**10, dtype=int)
	dp[0] = 0
	for i in range(1, N):
		dp[i:i+K] = np.fmin(dp[i:i+K], dp[i-1] + np.abs(h_i[i:i+K] - h_i[i-1]))
		
	return dp[-1]


if __name__ == "__main__":
	print(main())

Buggy python submission (verdict: Time Limit Exceeded):
import sys
def IL():
	return list(map(int, sys.stdin.readline().rstrip().split()))


N, K = IL()
h_i = IL()
dp = [sys.maxsize for _ in range(N)]
dp[0] = 0
for i in range(N):
	for j in range(1, K+1):
		if i+j < N:
			dp[i+j] = min(dp[i+j], abs(h_i[i] - h_i[i+j]) + dp[i])
	
print(dp[-1])

Fixed submission:
import sys
import numpy as np

def LI():
	return list(map(int, sys.stdin.readline().rstrip().split()))

def main():
	N, K = LI()
	h_i = np.array(LI())
	dp = np.full(N, 10**10, dtype=int)
	dp[0] = 0
	for i in range(1, N):
		dp[i:i+K] = np.fmin(dp[i:i+K], dp[i-1] + np.abs(h_i[i:i+K] - h_i[i-1]))
		
	return dp[-1]


if __name__ == "__main__":
	print(main())