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:
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)]
C = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]

dp = [[None] * W for _ in range(H)]
dp[0][0] = {C[0][0]}
for i in range(H):
    for j in range(W):
        if i == j == 0:
            continue
        st = set()
        if i > 0:
            st |= dp[i-1][j]
        if j > 0:
            st |= dp[i][j-1]
        nst = set()
        for n in st:
            if abs(n-C[i][j]) <= 80:
                nst.add(abs(n-C[i][j]))
            if abs(n+C[i][j]) <= 80:
                nst.add(abs(n+C[i][j]))
        dp[i][j] = nst

ans = list(dp[H-1][W-1])
ans.sort()
print(ans[0])

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)]
d=[[a[i][j]-b[i][j]for j in range(w)]for i in range(h)]
dp=[w*[0]for _ in range(h)]
dp[0][0]=1<<(80+d[0][0])|1<<(80-d[0][0])
for i in range(h):
  for j in range(w):
    if i!=0:
      dp[i][j]|=(dp[i-1][j]<<80+d[i][j])
      dp[i][j]|=(dp[i-1][j]<<80-d[i][j])
    if j!=0:
      dp[i][j]|=(dp[i][j-1]<<80+d[i][j])
      dp[i][j]|=(dp[i][j-1]<<80-d[i][j])
n=dp[-1][-1]+(1<<9999)
ans=1<<1000
for i in range(9999):
  if n&(1<<i):ans=min(ans,abs(i-(h+w-1)*80))
print(ans)

Submission status: Wrong Answer

Solution code:
h, w = [int(i) for i in input().split()]
a = [[int(i) for i in input().split()] for _ in range(h)]
b = [[int(i) for i in input().split()] for _ in range(h)]

dp = [[[False] * (160 * 160) for _ in range(w)] for _ in range(h)]
dp[0][0][0] = True

for i in range(h):
    for j in range(w):
        if i != 0:
            for k in range(-160 * 80, 160 * 80):
                if dp[i - 1][j][k]:
                    dp[i][j][k + a[i][j] - b[i][j]] = True
                    dp[i][j][k - a[i][j] + b[i][j]] = True

        if j != 0:
            for k in range(-160 * 80, 160 * 80):
                if dp[i][j-1][k]:
                    dp[i][j][k + a[i][j] - b[i][j]] = True
                    dp[i][j][k - a[i][j] + b[i][j]] = True
ans = 10**9
for i in range(-160*80,160*80):
    if dp[-1][-1][i]:
        ans = min(ans, abs(i))

print(ans)

Submission status: Wrong Answer

Solution code:
import math

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

diff = [[abs(x-y) for x,y in zip(ar,br)] for ar,br in zip (a,b)]

M = 80*2+5
dp = [[[False]*M for j in range(w)] for i in range(h)]
dp[0][0][diff[0][0]] = True
for i in range(h):
    for j in range(w):
        for k in range(M):
            if not dp[i][j][k]:
                continue
            if i+1 < h:
                dp[i+1][j][abs(k-diff[i+1][j])] = True
                if k+diff[i+1][j] < M:
                    dp[i+1][j][k+diff[i+1][j]] = True
            if j+1 < w:
                dp[i][j+1][abs(k-diff[i][j+1])] = True
                if k+diff[i][j+1] < M:
                    dp[i][j+1][k+diff[i][j+1]] = True
for k in range(M):
    if dp[h-1][w-1][k]:
        print(k)
        break

Submission status: Wrong Answer

Solution code:
import math

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

diff = [[abs(x-y) for x,y in zip(ar,br)] for ar,br in zip (a,b)]

M = 80*2+1
dp = [[[False]*M for j in range(w)] for i in range(h)]
dp[0][0][diff[0][0]] = True
for i in range(h):
    for j in range(w):
        for k in range(M):
            if not dp[i][j][k]:
                continue
            if i+1 < h:
                dp[i+1][j][abs(k-diff[i+1][j])] = True
                if k+diff[i+1][j] < M:
                    dp[i+1][j][k+diff[i+1][j]] = True
            if j+1 < w:
                dp[i][j+1][abs(k-diff[i][j+1])] = True
                if k+diff[i][j+1] < M:
                    dp[i][j+1][k+diff[i][j+1]] = True
