Problem p02839 (python)

Fixed submission:
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, W = lr()
A = [lr() for _ in range(H)]
B = [lr() for _ in range(H)]
diff = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        diff[i][j] = abs(A[i][j] - B[i][j])

X = (H+W) * 80
L = X + X + 1 #真ん中を0とする、最後に-X
dp = [[None] * W for _ in range(H)]
dp[0][0] = np.zeros(L, np.bool)
dp[0][0][X+diff[0][0]] = 1; dp[0][0][X-diff[0][0]] = 1 #非負整数のみでも良い
for h in range(H):
    for w, di in enumerate(diff[h]):
        if h == 0 and w == 0:
            continue
        x = np.zeros(L, np.bool)
        if h > 0:
            x[di:] |= dp[h-1][w][:L-di]
            x[:L-di] |= dp[h-1][w][di:]
        if w > 0:
            x[di:] |= dp[h][w-1][:L-di]
            x[:L-di] |= dp[h][w-1][di:]
        dp[h][w] = x

dp = dp[-1][-1]
possible = np.where(dp)[0] - X
answer = np.abs(possible).min()
print(answer)
# 07

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

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, W = lr()
A = np.array([lr() for _ in range(H)])
B = np.array([lr() for _ in range(H)])
diff = np.abs(A - B).tolist()
dp = np.array([[np.zeros((H+W)*80+1) for _ in range(W)] for _ in range(H)], np.bool)
base = ((H+W)*80+1)//2
begin = diff[0][0]
dp[0][0][base+begin] = 1
dp[0][0][base-begin] = 1
for i in range(H):
    for j in range(W):
        d = diff[i][j]
        if i > 0:
            dp[i][j][d:] |= dp[i-1][j][:-d]
            dp[i][j][:-d] |= dp[i-1][j][d:]
        if j > 0:
            dp[i][j][d:] |= dp[i][j-1][:-d]
            dp[i][j][:-d] |= dp[i][j-1][d:]

result = dp[H-1][W-1][base:]
answer = np.nonzero(result)[0].min()
print(answer)

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

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, W = lr()
A = np.array([lr() for _ in range(H)])
B = np.array([lr() for _ in range(H)])
diff = np.abs(A - B)
dp = np.array([[np.zeros(80*3) for _ in range(W)] for _ in range(H)], np.bool)
start = 80*3//2
begin = diff[0][0]
dp[0][0][start+begin] = True
dp[0][0][start-begin] = True
for i in range(H):
    for j in range(W):
        d = diff[i][j]
        if i > 0:
            dp[i][j][d:] = np.logical_or(dp[i][j][d:], dp[i-1][j][:-d])
            dp[i][j][:-d] = np.logical_or(dp[i][j][:-d], dp[i-1][j][d:])
        if j > 0:
            dp[i][j][d:] = np.logical_or(dp[i][j][d:], dp[i][j-1][:-d])
            dp[i][j][:-d] = np.logical_or(dp[i][j][:-d], dp[i][j-1][d:])

result = dp[H-1][W-1][start:]
answer = np.where(result==True)[0].min()
print(answer)
# 44

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

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, W = lr()
A = np.array([lr() for _ in range(H)])
B = np.array([lr() for _ in range(H)])
diff = np.abs(A - B)
dp = np.array([[np.zeros(80**2) for _ in range(W)] for _ in range(H)], np.bool)
start = 80**2//2
begin = diff[0][0]
dp[0][0][start+begin] = True
dp[0][0][start-begin] = True
for i in range(H):
    for j in range(W):
        d = diff[i][j]
        if i > 0:
            dp[i][j][d:] = np.logical_or(dp[i][j][d:], dp[i-1][j][:-d])
            dp[i][j][:-d] = np.logical_or(dp[i][j][:-d], dp[i-1][j][d:])
        if j > 0:
            dp[i][j][d:] = np.logical_or(dp[i][j][d:], dp[i][j-1][:-d])
            dp[i][j][:-d] = np.logical_or(dp[i][j][:-d], dp[i][j-1][d:])

result = dp[H-1][W-1][start:]
answer = np.where(result==True)[0].min()
print(answer)
# 44