Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

def solve():
    maxV = 12800

    H, W = map(int, input().split())
    Ass = [tuple(map(int, input().split())) for _ in range(H)]
    Bss = [tuple(map(int, input().split())) for _ in range(H)]

    dp = [[[0]*(2*maxV+1) for j in range(W)] for i in range(H)]
    diff = abs(Ass[0][0] - Bss[0][0])
    dp[0][0][diff+maxV] = dp[0][0][-diff+maxV] = 1
    for i in range(H):
        for j in range(W):
            diff = abs(Ass[i][j] - Bss[i][j])
            if i > 0:
                dp0 = dp[i-1][j]
                dpij = dp[i][j]
                for k in range(diff, 2*maxV+1):
                    if dp0[k]:
                        dpij[k-diff] = 1
                for k in range(2*maxV+1-diff):
                    if dp0[k]:
                        dpij[k+diff] = 1
            if j > 0:
                dp0 = dp[i][j-1]
                dpij = dp[i][j]
                for k in range(diff, 2*maxV+1):
                    if dp0[k]:
                        dpij[k-diff] = 1
                for k in range(2*maxV+1-diff):
                    if dp0[k]:
                        dpij[k+diff] = 1

    ans = maxV
    dpHW = dp[-1][-1]
    for k in range(2*maxV+1):
        if dpHW[k]:
            j = abs(k-maxV)
            if j < ans:
                ans = j

    print(ans)


solve()


Fixed submission:
maxV = 80*(80+80)

H, W = map(int, input().split())
Ass = [tuple(map(int, input().split())) for _ in range(H)]
Bss = [tuple(map(int, input().split())) for _ in range(H)]

dp = [[0 for j in range(W)] for i in range(H)]

for i in range(H):
    for j in range(W):
        A, B = Ass[i][j], Bss[i][j]
        if A > B:
            A, B = B, A
        if i == 0 and j == 0:
            dp[i][j] = 1<<(maxV+B-A) | 1<<(maxV-B+A)
        if i > 0:
            dp[i][j] |= dp[i-1][j]<<(B-A) | dp[i-1][j]>>(B-A)
        if j > 0:
            dp[i][j] |= dp[i][j-1]<<(B-A) | dp[i][j-1]>>(B-A)

bitset = dp[-1][-1]
mask = 1<<(2*maxV)
ans = maxV
for i in range(-maxV, maxV+1):
    if bitset & mask:
        ans = min(ans, abs(i))
    mask >>= 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

def solve():
    maxV = 12800

    H, W = map(int, input().split())
    Ass = [tuple(map(int, input().split())) for _ in range(H)]
    Bss = [tuple(map(int, input().split())) for _ in range(H)]

    dp = [[[0]*(2*maxV+1) for j in range(W)] for i in range(H)]
    diff = abs(Ass[0][0] - Bss[0][0])
    dp[0][0][diff+maxV] = dp[0][0][-diff+maxV] = 1
    for i in range(H):
        for j in range(W):
            diff = abs(Ass[i][j] - Bss[i][j])
            if i > 0:
                dp0 = dp[i-1][j]
                dpij = dp[i][j]
                for k in range(diff, 2*maxV+1):
                    if dp0[k]:
                        dpij[k-diff] = 1
                for k in range(2*maxV+1-diff):
                    if dp0[k]:
                        dpij[k+diff] = 1
            if j > 0:
                dp0 = dp[i][j-1]
                dpij = dp[i][j]
                for k in range(diff, 2*maxV+1):
                    if dp0[k]:
                        dpij[k-diff] = 1
                for k in range(2*maxV+1-diff):
                    if dp0[k]:
                        dpij[k+diff] = 1

    ans = maxV
    dpHW = dp[-1][-1]
    for k in range(2*maxV+1):
        if dpHW[k]:
            j = abs(k-maxV)
            if j < ans:
                ans = j

    print(ans)


solve()


Fixed submission:
maxV = 80*(80+80)

H, W = map(int, input().split())
Ass = [tuple(map(int, input().split())) for _ in range(H)]
Bss = [tuple(map(int, input().split())) for _ in range(H)]

dp = [[0 for j in range(W)] for i in range(H)]

