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):
for xy in range(h*w):
    i=xy//w
    j=xy%w
    z=a[i][j]-b[i][j]
    if i==0 and j==0:
        x[0].add(abs(a[0][0]-b[0][0]))
    elif i==0 and j!=0:
        for k in x[j-1]:
            if abs(k+z)<81: x[j].add(abs(k+z))
            elif abs(k-z)<81: x[j].add(abs(k-z))
    elif i%2:
        if j==0:
            for k in x[0]:
                if abs(k+z)<81: y[0].add(abs(k+z))
                if abs(k-z)<81: y[0].add(abs(k-z))
        else:
            for k in x[j]:
                if abs(k+z)<81: y[j].add(abs(k+z))
                if abs(k-z)<81: y[j].add(abs(k-z))
            for k in y[j-1]:
                if abs(k+z)<81: y[j].add(abs(k+z))
                if abs(k-z)<81: y[j].add(abs(k-z))
    else:
        if j==0:
            for k in y[0]:
                if abs(k+z)<81: x[0].add(abs(k+z))
                if abs(k-z)<81: x[0].add(abs(k-z))
        else:
            for k in y[j]:
                if abs(k+z)<81: x[j].add(abs(k+z))
                if abs(k-z)<81: x[j].add(abs(k-z))
            for k in x[j-1]:
                if abs(k+z)<81: x[j].add(abs(k+z))
                if abs(k-z)<81: x[j].add(abs(k-z))
    if j==w-1 and i%2==0:
        y=[set([]) for _ in range(w)]
        ans=min(x[-1])
    elif j==w-1:
        x=[set([]) for _ in range(w)]
        ans=min(y[-1])
print(ans)


h,w=map(int,input().split())
a=[[int(i) for i in input().split()] for i in range(h)]
b=[[int(i) for i in input().split()] for i in range(h)]
for i in range(h):
    if i==0:
        x=[set([]) for _ in range(w)]
    for j in range(w):
        z=a[i][j]-b[i][j]
        if i==0 and j==0:
            x[0].add(abs(a[0][0]-b[0][0]))
        elif i==0 and j!=0:
            for k in x[j-1]:
                if abs(k+z)<81: x[j].add(abs(k+z))
                elif abs(k-z)<81: x[j].add(abs(k-z))
        elif i%2:
            if j==0:
                for k in x[0]:
                    if abs(k+z)<81: y[j].add(abs(k+z))
                    elif abs(k-z)<81: y[j].add(abs(k-z))
            else:
                for k in x[j]:
                    if abs(k+z)<81: y[j].add(abs(k+z))
                    elif abs(k-z)<81: y[j].add(abs(k+(b[i][j]-a[i][j])))
                for k in y[j-1]:
                    if abs(k+z)<81: y[j].add(abs(k+(a[i][j]-b[i][j])))
                    elif abs(k-z)<81: y[j].add(abs(k+(b[i][j]-a[i][j])))
        else:
            if j==0:
                for k in y[0]:
                    if abs(k+z)<81: x[j].add(abs(k+z))
                    elif abs(k-z)<81: x[j].add(abs(k-z))
            else:
                for k in y[j]:
                    if abs(k+z)<81: x[j].add(abs(k+z))
                    elif abs(k-z)<81: x[j].add(abs(k+(b[i][j]-a[i][j])))
                for k in x[j-1]:
                    if abs(k+z)<81: x[j].add(abs(k+z))
                    elif abs(k-z)<81: x[j].add(abs(k-z))

    if i%2==0:
        y=[set([]) for _ in range(w)]
        ans=min(x[-1])
    else:
        x=[set([]) for _ in range(w)]
        ans=min(y[-1])
print(ans)


import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
 
H,W = map(int,readline().split())
A = [list(map(int,readline().split())) for _ in range(H)]
B = [list(map(int,readline().split())) for _ in range(H)]
 
X = (H + W) * 80

dp = [[0] * W for _ in range(H)]
d = abs(A[0][0] - B[0][0])
dp[0][0] = (1 << (X - d))
for h in range(H):
    for w,(a,b) in enumerate(zip(A[h],B[h])):
        if h == w == 0:
            continue
        d = abs(a - b)
        x = 0
        if h != 0:
            x |= (dp[h-1][w] << d) 
            x |= (dp[h-1][w] >> d)
        if w != 0:
            x |= (dp[h][w-1] << d) 
            x |= (dp[h][w-1] >> d)
        dp[h][w] = x
 
dp = dp[-1][-1]
 
B = bin(dp)[2:]
can_make = [i - X for i,b in enumerate(B[::-1]) if b == '1']
 
answer = min(x if x >= 0 else -x for x in can_make)
print(answer)

Verdicts: Runtime Error

Wrong Answer

Accepted