Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys

    H, W = map(int, sys.stdin.readline().split())
    A = [list(map(int, sys.stdin.readline().split())) for i in range(H)]
    B = [list(map(int, sys.stdin.readline().split())) for i in range(H)]
    X = [[0] * W for i in range(H)]
    for h in range(H):
        for w in range(W):
            X[h][w] = abs(A[h][w] - B[h][w])

    dp = {}
    dp[0] = set()
    dp[0].add(X[0][0])
    dp[0].add(-X[0][0])

    for h in range(H):
        for w in range(W):
            if h == 0 and w == 0:
                continue
            x = X[h][w]
            s = set()

            threshold = ((H - h) + (W - w)) * 79
            if W * h + w > W - 1:
                for d in dp[W * (h - 1) + w]:

                    if d + x <= threshold:
                        s.add(d + x)
                    if d - x >= -threshold:
                        s.add(d - x)

            if (W * h + w) % W != 0:
                for d in dp[W * h + w - 1]:
                    if d + x <= threshold:
                        s.add(d + x)
                    if d - x >= -threshold:
                        s.add(d - x)

            dp[W * h + w] = s

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


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    import sys

    H, W = map(int, sys.stdin.readline().split())
    A = [list(map(int, sys.stdin.readline().split())) for i in range(H)]
    B = [list(map(int, sys.stdin.readline().split())) for i in range(H)]
    X = [[0] * W for i in range(H)]
    for h in range(H):
        for w in range(W):
            X[h][w] = abs(A[h][w] - B[h][w])

    dp = [[set() for j in range(W)] for i in range(H)]
    dp[0][0].add(X[0][0])
    dp[0][0].add(-X[0][0])

    for h in range(H):
        for w in range(W):
            if h == 0 and w == 0:
                continue
            x = X[h][w]
            s = set()

            if h > 0:
                for d in dp[h - 1][w]:
                    s.add(d + x)
                    s.add(abs(d - x))

            if w > 0:
                for d in dp[h][w - 1]:
                    s.add(d + x)
                    s.add(abs(d - x))

            dp[h][w] = s

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


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys

    H, W = map(int, sys.stdin.readline().split())
    A = [list(map(int, sys.stdin.readline().split())) for i in range(H)]
    B = [list(map(int, sys.stdin.readline().split())) for i in range(H)]
    X = [[None] * W for i in range(H)]
    for h in range(H):
        for w in range(W):
            X[h][w] = abs(A[h][w] - B[h][w])

    dp = {}
    dp[0] = set()
    dp[0].add(X[0][0])
    dp[0].add(-X[0][0])

    for h in range(H):
        for w in range(W):
            if h == 0 and w == 0:
                continue
            x = X[h][w]
            s = set()

            threshold = ((H - h) + (W - w)) * 79
            if W * h + w > W - 1:
                for d in dp[W * (h - 1) + w]:
                    
                    if d + x <= threshold:
                        s.add(d + x)
                    if d - x >= -threshold:
                        s.add(d - x)

            if (W * h + w) % W != 0:
                for d in dp[W * h + w - 1]:
                    if d + x <= threshold:
                        s.add(d + x)
                    if d - x >= -threshold:
                        s.add(d - x)

            dp[W * h + w] = s

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


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    import sys

    H, W = map(int, sys.stdin.readline().split())
    A = [list(map(int, sys.stdin.readline().split())) for i in range(H)]
    B = [list(map(int, sys.stdin.readline().split())) for i in range(H)]
    X = [[0] * W for i in range(H)]
    for h in range(H):
        for w in range(W):
            X[h][w] = abs(A[h][w] - B[h][w])

    dp = [[set() for j in range(W)] for i in range(H)]
    dp[0][0].add(X[0][0])
    dp[0][0].add(-X[0][0])

    for h in range(H):
        for w in range(W):
            if h == 0 and w == 0:
                continue
            x = X[h][w]
            s = set()

            if h > 0:
                for d in dp[h - 1][w]:
                    s.add(d + x)
                    s.add(abs(d - x))

            if w > 0:
                for d in dp[h][w - 1]:
                    s.add(d + x)
                    s.add(abs(d - x))

            dp[h][w] = s

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


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys

    H, W = map(int, sys.stdin.readline().split())
    A = [list(map(int, sys.stdin.readline().split())) for i in range(H)]
    B = [list(map(int, sys.stdin.readline().split())) for i in range(H)]
    X = [[None] * W for i in range(H)]
    for h in range(H):
        for w in range(W):
            X[h][w] = abs(A[h][w] - B[h][w])

    dp = {}
    dp[0] = set()
    dp[0].add(X[0][0])
    dp[0].add(-X[0][0])

    for h in range(H):
        for w in range(W):
            if h == 0 and w == 0:
                continue
            x = X[h][w]
            s = set()

            if W * h + w > W - 1:
                for d in dp[W * (h - 1) + w]:
                    if d + x <= ((H - h) + (W - w)) * 79:
                        s.add(d + x)
                    if d - x >= -((H - h) + (W - w)) * 79:
                        s.add(d - x)

            if (W * h + w) % W != 0:
                for d in dp[W * h + w - 1]:
                    if d + x <= ((H - h) + (W - w)) * 79:
                        s.add(d + x)
                    if d - x >= -((H - h) + (W - w)) * 79:
                        s.add(d - x)

            dp[W * h + w] = s

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


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    import sys

    H, W = map(int, sys.stdin.readline().split())
    A = [list(map(int, sys.stdin.readline().split())) for i in range(H)]
    B = [list(map(int, sys.stdin.readline().split())) for i in range(H)]
    X = [[0] * W for i in range(H)]
    for h in range(H):
        for w in range(W):
            X[h][w] = abs(A[h][w] - B[h][w])

    dp = [[set() for j in range(W)] for i in range(H)]
    dp[0][0].add(X[0][0])
    dp[0][0].add(-X[0][0])

    for h in range(H):
        for w in range(W):
            if h == 0 and w == 0:
                continue
            x = X[h][w]
            s = set()

            if h > 0:
                for d in dp[h - 1][w]:
                    s.add(d + x)
                    s.add(abs(d - x))

            if w > 0:
                for d in dp[h][w - 1]:
                    s.add(d + x)
                    s.add(abs(d - x))

            dp[h][w] = s

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


