Problem p03160 (python)

Fixed submission:
import sys
def solve(stns, idx, mem):

    if idx >= len(stns) -1:
        return 0

    if idx in mem: 
        return mem[idx]

    if idx == len(stns) - 2: 
        # can take only one step
        # print(idx)  
        # final_step = solve(stns, idx + 1, mem) + abs(stns[idx] - stns[idx+1])
        final_step = abs(stns[idx] - stns[idx+1])
        mem[idx] = final_step
        return final_step
    else: 
        # step: i + 11
        step1 = solve(stns, idx + 1, mem) + abs(stns[idx] - stns[idx+1])
        # step: i + 2
        step2 = solve(stns, idx + 2, mem) + abs(stns[idx] - stns[idx+2])

        min_cost = min(step1, step2)
        mem[idx] = min_cost 

        return min_cost



def take_input():
    # n = input()
    # stns = [int(x) for x in input().split(' ')]



    reader = (s.rstrip() for s in sys.stdin)
    input = reader.__next__
    sys.setrecursionlimit(1000000)
    # do magic here
    n = int(input())
    stns = [int(x) for x in input().split()]
    mem = {}
    res = solve(stns, 0, mem)
    print(res)

take_input()

Buggy submission (verdict: Runtime Error):
def solve():
    # n, k = map(int, input().split())
    # stns = [int(x) for x in input().split()]
    n,k = map(int,input().split())
    stns = list(map(int, input().split()))

    INF = 10**9
    dp = [INF] * n
    dp[0] = 0

    for i in range(n):
        for j in range(1, k + 1): # consider more than one step
            j = i + j # step i + 1, i + 2
            if j < n:
                # min(existing min, new path using i)
                dp[j] = min(dp[j], dp[i] + abs(stns[i] - stns[j]))
    print(dp[n-1])

solve()

Buggy submission (verdict: Runtime Error):
import sys
def solve(stns, idx, mem):

    if idx >= len(stns) -1:
        return 0

    if idx in mem: 
        return mem[idx]

    if idx == len(stns) - 2: 
        #cant take more than one step
        final_step = abs(stns[idx] - stns[idx+1])
        mem[idx] = final_step
        return final_step
    else: 
        # step: i + 11
        step1 = solve(stns, idx + 1, mem) + abs(stns[idx] - stns[idx+1])
        # step: i + 2
        step2 = solve(stns, idx + 2, mem) + abs(stns[idx] - stns[idx+2])

        min_cost = min(step1, step2)
        mem[idx] = min_cost 

        return min_cost



def take_input():
    n = input()
    stns = [int(x) for x in input().strip().split()]



    # reader = (s.rstrip() for s in sys.stdin)
    # input = reader.__next__
    # sys.setrecursionlimit(1000000)
    # do magic here
    # n = int(input())
    # stns = [int(x) for x in input().split()]
    mem = {}
    res = solve(stns, 0, mem)
    print(res)

take_input()

Buggy submission (verdict: Runtime Error):
import sys
def solve(stns, idx, mem):

    if idx >= len(stns) -1:
        return 0

    if idx in mem: 
        return mem[idx]

    if idx == len(stns) - 2: 
        # can take only one step
        # print(idx)  
        # final_step = solve(stns, idx + 1, mem) + abs(stns[idx] - stns[idx+1])
        final_step = abs(stns[idx] - stns[idx+1])
        mem[idx] = final_step
        return final_step
    else: 
        # step: i + 11
        step1 = solve(stns, idx + 1, mem) + abs(stns[idx] - stns[idx+1])
        # step: i + 2
        step2 = solve(stns, idx + 2, mem) + abs(stns[idx] - stns[idx+2])

        min_cost = min(step1, step2)
        mem[idx] = min_cost 

        return min_cost



def take_input():
    n = input().strip()
    stns = [int(x) for x in input().strip().split()]



    # reader = (s.rstrip() for s in sys.stdin)
    # input = reader.__next__
    # sys.setrecursionlimit(1000000)
    # do magic here
    # n = int(input())
    # stns = [int(x) for x in input().split()]
    mem = {}
    res = solve(stns, 0, mem)
    print(res)

take_input()

