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


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,stdin.readline().split())
cost = list(map(int,stdin.readline().split()))
cost = [0]+cost


print(totalcost_dp(cost,n,k))

Fixed submission:

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
print(totalcost_dp(cost,n,k))

Buggy python submission (verdict: Time Limit Exceeded):
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_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
print(totalcost_dp(cost,n,k))

Buggy python submission (verdict: Time Limit Exceeded):
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 = [0]*(N+1)
    dp[1] = 0
    
    
    for i in range(2,N+1):
        ans = float('inf')
        for j in range(1,k+1):
            if i-j < 1:
                break
            option = dp[i-j]+abs(cost[i]-cost[i-j])
            ans = min(ans,option)
        
        dp[i] = ans
        

    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_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
print(totalcost_dp(cost,n,k))

Buggy python submission (verdict: Time Limit Exceeded):
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 = [0]*(N+1)
    dp[1] = 0
    
    i = 2
    while i<=N:
        ans = float('inf')
        for j in range(1,k+1):
            if i-j < 1:
                break
            option = dp[i-j]+abs(cost[i]-cost[i-j])
            ans = min(ans,option)
        
        dp[i] = ans
        i+=1

    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_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
print(totalcost_dp(cost,n,k))