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

def input():
    return sys.stdin.readline().strip()

def main():
    N,K = map(int,input().split())
    H = list(map(int,input().split()))

    dp = [0] * N
    dp[1] = abs(H[1] - H[0])
    for i in range(2,N):
        dp[i] = min(dp[i-j] + abs(H[i] - H[i-j]) for j in range(1,max(K,i)))
    print(dp[-1])

if __name__ == "__main__":
    main()

Fixed submission:
import sys

def input():
    return sys.stdin.readline().strip()

def main():
    N,K = map(int,input().split())
    h = list(map(int,input().split()))
    dp = [0] * (N + 1)

    dp[0] = 0
    dp[1] = abs(h[1]-h[0])
    for i in range(2,N):
        dp[i] = min(dp[j] + abs(h[i]-h[j]) for j in range(max(0,i-K),i))
    print(dp[N-1])

if __name__ == "__main__":
    main()

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

def input():
    return sys.stdin.readline().strip()

def main():
    N,K = map(int,input().split())
    H = list(map(int,input().split()))

    dp = [0] * N
    dp[1] = abs(H[1] - H[0])
    for i in range(2,N):
        dp[i] = min(dp[i-j] + abs(H[i] - H[i-j]) for j in range(1,max(K,i)))
    print(dp[-1])

if __name__ == "__main__":
    main()

Fixed submission:
import sys

def input():
    return sys.stdin.readline().strip()

def main():
    N,K = map(int,input().split())
    h = list(map(int,input().split()))
    dp = [0] * (N + 1)

    dp[0] = 0
    dp[1] = abs(h[1]-h[0])
    for i in range(2,N):
        dp[i] = min(dp[j] + abs(h[i]-h[j]) for j in range(max(0,i-K),i))
    print(dp[N-1])

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split())
H = list(map(int,input().split()))

dp = [0] * N
dp[1] = abs(H[1] - H[0])
for i in range(2,N):
    dp[i] = min(dp[i-j] + abs(H[i] - H[i-j]) for j in range(1,max(K,i)))
print(dp[-1])

Fixed submission:
import sys

def input():
    return sys.stdin.readline().strip()

def main():
    N,K = map(int,input().split())
    h = list(map(int,input().split()))
    dp = [0] * (N + 1)

    dp[0] = 0
    dp[1] = abs(h[1]-h[0])
    for i in range(2,N):
        dp[i] = min(dp[j] + abs(h[i]-h[j]) for j in range(max(0,i-K),i))
    print(dp[N-1])

if __name__ == "__main__":
    main()

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

def input():
    return sys.stdin.readline().strip()

N,K = map(int,input().split())
h = list(map(int,input().split()))
dp = [0] * (N + 1)

dp[0] = 0
dp[1] = abs(h[1]-h[0])
for i in range(2,N):
    dp[i] = min(dp[j] + abs(h[i]-h[j]) for j in range(max(0,i-K),i))
print(dp[N-1])

Fixed submission:
import sys

def input():
    return sys.stdin.readline().strip()

def main():
    N,K = map(int,input().split())
    h = list(map(int,input().split()))
    dp = [0] * (N + 1)

    dp[0] = 0
    dp[1] = abs(h[1]-h[0])
    for i in range(2,N):
        dp[i] = min(dp[j] + abs(h[i]-h[j]) for j in range(max(0,i-K),i))
    print(dp[N-1])

if __name__ == "__main__":
    main()

Buggy python submission (verdict: Time Limit Exceeded):
N,K = map(int,input().split())
h = list(map(int,input().split()))
dp = [0] * (N + 1)

dp[0] = 0
dp[1] = abs(h[1]-h[0])
for i in range(2,N):
    dp[i] = min(dp[j] + abs(h[i]-h[j]) for j in range(max(0,i-K),i))
print(dp[N-1])

Fixed submission:
import sys

def input():
    return sys.stdin.readline().strip()

def main():
    N,K = map(int,input().split())
    h = list(map(int,input().split()))
    dp = [0] * (N + 1)

    dp[0] = 0
    dp[1] = abs(h[1]-h[0])
    for i in range(2,N):
        dp[i] = min(dp[j] + abs(h[i]-h[j]) for j in range(max(0,i-K),i))
    print(dp[N-1])

if __name__ == "__main__":
    main()