if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    import sys
    from collections import defaultdict

    H, W = map(int, sys.stdin.readline().split())
    A = [list(map(int, sys.stdin.readline().split())) for i in range(H)]
    B = [list(map(int, sys.stdin.readline().split())) for i in range(H)]
    X = [[None] * W for i in range(H)]
    for h in range(H):
        for w in range(W):
            X[h][w] = abs(A[h][w] - B[h][w])

    dp = defaultdict(set)
    dp[(0, 0)].add(X[0][0])
    dp[(0, 0)].add(-X[0][0])

    for h in range(H):
        for w in range(W):
            if h == 0 and w == 0:
                continue
            x = X[h][w]
            s = set()
            if h > 0:
                for d in dp[(h - 1, w)]:
                    s.add(d + x)
                    s.add(d - x)

            if w > 0:
                for d in dp[(h, w - 1)]:
                    s.add(d + x)
                    s.add(d - x)

            dp[(h, w)] = s

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


if __name__ == '__main__':
    main()


Fixed submission:
def main():
    import sys

    H, W = map(int, sys.stdin.readline().split())
    A = [list(map(int, sys.stdin.readline().split())) for i in range(H)]
    B = [list(map(int, sys.stdin.readline().split())) for i in range(H)]
    X = [[0] * W for i in range(H)]
    for h in range(H):
        for w in range(W):
            X[h][w] = abs(A[h][w] - B[h][w])

    dp = [[set() for j in range(W)] for i in range(H)]
    dp[0][0].add(X[0][0])
    dp[0][0].add(-X[0][0])

    for h in range(H):
        for w in range(W):
            if h == 0 and w == 0:
                continue
            x = X[h][w]
            s = set()

            if h > 0:
                for d in dp[h - 1][w]:
                    s.add(d + x)
                    s.add(abs(d - x))

            if w > 0:
                for d in dp[h][w - 1]:
                    s.add(d + x)
                    s.add(abs(d - x))

            dp[h][w] = s

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


