Problem:


Score : 500 points

Problem Statement

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.

Constraints

2 \leq H \leq 80

2 \leq W \leq 80

0 \leq A_{ij} \leq 80

0 \leq B_{ij} \leq 80

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W
A_{11} A_{12} \ldots A_{1W}
:
A_{H1} A_{H2} \ldots A_{HW}
B_{11} B_{12} \ldots B_{1W}
:
B_{H1} B_{H2} \ldots B_{HW}

Output

Print the minimum unbalancedness possible.

Sample Input 1

2 2
1 2
3 4
3 4
2 1

Sample Output 1

0

By painting the grid and traveling on it as shown in the figure below, the sum of red numbers and the sum of blue numbers are 3+3+1=7 and 1+2+4=7, respectively, for the unbalancedness of 0.

Sample Input 2

2 3
1 10 80
80 10 1
1 2 3
4 5 6

Sample Output 2

2



Sample input:
2 2
1 2
3 4
3 4
2 1


Sample output:
0

Submission status: Wrong Answer

Solution code:
import numpy as np

H, W = list(map(int, input().split()))

A = []
B = []

for i in range(H):
    A.append(list(map(int, input().split())))

for i in range(H):
    B.append(list(map(int, input().split())))

dp = np.inf * np.ones([H, W])
dp[0][0] = A[0][0] - B[0][0]

for i in range(H):
    for j in range(W):
        if i == 0 and j == 0:
            continue
        lst = []
        dif = A[i][j] - B[i][j]
        if j > 0:
            lst.append(dp[i][j - 1] + dif)
            lst.append(dp[i][j - 1] - dif)
        if i > 0:
            lst.append(dp[i - 1][j] + dif)
            lst.append(dp[i - 1][j] - dif)
        
        idx = np.argmin(np.abs(lst))
        if abs(dp[i][j]) > lst[idx]:
            dp[i][j] = lst[idx]

print(int(abs(dp[H - 1][W - 1])))

Submission status: Wrong Answer

Solution code:
import time
start = time.time()
H,W=[int(s) for s in input().split()]
inls=[[int(s) for s in input().split()] for _ in range(H)]
for i in range(H):
  x=[int(s) for s in input().split()]
  for j in range(W):
    inls[i][j]=abs(inls[i][j]-x[j])
dp=[[set() for _ in range(W)] for _ in range(H)]
dp[0][0].add(inls[0][0])
x=80*(H+W)
for i in range(H):
  for j in range(W):
    for e in dp[i-1][j]:
      if e+inls[i][j]<x:
        dp[i][j].add(e+inls[i][j])
      dp[i][j].add(abs(e-inls[i][j]))
    for e in dp[i][j-1]:
      if e+inls[i][j]<x:
        dp[i][j].add(e+inls[i][j])
      dp[i][j].add(abs(e-inls[i][j]))
  if time.time() - start>1.8:
    dp[H-1][W-1].add(0)
    break
print(min(dp[H-1][W-1]))

Submission status: Wrong Answer

Solution code:
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)]
dp1 = [[] for _ in range(W)]
dp2 = [[] for _ in range(W)]
for w in range(W):
    dp1[w].append(0)

for h in range(H):
    for w in range(W):
        va = A[h][w]
        vb = B[h][w]
        for v in dp1[w]:
            dp2[w].append(abs(v+va-vb))
            dp2[w].append(abs(v-va+vb))
        if w!=0:
            for v in dp2[w-1]:
                dp2[w].append(abs(v+va-vb))
                dp2[w].append(abs(v-va+vb))
        dp2[w] = list(set(dp2[w]))
    dp1 = dp2
    dp2 = [[] for _ in range(W)]

ans = min(dp1[W-1])

print(ans)

Submission status: Wrong Answer

Solution code:
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)]
C=[[0]*w for i in range(h)]
for i in range(h):
    for j in range(w):
        C[i][j]=abs(A[i][j]-B[i][j])
        
