Buggy python submission (verdict: Time Limit Exceeded):
import sys
def main():
    input = sys.stdin.readline
    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)]

    mx=80*(H+W)
    C=[[abs(A[i][j]-B[i][j]) for j in range(W)] for i in range(H)]
    dp=[[0 for _ in range(W)] for __ in range(H)]
    dp[0][0]=1<<(mx+C[0][0]) | 1<<(mx-C[0][0])
    from collections import deque
    q=deque([(0,0)])
    while q:
        i,j=q.popleft()
        if i>0:
            dp[i][j] |= dp[i-1][j] << C[i][j]
            dp[i][j] |= dp[i-1][j] >> C[i][j]
        if j>0:
            dp[i][j] |= dp[i][j-1] << C[i][j]
            dp[i][j] |= dp[i][j-1] >> C[i][j]
        if i<H-1: q.append((i+1,j))
        if j<W-1: q.append((i,j+1))

    for ans in range(mx+1):
        if dp[H-1][W-1] & (1<<(mx+ans) | 1<<(mx-ans)):
            print(ans)
            break

if __name__ == '__main__':
    main()

Fixed submission:
import sys
def main():
    input = sys.stdin.readline
    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)]
    C = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]

    M = 160*80
    dp = [[0 for _ in range(W)] for __ in range(H)]
    dp[0][0] = 1 << (C[0][0] + M)
    for i in range(H):
        for j in range(W):
            for ni,nj in [(i,j+1),(i+1,j)]:
                if ni >= H or nj >= W: continue
                t = C[ni][nj]
                dp[ni][nj] |= dp[i][j] << t
                dp[ni][nj] |= dp[i][j] >> t
    
    a = dp[H-1][W-1]
    ans = 0
    while True:
        if a & (1 << (M+ans)) or a & (1 >> (M-ans)): break
        ans += 1
    print(ans)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
def main():
    input = sys.stdin.readline
    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)]

    mx=80*(80+80)
    C=[[A[i][j]-B[i][j] for j in range(W)] for i in range(H)]
    dp=[[[0]*(mx*2+1) for _ in range(W)] for __ in range(H)]
    dp[0][0][C[0][0]+mx]=1
    dp[0][0][-C[0][0]+mx]=1
    from collections import deque
    q=deque([(0,0)])
    while q:
        i,j=q.popleft()
        for d in range(80*(i+j+1)*2):
            d=d-80*(i+j+1)+mx
            if i>0:
                if 0<=d-C[i][j]<=mx*2: dp[i][j][d] = max(dp[i][j][d], dp[i-1][j][d-C[i][j]])
                if 0<=d+C[i][j]<=mx*2: dp[i][j][d] = max(dp[i][j][d], dp[i-1][j][d+C[i][j]])
            if j>0:
                if 0<=d-C[i][j]<=mx*2: dp[i][j][d] = max(dp[i][j][d], dp[i][j-1][d-C[i][j]])
                if 0<=d+C[i][j]<=mx*2: dp[i][j][d] = max(dp[i][j][d], dp[i][j-1][d+C[i][j]])
        if i<H-1: q.append((i+1,j))
        if j<W-1: q.append((i,j+1))

    for ans in range(mx+1):
        if dp[H-1][W-1][mx+ans] or dp[H-1][W-1][mx-ans]:
            print(ans)
            break

if __name__ == '__main__':
    main()

Fixed submission:
import sys
def main():
    input = sys.stdin.readline
    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)]
    C = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]

    M = 160*80
    dp = [[0 for _ in range(W)] for __ in range(H)]
    dp[0][0] = 1 << (C[0][0] + M)
    for i in range(H):
        for j in range(W):
            for ni,nj in [(i,j+1),(i+1,j)]:
                if ni >= H or nj >= W: continue
                t = C[ni][nj]
                dp[ni][nj] |= dp[i][j] << t
                dp[ni][nj] |= dp[i][j] >> t
    
    a = dp[H-1][W-1]
    ans = 0
    while True:
        if a & (1 << (M+ans)) or a & (1 >> (M-ans)): break
        ans += 1
    print(ans)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
