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):
# Atcoder Problem Solving
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)]
for i in range(H):
    for j in range(W):
        a[i][j]=abs(a[i][j]-b[i][j])
dp=[[0 for   i in range(W)] for j in range(H)]




X=80

dp[0][0]|= (1<<(X+a[0][0]))  
dp[0][0]|= (1<<(X-a[0][0]))


for i in range(H):
    for j in range(W):
        if 0<=i-1<H and 0<=j<W:
            dd=a[i-1][j]
            dp[i][j]|=(dp[i-1][j]<<dd)
            dp[i][j]|=(dp[i-1][j]>>dd) 
            
        if 0<=i<H and 0<=j-1<W:
            d=a[i][j-1]
            dp[i][j]|=(dp[i][j-1]<<d)
            dp[i][j]|=(dp[i][j-1]>>d)
            
A=str(bin(dp[H-1][W-1]))[::-1]
L=len(A)

ans=80
for i in range(L-2):
    if A[i]=="1":
        ans=min(ans,abs(i-X))
print(ans)


# Atcoder Problem Solving
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)]
for i in range(H):
    for j in range(W):
        a[i][j]=abs(a[i][j]-b[i][j])
dp=[[0 for   i in range(W)] for j in range(H)]




X=80

dp[0][0]|= (1<<(X+a[0][0]))  
dp[0][0]|= (1<<(X-a[0][0]))


for i in range(H):
    for j in range(W):
        if 0<=i<H and 0<=j+1<W:
            dp[i][j+1]|=(dp[i][j]<<a[i][j+1])
            dp[i][j+1]|=(dp[i][j]>>a[i][j+1]) 
            
        if 0<=i+1<H and 0<=j-1<W:
            d=a[i+1][j]
            dp[i+1][j]|=(dp[i][j]<<a[i+1][j])
            dp[i+1][j]|=(dp[i][j]>>a[i+1][j])
            
A=str(bin(dp[H-1][W-1]))[::-1]
L=len(A)

ans=80
for i in range(L-2):
    if A[i]=="1":
        ans=min(ans,abs(i-X))
print(ans)

# Atcoder Problem Solving
H,W=map(int,input().split())
c=[list(map(int,input().split())) for i in range(H)]
b=[list(map(int,input().split())) for i in range(H)]
a=[[0 for i  in range(W)] for j in range(H)]

for i in range(H):
    for j in range(W):
        a[i][j]=abs(c[i][j]-b[i][j])
        
dp=[[0 for i in range(W)] for j in range(H)]




X=3200

dp[0][0]= (1<<(X+a[0][0]))  | (1<<(X-a[0][0]))


for i in range(H):
    for j in range(W):
        if 0<=i<H and 0<=j+1<W:
            dp[i][j+1]|=(dp[i][j]<<a[i][j+1])
            dp[i][j+1]|=(dp[i][j]>>a[i][j+1]) 
            
        if 0<=i+1<H and 0<=j<W:
            dp[i+1][j]|=(dp[i][j]<<a[i+1][j])
            dp[i+1][j]|=(dp[i][j]>>a[i+1][j])
            
A=str(bin(dp[H-1][W-1]))[::-1]
L=len(A)
ans=3200
for i in range(L):
    if A[i]=="1":
        ans=min(ans,abs(i-X))
print(ans)

Verdicts: Wrong Answer

Wrong Answer

Accepted