dp=[[0]*w for i in range(h)]
m=80
dp[0][0] = (1<<m+C[0][0]) | (1<<m-C[0][0])

for i in range(h):
    for j in range(w):
        if i>=1:
            dp[i][j] |= (dp[i-1][j]<<C[i][j]) | (dp[i-1][j]>>C[i][j])
        if j>=1:
            dp[i][j] |= (dp[i][j-1]<<C[i][j]) | (dp[i][j-1]>>C[i][j])
            
b=dp[h-1][w-1]
ans=float("inf")

for i in range(m+1):
    if (b>>m+i)&1==1:
        ans=i
        break
for i in range(m+1):
    if (b>>m-i)&1==1:
        ans=min(ans,i)
        break

print(ans)

Submission status: Wrong Answer

Solution code:
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)]
C=[[0]*w for i in range(h)]
for i in range(h):
    for j in range(w):
        C[i][j]=abs(A[i][j]-B[i][j])
        
dp=[[0]*w for i in range(h)]
m=80
dp[0][0] = (1<<m+C[0][0]) | (1<<m-C[i][j])

for i in range(h):
    for j in range(w):
        if i>=1:
            dp[i][j] |= (dp[i-1][j]<<C[i][j]) | (dp[i-1][j]>>C[i][j])
        if j>=1:
            dp[i][j] |= (dp[i][j-1]<<C[i][j]) | (dp[i][j-1]>>C[i][j])
            
b=dp[h-1][w-1]
ans=float("inf")

for i in range(m+1):
    if (b>>m+i)&1==1:
        ans=i
        break
for i in range(m+1):
    if (b>>m-i)&1==1:
        ans=min(ans,i)
        break

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
inf = float('inf')

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)]
D = []
for i in range(H):
    d = [a-b for a, b in zip(A[i], B[i])]
    D.append(d)

# dp[i][j][k] := (i,j)にk = 赤-青で到達できるか (-(H+W)*K <= k <= +(H+W)*K)
dp = [[[0] * (2*(H+W)*80 + 1) for _ in range(W)] for _ in range(H)]
adjust = (H+W) * 80
d = D[0][0]
dp[0][0][d + adjust] = 1
dp[0][0][-d + adjust] = 1

for i in range(H):
    for j in range(W):
        d = D[0][0]
        for k in range(2*(H+W)*80 + 1):
            res = 0
            try:
                res |= dp[i-1][j][k-d]
            except:
                pass
            try:
                res |= dp[i-1][j][k+d]
            except:
                pass
            try:
                res |= dp[i][j-1][k-d]
            except:
                pass
            try:
                res |= dp[i][j-1][k+d]
            except:
                pass
            dp[i][j][k] |= res

ans = inf

for k in range(2*(H+W)*80 + 1):
    if dp[H-1][W-1][k]:
        ans = min(ans, abs(k-adjust))

print(ans)

Submission status: Wrong Answer

Solution code:
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)]
x=[set([]) for _ in range(w)]

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)

Submission status: Wrong Answer

Solution code:
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)]
x=[set([]) for _ in range(w)]
for i in range(h):
    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))
                    if 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))
                    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[j].add(abs(k+z))
                    if 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))
                    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 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)

Submission status: Wrong Answer