def main():
    input = sys.stdin.readline
    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)]

    mx=80*(80+80)
    C=[[A[i][j]-B[i][j] for j in range(W)] for i in range(H)]
    dp=[[[0]*(mx*2+1) for _ in range(W)] for __ in range(H)]
    dp[0][0][C[0][0]+mx]=1
    dp[0][0][-C[0][0]+mx]=1
    from collections import deque
    q=deque([(0,0)])
    while q:
        i,j=q.popleft()
        for d in range(mx*2):
            if i>0:
                if 0<=d-C[i][j]<=mx*2: dp[i][j][d] = max(dp[i][j][d], dp[i-1][j][d-C[i][j]])
                if 0<=d+C[i][j]<=mx*2: dp[i][j][d] = max(dp[i][j][d], dp[i-1][j][d+C[i][j]])
            if j>0:
                if 0<=d-C[i][j]<=mx*2: dp[i][j][d] = max(dp[i][j][d], dp[i][j-1][d-C[i][j]])
                if 0<=d+C[i][j]<=mx*2: dp[i][j][d] = max(dp[i][j][d], dp[i][j-1][d+C[i][j]])
        if i<H-1: q.append((i+1,j))
        if j<W-1: q.append((i,j+1))

    for ans in range(mx+1):
        if dp[H-1][W-1][mx+ans] or dp[H-1][W-1][mx-ans]:
            print(ans)
            break

if __name__ == '__main__':
    main()

Fixed submission:
import sys
def main():
    input = sys.stdin.readline
    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)]
    C = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]

    M = 160*80
    dp = [[0 for _ in range(W)] for __ in range(H)]
    dp[0][0] = 1 << (C[0][0] + M)
    for i in range(H):
        for j in range(W):
            for ni,nj in [(i,j+1),(i+1,j)]:
                if ni >= H or nj >= W: continue
                t = C[ni][nj]
                dp[ni][nj] |= dp[i][j] << t
                dp[ni][nj] |= dp[i][j] >> t
    
    a = dp[H-1][W-1]
    ans = 0
    while True:
        if a & (1 << (M+ans)) or a & (1 >> (M-ans)): break
        ans += 1
    print(ans)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
def main():
    input = sys.stdin.readline
    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)]

    S = (80+80)*80//2
    dp = [[[0]*(S+1) for _ in range(W)] for __ in range(H)]
    dp[0][0][abs(A[0][0] - B[0][0])] = 1
    for i in range(H):
        for j in range(W):
            for s in range(S+1):
                for ni,nj in [(i,j+1),(i+1,j)]:
                    if ni >= H or nj >= W: continue
                    t = abs(A[ni][nj] - B[ni][nj])
                    if s+t <= S:
                        dp[ni][nj][s+t] |= dp[i][j][s]
                    dp[ni][nj][abs(s-t)] |= dp[i][j][s]
    for s in range(S+1):
        if dp[H-1][W-1][s]:
            print(s)
            break

if __name__ == '__main__':
    main()

Fixed submission:
import sys
def main():
    input = sys.stdin.readline
    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)]
    C = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]

    M = 160*80
    dp = [[0 for _ in range(W)] for __ in range(H)]
    dp[0][0] = 1 << (C[0][0] + M)
    for i in range(H):
        for j in range(W):
            for ni,nj in [(i,j+1),(i+1,j)]:
                if ni >= H or nj >= W: continue
                t = C[ni][nj]
                dp[ni][nj] |= dp[i][j] << t
                dp[ni][nj] |= dp[i][j] >> t
    
    a = dp[H-1][W-1]
    ans = 0
    while True:
        if a & (1 << (M+ans)) or a & (1 >> (M-ans)): break
        ans += 1
    print(ans)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
