Buggy python submission (verdict: Runtime Error):
import numpy as np
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

H, W = map(int, input().split())
A = np.array([input().split() for _ in range(H)], dtype=int)
B = np.array([input().split() for _ in range(H)], dtype=int)
dif = np.abs(A - B).tolist()

U = (H + W) * 80
Um = U // 2

dp = [[[] for _ in range(W)] for _ in range(H)]
X = np.zeros(U + 1, dtype=bool)
X[Um + dif[0][0]] = True
X[Um - dif[0][0]] = True
dp[0][0] = X


for i in range(H):
    for j in range(W):
        if i == j == 0:
            continue
        X = np.zeros(U + 1, dtype=bool)
        d = dif[i][j]
        if i > 0:
            X[d:] |= dp[i - 1][j][:-d]
            X[:-d] |= dp[i - 1][j][d:]
        if j > 0:
            X[d:] |= dp[i][j - 1][:-d]
            X[:-d] |= dp[i][j - 1][d:]
        dp[i][j] = X

tmp = np.arange(U+1)[dp[H-1][W-1]]
ans = np.abs(tmp - Um).min()
print(ans)

Fixed submission:
import numpy as np
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

H, W = map(int, input().split())
A = np.array([input().split() for _ in range(H)], dtype=int)
B = np.array([input().split() for _ in range(H)], dtype=int)
dif = np.abs(A - B).tolist()

U = (H + W) * 80
Um = U // 2

dp = [[[] for _ in range(W)] for _ in range(H)]
X = np.zeros(U + 1, dtype=bool)
X[Um + dif[0][0]] = True
X[Um - dif[0][0]] = True
dp[0][0] = X


for i in range(H):
    for j in range(W):
        if i == j == 0:
            continue
        d = dif[i][j]
        X = np.zeros(U + 1, dtype=bool)
        if d:
            if i > 0:
                X[d:] |= dp[i - 1][j][:-d]
                X[:-d] |= dp[i - 1][j][d:]
            if j > 0:
                X[d:] |= dp[i][j - 1][:-d]
                X[:-d] |= dp[i][j - 1][d:]
        else:
            if i > 0:
                X |= dp[i - 1][j]
            if j > 0:
                X |= dp[i][j - 1]
        dp[i][j] = X

tmp = np.arange(U+1)[dp[H-1][W-1]]
ans = np.abs(tmp - Um).min()
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

H, W = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
B = [list(map(int, input().split())) for _ in range(H)]

U = (H + W) * 80
Um = U // 2

dp = [[[0] * (U + 1) for _ in range(W)] for _ in range(H)]
d = abs(A[0][0] - B[0][0])
dp[0][0][Um + d] = 1
dp[0][0][Um - d] = 1


for i in range(H):
    for j in range(W):
        if i < H - 1:
            d = abs(A[i + 1][j] - B[i + 1][j])
            for k in range(U - d + 1):
                dp[i + 1][j][k + d] |= dp[i][j][k]
            for k in range(d, U + 1):
                dp[i + 1][j][k - d] |= dp[i][j][k]
        if j < W - 1:
            d = abs(A[i][j + 1] - B[i][j + 1])
            for k in range(U - d + 1):
                dp[i][j + 1][k + d] |= dp[i][j][k]
            for k in range(d, U + 1):
                dp[i][j + 1][k - d] |= dp[i][j][k]

ans = 10 ** 18
for i, p in enumerate(dp[H - 1][W - 1]):
    if p:
        ans = min(ans, abs(Um - i))
print(ans)

Fixed submission:
import numpy as np
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

H, W = map(int, input().split())
A = np.array([input().split() for _ in range(H)], dtype=int)
B = np.array([input().split() for _ in range(H)], dtype=int)
dif = np.abs(A - B).tolist()

U = (H + W) * 80
Um = U // 2

dp = [[[] for _ in range(W)] for _ in range(H)]
X = np.zeros(U + 1, dtype=bool)
X[Um + dif[0][0]] = True
X[Um - dif[0][0]] = True
dp[0][0] = X