for i in range(H):
    for j in range(W):
        A, B = Ass[i][j], Bss[i][j]
        if A > B:
            A, B = B, A
        if i == 0 and j == 0:
            dp[i][j] = 1<<(maxV+B-A) | 1<<(maxV-B+A)
        if i > 0:
            dp[i][j] |= dp[i-1][j]<<(B-A) | dp[i-1][j]>>(B-A)
        if j > 0:
            dp[i][j] |= dp[i][j-1]<<(B-A) | dp[i][j-1]>>(B-A)

bitset = dp[-1][-1]
mask = 1<<(2*maxV)
ans = maxV
for i in range(-maxV, maxV+1):
    if bitset & mask:
        ans = min(ans, abs(i))
    mask >>= 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

def solve():
    H, W = map(int, input().split())
    Ass = [tuple(map(int, input().split())) for _ in range(H)]
    Bss = [tuple(map(int, input().split())) for _ in range(H)]

    dp1 = [[set() for j in range(W)] for i in range(H)]
    dp2 = [[set() for j in range(W)] for i in range(H)]

    dist = H+W-3
    d1, d2 = (dist+1)//2, dist//2
#    print('d1:', d1, '/ d2:', d2)

    A, B = Ass[0][0], Bss[0][0]
    dp1[0][0] = set([abs(A-B)])
    for d in range(d1):
#        print('d:', d)
        for i in range(d+1):
            j = d-i
#            print((i, j))
            if 0 <= i+1 <= H-1 and 0 <= j <= W-1:
                A, B = Ass[i+1][j], Bss[i+1][j]
                for diff in dp1[i][j]:
                    dp1[i+1][j].add(abs(diff+A-B))
                    dp1[i+1][j].add(abs(diff+B-A))
            if 0 <= i <= H-1 and 0 <= j+1 <= W-1:
                A, B = Ass[i][j+1], Bss[i][j+1]
                for diff in dp1[i][j]:
                    dp1[i][j+1].add(abs(diff+A-B))
                    dp1[i][j+1].add(abs(diff+B-A))
#    for dpi in dp1:
#        print(dpi)
#    print()

    A, B = Ass[-1][-1], Bss[-1][-1]
    dp2[-1][-1] = set([abs(A-B)])
    for d in range(d2):
        for i0 in range(d+1):
            j0 = d-i0
            i, j = H-1-i0, W-1-j0
            if 0 <= i-1 <= H-1 and 0 <= j <= W-1:
                A, B = Ass[i-1][j], Bss[i-1][j]
                for diff in dp2[i][j]:
                    dp2[i-1][j].add(abs(diff+A-B))
                    dp2[i-1][j].add(abs(diff+B-A))
            if 0 <= i <= H-1 and 0 <= j-1 <= W-1:
                A, B = Ass[i][j-1], Bss[i][j-1]
                for diff in dp2[i][j]:
                    dp2[i][j-1].add(abs(diff+A-B))
                    dp2[i][j-1].add(abs(diff+B-A))
#    for dpi in dp2:
#        print(dpi)
#    print()

    ans = float('inf')
    d = d1
    for i in range(d+1):
        j = d-i
        if 0 <= i+1 <= H-1 and 0 <= j <= W-1:
            for diff1 in dp1[i][j]:
                for diff2 in dp2[i+1][j]:
                    diff = abs(diff1-diff2)
                    ans = min(ans, diff)
        if 0 <= i <= H-1 and 0 <= j+1 <= W-1:
            for diff1 in dp1[i][j]:
                for diff2 in dp2[i][j+1]:
                    diff = abs(diff1-diff2)
                    ans = min(ans, diff)

    print(ans)


solve()


Fixed submission:
maxV = 80*(80+80)

H, W = map(int, input().split())
Ass = [tuple(map(int, input().split())) for _ in range(H)]
Bss = [tuple(map(int, input().split())) for _ in range(H)]

dp = [[0 for j in range(W)] for i in range(H)]

for i in range(H):
    for j in range(W):
        A, B = Ass[i][j], Bss[i][j]
        if A > B:
            A, B = B, A
        if i == 0 and j == 0:
            dp[i][j] = 1<<(maxV+B-A) | 1<<(maxV-B+A)
        if i > 0:
            dp[i][j] |= dp[i-1][j]<<(B-A) | dp[i-1][j]>>(B-A)
        if j > 0:
            dp[i][j] |= dp[i][j-1]<<(B-A) | dp[i][j-1]>>(B-A)