def main():
    input = sys.stdin.readline
    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)]

    S = (80+80)*80//2
    dp = [[[0]*(S+1) for _ in range(W)] for __ in range(H)]
    dp[0][0][abs(A[0][0] - B[0][0])] = 1
    for i in range(H):
        for j in range(W):
            for s in range(S+1):
                if i < H-1:
                    ni = i + 1
                    nj = j
                    t = abs(A[ni][nj] - B[ni][nj])
                    ns = s + t
                    if ns <= S:
                        dp[ni][nj][ns] |= dp[i][j][s]
                    ns = abs(s - t)
                    dp[ni][nj][ns] |= dp[i][j][s]
                if j < W-1:
                    ni = i
                    nj = j + 1
                    t = abs(A[ni][nj] - B[ni][nj])
                    ns = s + t
                    if ns <= S:
                        dp[ni][nj][ns] |= dp[i][j][s]
                    ns = abs(s - t)
                    dp[ni][nj][ns] |= dp[i][j][s]
    for s in range(S+1):
        if dp[H-1][W-1][s]:
            print(s)
            break

if __name__ == '__main__':
    main()

Fixed submission:
import sys
def main():
    input = sys.stdin.readline
    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)]
    C = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]

    M = 160*80
    dp = [[0 for _ in range(W)] for __ in range(H)]
    dp[0][0] = 1 << (C[0][0] + M)
    for i in range(H):
        for j in range(W):
            for ni,nj in [(i,j+1),(i+1,j)]:
                if ni >= H or nj >= W: continue
                t = C[ni][nj]
                dp[ni][nj] |= dp[i][j] << t
                dp[ni][nj] |= dp[i][j] >> t
    
    a = dp[H-1][W-1]
    ans = 0
    while True:
        if a & (1 << (M+ans)) or a & (1 >> (M-ans)): break
        ans += 1
    print(ans)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
def main():
    input = sys.stdin.readline
    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)]

    dp = [[set() for _ in range(W)] for __ in range(H)]
    dp[0][0].add(abs(A[0][0] - B[0][0]))
    for i in range(H):
        for j in range(W):
            for s in dp[i][j]:
                for ni,nj in [(i,j+1),(i+1,j)]:
                    if ni >= H or nj >= W: continue
                    t = abs(A[ni][nj] - B[ni][nj])
                    if s+t <= 80*80:
                        dp[ni][nj].add(s + t)
                    dp[ni][nj].add(abs(s - t))
    print(min(dp[H-1][W-1]))

if __name__ == '__main__':
    main()

Fixed submission:
import sys
def main():
    input = sys.stdin.readline
    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)]
    C = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]

    M = 160*80
    dp = [[0 for _ in range(W)] for __ in range(H)]
    dp[0][0] = 1 << (C[0][0] + M)
    for i in range(H):
        for j in range(W):
            for ni,nj in [(i,j+1),(i+1,j)]:
                if ni >= H or nj >= W: continue
                t = C[ni][nj]
                dp[ni][nj] |= dp[i][j] << t
                dp[ni][nj] |= dp[i][j] >> t
    
    a = dp[H-1][W-1]
    ans = 0
    while True:
        if a & (1 << (M+ans)) or a & (1 >> (M-ans)): break
        ans += 1
    print(ans)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
def main():
    input = sys.stdin.readline
    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)]

    S = (80+80)*80
    dp = [[[0]*(S+1) for _ in range(W)] for __ in range(H)]
    dp[0][0][abs(A[0][0] - B[0][0])] = 1
    for i in range(H):
        for j in range(W):
            for s in range((i+j+1)*80+1):
                if i < H-1:
                    ni = i + 1
                    nj = j
                    t = abs(A[ni][nj] - B[ni][nj])
                    ns = s + t
                    if ns <= S:
                        dp[ni][nj][ns] |= dp[i][j][s]
                    ns = abs(s - t)
                    dp[ni][nj][ns] |= dp[i][j][s]
                if j < W-1:
                    ni = i
                    nj = j + 1
                    t = abs(A[ni][nj] - B[ni][nj])
                    ns = s + t
                    if ns <= S:
                        dp[ni][nj][ns] |= dp[i][j][s]
                    ns = abs(s - t)
                    dp[ni][nj][ns] |= dp[i][j][s]
    for s in range(S+1):
        if dp[H-1][W-1][s]:
            print(s)
            break

if __name__ == '__main__':
    main()

