Problem p02839 (python)

Fixed submission:
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)]
D = [[abs(A[h][w]-B[h][w]) for w in range(W)] for h in range(H)]
def cal():
    dp1 = [set() for _ in range(W)]
    dp2 = [set() for _ in range(W)]
    for h in range(H):
        for w in range(W):
            d = D[h][w]
            if h==0 and w==0:
                dp2[0].add(d)
            for v in dp1[w]:
                v1 = v+d
                v2 = v-d if v>=d else -v+d
                dp2[w].add(v1)
                dp2[w].add(v2)
            if 0 < w:
                for v in dp2[w-1]:
                    v1 = v+d
                    v2 = v-d if v>=d else -v+d
                    dp2[w].add(v1)
                    dp2[w].add(v2)
        dp1 = dp2
        dp2 = [set() for _ in range(W)]
    return min(dp1[W-1])
ans = cal()
print(ans)

Buggy submission (verdict: Runtime Error):
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)]
M = 0
for h in range(H):
    for w in range(W):
        M = max(abs(A[h][w]-B[h][w]),M)
M *= H+W
dp = [[[False]*M for _ in range(W)] for _ in range(H)]

dp[0][0][abs(A[0][0]-B[0][0])] = True
for h in range(H-1):
    for w in range(W-1):
        v1 = abs(A[h+1][w]-B[h+1][w])
        v2 = abs(A[h][w+1]-B[h][w+1])
        for v in range(M):
            if dp[h][w][v]:
                dp[h+1][w][v1+v] = True
                dp[h+1][w][abs(v1-v)] = True
                dp[h][w+1][v2+v] = True
                dp[h][w+1][abs(v2-v)] = True

v0 = abs(A[H-1][W-1]-B[H-1][W-1])
for v in range(M):
    if dp[H-1][W-2][v]:
        dp[H-1][W-1][v0+v] = True
        dp[H-1][W-1][abs(v0-v)] = True
    if dp[H-2][W-1][v]:
        dp[H-1][W-1][v0+v] = True
        dp[H-1][W-1][abs(v0-v)] = True

for v in range(M):
    if dp[H-1][W-1][v]:
        ans = v
        break

print(ans)

Buggy submission (verdict: Runtime Error):
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)]
M = 0
for h in range(H):
    for w in range(W):
        M += abs(A[h][w]-B[h][w])
dp = [[[False]*M for _ in range(W)] for _ in range(H)]

dp[0][0][abs(A[0][0]-B[0][0])] = True
for h in range(H-1):
    for w in range(W-1):
        v1 = abs(A[h+1][w]-B[h+1][w])
        v2 = abs(A[h][w+1]-B[h][w+1])
        for v in range(M):
            if dp[h][w][v]:
                dp[h+1][w][v1+v] = True
                dp[h+1][w][abs(v1-v)] = True
                dp[h][w+1][v2+v] = True
                dp[h][w+1][abs(v2-v)] = True

v0 = abs(A[H-1][W-1]-B[H-1][W-1])
for v in range(M):
    if dp[H-1][W-2][v]:
        dp[H-1][W-1][v0+v] = True
        dp[H-1][W-1][abs(v0-v)] = True
    if dp[H-2][W-1][v]:
        dp[H-1][W-1][v0+v] = True
        dp[H-1][W-1][abs(v0-v)] = True

for v in range(M):
    if dp[H-1][W-1][v]:
        ans = v
        break

print(ans)

Buggy submission (verdict: Runtime Error):
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)]
dp1 = [[False]*((H+W)*80) for _ in range(W)]
dp2 = [[False]*((H+W)*80) for _ in range(W)]
for w in range(W):
    dp1[w][0] = True

for h in range(H):
    for w in range(W):
        va = A[h][w]
        vb = B[h][w]
        for v in range((H+W)*80):
            if dp1[w][v]:
                dp2[w][abs(v+va-vb)] = True
                dp2[w][abs(v-va+vb)] = True
        if w!=0:
            for v in range((H+W)*80):
                if dp2[w-1][v]:
                    dp2[w][abs(v+va-vb)] = True
                    dp2[w][abs(v-va+vb)] = True
    dp1 = dp2.copy()
    dp2 = [[False]*((H+W)*80) for _ in range(W)]

for v in range((H+W)*80):
    if dp1[W-1][v]:
        ans = v
        break

print(ans)