Buggy python submission (verdict: Runtime Error):
def solve(N, K, h):
	INF = 10000 * N * K + 10000
	cost = [INF for x in range(N)]
	cost[0] = 0
	for i in range(N):
		for j in range(min(N, i + 1), min(N, i + K + 1)):
			cost_ij = abs(h[i] - h[j]) + cost[i]
			cost[j] = min(cost[j], cost_ij)
	return cost[N - 1]


def main():
	N , K = list(map(int, input().split()))
	h = list(map(int, input().split()))
	assert len(h) == N
	assert N >= 2
	res = solve(N, K, h)
	print(res)


if __name__ == '__main__':
	main()

Fixed submission:
def solve(h):
	best_p = abs(h[0] - h[1])
	best_pp = 0
	n = 2
	while n < len(h):
		cost1 = best_p + abs(h[n] - h[n-1])
		cost2 = best_pp + abs(h[n] - h[n-2])
		best = min(cost1, cost2)
		best_pp, best_p = best_p, best
		n = n + 1
	return best_p


def main():
	N = int(input())
	heights = list(map(int, input().split()))
	assert len(heights) == N
	assert N >= 2
	res = solve(heights)
	print(res)


if __name__ == '__main__':
	main()

Buggy python submission (verdict: Runtime Error):
def solve(N, K, h):
	INF = 10000 * N * K + 10000
	cost = [INF for _ in range(N)]
	cost[0] = 0
	for i in range(N):
		for j in range(min(N, i + 1), min(N, i + K + 1)):
			cost_ij = abs(h[i] - h[j]) + cost[i]
			cost[j] = min(cost[j], cost_ij)
	return cost[N - 1]


def main():
	N , K = list(map(int, input().split()))
	h = list(map(int, input().split()))
	assert len(h) == N
	assert N >= 2
	res = solve(N, K, h)
	print(res)


if __name__ == '__main__':
	main()

Fixed submission:
def solve(h):
	best_p = abs(h[0] - h[1])
	best_pp = 0
	n = 2
	while n < len(h):
		cost1 = best_p + abs(h[n] - h[n-1])
		cost2 = best_pp + abs(h[n] - h[n-2])
		best = min(cost1, cost2)
		best_pp, best_p = best_p, best
		n = n + 1
	return best_p


def main():
	N = int(input())
	heights = list(map(int, input().split()))
	assert len(heights) == N
	assert N >= 2
	res = solve(heights)
	print(res)


if __name__ == '__main__':
	main()

Buggy python submission (verdict: Runtime Error):
def frog1(h):
    dp = [float('inf')]*3
    dp[0] = 0
    for i in range(1,len(h)):
        dp[i%3] = min([dp[(i-j)%3] + abs(h[i]-h[i-j]) for j in range(1,3)])
    return dp[(len(h)-1)%3]
    
n = input()
vals = map(int, input().split())

print(frog1(vals))

Fixed submission:
def frog1(h):
    dp = [float('inf')]*3
    dp[0] = 0
    for i in range(1,len(h)):
        dp[i%3] = min([dp[(i-j)%3] + abs(h[i]-h[i-j]) for j in range(1,3)])
    return dp[(len(h)-1)%3]
    
n = input()
vals = list(map(int, input().split()))

print(frog1(vals))

Buggy python submission (verdict: Runtime Error):
def frog1(h):
    dp = [float('inf')]*3
    dp[0] = 0
    for i in range(1,len(h)):
        dp[i%3] = min([dp[(i-j)%3] + abs(h[i]-h[i-j]) for j in range(1,3)])
    return dp[(len(h)-1)%3]
    
n = input()
vals = map(int, input.split())

print(frog1(vals))

Fixed submission:
def frog1(h):
    dp = [float('inf')]*3
    dp[0] = 0
    for i in range(1,len(h)):
        dp[i%3] = min([dp[(i-j)%3] + abs(h[i]-h[i-j]) for j in range(1,3)])
    return dp[(len(h)-1)%3]
    
