Buggy python submission (verdict: Runtime Error):
N,K=list(map(int,input().split()))
A=list(map(int,input().split()))

INF=float("inf")

dp=[INF]*N

for i in range(0, N-1):
  for k in range(1, K+1):
    if i+k <= N-1:
      dp[i+k]=min(dp[i+k],dp[i]+abs(A[i]-A[i+k]))
print(dp[N-1])


Fixed submission:
# -*- coding:utf-8 -*-

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

    INF = 10**4+10
    dp = [INF]*(N+10)
    dp[0] = 0
    dp[1] = abs(H[1] - H[0])
    for i in range(2, N):
        dp[i] = min(dp[i-1]+abs(H[i]-H[i-1]), dp[i-2]+abs(H[i]-H[i-2]))
    
    print(dp[N-1])


if __name__ == "__main__":
    solve()

Buggy python submission (verdict: Runtime Error):
# -*- coding:utf-8 -*-

def solve():
    """ DP """
    N = int(input())
    H = list(map(int, input().split()))

    INF = 10**4+10
    dp = [INF]*(N+10)
    dp[0] = 0
    dp[1] = abs(H[1] - H[0])
    for i in range(2, N):
        dp[i] = min(dp[i-1]+abs(H[i]-H[i-1]), dp[i-2]+abs(H[i]-H[i-2]))
    
    print(dp[N-1])


def solve2():
    """ メモ化再帰 """
    N = int(input())
    H = list(map(int, input().split()))

    INF = 10**14+10
    dp = [INF] * (N+10)
    dp[0] = 0
    dp[1] = abs(H[0]-H[1])

    def dfs(i):
        """ 足場iの時点の最小値を返す """
        if dp[i] != INF: return dp[i]
                
        ans2 = INF
        ans1 = dfs(i-1) + abs(H[i]-H[i-1])  # i-1の足場から来た場合
        if i-2 >= 0:
            ans2 = dfs(i-2) + abs(H[i]-H[i-2])  # i-2の足場から来た場合

        dp[i] = min(ans1, ans2)

        return dp[i]
    
    print(dfs(N-1))



if __name__ == "__main__":
    solve2()

Fixed submission:
# -*- coding:utf-8 -*-

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

    INF = 10**4+10
    dp = [INF]*(N+10)
    dp[0] = 0
    dp[1] = abs(H[1] - H[0])
    for i in range(2, N):
        dp[i] = min(dp[i-1]+abs(H[i]-H[i-1]), dp[i-2]+abs(H[i]-H[i-2]))
    
    print(dp[N-1])


if __name__ == "__main__":
    solve()

Buggy python submission (verdict: Runtime Error):
# -*- coding:utf-8 -*-

def solve():
    """ DP """
    N = int(input())
    H = list(map(int, input().split()))

    INF = 10**4+10
    dp = [INF]*(N+10)
    dp[0] = 0
    dp[1] = abs(H[1] - H[0])
    for i in range(2, N):
        dp[i] = min(dp[i-1]+abs(H[i]-H[i-1]), dp[i-2]+abs(H[i]-H[i-2]))
    
    print(dp[N-1])


def solve2():
    """ メモ化再帰 """
    N = int(input())
    H = list(map(int, input().split()))

    INF = 10**14+10
    dp = [INF] * (N+10)
    dp[0] = 0
    dp[1] = abs(H[0]-H[1])

    def dfs(i):
        """ 足場iの時点の最小値を返す """
        if dp[i] != INF: return dp[i]
                
        ans2 = INF
        ans1 = dfs(i-1) + abs(H[i]-H[i-1])  # i-1の足場から来た場合
        if i-2 >= 0:
            ans2 = dfs(i-2) + abs(H[i]-H[i-2])  # i-2の足場から来た場合

        ans = min(ans1, ans2)

        return ans
    
    print(dfs(N-1))



if __name__ == "__main__":
    solve2()

Fixed submission:
# -*- coding:utf-8 -*-

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

    INF = 10**4+10
    dp = [INF]*(N+10)
    dp[0] = 0
    dp[1] = abs(H[1] - H[0])
    for i in range(2, N):
        dp[i] = min(dp[i-1]+abs(H[i]-H[i-1]), dp[i-2]+abs(H[i]-H[i-2]))
    
    print(dp[N-1])


if __name__ == "__main__":
    solve()