for k in range(M):
    if dp[h-1][w-1][k]:
        print(k)
        break

Submission status: Wrong Answer

Solution code:
h,w = map(int, input().split( ))
a = []
for i in range(h):
    ai = list(map(int, input().split( )))
    a.append(ai)
for i in range(h):
    bi = list(map(int, input().split( )))
    for j in range(w):
        a[i][j]-=bi[j]
        a[i][j] = abs(a[i][j])
###差のmaxまでをlistで確保する代わりに可能な値をsetで保持
###値が小さいのでsetからbitで保持に変更
dp = [[0 for j in range(w)] for i in range(h)]
a2 = [a[i][j] for i in range(h) for j in range(w)]
mxa = max(a2)
###中心を取る
tmp=1<<mxa*(h+w+10)

dp[0][0]|=tmp<<a[0][0]
dp[0][0]|=tmp>>a[0][0]

for j in range(1,w):
    dp[0][j]=dp[0][j-1]<<a[0][j]
    dp[0][j]|=dp[0][j-1]>>a[0][j]

for i in range(1,h):
    for j in range(w):###ループ一つ抜けてるわw
        dp[i][j]=dp[i-1][j]<<a[i][j]
        dp[i][j]|=dp[i-1][j]>>a[i][j]
        if j:
            dp[i][j]|=dp[i][j-1]<<a[i][j]
            dp[i][j]|=dp[i][j-1]>>a[i][j]

tmp1 = dp[h-1][w-1]

tmp1>>=max(a2)*(h+w+10)
for k in range(mxa*(h+w+10)):
    if tmp1&1:
        print(k)
        exit()
    tmp1>>=1

Submission status: Wrong Answer

Solution code:
h,w = map(int, input().split( ))
a = []
for i in range(h):
    ai = list(map(int, input().split( )))
    a.append(ai)
for i in range(h):
    bi = list(map(int, input().split( )))
    for j in range(w):
        a[i][j]-=bi[j]
        a[i][j] = abs(a[i][j])
###差のmaxまでをlistで確保する代わりに可能な値をsetで保持
###値が小さいのでsetからbitで保持に変更
dp = [[0 for j in range(w)] for i in range(h)]
a2 = [a[i][j] for i in range(h) for j in range(w)]
mxa = max(a2)
###中心を取る
###これ間違えるから制約最大で決め打ちのほうがいいかもしれない
tmp=1<<mxa*(h+w)

dp[0][0]|=tmp<<a[0][0]
dp[0][0]|=tmp>>a[0][0]

for j in range(1,w):
    dp[0][j]=dp[0][j-1]<<a[0][j]
    dp[0][j]|=dp[0][j-1]>>a[0][j]

for i in range(1,h):
    for j in range(w):###ループ一つ抜けてるわw
        dp[i][j]=dp[i-1][j]<<a[i][j]
        dp[i][j]|=dp[i-1][j]>>a[i][j]
        if j:
            dp[i][j]|=dp[i][j-1]<<a[i][j]
            dp[i][j]|=dp[i][j-1]>>a[i][j]

tmp1 = dp[h-1][w-1]

tmp1>>=max(a2)*(h+w)
for k in range(mxa*(h+w)):
    if tmp1&1:
        print(k)
        exit()
    tmp1>>=1

Submission status: Wrong Answer

Solution code:
h,w = map(int, input().split( ))
a = []
for i in range(h):
    ai = list(map(int, input().split( )))
    a.append(ai)
for i in range(h):
    bi = list(map(int, input().split( )))
    for j in range(w):
        a[i][j]-=bi[j]
        a[i][j] = abs(a[i][j])
###差のmaxまでをlistで確保する代わりに可能な値をsetで保持
###値が小さいのでsetからbitで保持に変更
dp = [[0 for j in range(w)] for i in range(h)]
a2 = [a[i][j] for i in range(h) for j in range(w)]
mxa = max(a2)
###中心を取る
tmp=1<<mxa*(h+w)

