Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
h = list(map(int, input().split()))
dp = [[None] * 10**6 for _ in range(n)]

def solve(idx, cost):
    if dp[idx][cost] is not None:
        return dp[idx][cost]

    if idx == n - 1:
        return cost

    if idx == n - 2:
        return solve(idx + 1, cost + abs(h[idx] - h[idx+1]))

    c1 = solve(idx + 1, cost + abs(h[idx] - h[idx + 1]))
    c2 = solve(idx + 2, cost + abs(h[idx] - h[idx + 2]))

    dp[idx][cost] = min(c1, c2)
    return dp[idx][cost]

    # return min(c1, c2)

ans = solve(0, 0)

print(ans)

Fixed submission:
n = int(input())
hs = list(map(int, input().split()))
dp = [0]*n

# cost1 = dp[i-1] + abs(dp[i-1]-dp[i])
# cost2 = dp[i-2] + abs(dp[i-2]-dp[i])
# dp[i] = min(cost1, cost2)

#for i in reversed(range(n)):
for i in range(n):
    if i == 0:
        dp[0] = 0
    elif i == 1:
        dp[1] = abs(hs[0]-hs[1])
    else:
        cost1 = dp[i-1] + abs(hs[i-1]-hs[i])
        cost2 = dp[i-2] + abs(hs[i-2]-hs[i])
        dp[i] = min(cost1, cost2)

print(dp[n-1])

Buggy python submission (verdict: Runtime Error):
# import sys
# sys.setrecursionlimit(10**6)

n = int(input())
h = list(map(int, input().split()))
# dp = [[None] * 10**6 for _ in range(n)]
dp = [None] * (n+1)

# Return the smallest cost to reach the idx
def solve(idx):
    if dp[idx] is not None:
        return dp[idx]

    if idx == 1:
        dp[idx] = abs(h[idx] - h[idx - 1])
    elif idx == 0:
        dp[idx] = 0
    else:
        # jump from idx - 1
        c1 = solve(idx - 1) + abs(h[idx] - h[idx - 1])

        # jump from idx - 2
        c2 = solve(idx - 2) + abs(h[idx] - h[idx - 2])

        dp[idx] = min(c1, c2)

    return dp[idx]

ans = solve(n-1)

print(ans)

Fixed submission:
n = int(input())
hs = list(map(int, input().split()))
dp = [0]*n

# cost1 = dp[i-1] + abs(dp[i-1]-dp[i])
# cost2 = dp[i-2] + abs(dp[i-2]-dp[i])
# dp[i] = min(cost1, cost2)

#for i in reversed(range(n)):
for i in range(n):
    if i == 0:
        dp[0] = 0
    elif i == 1:
        dp[1] = abs(hs[0]-hs[1])
    else:
        cost1 = dp[i-1] + abs(hs[i-1]-hs[i])
        cost2 = dp[i-2] + abs(hs[i-2]-hs[i])
        dp[i] = min(cost1, cost2)

print(dp[n-1])

Buggy python submission (verdict: Runtime Error):
n = int(input())
h = list(map(int, input().split()))
# dp = [[None] * 10**6 for _ in range(n)]
dp = [None] * (n+1)

# Return the smallest cost to reach the idx
def solve(idx):
    if dp[idx] is not None:
        return dp[idx]

    if idx == 1:
        dp[idx] = abs(h[idx] - h[idx - 1])
    elif idx == 0:
        dp[idx] = 0
    else:
        # jump from idx - 1
        c1 = solve(idx - 1) + abs(h[idx] - h[idx - 1])

        # jump from idx - 2
        c2 = solve(idx - 2) + abs(h[idx] - h[idx - 2])

        dp[idx] = min(c1, c2)

    return dp[idx]

ans = solve(n-1)

print(ans)

Fixed submission:
n = int(input())
hs = list(map(int, input().split()))
dp = [0]*n

# cost1 = dp[i-1] + abs(dp[i-1]-dp[i])
# cost2 = dp[i-2] + abs(dp[i-2]-dp[i])
# dp[i] = min(cost1, cost2)