Solution code:
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)]
x=[set([]) for _ in range(w)]
for i in range(h):
    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)<162: x[j].add(abs(k+z))
                elif abs(k-z)<162: x[j].add(abs(k-z))
        elif i%2:
            if j==0:
                for k in x[0]:
                    if abs(k+z)<162: y[j].add(abs(k+z))
                    if abs(k-z)<162: y[j].add(abs(k-z))
                    if abs(-k+z)<162: y[j].add(abs(-k+z))
                    if abs(-k-z)<162: y[j].add(abs(-k-z))
            else:
                for k in x[j]:
                    if abs(k+z)<162: y[j].add(abs(k+z))
                    if abs(k-z)<162: y[j].add(abs(k-z))
                    if abs(-k+z)<162: y[j].add(abs(-k+z))
                    if abs(-k-z)<162: y[j].add(abs(-k-z))
                for k in y[j-1]:
                    if abs(k+z)<162: y[j].add(abs(k+z))
                    if abs(k-z)<162: y[j].add(abs(k-z))
                    if abs(-k+z)<162: y[j].add(abs(-k+z))
                    if abs(-k-z)<162: y[j].add(abs(-k-z))
        else:
            if j==0:
                for k in y[0]:
                    if abs(k+z)<162: x[j].add(abs(k+z))
                    if abs(k-z)<162: x[j].add(abs(k-z))
                    if abs(-k+z)<162: x[j].add(abs(-k+z))
                    if abs(-k-z)<162: x[j].add(abs(-k-z))
            else:
                for k in y[j]:
                    if abs(k+z)<162: x[j].add(abs(k+z))
                    if abs(k-z)<162: x[j].add(abs(k-z))
                    if abs(-k+z)<162: x[j].add(abs(-k+z))
                    if abs(-k-z)<162: x[j].add(abs(-k-z))
                for k in x[j-1]:
                    if abs(k+z)<162: x[j].add(abs(k+z))
                    if abs(k-z)<162: x[j].add(abs(k-z))
                    if abs(-k+z)<162: x[j].add(abs(-k+z))
                    if abs(-k-z)<162: 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)

Submission status: Wrong Answer

Solution code:
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)]
x=[set([]) for _ in range(w)]
for i in range(h):
    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))
                    if abs(k-z)<81: y[j].add(abs(k-z))
                    if abs(-k+z)<81: y[j].add(abs(-k+z))
                    if 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))
                    if abs(k-z)<81: y[j].add(abs(k-z))
                    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))
                    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[j].add(abs(k+z))
                    if abs(k-z)<81: x[j].add(abs(k-z))
                    if abs(-k+z)<81: x[j].add(abs(-k+z))
                    if 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))
                    if abs(k-z)<81: x[j].add(abs(k-z))
                    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 abs(-k+z)<81: x[j].add(abs(-k+z))
                    if 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)

Submission status: Wrong Answer

Solution code:
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)

Submission status: Wrong Answer

Solution code:
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)]
x=[[0]*w for i in range(h)]
if abs(a[0][0]-b[0][0]):
    x[0][0]=a[0][0]-b[0][0]
else:
    x[0][0]=b[0][0]-a[0][0]
for i in range(1,w):
    p=a[0][i]-b[0][i]
    if abs(p+x[0][i-1])<abs(-p+x[0][i-1]):
        x[0][i]=x[0][i-1]+p
    else:
        x[0][i]=x[0][i-1]-p

for i in range(1,h):
    p=a[i][0]-b[i][0]
    if abs(p+x[i-1][0])<abs(-p+x[i-1][0]):
        x[i][0]=x[i-1][0]+p
    else:
        x[i][0]=x[i-1][0]-p
for i in range(1,h):
    for j in range(1,w):
        p=a[i][0]-b[i][0]
        l=[abs(x[i-1][j]+p),abs(x[i-1][j]-p),abs(x[i][j-1]+p),abs(x[i][j-1]-p)]
        if min(l)==l[0]:
            x[i][j]=x[i-1][j]+p
        elif min(l)==l[1]:
            x[i][j]=x[i-1][j]-p

        elif min(l)==l[2]:
            x[i][j]=x[i][j-1]+p
        elif min(l)==l[3]:
            x[i][j]=x[i][j-1]-p