if __name__ == '__main__':
    main()

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


X = [[0] * W for i in range(H)]
for h in range(H):
    for w in range(W):
        X[h][w] = abs(A[h][w] - B[h][w])


# dp[h][w][x] := (h, w)の時点で差をxにできるか？
M = 12800
dp = [[[False] * (M + 1) for j in range(W)] for i in range(H)]
dp[0][0][X[0][0]] = True


for h in range(H):
    for w in range(W):
        for m in range(M):
            if not dp[h][w][m]:
                continue

            x = X[h][w]
            if h + 1 < H and abs(m + X[h + 1][w]) <= M:
                dp[h + 1][w][abs(m + X[h + 1][w])] = True

            if h + 1 < H and abs(m - X[h + 1][w]) <= M:
                dp[h + 1][w][abs(m - X[h + 1][w])] = True

            if (w + 1 < W) and abs(m + X[h][w + 1]) <= M:
                dp[h][w + 1][abs(m + X[h][w + 1])] = True

            if (w + 1 < W) and abs(m - X[h][w + 1]) <= M:
                dp[h][w + 1][abs(m - X[h][w + 1])] = True


ans = float('inf')
for x in range(M + 1):
    if dp[H - 1][W - 1][x]:
        ans = min(ans, x)

print(ans)


Fixed submission:
def main():
    import sys

    H, W = map(int, sys.stdin.readline().split())
    A = [list(map(int, sys.stdin.readline().split())) for i in range(H)]
    B = [list(map(int, sys.stdin.readline().split())) for i in range(H)]
    X = [[0] * W for i in range(H)]
    for h in range(H):
        for w in range(W):
            X[h][w] = abs(A[h][w] - B[h][w])

    dp = [[set() for j in range(W)] for i in range(H)]
    dp[0][0].add(X[0][0])
    dp[0][0].add(-X[0][0])

    for h in range(H):
        for w in range(W):
            if h == 0 and w == 0:
                continue
            x = X[h][w]
            s = set()

            if h > 0:
                for d in dp[h - 1][w]:
                    s.add(d + x)
                    s.add(abs(d - x))

            if w > 0:
                for d in dp[h][w - 1]:
                    s.add(d + x)
                    s.add(abs(d - x))

            dp[h][w] = s

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


if __name__ == '__main__':
    main()

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


X = [[0] * W for i in range(H)]
for h in range(H):
    for w in range(W):
        X[h][w] = abs(A[h][w] - B[h][w])


# dp[h][w][x] := (h, w)の時点で差をxにできるか？
M = 3200
dp = [[[False] * (M + 1) for j in range(W)] for i in range(H)]
dp[0][0][X[0][0]] = True


for h in range(H):
    for w in range(W):
        for m in range(M):
            x = X[h][w]
            if (0 <= h - 1) and abs(m + X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h - 1][w][abs(m + x)]

            if (0 <= h - 1) and abs(m - X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h - 1][w][abs(m - x)]

            if (0 <= w - 1) and abs(m + X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h][w - 1][abs(m + x)]

            if (0 <= w - 1) and abs(m + X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h][w - 1][abs(m - x)]


ans = float('inf')
for x in range(M + 1):
    if dp[H - 1][W - 1][x]:
        ans = min(ans, x)

print(ans)


Fixed submission:
def main():
    import sys

    H, W = map(int, sys.stdin.readline().split())
    A = [list(map(int, sys.stdin.readline().split())) for i in range(H)]
    B = [list(map(int, sys.stdin.readline().split())) for i in range(H)]
    X = [[0] * W for i in range(H)]
    for h in range(H):
        for w in range(W):
            X[h][w] = abs(A[h][w] - B[h][w])

    dp = [[set() for j in range(W)] for i in range(H)]
    dp[0][0].add(X[0][0])
    dp[0][0].add(-X[0][0])

    for h in range(H):
        for w in range(W):
            if h == 0 and w == 0:
                continue
            x = X[h][w]
            s = set()

            if h > 0:
                for d in dp[h - 1][w]:
                    s.add(d + x)
                    s.add(abs(d - x))

            if w > 0:
                for d in dp[h][w - 1]:
                    s.add(d + x)
                    s.add(abs(d - x))

            dp[h][w] = s

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


