Problem: We have a grid with H horizontal rows and W vertical columns. Let (i,j) denote the square at the i-th row from the top and the j-th column from the left.  The square (i, j) has two numbers A_{ij} and B_{ij} written on it.  First, for each square, Takahashi paints one of the written numbers red and the other blue.  Then, he travels from the square (1, 1) to the square (H, W). In one move, he can move from a square (i, j) to the square (i+1, j) or the square (i, j+1). He must not leave the grid.  Let the _unbalancedness_ be the absolute difference of the sum of red numbers and the sum of blue numbers written on the squares along Takahashi's path, including the squares (1, 1) and (H, W).  Takahashi wants to make the unbalancedness as small as possible by appropriately painting the grid and traveling on it.  Find the minimum unbalancedness possible.

Submission attempts (in order):
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)]

for w in range(W):
    for h in range(H):
        
        if w == 0 & h == 0:
            pv = set([0])
        else:
            if w:
                pvw = dp[h][w - 1]
            else:
                pvw = set()
            if h:
                pvh = dp[h - 1][w]
            else:
                pvh = set()
            pv = pvh | pvw

        diff = abs(A[h][w] - B[h][w])

        for c in pv:
            if c < (H + W) * 80:
                dp[h][w].add(c + diff)
                dp[h][w].add(abs(c - diff))
print(min(dp[H - 1][W - 1]))


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)]

for w in range(W):
    for h in range(H):
        if w:
            pvw = dp[h][w - 1]
        else:
            pvw = set()
        if h:
            pvh = dp[h - 1][w]
        else:
            pvh = set()

        pv = pvh | pvw
        if w == 0 & h == 0:
            pv = set([0])

        for c in pv:
            dp[h][w].add(c + abs(A[h][w] - B[h][w]))
            dp[h][w].add(abs(c - abs(A[h][w] - B[h][w])))
print(min(dp[H - 1][W - 1]))


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)]


def reverse_binary(n, width):
    return int("{:0{width}b}".format(n, width=width)[::-1], 2)


dp = [[0 for _ in range(W)] for _ in range(H)]
dp[0][0] = 1 << abs(A[0][0] - B[0][0])

for h in range(H):
    for w in range(W):
        diff = abs(A[h][w] - B[h][w])
        if w:
            dp[h][w] |= (reverse_binary(dp[h][w - 1] & ((1 << diff) - 1), diff)) << 1
            dp[h][w] |= dp[h][w - 1] >> diff
            dp[h][w] |= dp[h][w - 1] << diff
        if h:
            dp[h][w] |= (reverse_binary(dp[h - 1][w] & ((1 << diff) - 1), diff)) << 1
            dp[h][w] |= dp[h - 1][w] >> diff
            dp[h][w] |= dp[h - 1][w] << diff

tmp = dp[-1][-1]
ans = 0
while tmp & 1 == 0:
    tmp >>= 1
    ans += 1
print(ans)


Verdicts: Wrong Answer

Wrong Answer

Accepted