bitset = dp[-1][-1]
mask = 1<<(2*maxV)
ans = maxV
for i in range(-maxV, maxV+1):
    if bitset & mask:
        ans = min(ans, abs(i))
    mask >>= 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

def solve():
    H, W = map(int, input().split())
    Ass = [tuple(map(int, input().split())) for _ in range(H)]
    Bss = [tuple(map(int, input().split())) for _ in range(H)]

    dp1 = [[set() for j in range(W)] for i in range(H)]
    dp2 = [[set() for j in range(W)] for i in range(H)]

    dist = H+W-3
    d1, d2 = (dist+1)//2, dist//2
#    print('d1:', d1, '/ d2:', d2)

    A, B = Ass[0][0], Bss[0][0]
    dp1[0][0] = set([abs(A-B)])
    for d in range(d1):
#        print('d:', d)
        for i in range(d+1):
            j = d-i
#            print((i, j))
            if 0 <= i+1 <= H-1 and 0 <= j <= W-1:
                A, B = Ass[i+1][j], Bss[i+1][j]
                for diff in dp1[i][j]:
                    dp1[i+1][j].add(abs(diff+A-B))
                    dp1[i+1][j].add(abs(diff+B-A))
            if 0 <= i <= H-1 and 0 <= j+1 <= W-1:
                A, B = Ass[i][j+1], Bss[i][j+1]
                for diff in dp1[i][j]:
                    dp1[i][j+1].add(abs(diff+A-B))
                    dp1[i][j+1].add(abs(diff+B-A))
#    for dpi in dp1:
#        print(dpi)
#    print()

    A, B = Ass[-1][-1], Bss[-1][-1]
    dp2[-1][-1] = set([abs(A-B)])
    for d in range(d2):
        for i0 in range(d+1):
            j0 = d-i0
            i, j = H-1-i0, W-1-j0
            if 0 <= i-1 <= H-1 and 0 <= j <= W-1:
                A, B = Ass[i-1][j], Bss[i-1][j]
                for diff in dp2[i][j]:
                    dp2[i-1][j].add(abs(diff+A-B))
                    dp2[i-1][j].add(abs(diff+B-A))
            if 0 <= i <= H-1 and 0 <= j-1 <= W-1:
                A, B = Ass[i][j-1], Bss[i][j-1]
                for diff in dp2[i][j]:
                    dp2[i][j-1].add(abs(diff+A-B))
                    dp2[i][j-1].add(abs(diff+B-A))
#    for dpi in dp2:
#        print(dpi)
#    print()

    ans = float('inf')
    d = d1
    for i in range(d+1):
        j = d-i
        if 0 <= i+1 <= H-1 and 0 <= j <= W-1:
            for diff1 in dp1[i][j]:
                for diff2 in dp2[i+1][j]:
                    diff = abs(diff1-diff2)
                    ans = min(ans, diff)
        if 0 <= i <= H-1 and 0 <= j+1 <= W-1:
            for diff1 in dp1[i][j]:
                for diff2 in dp2[i][j+1]:
                    diff = abs(diff1-diff2)
                    ans = min(ans, diff)

    print(ans)


solve()


Fixed submission:
maxV = 80*(80+80)

H, W = map(int, input().split())
Ass = [tuple(map(int, input().split())) for _ in range(H)]
Bss = [tuple(map(int, input().split())) for _ in range(H)]

dp = [[0 for j in range(W)] for i in range(H)]

for i in range(H):
    for j in range(W):
        A, B = Ass[i][j], Bss[i][j]
        if A > B:
            A, B = B, A
        if i == 0 and j == 0:
            dp[i][j] = 1<<(maxV+B-A) | 1<<(maxV-B+A)
        if i > 0:
            dp[i][j] |= dp[i-1][j]<<(B-A) | dp[i-1][j]>>(B-A)
        if j > 0:
            dp[i][j] |= dp[i][j-1]<<(B-A) | dp[i][j-1]>>(B-A)

bitset = dp[-1][-1]
mask = 1<<(2*maxV)
ans = maxV
for i in range(-maxV, maxV+1):
    if bitset & mask:
        ans = min(ans, abs(i))
    mask >>= 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