if __name__ == '__main__':
    main()

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


X = [[0] * W for i in range(H)]
for h in range(H):
    for w in range(W):
        X[h][w] = abs(A[h][w] - B[h][w])


# dp[h][w][x] := (h, w)の時点で差をxにできるか？
M = max(H, W) * 80
dp = [[[False] * (M + 1) for j in range(W)] for i in range(H)]
dp[0][0][X[0][0]] = True


for h in range(H):
    for w in range(W):
        for m in range(M):
            nx = X[h][w]
            if (0 <= h - 1) and abs(m + X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h - 1][w][abs(m + X[h][w])]

            if (0 <= h - 1) and abs(m - X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h - 1][w][abs(m - X[h][w])]

            if (0 <= w - 1) and abs(m + X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h][w - 1][abs(m + X[h][w])]

            if (0 <= w - 1) and abs(m + X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h][w - 1][abs(m - X[h][w])]


ans = float('inf')
for x in range(M + 1):
    if dp[H - 1][W - 1][x]:
        ans = min(ans, x)

print(ans)


Fixed submission:
def main():
    import sys

    H, W = map(int, sys.stdin.readline().split())
    A = [list(map(int, sys.stdin.readline().split())) for i in range(H)]
    B = [list(map(int, sys.stdin.readline().split())) for i in range(H)]
    X = [[0] * W for i in range(H)]
    for h in range(H):
        for w in range(W):
            X[h][w] = abs(A[h][w] - B[h][w])

    dp = [[set() for j in range(W)] for i in range(H)]
    dp[0][0].add(X[0][0])
    dp[0][0].add(-X[0][0])

    for h in range(H):
        for w in range(W):
            if h == 0 and w == 0:
                continue
            x = X[h][w]
            s = set()

            if h > 0:
                for d in dp[h - 1][w]:
                    s.add(d + x)
                    s.add(abs(d - x))

            if w > 0:
                for d in dp[h][w - 1]:
                    s.add(d + x)
                    s.add(abs(d - x))

            dp[h][w] = s

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


if __name__ == '__main__':
    main()

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


X = [[0] * W for i in range(H)]
for h in range(H):
    for w in range(W):
        X[h][w] = abs(A[h][w] - B[h][w])


# dp[h][w][x] := (h, w)の時点で差をxにできるか？
M = H * W * 80
dp = [[[False] * (M + 1) for j in range(W)] for i in range(H)]
dp[0][0][X[0][0]] = True


for h in range(H):
    for w in range(W):
        for m in range(M):
            nx = X[h][w]
            if (0 <= h - 1) and abs(m + X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h - 1][w][abs(m + X[h][w])]

            if (0 <= h - 1) and abs(m - X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h - 1][w][abs(m - X[h][w])]

            if (0 <= w - 1) and abs(m + X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h][w - 1][abs(m + X[h][w])]

            if (0 <= w - 1) and abs(m + X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h][w - 1][abs(m - X[h][w])]


ans = float('inf')
for x in range(M + 1):
    if dp[H - 1][W - 1][x]:
        ans = min(ans, x)

print(ans)


Fixed submission:
def main():
    import sys

    H, W = map(int, sys.stdin.readline().split())
    A = [list(map(int, sys.stdin.readline().split())) for i in range(H)]
    B = [list(map(int, sys.stdin.readline().split())) for i in range(H)]
    X = [[0] * W for i in range(H)]
    for h in range(H):
        for w in range(W):
            X[h][w] = abs(A[h][w] - B[h][w])

    dp = [[set() for j in range(W)] for i in range(H)]
    dp[0][0].add(X[0][0])
    dp[0][0].add(-X[0][0])

    for h in range(H):
        for w in range(W):
            if h == 0 and w == 0:
                continue
            x = X[h][w]
            s = set()

            if h > 0:
                for d in dp[h - 1][w]:
                    s.add(d + x)
                    s.add(abs(d - x))

            if w > 0:
                for d in dp[h][w - 1]:
                    s.add(d + x)
                    s.add(abs(d - x))

            dp[h][w] = s

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


if __name__ == '__main__':
    main()