for i in range(H):
    for j in range(W):
        if i == j == 0:
            continue
        d = dif[i][j]
        X = np.zeros(U + 1, dtype=bool)
        if d:
            if i > 0:
                X[d:] |= dp[i - 1][j][:-d]
                X[:-d] |= dp[i - 1][j][d:]
            if j > 0:
                X[d:] |= dp[i][j - 1][:-d]
                X[:-d] |= dp[i][j - 1][d:]
        else:
            if i > 0:
                X |= dp[i - 1][j]
            if j > 0:
                X |= dp[i][j - 1]
        dp[i][j] = X

tmp = np.arange(U+1)[dp[H-1][W-1]]
ans = np.abs(tmp - Um).min()
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

H, W = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
B = [list(map(int, input().split())) for _ in range(H)]

U = 80 * 80 + 10
Um = U // 2

dp = [[[0] * (U + 1) for _ in range(W)] for _ in range(H)]
d = abs(A[0][0] - B[0][0])
dp[0][0][Um + d] = 1
dp[0][0][Um - d] = 1


for i in range(H):
    for j in range(W):
        if i < H - 1:
            d = abs(A[i + 1][j] - B[i + 1][j])
            for k in range(U - d + 1):
                dp[i + 1][j][k + d] |= dp[i][j][k]
            for k in range(d, U + 1):
                dp[i + 1][j][k - d] |= dp[i][j][k]
        if j < W - 1:
            d = abs(A[i][j + 1] - B[i][j + 1])
            for k in range(U - d + 1):
                dp[i][j + 1][k + d] |= dp[i][j][k]
            for k in range(d, U + 1):
                dp[i][j + 1][k - d] |= dp[i][j][k]

ans = 10 ** 18
for i, p in enumerate(dp[H - 1][W - 1]):
    if p:
        ans = min(ans, abs(Um - i))
print(ans)

Fixed submission:
import numpy as np
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

H, W = map(int, input().split())
A = np.array([input().split() for _ in range(H)], dtype=int)
B = np.array([input().split() for _ in range(H)], dtype=int)
dif = np.abs(A - B).tolist()

U = (H + W) * 80
Um = U // 2

dp = [[[] for _ in range(W)] for _ in range(H)]
X = np.zeros(U + 1, dtype=bool)
X[Um + dif[0][0]] = True
X[Um - dif[0][0]] = True
dp[0][0] = X


for i in range(H):
    for j in range(W):
        if i == j == 0:
            continue
        d = dif[i][j]
        X = np.zeros(U + 1, dtype=bool)
        if d:
            if i > 0:
                X[d:] |= dp[i - 1][j][:-d]
                X[:-d] |= dp[i - 1][j][d:]
            if j > 0:
                X[d:] |= dp[i][j - 1][:-d]
                X[:-d] |= dp[i][j - 1][d:]
        else:
            if i > 0:
                X |= dp[i - 1][j]
            if j > 0:
                X |= dp[i][j - 1]
        dp[i][j] = X

tmp = np.arange(U+1)[dp[H-1][W-1]]
ans = np.abs(tmp - Um).min()
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
A = tuple(tuple(map(int, input().split())) for _ in range(H))
B = tuple(tuple(map(int, input().split())) for _ in range(H))

dp = dict()
c = A[0][0]-B[0][0]
dp[(0, 0)] = {c, -c}
lim = 160 * 80

for i in range(H):
    for j in range(W):
        if i == j == 0:
            continue
        c = abs(A[i][j]-B[i][j])
        d = set()
        if i > 0:
            d |= {x + c for x in dp[(i - 1, j)] if x + c < lim}
            d |= {x - c for x in dp[(i - 1, j)] if x - c > -lim}
        if j > 0:
            d |= {x + c for x in dp[(i, j - 1)] if x + c < lim}
            d |= {x - c for x in dp[(i, j - 1)] if x - c > -lim}
        dp[(i, j)] = d

ans = min(abs(x) for x in dp[H - 1, W - 1])
print(ans)