Buggy submission (verdict: Runtime Error):
import sys
def solve(stns, idx, mem):

    if idx >= len(stns) -1:
        return 0

    if idx in mem: 
        return mem[idx]

    if idx == len(stns) - 2: 
        # can take only one step
        # print(idx)  
        # final_step = solve(stns, idx + 1, mem) + abs(stns[idx] - stns[idx+1])
        final_step = abs(stns[idx] - stns[idx+1])
        mem[idx] = final_step
        return final_step
    else: 
        # step: i + 11
        step1 = solve(stns, idx + 1, mem) + abs(stns[idx] - stns[idx+1])
        # step: i + 2
        step2 = solve(stns, idx + 2, mem) + abs(stns[idx] - stns[idx+2])

        min_cost = min(step1, step2)
        mem[idx] = min_cost 

        return min_cost



def take_input():
    n = input()
    stns = [int(x) for x in input().split()]



    # reader = (s.rstrip() for s in sys.stdin)
    # input = reader.__next__
    # sys.setrecursionlimit(1000000)
    # do magic here
    # n = int(input())
    # stns = [int(x) for x in input().split()]
    mem = {}
    res = solve(stns, 0, mem)
    print(res)

take_input()

Buggy submission (verdict: Runtime Error):

def solve(stns, idx, mem):

    if idx >= len(stns) -1:
        return 0

    if idx in mem: 
        return mem[idx]

    if idx == len(stns) - 2: 
        # can take only one step
        # print(idx)  
        # final_step = solve(stns, idx + 1, mem) + abs(stns[idx] - stns[idx+1])
        final_step = abs(stns[idx] - stns[idx+1])
        mem[idx] = final_step
        return final_step
    else: 
        # step: i + 11
        step1 = solve(stns, idx + 1, mem) + abs(stns[idx] - stns[idx+1])
        # step: i + 2
        step2 = solve(stns, idx + 2, mem) + abs(stns[idx] - stns[idx+2])

        min_cost = min(step1, step2)
        mem[idx] = min_cost 

        return min_cost



def take_input():
    n = input()
    stns = [int(x) for x in input().split(' ')]
    mem = {}
    res = solve(stns, 0, mem)
    print(res)

take_input()

Buggy submission (verdict: Runtime Error):

def solve(stns, idx, mem):

    if idx >= len(stns) -1:
        return 0

    if idx in mem: 
        return mem[idx]

    if idx == len(stns) - 2: 
        # can take only one step
        final_step = solve(stns, idx + 1, mem) + abs(stns[idx] - stns[idx+1])
        mem[idx] = final_step
        return final_step
    else: 
        # step: i + 11
        step1 = solve(stns, idx + 1, mem) + abs(stns[idx] - stns[idx+1])
        # step: i + 2
        step2 = solve(stns, idx + 1, mem) + abs(stns[idx] - stns[idx+2])

    min_cost = min(step1, step2)
    mem[idx] = min_cost 

    return min_cost



def take_input():
    n = input()
    stns = [int(x) for x in input().split(' ')]
    mem = {}
    res = solve(stns, 0, mem)
    print(res)

take_input()

Buggy submission (verdict: Runtime Error):

def solve(stns, idx, mem):

    if idx >= len(stns) -1:
        return 0

    if idx in mem: 
        return mem[idx]

    if idx == len(stns) - 2: 
        # can take only one step
        final_step = solve(stns, idx + 1, mem) + abs(stns[idx] - stns[idx+1])
        mem[idx] = final_step
        return final_step
    else: 
        # step: i + 11
        step1 = solve(stns, idx + 1, mem) + abs(stns[idx] - stns[idx+1])
        # step: i + 2
        step2 = solve(stns, idx + 1, mem) + abs(stns[idx] - stns[idx+2])

    min_cost = min(step1, step2)
    mem[idx] = min_cost 

    return min_cost



def take_input():
    n = input()
    stns = [int(x) for x in input().split(' ')]
    mem = {}
    res = solve(stns, 0, mem)
    print(res)

take_input()

Buggy submission (verdict: Runtime Error):

def solve(stns, idx, mem):
    
    if idx >= len(stns) -1:
        return 0

    if idx in mem: 
        return mem[idx]

    if idx == len(stns) - 2: 
        # can take only one step
        final_step = solve(stns, idx + 1, mem) + abs(stns[idx] - stns[idx+1])
        mem[idx] = final_step
        return final_step
    else: 
        # step: i + 1
        step1 = solve(stns, idx + 1, mem) + abs(stns[idx] - stns[idx+1])
        # step: i + 2
        step2 = solve(stns, idx + 2, mem) + abs(stns[idx] - stns[idx+2])

    min_cost = min(step1, step2)
    mem[idx] = min_cost 

    return min_cost



def take_input():
    n = input()
    stns = [int(x) for x in input().split(' ')]
    mem = {}
    res = solve(stns, 0, mem)
    print(res)

take_input()