dp[0][0]|=tmp<<a[0][0]
dp[0][0]|=tmp>>a[0][0]

for j in range(1,w):
    dp[0][j]=dp[0][j-1]<<a[0][j]
    dp[0][j]|=dp[0][j-1]>>a[0][j]

for i in range(1,h):
    dp[i][j]=dp[i-1][j]<<a[i][j]
    dp[i][j]|=dp[i-1][j]>>a[i][j]
    if j:
        dp[i][j]|=dp[i][j-1]<<a[i][j]
        dp[i][j]|=dp[i][j-1]>>a[i][j]

tmp1 = dp[h-1][w-1]

tmp1>>=max(a2)*h*w
for k in range(mxa*(h+w)):
    if tmp1&1:
        print(k)
        exit()
    tmp1>>=1

Submission status: Wrong Answer

Solution code:
h,w = map(int, input().split( ))
a = []
for i in range(h):
    ai = list(map(int, input().split( )))
    a.append(ai)
for i in range(h):
    bi = list(map(int, input().split( )))
    for j in range(w):
        a[i][j]-=bi[j]
        a[i][j] = abs(a[i][j])
###差のmaxまでをlistで確保する代わりに可能な値をsetで保持
###値が小さいのでsetからbitで保持に変更
dp = [[0 for j in range(w)] for i in range(h)]
a2 = [a[i][j] for i in range(h) for j in range(w)]
mxa = max(a2)
###中心を取る
tmp=1<<mxa*(h+w)

dp[0][0]|=tmp<<a[0][0]
dp[0][0]|=tmp>>a[0][0]

for j in range(1,w):
    dp[0][j]=dp[0][j-1]<<a[0][j]
    dp[0][j]|=dp[0][j-1]>>a[0][j]

for i in range(1,h):
    dp[i][j]=dp[i-1][j]<<a[i][j]
    dp[i][j]|=dp[i-1][j]>>a[i][j]
    if j:
        dp[i][j]|=dp[i][j-1]<<a[i][j]
        dp[i][j]|=dp[i][j-1]>>a[i][j]

tmp1 = dp[h-1][w-1]

tmp1>>=max(a2)*h*w
for k in range(mxa*(h+w)):
    if tmp1&1:
        print(k-1)###？
        exit()
    tmp1>>=1

Submission status: Wrong Answer

Solution code:
import numpy as np
import itertools

H,W=map(int,input().split())
A=[list(map(int, input().split())) for _ in range(H*2)]
M=[[abs(A[i][j]-A[H+i][j]) for j in range(W)] for i in range(H)]
m=max(max(j for j in i) for i in M)
r=m*(H+W)+1
dp=np.zeros((H,W,r),dtype='bool')
dp[0][0][M[0][0]]=True
it = itertools.product(range(H),range(W))
next(it)
for h,w in it:
    m=M[h][w]
    dp[h][w][m:] |= dp[h][w-1][:r-m] | dp[h-1][w][:r-m]
    dp[h][w][:r-m] |= dp[h][w-1][m:] | dp[h-1][w][m]
    dp[h][w][:m] |= dp[h][w-1][m:0:-1] | dp[h-1][w][m:0:-1]

for i in range(r):
    if dp[-1][-1][i]:
        print(i)
        break

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
ab = []
for i in range(h * 2):
    ab.extend(list(map(int, input().split())))

abs_table = [[0] * w for _ in range(h)]
dp = [[0] * w for _ in range(h)]

s = h * w
i = 0
j = 0
for k in range(s):
    if w == j:
        j = 0
        i += 1
    a, b = ab[k], ab[k+s]
    abs_table[i][j] = abs(a - b)
    j += 1
   
n = abs_table[0][0]  
dp[0][0] = 2**(n * 2)  | 1
    