Fixed submission:
import numpy as np
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

H, W = map(int, input().split())
A = np.array([input().split() for _ in range(H)], dtype=int)
B = np.array([input().split() for _ in range(H)], dtype=int)
dif = np.abs(A - B).tolist()

U = (H + W) * 80
Um = U // 2

dp = [[[] for _ in range(W)] for _ in range(H)]
X = np.zeros(U + 1, dtype=bool)
X[Um + dif[0][0]] = True
X[Um - dif[0][0]] = True
dp[0][0] = X


for i in range(H):
    for j in range(W):
        if i == j == 0:
            continue
        d = dif[i][j]
        X = np.zeros(U + 1, dtype=bool)
        if d:
            if i > 0:
                X[d:] |= dp[i - 1][j][:-d]
                X[:-d] |= dp[i - 1][j][d:]
            if j > 0:
                X[d:] |= dp[i][j - 1][:-d]
                X[:-d] |= dp[i][j - 1][d:]
        else:
            if i > 0:
                X |= dp[i - 1][j]
            if j > 0:
                X |= dp[i][j - 1]
        dp[i][j] = X

tmp = np.arange(U+1)[dp[H-1][W-1]]
ans = np.abs(tmp - Um).min()
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
A = tuple(tuple(map(int, input().split())) for _ in range(H))
B = tuple(tuple(map(int, input().split())) for _ in range(H))

dp = [[set() for _ in range(W)] for _ in range(H)]
dp[0][0].add(A[0][0] - B[0][0])
dp[0][0].add(B[0][0] - A[0][0])
lim = 160 * 80 // 2

for i in range(H):
    for j in range(W):
        c = abs(A[i][j]-B[i][j])
        if i > 0:
            dp[i][j] |= {x + c for x in dp[i - 1][j] if x + c <= lim}
            dp[i][j] |= {x - c for x in dp[i - 1][j] if x - c >= -lim}
        if j > 0:
            dp[i][j] |= {x + c for x in dp[i][j - 1] if x + c <= lim}
            dp[i][j] |= {x - c for x in dp[i][j - 1] if x - c >= -lim}

ans = min(abs(x) for x in dp[-1][-1])
print(ans)

Fixed submission:
import numpy as np
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

H, W = map(int, input().split())
A = np.array([input().split() for _ in range(H)], dtype=int)
B = np.array([input().split() for _ in range(H)], dtype=int)
dif = np.abs(A - B).tolist()

U = (H + W) * 80
Um = U // 2

dp = [[[] for _ in range(W)] for _ in range(H)]
X = np.zeros(U + 1, dtype=bool)
X[Um + dif[0][0]] = True
X[Um - dif[0][0]] = True
dp[0][0] = X


for i in range(H):
    for j in range(W):
        if i == j == 0:
            continue
        d = dif[i][j]
        X = np.zeros(U + 1, dtype=bool)
        if d:
            if i > 0:
                X[d:] |= dp[i - 1][j][:-d]
                X[:-d] |= dp[i - 1][j][d:]
            if j > 0:
                X[d:] |= dp[i][j - 1][:-d]
                X[:-d] |= dp[i][j - 1][d:]
        else:
            if i > 0:
                X |= dp[i - 1][j]
            if j > 0:
                X |= dp[i][j - 1]
        dp[i][j] = X

tmp = np.arange(U+1)[dp[H-1][W-1]]
ans = np.abs(tmp - Um).min()
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
A = tuple(tuple(map(int, input().split())) for _ in range(H))
B = tuple(tuple(map(int, input().split())) for _ in range(H))

dp = [[set() for _ in range(W)] for _ in range(H)]
dp[0][0].add(A[0][0] - B[0][0])
dp[0][0].add(B[0][0] - A[0][0])
lim = 160 * 80 // 2