print(abs(x[-1][-1]))

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
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)]
l=80*80+1
dp=[l*[False]for _ in range((h+1)*(w+1))]
mid=l//2
for i in range(h):
  for j in range(w):
    c=abs(a[i][j]-b[i][j])
    if i==j==0:dp[w+2][c]=True
    for k in range(l):
      if dp[(i+1)*(w+1)+(j+1)]:
        if (i < h - 1):
          c1 = abs(a[i+1][j]-b[i+1][j])
          dp[(i+2)*(w+1)+j+1][abs(k-c1)]=True
          if (k+c1<=80*80):
            dp[(i+2)*(w+1)+j+1][k+c1]=True
        if (j < w - 1):
          c2 = abs(a[i][j+1]-b[i][j+1])
          dp[(i+1)*(w+1)+j+2][abs(k-c2)]=True
          if (k+c2<=80*80):
            dp[(i+1)*(w+1)+j+2][k+c2]=True
for i in range(l):
  if dp[-1][i]:print(i);exit()

Submission status: Wrong Answer

Solution code:
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)]
d1=[[0]*w for i in range(h)]
d2=[[0]*w for i in range(h)]
x1=[[0]*w for i in range(h)]
x2=[[0]*w for i in range(h)]
for i in range(h):
    for j in range(w):
        if (i+j)%2:
            d1[i][j]=b[i][j]-a[i][j]
            d2[i][j]=a[i][j]-b[i][j]
        else:
            d1[i][j]=a[i][j]-b[i][j]
            d2[i][j]=b[i][j]-a[i][j]
x1[0][0]+=d1[0][0]
x2[0][0]+=d2[0][0]
for i in range(1,w):
    x1[0][i]+=x1[0][i-1]+d1[0][i]
    x2[0][i]+=x2[0][i-1]+d2[0][i]
for i in range(1,h):
    x1[i][0]+=x1[i-1][0]+d1[i][0]
    x2[i][0]+=x2[i-1][0]+d2[i][0]
for i in range(1,h):
    for j in range(1,w):
        p1=x1[i-1][j]+d1[i][j]
        q1=x1[i][j-1]+d1[i][j]
        p2=x2[i-1][j]+d2[i][j]
        q2=x2[i][j-1]+d2[i][j]
        if abs(p1)<abs(q1):
            x1[i][j]=p1
        elif abs(p1)>=abs(q1):
            x1[i][j]=q1

        if abs(p2)<abs(q2):
            x2[i][j]=p2
        elif abs(p2)>=abs(q2):
            x2[i][j]=q2
print(min(abs(x2[-1][-1]),abs(x1[-1][-1])))

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
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)]
l=80*80+1
dp=[l*[False]for _ in range((h+1)*(w+1))]
mid=l//2
for i in range(h):
  for j in range(w):
    c=abs(a[i][j]-b[i][j])
    if i==j==0:dp[w+2][c]=True;continue
    else:
      for k in range(l-c):
          kmc = abs(k-c)
          kpc = k + c
          dp[(i+1)*(w+1)+j+1][k]=dp[i*(w+1)+j+1][kmc] or dp[(i+1)*(w+1)+j][kpc] or dp[i*(w+1)+j+1][kmc] or dp[(i+1)*(w+1)+j][kmc]
for i in range(l):
  if dp[-1][i]:print(i);exit()

Submission status: Wrong Answer

Solution code:
import sys
input=sys.stdin.readline
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)]
l=80*40+1
dp=[l*[False]for _ in range((h+1)*(w+1))]
mid=l//2
for i in range(h):
  for j in range(w):
    c=abs(a[i][j]-b[i][j])
    if i==j==0:dp[w+2][c]=True;continue
    else:
      for k in range(l-c):dp[(i+1)*(w+1)+j+1][k]=dp[i*(w+1)+j+1][abs(k-c)] or dp[(i+1)*(w+1)+j][abs(k-c)] or dp[i*(w+1)+j+1][abs(k+c)] or dp[(i+1)*(w+1)+j][abs(k+c)]
for i in range(l):
  if dp[-1][i]:print(i);exit()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline

