Buggy python submission (verdict: Runtime Error):
def totalcost_recur(i,cost,N,lookup):
  if i>=N:
    return 0
  if i in lookup:
      return lookup[i]
  option1 = abs(cost[i]-cost[i+1]) + totalcost_recur(i+1,cost,N,lookup)
  ans = option1
  if i+2<=N:
    option2 = abs(cost[i]-cost[i+2]) + totalcost_recur(i+2,cost,N,lookup)
    ans = min(option2,ans)
  lookup[i] = ans
  return ans



def totalcost_dp(cost,N,k):
    dp = [float('inf')]*(N+1)
    dp[1] = 0
    
    
    for i in range(1,N+1):
        
        for j in range(i+1,k+i+1):
            if j < N+1:
                dp[j] = min(dp[j],dp[i]+abs(cost[i]-cost[j]))
                
            
        
        

    return dp[N]


n ,k= map(int,input().split())
cost = list(map(int,input().split()))
cost = [0]+cost
lookup = {}

print(totalcost_dp(cost,n,k))

Fixed submission:
def totalcost_recur(i,cost,N,lookup):
  if i>=N:
    return 0
  if i in lookup:
      return lookup[i]
  option1 = abs(cost[i]-cost[i+1]) + totalcost_recur(i+1,cost,N,lookup)
  ans = option1
  if i+2<=N:
    option2 = abs(cost[i]-cost[i+2]) + totalcost_recur(i+2,cost,N,lookup)
    ans = min(option2,ans)
  lookup[i] = ans
  return ans



def totalcost_dp(cost,N):
    dp = [0]*(N+1)
    dp[1] = 0
    dp[2] = abs(cost[2]-cost[1])
    i = 3
    while i<=N:
        option1 = dp[i-1]+abs(cost[i]-cost[i-1])
        option2 = dp[i-2]+abs(cost[i]-cost[i-2])
        dp[i] = min(option1,option2)
        i+=1

    return dp[N]


n = int(input())
cost = list(map(int,input().split()))
cost = [0]+cost
lookup = {}

print(totalcost_dp(cost,n))

Buggy python submission (verdict: Runtime Error):
def totalcost(i,cost,N,lookup):
  if i>=N:
    return 0
  if i in lookup:
      return lookup[i]
  option1 = abs(cost[i]-cost[i+1]) + totalcost(i+1,cost,N,lookup)
  ans = option1
  if i+2<=N:
    option2 = abs(cost[i]-cost[i+2]) + totalcost(i+2,cost,N,lookup)
    ans = min(option2,ans)
  lookup[i] = ans
  return ans

n = int(input())
cost = list(map(int,input().split()))
cost = [0]+cost
lookup = {}
print(totalcost(1,cost,n,lookup))

Fixed submission:
def totalcost_recur(i,cost,N,lookup):
  if i>=N:
    return 0
  if i in lookup:
      return lookup[i]
  option1 = abs(cost[i]-cost[i+1]) + totalcost_recur(i+1,cost,N,lookup)
  ans = option1
  if i+2<=N:
    option2 = abs(cost[i]-cost[i+2]) + totalcost_recur(i+2,cost,N,lookup)
    ans = min(option2,ans)
  lookup[i] = ans
  return ans



def totalcost_dp(cost,N):
    dp = [0]*(N+1)
    dp[1] = 0
    dp[2] = abs(cost[2]-cost[1])
    i = 3
    while i<=N:
        option1 = dp[i-1]+abs(cost[i]-cost[i-1])
        option2 = dp[i-2]+abs(cost[i]-cost[i-2])
        dp[i] = min(option1,option2)
        i+=1

    return dp[N]


n = int(input())
cost = list(map(int,input().split()))
cost = [0]+cost
lookup = {}

print(totalcost_dp(cost,n))

Buggy python submission (verdict: Runtime Error):
def totalcost(i,cost,N):
  if i>=N:
    return 0
  option1 = abs(cost[i]-cost[i+1]) + totalcost(i+1,cost,N)
  ans = option1
  if i+2<=N:
    option2 = abs(cost[i]-cost[i+2]) + totalcost(i+2,cost,N)
    ans = min(option2,ans)
  return ans

n = int(input())
cost = list(map(int,input().split()))
cost = [0]+cost
print(totalcost(1,cost,n))

Fixed submission:
def totalcost_recur(i,cost,N,lookup):
  if i>=N:
    return 0
  if i in lookup:
      return lookup[i]
  option1 = abs(cost[i]-cost[i+1]) + totalcost_recur(i+1,cost,N,lookup)
  ans = option1
  if i+2<=N:
    option2 = abs(cost[i]-cost[i+2]) + totalcost_recur(i+2,cost,N,lookup)
    ans = min(option2,ans)
  lookup[i] = ans
  return ans



def totalcost_dp(cost,N):
    dp = [0]*(N+1)
    dp[1] = 0
    dp[2] = abs(cost[2]-cost[1])
    i = 3
    while i<=N:
        option1 = dp[i-1]+abs(cost[i]-cost[i-1])
        option2 = dp[i-2]+abs(cost[i]-cost[i-2])
        dp[i] = min(option1,option2)
        i+=1

    return dp[N]


n = int(input())
cost = list(map(int,input().split()))
cost = [0]+cost
lookup = {}

print(totalcost_dp(cost,n))

Buggy python submission (verdict: Runtime Error):
def totalcost(i,cost,N):
  if i>=N:
    return 0
  option1 = abs(cost(i)-cost(i+1)) + totalcost(i+1,cost,N)
  ans = option1
  if i+2<=N:
    option2 = abs(cost(i)-cost(i+2)) + totalcost(i+2,cost,N)
    ans = min(option1,ans)
  return ans

n = int(input())
cost = list(map(int,input().split()))
cost = [0]+cost
print(totalcost(1,cost,n))

Fixed submission:
def totalcost_recur(i,cost,N,lookup):
  if i>=N:
    return 0
  if i in lookup:
      return lookup[i]
  option1 = abs(cost[i]-cost[i+1]) + totalcost_recur(i+1,cost,N,lookup)
  ans = option1
  if i+2<=N:
    option2 = abs(cost[i]-cost[i+2]) + totalcost_recur(i+2,cost,N,lookup)
    ans = min(option2,ans)
  lookup[i] = ans
  return ans



def totalcost_dp(cost,N):
    dp = [0]*(N+1)
    dp[1] = 0
    dp[2] = abs(cost[2]-cost[1])
    i = 3
    while i<=N:
        option1 = dp[i-1]+abs(cost[i]-cost[i-1])
        option2 = dp[i-2]+abs(cost[i]-cost[i-2])
        dp[i] = min(option1,option2)
        i+=1

    return dp[N]


n = int(input())
cost = list(map(int,input().split()))
cost = [0]+cost
lookup = {}

print(totalcost_dp(cost,n))