n = input()
vals = list(map(int, input().split()))

print(frog1(vals))

Buggy python submission (verdict: Runtime Error):
def frog1(h):
    dp = [float('inf')]*3
    dp[0] = 0
    for i in range(1,len(h)):
        dp[i%3] = min([dp[(i-j)%3] + abs(h[i]-h[i-j]) for j in range(1,3)])
    return dp[(len(h)-1)%3]
    
n = raw_input()
vals = map(int, raw_input.split())

print(frog1(vals))

Fixed submission:
def frog1(h):
    dp = [float('inf')]*3
    dp[0] = 0
    for i in range(1,len(h)):
        dp[i%3] = min([dp[(i-j)%3] + abs(h[i]-h[i-j]) for j in range(1,3)])
    return dp[(len(h)-1)%3]
    
n = input()
vals = list(map(int, input().split()))

print(frog1(vals))

Buggy python submission (verdict: Runtime Error):
def frog1(h):
    dp = [0]*3
    for i in range(len(h)):
        dp[i%3] = min([dp[(i-j)%3] + abs(h[i]-h[i-j]) for j in range(1,3)])
    return dp[len(h)%3]

n = int(raw_input())
h = map(int, raw_input().split()
        
print(frog1(h))

Fixed submission:
def frog1(h):
    dp = [float('inf')]*3
    dp[0] = 0
    for i in range(1,len(h)):
        dp[i%3] = min([dp[(i-j)%3] + abs(h[i]-h[i-j]) for j in range(1,3)])
    return dp[(len(h)-1)%3]
    
n = input()
vals = list(map(int, input().split()))

print(frog1(vals))

Buggy python submission (verdict: Runtime Error):
n,k= map(int,input().split())
h1=list(map(int,input().split()))
if k>=n-1:
    print(abs(h1[0]-h1[n-1]))
else:
    dp=[-1]*n
    dp[n-1]=0
    for y in range(n-k-1,n-1):
       dp[y]=abs(h1[n-1]-h1[y])    
    for u in range(k+1,n):
     i=(n-1-u)
     dp[i]=min([dp[i+h]+abs(h1[i+h]-h1[i]) for h in range(1,k+1)])
    print(dp[0])  

Fixed submission:
n= int(input())
h1=list(map(int,input().split()))
dp=[-1 for i in range(n)]
def fun(i):
    if dp[i]!=-1:
        return dp[i]
    if i==n-1:
        best=0
    elif i==n-2:
        best=abs(h1[n-1]-h1[n-2])
    else:
        best=min(fun(i+1)+abs(h1[i+1]-h1[i]),fun(i+2)+abs(h1[i+2]-h1[i]))
    dp[i]=best
    return best
for k in range(n):
    fun(n-1-k)

print(fun(0))

Buggy python submission (verdict: Runtime Error):
def fun(i):
    if dp[i]!=-1:
        return dp[i]
    if i==n-1:
        best=0
    elif i==n-2:
        best=abs(h1[n-1]-h1[n-2])
    else:
        best=min(fun(i+1)+abs(h1[i+1]-h1[i]),fun(i+2)+abs(h1[i+2]-h1[i]))
    dp[i]=best
    return best
for k in range(n):
    fun(n-1-k)

print(fun(0)) 

Fixed submission:
n= int(input())
h1=list(map(int,input().split()))
dp=[-1 for i in range(n)]
def fun(i):
    if dp[i]!=-1:
        return dp[i]
    if i==n-1:
        best=0
    elif i==n-2:
        best=abs(h1[n-1]-h1[n-2])
    else:
        best=min(fun(i+1)+abs(h1[i+1]-h1[i]),fun(i+2)+abs(h1[i+2]-h1[i]))
    dp[i]=best
    return best
for k in range(n):
    fun(n-1-k)

print(fun(0))

Buggy python submission (verdict: Runtime Error):
n= int(input())
h1=list(map(int,input().split()))
dp=[-1 for i in range(n)]

def fun(i):
    if dp[i]!=-1:
        return dp[i]
    if i==n-1:
        best=0
    elif i==n-2:
        best=abs(h1[n-1]-h1[n-2])
    else:
        best=min(fun(i+1)+abs(h1[i+1]-h1[i]),fun(i+2)+abs(h1[i+2]-h1[i]))
    dp[i]=best
    return best

print(fun(0))  

Fixed submission:
n= int(input())
h1=list(map(int,input().split()))
dp=[-1 for i in range(n)]
def fun(i):
    if dp[i]!=-1:
        return dp[i]
    if i==n-1:
        best=0
    elif i==n-2:
        best=abs(h1[n-1]-h1[n-2])
    else:
        best=min(fun(i+1)+abs(h1[i+1]-h1[i]),fun(i+2)+abs(h1[i+2]-h1[i]))
    dp[i]=best
    return best
for k in range(n):
    fun(n-1-k)

print(fun(0))

Buggy python submission (verdict: Runtime Error):
n= int(input())
h1=list(map(int,input().split()))
def fun(i):
    if i==n-1:
        best=0
    elif i==n-2:
        best=abs(h1[n-1]-h1[n-2])
    else:
        best=min(fun(i+1)+abs(h1[i+1]-h1[i]),fun(i+2)+abs(h1[i+2]-h1[i]))
    return best
print(fun(0))  
        

Fixed submission:
n= int(input())
h1=list(map(int,input().split()))
dp=[-1 for i in range(n)]
def fun(i):
    if dp[i]!=-1:
        return dp[i]
    if i==n-1:
        best=0
    elif i==n-2:
        best=abs(h1[n-1]-h1[n-2])
    else:
        best=min(fun(i+1)+abs(h1[i+1]-h1[i]),fun(i+2)+abs(h1[i+2]-h1[i]))
    dp[i]=best
    return best
for k in range(n):
    fun(n-1-k)

print(fun(0))

Buggy python submission (verdict: Runtime Error):
import sys
input=sys.stdin.readline
sys.setrecursionlimit(10**8)
n,k=map(int,input().split())
z=list(map(int,input().split()))
dp=[0 for i in range(len(z))]
vi=[0 for i in range(len(z))]
def dps(i,dp,vi):
    if(i<=0):
        return 0;
    if(vi[i]==1):
        return dp[i];
    
    vi[i]=1
    maxa=98778979878
    for j in range(1,min(i+1,k+1)):
        maxa=min((dps(i-j,dp,vi)+abs(z[i]-z[i-j])),maxa)
    dp[i]=maxa
    return dp[i];
print(dps(len(z)-1,dp,vi))



Fixed submission:
n=int(input())
z=list(map(int,input().split()))
ans=[0,abs(z[1]-z[0])]

for i in range(2,len(z)):
    ans.append(min(abs(z[i]-z[i-1])+ans[-1],abs(z[i]-z[i-2])+ans[-2]))
print(ans[-1])

Buggy python submission (verdict: Runtime Error):

n,k=map(int,input().split())
z=list(map(int,input().split()))
r=sum(z)
cost=[(2*r for i in range(len(z)))]
cost[0]=0
for i in range(1,len(z)):
    for t in range(1,min(i+1,k+1)):
        cost[i]=min(cost[i-t]+abs(z[i]-z[i-t]),cost[i])
        
print(cost[-1])  
   
    
    


Fixed submission:
n=int(input())
z=list(map(int,input().split()))
ans=[0,abs(z[1]-z[0])]

for i in range(2,len(z)):
    ans.append(min(abs(z[i]-z[i-1])+ans[-1],abs(z[i]-z[i-2])+ans[-2]))
print(ans[-1])