h, w = map(int, input().split())
a = [map(int, input().split()) for _ in range(h)]
b = [map(int, input().split()) for _ in range(h)]
g = [list(abs(x - y) for x, y in zip(l1, l2)) for l1, l2 in zip(a, b)]
poss = [[1 << 20000] * w for _ in range(h)]
poss[0][0] |= 1 << 10000 + g[0][0]
poss[0][0] |= 1 << 10000 - g[0][0]
for i in range(h):
	for j in range(w - 1):
		poss[i][j + 1] |= poss[i][j] << g[i][j + 1]
		poss[i][j + 1] |= poss[i][j] >> g[i][j + 1]
	if i != h - 1:
		for j in range(w):
			poss[i + 1][j] |= poss[i][j] << g[i + 1][j]
			poss[i + 1][j] |= poss[i][j] >> g[i + 1][j]
res = poss[-1][-1]
for i in range(10000):
	if res & (1 << 10000 + i) or res & (1 << 10000 - i):
		print(i)
		exit()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline

h, w = map(int, input().split())
a = [map(int, input().split()) for _ in range(h)]
b = [map(int, input().split()) for _ in range(h)]
g = [list(abs(x - y) for x, y in zip(l1, l2)) for l1, l2 in zip(a, b)]
poss = [[1 << 13000] * w for _ in range(h)]
poss[0][0] |= 1 << 6500 + g[0][0]
poss[0][0] |= 1 << 6500 - g[0][0]
for i in range(h):
	for j in range(w - 1):
		poss[i][j + 1] |= poss[i][j] << g[i][j + 1]
		poss[i][j + 1] |= poss[i][j] >> g[i][j + 1]
	if i != h - 1:
		for j in range(w):
			poss[i + 1][j] |= poss[i][j] << g[i + 1][j]
			poss[i + 1][j] |= poss[i][j] >> g[i + 1][j]
res = poss[-1][-1]
for i in range(6500):
	if res & (1 << 6500 + i) or res & (1 << 6500 - i):
		print(i)
		exit()

Submission status: Wrong Answer

Solution code:
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)]
d=[[0]*w for i in range(h)]
x=[[0]*w for i in range(h)]
for i in range(h):
    for j in range(w):
        if (i+j)%2: d[i][j]=b[i][j]-a[i][j]
        else: d[i][j]=a[i][j]-b[i][j]
x[0][0]=d[0][0]
for i in range(1,w):
    x[0][i]=x[0][i-1]+d[0][i]
for i in range(1,h):
    x[i][0]=x[i-1][0]+d[i][0]
for i in range(1,h):
    for j in range(1,w):
        p=x[i-1][j]+d[i][j]
        q=x[i][j-1]+d[i][j]
        if abs(p)<abs(q):
            x[i][j]=p
        else:
            x[i][j]=q
print(abs(x[-1][-1]))

Submission status: Wrong Answer

Solution code:
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)]
d=[[0]*w for i in range(h)]
x=[[0]*w for i in range(h)]
for i in range(h):
    for j in range(w):
        if (i+j)%2: d[i][j]=b[i][j]-a[i][j]
        else: d[i][j]=a[i][j]-b[i][j]
x[0][0]=d[0][0]
for i in range(1,w):
    x[0][i]=x[0][i-1]+d[0][i]
for i in range(1,h):
    x[i][0]=x[i-1][0]+d[i][0]
for i in range(1,h):
    for j in range(1,w):
        p=x[i-1][j]+d[i][j]
        q=x[i][j-1]+d[i][j]
        if abs(p)<abs(q):
            x[i][j]=p
        else:
            x[i][j]=q
print(x[-1][-1])

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline


h, w = map(int, input().split())
A = []

for i in range(h):
    line = list(map(int, input().split()))
    A.append(line)

for i in range(h):
    line = list(map(int, input().split()))
    for j, num in enumerate(line):
        A[i][j] = abs(A[i][j] - num)

const = 20000
bitset = 1 << const
DP = [[0]*w for _ in range(h)]
DP[0][0] = bitset << A[0][0]


def slide_down(y, x):
    bit = DP[y][x]
    delta = A[y+1][x]
    new1 = bit << delta
    new2 = bit >> delta
    DP[y+1][x] |= new1 | new2