for i in range(h):
    for j in range(w):
        a = abs_table[i][j]
        u = 0
        l = 0
        if i:
            u = dp[i-1][j]
            u = u << a * 2 | u | u
        if j:
            l = dp[i][j-1]
            l = l << a * 2 | l | l
            
        if not u and not l:
            pass
        elif not l:
            dp[i][j] = u
        elif not u:
            dp[i][j] = l
        else:
            len_u = len(bin(u)[2:])
            len_l = len(bin(l)[2:])
            delta = len_u - len_l
            if delta == 0:
                dp[i][j] = u
            elif delta < 0:
                dp[i][j] = u << abs(delta) // 2 | l
            else:
                dp[i][j] = u | l << delta // 2

flag = bin(dp[-1][-1])[2:]
zero = len(flag) // 2
for i, v in enumerate(flag[zero:]):
    if v == '1':
        print(i)
        break

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
arr=[list(map(int,input().split())) for _ in range(2*h)]
board=[[0]*w for _ in range(h)]
for i in range(h):
  for j in range(w):
    board[i][j]+=arr[i][j]
for i in range(h):
  for j in range(w):
    board[i][j]=abs(board[i][j]-arr[h+i][j])
dp=[[set() for _ in range(w)] for _ in range(h)]
dp[0][0].add(board[0][0])
for i in range(h):
  for j in range(w):
    if i!=0:
      for k in dp[i-1][j]:
        dp[i][j].add((k+board[i][j])%480)
        dp[i][j].add(abs(k-board[i][j])%480)
    if j!=0:
      for k in dp[i][j-1]:
        dp[i][j].add((k+board[i][j])%480)
        dp[i][j].add(abs(k-board[i][j])%480)
print(min(dp[h-1][w-1]))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
arr=[list(map(int,input().split())) for _ in range(2*h)]
board=[[0]*w for _ in range(h)]
for i in range(h):
  for j in range(w):
    board[i][j]+=arr[i][j]
for i in range(h):
  for j in range(w):
    board[i][j]=abs(board[i][j]-arr[h+i][j])
dp=[[set() for _ in range(w)] for _ in range(h)]
dp[0][0].add(board[0][0])
for i in range(h):
  for j in range(w):
    if i!=0:
      for k in dp[i-1][j]:
        dp[i][j].add((k+board[i][j])%320)
        dp[i][j].add(abs(k-board[i][j])%320)
    if j!=0:
      for k in dp[i][j-1]:
        dp[i][j].add((k+board[i][j])%320)
        dp[i][j].add(abs(k-board[i][j])%320)
print(min(dp[h-1][w-1]))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
arr=[list(map(int,input().split())) for _ in range(2*h)]
board=[[0]*w for _ in range(h)]
for i in range(h):
  for j in range(w):
    board[i][j]+=arr[i][j]
for i in range(h):
  for j in range(w):
    board[i][j]=abs(board[i][j]-arr[h+i][j])
dp=[[set() for _ in range(w)] for _ in range(h)]
dp[0][0].add(board[0][0])
for i in range(h):
  for j in range(w):
    if i!=0:
      for k in dp[i-1][j]:
        dp[i][j].add((k+board[i][j])%161)
        dp[i][j].add(abs(k-board[i][j])%161)
    if j!=0:
      for k in dp[i][j-1]:
        dp[i][j].add((k+board[i][j])%161)
        dp[i][j].add(abs(k-board[i][j])%161)
print(min(dp[h-1][w-1]))

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
arr=[list(map(int,input().split())) for _ in range(2*h)]
board=[[0]*w for _ in range(h)]
for i in range(h):
  for j in range(w):
    board[i][j]+=arr[i][j]
for i in range(h):
  for j in range(w):
    board[i][j]=abs(board[i][j]-arr[h+i][j])
dp=[[set() for _ in range(w)] for _ in range(h)]
dp[0][0].add(board[0][0])
for i in range(h):
  for j in range(w):
    if i!=0:
      for k in dp[i-1][j]:
        dp[i][j].add((k+board[i][j])%160)
        dp[i][j].add(abs(k-board[i][j])%160)
    if j!=0:
      for k in dp[i][j-1]:
        dp[i][j].add((k+board[i][j])%160)
        dp[i][j].add(abs(k-board[i][j])%160)
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)]