def solve():
    H, W = map(int, input().split())
    Ass = [tuple(map(int, input().split())) for _ in range(H)]
    Bss = [tuple(map(int, input().split())) for _ in range(H)]

    dp = [[set() for j in range(W)] for i in range(H)]
    A, B = Ass[0][0], Bss[0][0]
    dp[0][0] = set([abs(A-B)])

    for i in range(H):
        for j in range(W):
            if i < H-1:
                A, B = Ass[i+1][j], Bss[i+1][j]
                for diff in dp[i][j]:
                    dp[i+1][j].add(abs(diff+A-B))
                    dp[i+1][j].add(abs(diff+B-A))
            if j < W-1:
                A, B = Ass[i][j+1], Bss[i][j+1]
                for diff in dp[i][j]:
                    dp[i][j+1].add(abs(diff+A-B))
                    dp[i][j+1].add(abs(diff+B-A))

#    for dpi in dp:
#        print(dpi)
    ans = min(dp[-1][-1])
    print(ans)


solve()


Fixed submission:
maxV = 80*(80+80)

H, W = map(int, input().split())
Ass = [tuple(map(int, input().split())) for _ in range(H)]
Bss = [tuple(map(int, input().split())) for _ in range(H)]

dp = [[0 for j in range(W)] for i in range(H)]

for i in range(H):
    for j in range(W):
        A, B = Ass[i][j], Bss[i][j]
        if A > B:
            A, B = B, A
        if i == 0 and j == 0:
            dp[i][j] = 1<<(maxV+B-A) | 1<<(maxV-B+A)
        if i > 0:
            dp[i][j] |= dp[i-1][j]<<(B-A) | dp[i-1][j]>>(B-A)
        if j > 0:
            dp[i][j] |= dp[i][j-1]<<(B-A) | dp[i][j-1]>>(B-A)

bitset = dp[-1][-1]
mask = 1<<(2*maxV)
ans = maxV
for i in range(-maxV, maxV+1):
    if bitset & mask:
        ans = min(ans, abs(i))
    mask >>= 1

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
input = sys.stdin.readline

def solve():
    H, W = map(int, input().split())
    Ass = [tuple(map(int, input().split())) for _ in range(H)]
    Bss = [tuple(map(int, input().split())) for _ in range(H)]

    dp = [[set() for j in range(W)] for i in range(H)]
    A, B = Ass[0][0], Bss[0][0]
    dp[0][0] = set([(A, B), (B, A)])

    for i in range(H):
        for j in range(W):
            if i < H-1:
                A, B = Ass[i+1][j], Bss[i+1][j]
                for x, y in dp[i][j]:
                    dp[i+1][j].add((x+A, y+B))
                    dp[i+1][j].add((x+B, y+A))
            if j < W-1:
                A, B = Ass[i][j+1], Bss[i][j+1]
                for x, y in dp[i][j]:
                    dp[i][j+1].add((x+A, y+B))
                    dp[i][j+1].add((x+B, y+A))

    ans = float('inf')
    for x, y in dp[-1][-1]:
        score = abs(x-y)
        ans = min(ans, score)

    #for dpi in dp:
    #    print(dpi)
    print(ans)


solve()


Fixed submission:
maxV = 80*(80+80)

H, W = map(int, input().split())
Ass = [tuple(map(int, input().split())) for _ in range(H)]
Bss = [tuple(map(int, input().split())) for _ in range(H)]

dp = [[0 for j in range(W)] for i in range(H)]

for i in range(H):
    for j in range(W):
        A, B = Ass[i][j], Bss[i][j]
        if A > B:
            A, B = B, A
        if i == 0 and j == 0:
            dp[i][j] = 1<<(maxV+B-A) | 1<<(maxV-B+A)
        if i > 0:
            dp[i][j] |= dp[i-1][j]<<(B-A) | dp[i-1][j]>>(B-A)
        if j > 0:
            dp[i][j] |= dp[i][j-1]<<(B-A) | dp[i][j-1]>>(B-A)

bitset = dp[-1][-1]
mask = 1<<(2*maxV)
ans = maxV
for i in range(-maxV, maxV+1):
    if bitset & mask:
        ans = min(ans, abs(i))
    mask >>= 1

print(ans)