def slide_right(y, x):
    bit = DP[y][x]
    delta = A[y][x+1]
    new1 = bit << delta
    new2 = bit >> delta
    DP[y][x+1] |= new1 | new2


for y in range(h):
    for x in range(w):
        if y < h-1:
            slide_down(y, x)
        if x < w-1:
            slide_right(y, x)

ans = DP[h-1][w-1]
for i in range(80):
    if (ans >> const+i) & 1 or (ans >> const-i) & 1:
        print(i)
        exit()

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().split())
A = [list(map(int,input().split())) for x in range(H)]
B = [list(map(int,input().split())) for x in range(H)]

M = 0
for i in range(H):
  for j in range(W):
    M = max(abs(A[i][j] - B[i][j]),M)

dp = [[[False]*((H+W)*M + 1) for x in range(W)] for x in range(H)]
dp[0][0][abs(A[0][0] - B[0][0])] = True

for i in range(H):
  for j in range(W):
    if i == j == 0:
      continue
    r = abs(A[i][j] - B[i][j])
    for k in range((H+W)*M):
      if k - r > -1:
        if i > 0 and dp[i-1][j][k-r]:
          dp[i][j][k] = True
        if j > 0 and dp[i][j-1][k-r]:
          dp[i][j][k] = True
      if -1 < k + r < (H+W)*M + 1:
        if i > 0 and dp[i-1][j][k+r]:
          dp[i][j][k] = True
        if j > 0 and dp[i][j-1][k+r]:
          dp[i][j][k] = True
      if -1 < -k + r < (H+W)*M + 1:
        if i > 0 and dp[i-1][j][r-k]:
          dp[i][j][k] = True
        if j > 0 and dp[i][j-1][r-k]:
          dp[i][j][k] = True

for i in range((H+W)*M):
  if dp[H-1][W-1][i]:
    print(i)
    break

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline


h, w = map(int, input().split())
A = []

for i in range(h):
    line = list(map(int, input().split()))
    A.append(line)

for i in range(h):
    line = list(map(int, input().split()))
    for j, num in enumerate(line):
        A[i][j] = abs(A[i][j] - num)

const = 13000
bitset = 1 << const
DP = [[0]*w for _ in range(h)]
DP[0][0] = bitset << A[0][0]


def slide_down(y, x):
    bit = DP[y][x]
    delta = A[y+1][x]
    new1 = bit << delta
    new2 = bit >> delta
    DP[y+1][x] |= new1
    DP[y+1][x] |= new2


def slide_right(y, x):
    bit = DP[y][x]
    delta = A[y][x+1]
    new1 = bit << delta
    new2 = bit >> delta
    DP[y][x+1] |= new1
    DP[y][x+1] |= new2


for y in range(h):
    for x in range(w):
        if y < h-1:
            slide_down(y, x)
        if x < w-1:
            slide_right(y, x)

ans = DP[h-1][w-1]
for i in range(80):
    if (ans >> const+i) & 1 or (ans >> const-i) & 1:
        print(i)
        exit()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline


h, w = map(int, input().split())
A = []

for i in range(h):
    line = list(map(int, input().split()))
    A.append(line)

for i in range(h):
    line = list(map(int, input().split()))
    for j, num in enumerate(line):
        A[i][j] = abs(A[i][j] - num)

const = 10000
bitset = 1 << const
DP = [[0]*w for _ in range(h)]
DP[0][0] = bitset << A[0][0]


def slide_down(y, x):
    bit = DP[y][x]
    delta = A[y+1][x]
    new1 = bit << delta
    new2 = bit >> delta
    DP[y+1][x] |= new1
    DP[y+1][x] |= new2


def slide_right(y, x):
    bit = DP[y][x]
    delta = A[y][x+1]
    new1 = bit << delta
    new2 = bit >> delta
    DP[y][x+1] |= new1
    DP[y][x+1] |= new2


for y in range(h):
    for x in range(w):
        if y < h-1:
            slide_down(y, x)
        if x < w-1:
            slide_right(y, x)