C = [[0 for j in range(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 for k in range(1000)] for g in range(H*W)]
dp[0][C[0][0]] = 1

for g in range(H*W):
    i,j = divmod(g,W)
    for k in range(1000):
        if not dp[i*W+j][k]:
            continue
        ni,nj = i+1,j+1
        if ni < H:
            if k+C[ni][j]<1000:
                dp[ni*W+j][k+C[ni][j]] = 1
            dp[ni*W+j][abs(k-C[ni][j])] = 1
        if nj < W:
            if k+C[i][nj]<1000:
                dp[i*W+nj][k+C[i][nj]] = 1
            dp[i*W+nj][abs(k-C[i][nj])] = 1

for k in range(1000):
    if dp[-1][k]:
        print(k)
        break

Submission status: Wrong Answer

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, W = lr()
A = [lr() for _ in range(H)]
B = [lr() for _ in range(H)]
diff = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        diff[i][j] = abs(A[i][j] - B[i][j])

dp = [[[False] * 4001 for j in range(W)] for i in range(H)]
dp[0][0][diff[0][0]] = True
for i in range(H):
    for j in range(W):
        for k in range(4001):
            if dp[i][j][k]:
                if i < H-1:
                    dp[i+1][j][min(2600, k+diff[i+1][j])] = True
                    dp[i+1][j][abs(k-diff[i+1][j])] = True
                if j < W-1:
                    dp[i][j+1][min(2600, k+diff[i][j+1])] = True
                    dp[i][j+1][abs(k-diff[i][j+1])] = True

for k in range(100):
    if dp[H-1][W-1][k] == True:
        answer = k
        break

print(answer)

Submission status: Wrong Answer

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, W = lr()
A = [lr() for _ in range(H)]
B = [lr() for _ in range(H)]
diff = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        diff[i][j] = abs(A[i][j] - B[i][j])

dp = [[[False] * 4001 for j in range(W)] for i in range(H)]
dp[0][0][diff[0][0]] = True
for i in range(H):
    for j in range(W):
        for k in range(4001):
            if dp[i][j][k]:
                if i < H-1:
                    dp[i+1][j][min(4000, k+diff[i+1][j])] = True
                    dp[i+1][j][abs(k-diff[i+1][j])] = True
                if j < W-1:
                    dp[i][j+1][min(4000, k+diff[i][j+1])] = True
                    dp[i][j+1][abs(k-diff[i][j+1])] = True

for k in range(100):
    if dp[H-1][W-1][k] == True:
        answer = k
        break

print(answer)

Submission status: Wrong Answer

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, W = lr()
A = [lr() for _ in range(H)]
B = [lr() for _ in range(H)]
diff = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        diff[i][j] = abs(A[i][j] - B[i][j])

dp = [[[False] * 5001 for j in range(W)] for i in range(H)]
dp[0][0][diff[0][0]] = True
for i in range(H):
    for j in range(W):
        for k in range(5001):
            if dp[i][j][k]:
                if i < H-1:
                    dp[i+1][j][min(5000, k+diff[i+1][j])] = True
                    dp[i+1][j][abs(k-diff[i+1][j])] = True
                if j < W-1:
                    dp[i][j+1][min(5000, k+diff[i][j+1])] = True
                    dp[i][j+1][abs(k-diff[i][j+1])] = True

for k in range(100):
    if dp[H-1][W-1][k] == True:
        answer = k
        break

print(answer)

Submission status: Wrong Answer

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, W = lr()
A = [lr() for _ in range(H)]
B = [lr() for _ in range(H)]
diff = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        diff[i][j] = abs(A[i][j] - B[i][j])

dp = [[[False] * 3001 for j in range(W)] for i in range(H)]
dp[0][0][diff[0][0]] = True
for i in range(H):
    for j in range(W):
        for k in range(3001):
            if dp[i][j][k]:
                if i < H-1:
                    dp[i+1][j][min(3000, k+diff[i+1][j])] = True
                    dp[i+1][j][abs(k-diff[i+1][j])] = True
                if j < W-1:
                    dp[i][j+1][min(3000, k+diff[i][j+1])] = True
                    dp[i][j+1][abs(k-diff[i][j+1])] = True

for k in range(100):
    if dp[H-1][W-1][k] == True:
        answer = k
        break

print(answer)

Submission status: Wrong Answer

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, W = lr()
A = [lr() for _ in range(H)]
B = [lr() for _ in range(H)]
diff = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        diff[i][j] = abs(A[i][j] - B[i][j])

dp = [[[False] * 3001 for j in range(W)] for i in range(H)]
dp[0][0][diff[0][0]] = True
for i in range(H):
    for j in range(W):
        for k in range(3001):
            if dp[i][j][k]:
                if i < H-1:
                    if k < 2600:
                        dp[i+1][j][min(2500, k+diff[i+1][j])] = True
                    dp[i+1][j][abs(k-diff[i+1][j])] = True
                if j < W-1:
                    if k < 2600:
                        dp[i][j+1][min(2500, k+diff[i][j+1])] = True
                    dp[i][j+1][abs(k-diff[i][j+1])] = True

for k in range(100):
    if dp[H-1][W-1][k] == True:
        answer = k
        break

print(answer)

Submission status: Wrong Answer

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, W = lr()
A = [lr() for _ in range(H)]
B = [lr() for _ in range(H)]
diff = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        diff[i][j] = abs(A[i][j] - B[i][j])

dp = [[[False] * 3001 for j in range(W)] for i in range(H)]
dp[0][0][diff[0][0]] = True
for i in range(H):
    for j in range(W):
        for k in range(3001):
            if dp[i][j][k]:
                if i < H-1:
                    if k < 2600:
                        dp[i+1][j][min(3000, k+diff[i+1][j])] = True
                    dp[i+1][j][abs(k-diff[i+1][j])] = True
                if j < W-1:
                    if k < 2600:
                        dp[i][j+1][min(3000, k+diff[i][j+1])] = True
                    dp[i][j+1][abs(k-diff[i][j+1])] = True

for k in range(100):
    if dp[H-1][W-1][k] == True:
        answer = k
        break

print(answer)

Submission status: Wrong Answer

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, W = lr()
A = [lr() for _ in range(H)]
B = [lr() for _ in range(H)]
diff = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        diff[i][j] = abs(A[i][j] - B[i][j])

dp = [[[False] * 3001 for j in range(W)] for i in range(H)]
dp[0][0][diff[0][0]] = True
for i in range(H):
    for j in range(W):
        for k in range(3001):
            if dp[i][j][k]:
                if i < H-1:
                    if k < 2600:
                        dp[i+1][j][k+diff[i+1][j]] = True
                    dp[i+1][j][abs(k-diff[i+1][j])] = True
                if j < W-1:
                    if k < 2600:
                        dp[i][j+1][k+diff[i][j+1]] = True
                    dp[i][j+1][abs(k-diff[i][j+1])] = True

for k in range(100):
    if dp[H-1][W-1][k] == True:
        answer = k
        break

print(answer)

Submission status: Wrong Answer

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, W = lr()
A = [lr() for _ in range(H)]
B = [lr() for _ in range(H)]
diff = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        diff[i][j] = abs(A[i][j] - B[i][j])

dp = [[[False] * 3001 for j in range(W)] for i in range(H)]
dp[0][0][diff[0][0]] = True
for i in range(H):
    for j in range(W):
        for k in range(3001):
            if dp[i][j][k]:
                if i < H-1:
                    if k < 2600:
                        dp[i+1][j][min(2600, k+diff[i+1][j])] = True
                    dp[i+1][j][abs(k-diff[i+1][j])] = True
                if j < W-1:
                    if k < 2600:
                        dp[i][j+1][min(2600, k+diff[i][j+1])] = True
                    dp[i][j+1][abs(k-diff[i][j+1])] = True

for k in range(100):
    if dp[H-1][W-1][k] == True:
        answer = k
        break

print(answer)

Submission status: Wrong Answer

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, W = lr()
A = [lr() for _ in range(H)]
B = [lr() for _ in range(H)]
diff = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        diff[i][j] = abs(A[i][j] - B[i][j])

dp = [[[False] * 2001 for j in range(W)] for i in range(H)]
dp[0][0][diff[0][0]] = True
for i in range(H):
    for j in range(W):
        for k in range(2001):
            if dp[i][j][k]:
                if i < H-1:
                    if k < 1500:
                        dp[i+1][j][min(1500, k+diff[i+1][j])] = True
                    dp[i+1][j][abs(k-diff[i+1][j])] = True
                if j < W-1:
                    if k < 1100:
                        dp[i][j+1][min(1500, k+diff[i][j+1])] = True
                    dp[i][j+1][abs(k-diff[i][j+1])] = True

for k in range(100):
    if dp[H-1][W-1][k] == True:
        answer = k
        break

print(answer)

Submission status: Wrong Answer

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, W = lr()
A = [lr() for _ in range(H)]
B = [lr() for _ in range(H)]
diff = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        diff[i][j] = abs(A[i][j] - B[i][j])

dp = [[[False] * 1201 for j in range(W)] for i in range(H)]
dp[0][0][diff[0][0]] = True
for i in range(H):
    for j in range(W):
        for k in range(1201):
            if dp[i][j][k]:
                if i < H-1:
                    if k < 1100:
                        dp[i+1][j][min(1200, k+diff[i+1][j])] = True
                    dp[i+1][j][abs(k-diff[i+1][j])] = True
                if j < W-1:
                    dp[i][j+1][min(1200, k+diff[i][j+1])] = True
                    dp[i][j+1][abs(k-diff[i][j+1])] = True

for k in range(100):
    if dp[H-1][W-1][k] == True:
        answer = k
        break

print(answer)

Submission status: Wrong Answer

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, W = lr()
A = [lr() for _ in range(H)]
B = [lr() for _ in range(H)]
diff = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        diff[i][j] = abs(A[i][j] - B[i][j])

dp = [[[False] * 3000 for j in range(W)] for i in range(H)]
dp[0][0][diff[0][0]] = True
for i in range(H):
    for j in range(W):
        for k in range(3000):
            if dp[i][j][k]:
                if i < H-1:
                    if k < 2500:
                        dp[i+1][j][k+diff[i+1][j]] = True
                    dp[i+1][j][abs(k-diff[i+1][j])] = True
                if j < W-1:
                    if k < 2500:
                        dp[i][j+1][k+diff[i][j+1]] = True
                    dp[i][j+1][abs(k-diff[i][j+1])] = True

for k in range(200):
    if dp[H-1][W-1][k] == True:
        answer = k
        break

print(answer)

Submission status: Wrong Answer

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, W = lr()
A = [lr() for _ in range(H)]
B = [lr() for _ in range(H)]
diff = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        diff[i][j] = abs(A[i][j] - B[i][j])

dp = [[[False] * 3000 for j in range(W)] for i in range(H)]
dp[0][0][diff[0][0]] = True
for i in range(H):
    for j in range(W):
        for k in range(3000):
            if dp[i][j][k]:
                if i < H-1:
                    if k+diff[i+1][j] < 3000:
                        dp[i+1][j][k+diff[i+1][j]] = True
                    dp[i+1][j][abs(k-diff[i+1][j])] = True
                if j < W-1:
                    if k+diff[i][j+1] < 3000:
                        dp[i][j+1][k+diff[i][j+1]] = True
                    dp[i][j+1][abs(k-diff[i][j+1])] = True

for k in range(200):
    if dp[H-1][W-1][k] == True:
        answer = k
        break

print(answer)

Submission status: Wrong Answer

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, W = lr()
A = [lr() for _ in range(H)]
B = [lr() for _ in range(H)]
diff = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        diff[i][j] = abs(A[i][j] - B[i][j])

dp = [[[False] * 4000 for j in range(W)] for i in range(H)]
dp[0][0][diff[0][0]] = True
for i in range(H):
    for j in range(W):
        for k in range(4000):
            if dp[i][j][k]:
                if i < H-1:
                    dp[i+1][j][min(3999, k+diff[i+1][j])] = True
                    dp[i+1][j][abs(k-diff[i+1][j])] = True
                if j < W-1:
                    dp[i][j+1][min(3999, k+diff[i][j+1])] = True
                    dp[i][j+1][abs(k-diff[i][j+1])] = True

for k in range(200):
    if dp[H-1][W-1][k] == True:
        answer = k
        break

print(answer)

Submission status: Wrong Answer

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, W = lr()
A = [lr() for _ in range(H)]
B = [lr() for _ in range(H)]
diff = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        diff[i][j] = abs(A[i][j] - B[i][j])

dp = [[[False] * 2000 for j in range(W)] for i in range(H)]
dp[0][0][diff[0][0]] = True
for i in range(H):
    for j in range(W):
        for k in range(2000):
            if dp[i][j][k]:
                if i < H-1:
                    dp[i+1][j][min(1999, k+diff[i+1][j])] = True
                    dp[i+1][j][abs(k-diff[i+1][j])] = True
                if j < W-1:
                    dp[i][j+1][min(1999, k+diff[i][j+1])] = True
                    dp[i][j+1][abs(k-diff[i][j+1])] = True

for k in range(100):
    if dp[H-1][W-1][k] == True:
        answer = k
        break

print(answer)

Submission status: Wrong Answer

Solution code:
import sys

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, W = lr()
A = [lr() for _ in range(H)]
B = [lr() for _ in range(H)]
diff = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        diff[i][j] = abs(A[i][j] - B[i][j])

dp = [[[False] * 1201 for j in range(W)] for i in range(H)]
dp[0][0][diff[0][0]] = True
for i in range(H):
    for j in range(W):
        for k in range(1201):
            if dp[i][j][k]:
                if i < H-1:
                    dp[i+1][j][min(1200, k+diff[i+1][j])] = True
                    dp[i+1][j][abs(k-diff[i+1][j])] = True
                if j < W-1:
                    dp[i][j+1][min(1200, k+diff[i][j+1])] = True
                    dp[i][j+1][abs(k-diff[i][j+1])] = True

for k in range(100):
    if dp[H-1][W-1][k] == True:
        answer = k
        break

print(answer)

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)]
D = [[abs(A[y][x] - B[y][x]) for x in range(w)] for y in range(h)]