#for i in reversed(range(n)):
for i in range(n):
    if i == 0:
        dp[0] = 0
    elif i == 1:
        dp[1] = abs(hs[0]-hs[1])
    else:
        cost1 = dp[i-1] + abs(hs[i-1]-hs[i])
        cost2 = dp[i-2] + abs(hs[i-2]-hs[i])
        dp[i] = min(cost1, cost2)

print(dp[n-1])

Buggy python submission (verdict: Time Limit Exceeded):
n = int(input())
h = list(map(int, input().split()))
dp = [[None] * 10**6 for _ in range(n)]

def solve(idx, cost):
    if dp[idx][cost] is not None:
        return dp[idx][cost]

    if idx == n - 1:
        return cost

    if idx == n - 2:
        return solve(idx + 1, cost + abs(h[idx] - h[idx+1]))

    c1 = solve(idx + 1, cost + abs(h[idx] - h[idx + 1]))
    c2 = solve(idx + 2, cost + abs(h[idx] - h[idx + 2]))

    dp[idx][cost] = min(c1, c2)
    return dp[idx][cost]

    # return min(c1, c2)

ans = solve(0, 0)

print(ans)

Fixed submission:
n = int(input())
hs = list(map(int, input().split()))
dp = [0]*n

# cost1 = dp[i-1] + abs(dp[i-1]-dp[i])
# cost2 = dp[i-2] + abs(dp[i-2]-dp[i])
# dp[i] = min(cost1, cost2)

#for i in reversed(range(n)):
for i in range(n):
    if i == 0:
        dp[0] = 0
    elif i == 1:
        dp[1] = abs(hs[0]-hs[1])
    else:
        cost1 = dp[i-1] + abs(hs[i-1]-hs[i])
        cost2 = dp[i-2] + abs(hs[i-2]-hs[i])
        dp[i] = min(cost1, cost2)

print(dp[n-1])

Buggy python submission (verdict: Runtime Error):
n = int(input())
h = list(map(int, input().split()))

def solve(idx, cost):
    if idx == n - 1:
        return cost

    if idx == n - 2:
        return solve(idx + 1, cost + abs(h[idx] - h[idx+1]))

    c1 = solve(idx + 1, cost + abs(h[idx] - h[idx + 1]))
    c2 = solve(idx + 2, cost + abs(h[idx] - h[idx + 2]))

    return min(c1, c2)

ans = solve(0, 0)

print(ans)

Fixed submission:
n = int(input())
hs = list(map(int, input().split()))
dp = [0]*n

# cost1 = dp[i-1] + abs(dp[i-1]-dp[i])
# cost2 = dp[i-2] + abs(dp[i-2]-dp[i])
# dp[i] = min(cost1, cost2)

#for i in reversed(range(n)):
for i in range(n):
    if i == 0:
        dp[0] = 0
    elif i == 1:
        dp[1] = abs(hs[0]-hs[1])
    else:
        cost1 = dp[i-1] + abs(hs[i-1]-hs[i])
        cost2 = dp[i-2] + abs(hs[i-2]-hs[i])
        dp[i] = min(cost1, cost2)

print(dp[n-1])

Buggy python submission (verdict: Runtime Error):
n = int(input())
hs = list(map(int, input().split()))

def solve(array):
    if len(array) < 2:
        return 0
    elif len(array) == 2:
        return abs(array[0]-array[1])
    else:
        cost1 = solve(array[1:]) + abs(array[0]-array[1])
        cost2 = solve(array[2:]) + abs(array[0]-array[2])
        return min(cost1, cost2)

cost = solve(hs)
print(cost)



Fixed submission:
n = int(input())
hs = list(map(int, input().split()))
dp = [0]*n

# cost1 = dp[i-1] + abs(dp[i-1]-dp[i])
# cost2 = dp[i-2] + abs(dp[i-2]-dp[i])
# dp[i] = min(cost1, cost2)

#for i in reversed(range(n)):
for i in range(n):
    if i == 0:
        dp[0] = 0
    elif i == 1:
        dp[1] = abs(hs[0]-hs[1])
    else:
        cost1 = dp[i-1] + abs(hs[i-1]-hs[i])
        cost2 = dp[i-2] + abs(hs[i-2]-hs[i])
        dp[i] = min(cost1, cost2)

print(dp[n-1])