Fixed submission:
import sys
def main():
    input = sys.stdin.readline
    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)]
    C = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]

    M = 160*80
    dp = [[0 for _ in range(W)] for __ in range(H)]
    dp[0][0] = 1 << (C[0][0] + M)
    for i in range(H):
        for j in range(W):
            for ni,nj in [(i,j+1),(i+1,j)]:
                if ni >= H or nj >= W: continue
                t = C[ni][nj]
                dp[ni][nj] |= dp[i][j] << t
                dp[ni][nj] |= dp[i][j] >> t
    
    a = dp[H-1][W-1]
    ans = 0
    while True:
        if a & (1 << (M+ans)) or a & (1 >> (M-ans)): break
        ans += 1
    print(ans)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
def main():
    input = sys.stdin.readline
    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)]

    S = (80+80)*80
    dp = [[[0]*(S+1) for _ in range(W)] for __ in range(H)]
    dp[0][0][abs(A[0][0] - B[0][0])] = 1
    for i in range(H):
        for j in range(W):
            for s in range(S+1):
                if i < H-1:
                    ni = i + 1
                    nj = j
                    t = abs(A[ni][nj] - B[ni][nj])
                    ns = s + t
                    if ns <= S:
                        dp[ni][nj][ns] |= dp[i][j][s]
                    ns = abs(s - t)
                    dp[ni][nj][ns] |= dp[i][j][s]
                if j < W-1:
                    ni = i
                    nj = j + 1
                    t = abs(A[ni][nj] - B[ni][nj])
                    ns = s + t
                    if ns <= S:
                        dp[ni][nj][ns] |= dp[i][j][s]
                    ns = abs(s - t)
                    dp[ni][nj][ns] |= dp[i][j][s]
    for s in range(S+1):
        if dp[H-1][W-1][s]:
            print(s)
            break

if __name__ == '__main__':
    main()

Fixed submission:
import sys
def main():
    input = sys.stdin.readline
    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)]
    C = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]

    M = 160*80
    dp = [[0 for _ in range(W)] for __ in range(H)]
    dp[0][0] = 1 << (C[0][0] + M)
    for i in range(H):
        for j in range(W):
            for ni,nj in [(i,j+1),(i+1,j)]:
                if ni >= H or nj >= W: continue
                t = C[ni][nj]
                dp[ni][nj] |= dp[i][j] << t
                dp[ni][nj] |= dp[i][j] >> t
    
    a = dp[H-1][W-1]
    ans = 0
    while True:
        if a & (1 << (M+ans)) or a & (1 >> (M-ans)): break
        ans += 1
    print(ans)

if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
import sys
def main():
    input = sys.stdin.readline
    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)]

    dp = [[set() for _ in range(W)] for __ in range(H)]
    dp[0][0].add(abs(A[0][0] - B[0][0]))
    for i in range(H):
        for j in range(W):
            for s in dp[i][j]:
                for ni,nj in [(i,j+1),(i+1,j)]:
                    if ni >= H or nj >= W: continue
                    dp[ni][nj].add(s + abs(A[ni][nj] - B[ni][nj]))
                    dp[ni][nj].add(abs(s - abs(A[ni][nj] - B[ni][nj])))
    print(min(dp[H-1][W-1]))

if __name__ == '__main__':
    main()

Fixed submission:
import sys
def main():
    input = sys.stdin.readline
    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)]
    C = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]

    M = 160*80
    dp = [[0 for _ in range(W)] for __ in range(H)]
    dp[0][0] = 1 << (C[0][0] + M)
    for i in range(H):
        for j in range(W):
            for ni,nj in [(i,j+1),(i+1,j)]:
                if ni >= H or nj >= W: continue
                t = C[ni][nj]
                dp[ni][nj] |= dp[i][j] << t
                dp[ni][nj] |= dp[i][j] >> t
    
    a = dp[H-1][W-1]
    ans = 0
    while True:
        if a & (1 << (M+ans)) or a & (1 >> (M-ans)): break
        ans += 1
    print(ans)

if __name__ == '__main__':
    main()