mask = 1 << 8000

DP = [[0] * w for _ in range(h+1)]
DP[0][0] = mask >> D[0][0] | mask << D[0][0]

for y in range(h-1):
    for x in range(w-1):
        current = DP[y][x]
        DP[y][x+1] |= current << D[y][x+1] | current >> D[y][x+1]
        DP[y+1][x] |= current << D[y+1][x] | current >> D[y+1][x]

for x in range(w-1):
    current = DP[h-1][x]
    DP[h-1][x+1] |= current << D[h-1][x+1] | current >> D[h-1][x+1]

pattern = DP[h-1][w-1]
for idx in range(8000):
    if (pattern >> (8000+idx))&1 or (pattern >> (8000-idx))&1:
        print(idx)
        exit()

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)]
D = [[abs(A[y][x] - B[y][x]) for x in range(w)] for y in range(h)]

mask = 1 << 8000

DP = [[0] * w for _ in range(h+1)]
DP[0][0] = mask >> D[0][0]

for y in range(h-1):
    for x in range(w-1):
        current = DP[y][x]
        DP[y][x+1] |= current << D[y][x+1] | current >> D[y][x+1]
        DP[y+1][x] = current << D[y+1][x] | current >> D[y+1][x]

for x in range(w-1):
    current = DP[h-1][x]
    DP[h-1][x+1] |= current << D[h-1][x+1] | current >> D[h-1][x+1]

pattern = DP[h-1][w-1]
for idx in range(8000):
    if (pattern >> (8000+idx))&1 or (pattern >> (8000-idx))&1:
        print(idx)
        exit()