Problem p03160 (python)

Fixed submission:
def calc_cost(h):
    if (len(h) == 0):
        return 0

    cost = [None] * len(h)
    cost[0] = 0
    for i in range(1, len(h)):
        cost[i] = cost[i-1] + abs(h[i] - h[i-1])
        if i>1:
            cost[i] = min(cost[i], cost[i-2] + abs(h[i] - h[i-2]))

    return cost[len(h)-1]

if __name__ == "__main__":
    n = int(input())
    h = list(map(int, input().split(" ")))
    res = calc_cost(h)
    print(res)

Buggy submission (verdict: Runtime Error):
import sys

def jump(k: int, h, res):
    if ( k == 0 ):
        return 0

    if ( res[k] == -1 ):
        res[k] = jump(k - 1, h, res) + abs(h[k] - h[k - 1])
        if k >= 2:
            res[k] = min(res[k], jump(k - 2, h, res) + abs(h[k] - h[k - 2]))

    return res[k]


def main():
    #lines = []
    #for line in sys.stdin:
    #    lines.append([int(i) for i in line.strip().split(' ')])

    #n = int(lines[0][0])
    #h = lines[1]
    n = int(input())
    h = list(map(int, input().split(" ")))

    res = [ -1 ] * len(h)
    print(jump(n-1, h, res))


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys

def jump(k: int, h, res):
    if ( k == 0 ):
        return 0

    if ( res[k] == -1 ):
        res[k] = jump(k - 1, h, res) + abs(h[k] - h[k - 1])
        if k >= 2:
            res[k] = min(res[k], jump(k - 2, h, res) + abs(h[k] - h[k - 2]))

    return res[k]
   

def main():
    #lines = []
    #for line in sys.stdin:
    #    lines.append([int(i) for i in line.strip().split(' ')])

    #n = int(lines[0][0])
    #h = lines[1]
    n = int(input())
    h = list(map(int, input().split(" ")))

    res = [ -1 ] * len(h)
    print(jump(n-1, h, res))


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
    def calc_cost(h):
        if (len(h) == 0):
            return 0
     
        cost = [None] * len(h)
        cost[0] = 0
        for i in range(1, len(h)):
            cost[i] = cost[i-1] + abs(h[i] - h[i-1])
            if i>1:
                cost[i] = min(cost[i], cost[i-2] + abs(h[i] - h[i-2]))
     
        return cost[len(h)-1]
     
    if __name__ == "__main__":
        n = int(input())
        h = list(map(int, input().split(" ")))
        res = calc_cost(h)
        print(res)

Buggy submission (verdict: Runtime Error):
import sys

def jump(k: int, h, res):
    if ( k == 0 ):
        return 0

    if ( res[k] == -1 ):
        res[k] = jump(k - 1, h, res) + abs(h[k] - h[k - 1])
        if k >= 2:
            res[k] = min(res[k], jump(k - 2, h, res) + abs(h[k] - h[k - 2]))

    return res[k]

def main():
    #lines = []
    #for line in sys.stdin:
    #    lines.append([int(i) for i in line.strip().split(' ')])

    #n = int(lines[0][0])
    #h = lines[1]
    n = int(input())
    h = list(map(int, input().split(" ")))

    res = [ -1 for i in range(n)]
    print(jump(n-1, h, res))


if __name__ == '__main__':
    main()

Buggy submission (verdict: Runtime Error):
import sys

def jump(k: int, h, res):
    if ( k == 0 ):
        return 0

    if ( res[k] == -1 ):
        res[k] = jump(k - 1, h, res) + abs(h[k] - h[k - 1])
        if k >= 2:
            res[k] = min(res[k], jump(k - 2, h, res) + abs(h[k] - h[k - 2]))

    return res[k]

def main():
    lines = []
    for line in sys.stdin:
        lines.append([int(i) for i in line.strip().split(' ')])

    n = int(lines[0][0])
    h = lines[1]
    res = [ -1 for i in range(n)]
    print(jump(n-1, h, res))


if __name__ == '__main__':
    main()