ans = DP[h-1][w-1]
for i in range(80):
    if (ans >> const+i) & 1 or (ans >> const-i) & 1:
        print(i)
        exit()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline


h, w = map(int, input().split())
A = []

for i in range(h):
    line = list(map(int, input().split()))
    A.append(line)

for i in range(h):
    line = list(map(int, input().split()))
    for j, num in enumerate(line):
        A[i][j] = abs(A[i][j] - num)

bitset = 1 << 6400
DP = [[0]*w for _ in range(h)]
DP[0][0] = bitset << A[0][0]


def slide_down(y, x):
    bit = DP[y][x]
    delta = A[y+1][x]
    new1 = bit << delta
    new2 = bit >> delta
    DP[y+1][x] |= new1
    DP[y+1][x] |= new2


def slide_right(y, x):
    bit = DP[y][x]
    delta = A[y][x+1]
    new1 = bit << delta
    new2 = bit >> delta
    DP[y][x+1] |= new1
    DP[y][x+1] |= new2


for y in range(h):
    for x in range(w):
        if y < h-1:
            slide_down(y, x)
        if x < w-1:
            slide_right(y, x)

ans = DP[h-1][w-1]
for i in range(80):
    if (ans >> 6400+i) & 1 or (ans >> 6400-i) & 1:
        print(i)
        exit()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline


h, w = map(int, input().split())
A = []

for i in range(h):
    line = list(map(int, input().split()))
    A.append(line)

for i in range(h):
    line = list(map(int, input().split()))
    for j, num in enumerate(line):
        A[i][j] = abs(A[i][j] - num)

bitset = 1
DP = [[0]*w for _ in range(h)]
DP[0][0] = bitset << A[0][0]


def slide_down(y, x):
    bit = DP[y][x]
    delta = A[y+1][x]
    new1 = bit << delta
    new2 = bit >> delta
    DP[y+1][x] |= new1
    DP[y+1][x] |= new2


def slide_right(y, x):
    bit = DP[y][x]
    delta = A[y][x+1]
    new1 = bit << delta
    new2 = bit >> delta
    DP[y][x+1] |= new1
    DP[y][x+1] |= new2


for y in range(h):
    for x in range(w):
        if y < h-1:
            slide_down(y, x)
        if x < w-1:
            slide_right(y, x)

ans = DP[h-1][w-1]
for i in range(80):
    if (ans >> i) & 1:
        print(i)
        exit()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline


h, w = map(int, input().split())
inf = float('INF')
A = []
B = []

for i in range(h):
    line = list(map(int, input().split()))
    A.append(line)

for i in range(h):
    line = list(map(int, input().split()))
    B.append(line)

D = [[0]*w for i in range(h)]
for i in range(h):
    for j in range(w):
        D[i][j] = abs(A[i][j] - B[i][j])


def slide_down(y, x, possible):
    new = set()
    delta = D[y+1][x]
    limit = (h+w)//2 * 80
    for num in possible:
        if num + delta <= limit - (y+x-2)//2*80:
            new.add(num+delta)
        if abs(num-delta) <= limit - (y+x-2)//2*80:
            new.add(abs(num-delta))
    return new


def slide_right(y, x, possible):
    new = set()
    delta = D[y][x+1]
    limit = (h + w) // 2 * 80
    for num in possible:
        if num + delta <= limit - (y+x-2)//2*80:
            new.add(num+delta)
        if abs(num-delta) <= limit - (y+x-2)//2*80:
            new.add(abs(num-delta))
    return new


DP = [[set() for j in range(w)] for i in range(h)]
DP[0][0] = {D[0][0]}
for row in range(h):
    for col in range(w):
        possible = DP[row][col]
        if col < w-1:
            new = slide_right(row, col, possible)
            DP[row][col+1] |= new
        if row < h-1:
            new = slide_down(row, col, possible)
            DP[row+1][col] |= new

ans = min(DP[h-1][w-1])
print(ans)