for i in range(H):
    for j in range(W):
        c = abs(A[i][j]-B[i][j])
        if i > 0:
            dp[i][j] |= {x + c for x in dp[i - 1][j] if x + c <= lim}
            dp[i][j] |= {x - c for x in dp[i - 1][j] if x - c >= -lim}
        if j > 0:
            dp[i][j] |= {x + c for x in dp[i][j - 1] if x + c <= lim}
            dp[i][j] |= {x - c for x in dp[i][j - 1] if x - c >= -lim}

ans = min(abs(x) for x in dp[-1][-1])
print(ans)

Fixed submission:
import numpy as np
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

H, W = map(int, input().split())
A = np.array([input().split() for _ in range(H)], dtype=int)
B = np.array([input().split() for _ in range(H)], dtype=int)
dif = np.abs(A - B).tolist()

U = (H + W) * 80
Um = U // 2

dp = [[[] for _ in range(W)] for _ in range(H)]
X = np.zeros(U + 1, dtype=bool)
X[Um + dif[0][0]] = True
X[Um - dif[0][0]] = True
dp[0][0] = X


for i in range(H):
    for j in range(W):
        if i == j == 0:
            continue
        d = dif[i][j]
        X = np.zeros(U + 1, dtype=bool)
        if d:
            if i > 0:
                X[d:] |= dp[i - 1][j][:-d]
                X[:-d] |= dp[i - 1][j][d:]
            if j > 0:
                X[d:] |= dp[i][j - 1][:-d]
                X[:-d] |= dp[i][j - 1][d:]
        else:
            if i > 0:
                X |= dp[i - 1][j]
            if j > 0:
                X |= dp[i][j - 1]
        dp[i][j] = X

tmp = np.arange(U+1)[dp[H-1][W-1]]
ans = np.abs(tmp - Um).min()
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
H, W = map(int, input().split())
A = [list(map(int, input().split()))+[0] for _ in range(H)]
B = [list(map(int, input().split()))+[0] for _ in range(H)]
A.append([0]*(W+1))
B.append([0]*(W+1))


dp = [[set([]) for _ in range(W+2)] for _ in range(H+2)]
diff = A[0][0] - B[0][0]
dp[0][0].add(diff)
dp[0][0].add(-diff)

# 配るDP
for h in range(H):
    for w in range(W):
        dif_r = abs(A[h + 1][w] - B[h + 1][w])
        dif_d = abs(A[h][w + 1] - B[h][w + 1])
        for val in dp[h][w]:
            if val + dif_r <= 80:
                dp[h+1][w].add(val+dif_r)
            if val - dif_r <= 80:
                dp[h+1][w].add(val-dif_r)
            if val + dif_d <= 80:
                dp[h][w+1].add(val+dif_d)
            if val - dif_d <= 80:
                dp[h][w+1].add(val-dif_d)

ans = 10 ** 9
for val in dp[H-1][W-1]:
    ans = min(ans, abs(val))

print(ans)


Fixed submission:
import numpy as np
import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)

H, W = map(int, input().split())
A = np.array([input().split() for _ in range(H)], dtype=int)
B = np.array([input().split() for _ in range(H)], dtype=int)
dif = np.abs(A - B).tolist()

U = (H + W) * 80
Um = U // 2

dp = [[[] for _ in range(W)] for _ in range(H)]
X = np.zeros(U + 1, dtype=bool)
X[Um + dif[0][0]] = True
X[Um - dif[0][0]] = True
dp[0][0] = X


for i in range(H):
    for j in range(W):
        if i == j == 0:
            continue
        d = dif[i][j]
        X = np.zeros(U + 1, dtype=bool)
        if d:
            if i > 0:
                X[d:] |= dp[i - 1][j][:-d]
                X[:-d] |= dp[i - 1][j][d:]
            if j > 0:
                X[d:] |= dp[i][j - 1][:-d]
                X[:-d] |= dp[i][j - 1][d:]
        else:
            if i > 0:
                X |= dp[i - 1][j]
            if j > 0:
                X |= dp[i][j - 1]
        dp[i][j] = X

tmp = np.arange(U+1)[dp[H-1][W-1]]
ans = np.abs(tmp - Um).min()
print(ans)