Problem p03160 (python)

Fixed submission:
import numpy as np

N = int(input())
hs = list(map(int, input().split()))
hs = [0] + hs

# init
dp = np.zeros(N + 1).astype(int)
dp[2] = abs(hs[2] - hs[1])
# loop
if N > 2:
    for i in range(3, N + 1):
        dp[i] = min(
            dp[i-2] + abs(hs[i] - hs[i-2]),
            dp[i-1] + abs(hs[i] - hs[i-1]))

# output
print(dp[N])

Buggy submission (verdict: Runtime Error):
import numpy as np

N = int(input())
hs = list(map(int, input().split()))
hs = [0] + hs

# init
# dp[stage][jump_num]
# stage: 1 ~ N, jump_num: 1 ~ N-1
dp = np.zeros((N + 1, N)).astype(int)
# loop
# stage
for i in range(1, N):
    # jump_num
    js = np.where(dp[i, :] > 0)[0]
    if len(js) == 0:
        js = [0]
    for j in js:
        # i + 1
        if dp[i + 1][j + 1] == 0:
            dp[i + 1][j + 1] = dp[i][j] + abs(hs[i + 1] - hs[i])
        else:
            dp[i + 1][j + 1] = min(dp[i][j] + abs(hs[i + 1] - hs[i]), dp[i + 1][j + 1])
        # i + 2
        if i < N - 1:
            if dp[i + 2][j + 1] == 0:
                dp[i + 2][j + 1] = dp[i][j] + abs(hs[i + 2] - hs[i])
            else:
                dp[i + 2][j + 1] = min(dp[i][j] + abs(hs[i + 2] - hs[i]), dp[i + 2][j + 1])

# output
vals = dp[N][dp[N] > 0]
if len(vals) == 0:
    print(0)
else:
    print(min(vals))

Buggy submission (verdict: Runtime Error):
import numpy as np

N = int(input())
hs = list(map(int, input().split()))
hs = [0] + hs

# init
# dp[stage][jump_num]
dp = np.zeros((N + 1, N + 1)).astype(int)
# loop
# stage
for i in range(1, N + 1):
    # jump_num
    js = np.where(dp[i, :] > 0)[0]
    if len(js) == 0:
        js = [0]
    for j in js:
        # i + 1
        if i < N:
            if dp[i + 1][j + 1] == 0:
                dp[i + 1][j + 1] = dp[i][j] + abs(hs[i + 1] - hs[i])
            else:
                dp[i + 1][j + 1] = min(dp[i][j] + abs(hs[i + 1] - hs[i]), dp[i + 1][j + 1])
        # i + 2
        if i < N - 1:
            if dp[i + 2][j + 1] == 0:
                dp[i + 2][j + 1] = dp[i][j] + abs(hs[i + 2] - hs[i])
            else:
                dp[i + 2][j + 1] = min(dp[i][j] + abs(hs[i + 2] - hs[i]), dp[i + 2][j + 1])

# output
vals = dp[N][dp[N] > 0]
if len(vals) == 0:
    print(0)
else:
    print(min(vals))

Buggy submission (verdict: Runtime Error):
import numpy as np

N = int(input())
hs = list(map(int, input().split()))

# init
dp = np.zeros((N + 1, max(hs) + 1)).astype(int)
for i in range(1, N + 1):
    h = hs[i - 1]
    # i + 1
    if i < N:
        h1 = hs[i]
        if dp[i + 1][h1] == 0:
            dp[i + 1][h1] = dp[i][h] + abs(h1 - h)
        else:
            dp[i + 1][h1] = min(dp[i][h] + abs(h1 - h), dp[i + 1][h1])
    # i + 2
    if i < N - 1:
        h2 = hs[i + 1]
        if dp[i + 2][h2] == 0:
            dp[i + 2][h2] = dp[i][h] + abs(h2 - h)
        else:
            dp[i + 2][h2] = min(dp[i][h] + abs(h2 - h), dp[i + 2][h2])

# output
print(dp[N][hs[-1]])

Buggy submission (verdict: Runtime Error):
import numpy as np

N = int(input())
hs = list(map(int, input().split()))

# init
dp = np.zeros((N + 1, max(hs) + 1)).astype(int)
for i in range(1, N + 1):
    h = hs[i - 1]
    # i + 1
    if i < N:
        h1 = hs[i]
        if dp[i + 1][h1] == 0:
            dp[i + 1][h1] = dp[i][h] + abs(h1 - h)
        else:
            dp[i + 1][h1] = min(dp[i][h] + abs(h1 - h), dp[i + 1][h1])
    # i + 2
    if i < N - 1:
        h2 = hs[i + 1]
        if dp[i + 2][h2] == 0:
            dp[i + 2][h2] = dp[i][h] + abs(h2 - h)
        else:
            dp[i + 2][h2] = min(dp[i][h] + abs(h2 - h), dp[i + 2][h2])

# output
vals = dp[N][dp[N] > 0]
if len(vals) > 0:
    print(min(vals))
else:
    print(0)