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: Time Limit Exceeded

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)]
n = 80*(h+w)
dp = [[[False for k in range(n)]for j in range(w)] for i in range(h)]
dp[0][0][abs(a[0][0]-b[0][0])]=True

for i in range(h-1):
    ta = a[i+1][0]
    tb = b[i+1][0]
    td = abs(ta-tb)
    for k in range(n):
        if k+td < n:
            dp[i+1][0][k+td] = dp[i+1][0][k+td] or dp[i][0][k]
        if abs(k-td) < n:
            dp[i+1][0][abs(k-td)] = dp[i+1][0][abs(k-td)] or dp[i][0][k]

for j in range(w-1):
    ta = a[0][j+1]
    tb = b[0][j+1]
    td = abs(ta-tb)
    for k in range(n):
        if k+td < n:
            dp[0][j+1][k+td] = dp[0][j+1][k+td] or dp[0][j][k]
        if abs(k-td) < n:
            dp[0][j+1][abs(k-td)] = dp[0][j+1][abs(k-td)] or dp[0][j][k]



for i in range(h-1):
    for j in range(w-1):
        ta = a[i+1][j+1]
        tb = b[i+1][j+1]
        td = abs(ta-tb)
        for k in range(n):
            if k+td < n:
                dp[i+1][j+1][k+td] = dp[i+1][j+1][k+td] or dp[i+1][j][k] or dp[i][j+1][k]
            if abs(k-td) < n:
                dp[i+1][j+1][abs(k-td)] = dp[i+1][j+1][abs(k-td)] or dp[i+1][j][k] or dp[i][j+1][k]

res = -1
t = 0
while res == -1:
    if dp[h-1][w-1][t]:
        res = t
    else:
        t += 1
print(res)

Submission status: Time Limit Exceeded

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


h, w = map(int, input().split())
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)


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


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


DP = [[set() for j in range(w)] for i in range(h)]
DP[0][0] = {abs(A[0][0] - B[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)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
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])
 
V = [[2] * w for i in range(h)]
for i in range(h):
    V[i][0] -= 1
for i in range(w):
    V[0][i] -= 1
 
l = 7000
m = 0
 
M = [[[0] * l for i in range(w)] for j in range(h)]
N = [[0 for i in range(w)] for j in range(h)]
N[0][0] = C[0][0]
 
s = 0
Q = []
for i in range(h):
    Q.append([i, 0])
for i in range(1, w):
    Q.append([0, i])
for i in range(1, h):
    for j in range(1, w):
        Q.append([i, j])

M[0][0][C[0][0]] = 1
# M[0][0][m-C[0][0]] = 1
 
for s in range(len(Q)):
    i = Q[s][0]
    j = Q[s][1]
    if i+1 < h:
        for ii in range(min(N[i][j]+1, l)):
            if M[i][j][ii] == 1:
                if abs(ii+C[i+1][j]) < l:
                    M[i+1][j][abs(ii+C[i+1][j])] = 1
                if abs(ii-C[i+1][j]) < l:
                    M[i+1][j][abs(ii-C[i+1][j])] = 1

        N[i+1][j] = max(N[i+1][j], N[i][j] + C[i+1][j])

    if j+1 < w:
        for ii in range(min(N[i][j]+1, l)):
            if M[i][j][ii] == 1:
                if abs(ii+C[i][j+1]) < l:
                    M[i][j+1][abs(ii+C[i][j+1])] = 1
                if abs(ii-C[i][j+1]) < l:
                    M[i][j+1][abs(ii-C[i][j+1])] = 1

        N[i][j+1] = max(N[i][j+1], N[i][j] + C[i][j+1])
 
# print(M[-1][-1])
 
ans = 10**5
for i in range(len(M[-1][-1])):
    if M[-1][-1][i] == 1:
        ans = i
        break
print(ans)

Submission status: Time Limit Exceeded

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)]
dp=[[[] for i in range(W)] for j in range(H)]

dp[0][0].append(abs(A[0][0]-B[0][0]))
for i in range(1,W):
    for j in dp[0][i-1]:
        dp[0][i].append(j+abs(A[0][i]-B[0][i]))
        dp[0][i].append(abs(j-abs(A[0][i]-B[0][i])))
for i in range(1,H):
    for j in dp[i-1][0]:
        dp[i][0].append(j+abs(A[i][0]-B[i][0]))
        dp[i][0].append(abs(j-abs(A[i][0]-B[i][0])))
for i in range(1,H):
    for j in range(1,W):
        abij=abs(A[i][j]-B[i][j])
        for k in dp[i][j-1]:
            dp[i][j].append(k+abij)
            dp[i][j].append(abs(k-abij))
        for k in dp[i-1][j]:
            dp[i][j].append(k+abij)
            dp[i][j].append(abs(k-abij))

print(min(dp[H-1][W-1]))

Submission status: Time Limit Exceeded

Solution code:
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: set):
    new = set()
    delta = D[y+1][x]
    for num in possible:
        if num + delta <=6400:
            new.add(num+delta)
        if abs(num-delta) <= 6400:
            new.add(abs(num-delta))
    return new


def slide_right(y, x, possible: set):
    new = set()
    delta = D[y][x+1]
    for num in possible:
        if num + delta <=6400:
            new.add(num+delta)
        if abs(num-delta) <=6400:
            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)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[21]:


import sys
input = sys.stdin.readline


# In[1]:


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


# In[4]:


A = [[int(i) for i in input().split()] for i in range(H)]


# In[5]:


B = [[int(i) for i in input().split()] for i in range(H)]


# In[10]:


C = [[0 for _ in range(W)] for _ in range(H)]
for tate in range(H):
    for yoko in range(W):
        C[tate][yoko] = abs(A[tate][yoko] - B[tate][yoko])


# In[11]:


A


# In[12]:


B


# In[13]:


C


# In[15]:


M = 4000
d = [[[0 for _ in range(M+1)] for _ in range(W)] for _ in range(H)]


# 左上
d[0][0][C[0][0]] = 1
# 上1行
for yoko in range(1, W):
    for i in range(M+1):
        if i + C[0][yoko] <= M and d[0][yoko-1][i + C[0][yoko]] == 1:
            d[0][yoko][i] = 1
        if abs(i - C[0][yoko]) <= M and d[0][yoko-1][abs(i - C[0][yoko])] == 1:
            d[0][yoko][i] = 1
#         if 0 <= C[0][yoko] - i <= M and d[0][yoko-1][C[0][yoko] - i] == 1:
#             d[0][yoko][i] = 1

# # 左端
# for i in range(M+1):
#     # 上から
#     if 0 <= i - C[1][0] <= M and d[0][0][i - C[1][0]] == 1:
#         d[1][0][i] = 1
#     if 0 <= i + C[1][0] <= M and d[0][0][i + C[1][0]] == 1:
#         d[1][0][i] = 1
# 中1行
for tate in range(1, H):
    for yoko in range(0, W):
        # 上から
        for i in range(M+1):
#             if i - C[tate][yoko] <= M and d[tate-1][yoko][i - C[tate][yoko]] == 1:
#                 d[tate][yoko][i] = 1
            if abs(C[tate][yoko] - i) <= M and d[tate-1][yoko][abs(C[tate][yoko] - i)] == 1:
                d[tate][yoko][i] = 1
            if 0 <= i + C[tate][yoko] <= M and d[tate-1][yoko][i + C[tate][yoko]] == 1:
                d[tate][yoko][i] = 1
        
        if yoko >= 1:
            # 左から
            for i in range(M+1):
#                 if i - C[tate][yoko] <= M and d[tate][yoko-1][i - C[tate][yoko]] == 1:
#                     d[tate][yoko][i] = 1
                if abs(C[tate][yoko] - i) <= M and d[tate][yoko-1][abs(C[tate][yoko] - i)] == 1:
                    d[tate][yoko][i] = 1
                if 0 <= i + C[tate][yoko] <= M and d[tate][yoko-1][i + C[tate][yoko]] == 1:
                    d[tate][yoko][i] = 1


# In[16]:


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


# In[ ]:

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[21]:


import sys
input = sys.stdin.readline


# In[1]:


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


# In[4]:


A = [[int(i) for i in input().split()] for i in range(H)]


# In[5]:


B = [[int(i) for i in input().split()] for i in range(H)]


# In[10]:


C = [[0 for _ in range(W)] for _ in range(H)]
for tate in range(H):
    for yoko in range(W):
        C[tate][yoko] = abs(A[tate][yoko] - B[tate][yoko])


# In[11]:


A


# In[12]:


B


# In[13]:


C


# In[15]:


M = 6400
d = [[[0 for _ in range(M+1)] for _ in range(W)] for _ in range(H)]


# 左上
d[0][0][C[0][0]] = 1
# 上1行
for yoko in range(1, W):
    for i in range(M+1):
        if i + C[0][yoko] <= M and d[0][yoko-1][i + C[0][yoko]] == 1:
            d[0][yoko][i] = 1
        if abs(i - C[0][yoko]) <= M and d[0][yoko-1][abs(i - C[0][yoko])] == 1:
            d[0][yoko][i] = 1
#         if 0 <= C[0][yoko] - i <= M and d[0][yoko-1][C[0][yoko] - i] == 1:
#             d[0][yoko][i] = 1

# # 左端
# for i in range(M+1):
#     # 上から
#     if 0 <= i - C[1][0] <= M and d[0][0][i - C[1][0]] == 1:
#         d[1][0][i] = 1
#     if 0 <= i + C[1][0] <= M and d[0][0][i + C[1][0]] == 1:
#         d[1][0][i] = 1
# 中1行
for tate in range(1, H):
    for yoko in range(0, W):
        # 上から
        for i in range(M+1):
#             if i - C[tate][yoko] <= M and d[tate-1][yoko][i - C[tate][yoko]] == 1:
#                 d[tate][yoko][i] = 1
            if abs(C[tate][yoko] - i) <= M and d[tate-1][yoko][abs(C[tate][yoko] - i)] == 1:
                d[tate][yoko][i] = 1
            if 0 <= i + C[tate][yoko] <= M and d[tate-1][yoko][i + C[tate][yoko]] == 1:
                d[tate][yoko][i] = 1
        
        if yoko >= 1:
            # 左から
            for i in range(M+1):
#                 if i - C[tate][yoko] <= M and d[tate][yoko-1][i - C[tate][yoko]] == 1:
#                     d[tate][yoko][i] = 1
                if abs(C[tate][yoko] - i) <= M and d[tate][yoko-1][abs(C[tate][yoko] - i)] == 1:
                    d[tate][yoko][i] = 1
                if 0 <= i + C[tate][yoko] <= M and d[tate][yoko-1][i + C[tate][yoko]] == 1:
                    d[tate][yoko][i] = 1


# In[16]:


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


# In[ ]:

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[21]:


import sys
input = sys.stdin.readline


# In[1]:


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


# In[4]:


A = [[int(i) for i in input().split()] for i in range(H)]


# In[5]:


B = [[int(i) for i in input().split()] for i in range(H)]


# In[10]:


C = [[0 for _ in range(W)] for _ in range(H)]
for tate in range(H):
    for yoko in range(W):
        C[tate][yoko] = abs(A[tate][yoko] - B[tate][yoko])


# In[11]:


A


# In[12]:


B


# In[13]:


C


# In[15]:


M = 6400
d = [[[0 for _ in range(M+1)] for _ in range(W)] for _ in range(H)]


# 左上
d[0][0][C[0][0]] = 1
# 上1行
for yoko in range(1, W):
    for i in range(M+1):
        if 0 <= i + C[0][yoko] <= M and d[0][yoko-1][i + C[0][yoko]] == 1:
            d[0][yoko][i] = 1
        if 0 <= i - C[0][yoko] <= M and d[0][yoko-1][i - C[0][yoko]] == 1:
            d[0][yoko][i] = 1
        if 0 <= C[0][yoko] - i <= M and d[0][yoko-1][C[0][yoko] - i] == 1:
            d[0][yoko][i] = 1

# # 左端
# for i in range(M+1):
#     # 上から
#     if 0 <= i - C[1][0] <= M and d[0][0][i - C[1][0]] == 1:
#         d[1][0][i] = 1
#     if 0 <= i + C[1][0] <= M and d[0][0][i + C[1][0]] == 1:
#         d[1][0][i] = 1
# 中1行
for tate in range(1, H):
    for yoko in range(0, W):
        # 上から
        for i in range(M+1):
            if 0 <= i - C[tate][yoko] <= M and d[tate-1][yoko][i - C[tate][yoko]] == 1:
                d[tate][yoko][i] = 1
            if 0 <= C[tate][yoko] - i <= M and d[tate-1][yoko][C[tate][yoko] - i] == 1:
                d[tate][yoko][i] = 1
            if 0 <= i + C[tate][yoko] <= M and d[tate-1][yoko][i + C[tate][yoko]] == 1:
                d[tate][yoko][i] = 1
        
        if yoko >= 1:
            # 左から
            for i in range(M+1):
                if 0 <= i - C[tate][yoko] <= M and d[tate][yoko-1][i - C[tate][yoko]] == 1:
                    d[tate][yoko][i] = 1
                if 0 <= C[tate][yoko] - i <= M and d[tate][yoko-1][C[tate][yoko] - i] == 1:
                    d[tate][yoko][i] = 1
                if 0 <= i + C[tate][yoko] <= M and d[tate][yoko-1][i + C[tate][yoko]] == 1:
                    d[tate][yoko][i] = 1


# In[16]:


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


# In[ ]:

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[3]:


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


# In[4]:


A = [[int(i) for i in input().split()] for i in range(H)]


# In[5]:


B = [[int(i) for i in input().split()] for i in range(H)]


# In[10]:


C = [[0 for _ in range(W)] for _ in range(H)]
for tate in range(H):
    for yoko in range(W):
        C[tate][yoko] = abs(A[tate][yoko] - B[tate][yoko])


# In[11]:


A


# In[12]:


B


# In[13]:


C


# In[15]:


M = 3750
d = [[[0 for _ in range(M+1)] for _ in range(W)] for _ in range(H)]


# 左上
d[0][0][C[0][0]] = 1
# 上1行
for yoko in range(1, W):
    for i in range(M+1):
        if 0 <= i + C[0][yoko] <= M and d[0][yoko-1][i + C[0][yoko]] == 1:
            d[0][yoko][i] = 1
        if 0 <= i - C[0][yoko] <= M and d[0][yoko-1][i - C[0][yoko]] == 1:
            d[0][yoko][i] = 1
        if 0 <= C[0][yoko] - i <= M and d[0][yoko-1][C[0][yoko] - i] == 1:
            d[0][yoko][i] = 1

# # 左端
# for i in range(M+1):
#     # 上から
#     if 0 <= i - C[1][0] <= M and d[0][0][i - C[1][0]] == 1:
#         d[1][0][i] = 1
#     if 0 <= i + C[1][0] <= M and d[0][0][i + C[1][0]] == 1:
#         d[1][0][i] = 1
# 中1行
for tate in range(1, H):
    for yoko in range(0, W):
        # 上から
        for i in range(M+1):
            if 0 <= i - C[tate][yoko] <= M and d[tate-1][yoko][i - C[tate][yoko]] == 1:
                d[tate][yoko][i] = 1
            if 0 <= C[tate][yoko] - i <= M and d[tate-1][yoko][C[tate][yoko] - i] == 1:
                d[tate][yoko][i] = 1
            if 0 <= i + C[tate][yoko] <= M and d[tate-1][yoko][i + C[tate][yoko]] == 1:
                d[tate][yoko][i] = 1
        
        if yoko >= 1:
            # 左から
            for i in range(M+1):
                if 0 <= i - C[tate][yoko] <= M and d[tate][yoko-1][i - C[tate][yoko]] == 1:
                    d[tate][yoko][i] = 1
                if 0 <= C[tate][yoko] - i <= M and d[tate][yoko-1][C[tate][yoko] - i] == 1:
                    d[tate][yoko][i] = 1
                if 0 <= i + C[tate][yoko] <= M and d[tate][yoko-1][i + C[tate][yoko]] == 1:
                    d[tate][yoko][i] = 1


# In[16]:


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


# In[ ]:

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[3]:


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


# In[4]:


A = [[int(i) for i in input().split()] for i in range(H)]


# In[5]:


B = [[int(i) for i in input().split()] for i in range(H)]


# In[10]:


C = [[0 for _ in range(W)] for _ in range(H)]
for tate in range(H):
    for yoko in range(W):
        C[tate][yoko] = abs(A[tate][yoko] - B[tate][yoko])


# In[11]:


A


# In[12]:


B


# In[13]:


C


# In[15]:


M = 5000
d = [[[0 for _ in range(M+1)] for _ in range(W)] for _ in range(H)]


# 左上
d[0][0][C[0][0]] = 1
# 上1行
for yoko in range(1, W):
    for i in range(M+1):
        if 0 <= i + C[0][yoko] <= M and d[0][yoko-1][i + C[0][yoko]] == 1:
            d[0][yoko][i] = 1
        if 0 <= i - C[0][yoko] <= M and d[0][yoko-1][i - C[0][yoko]] == 1:
            d[0][yoko][i] = 1
        if 0 <= C[0][yoko] - i <= M and d[0][yoko-1][C[0][yoko] - i] == 1:
            d[0][yoko][i] = 1

# # 左端
# for i in range(M+1):
#     # 上から
#     if 0 <= i - C[1][0] <= M and d[0][0][i - C[1][0]] == 1:
#         d[1][0][i] = 1
#     if 0 <= i + C[1][0] <= M and d[0][0][i + C[1][0]] == 1:
#         d[1][0][i] = 1
# 中1行
for tate in range(1, H):
    for yoko in range(0, W):
        # 上から
        for i in range(M+1):
            if 0 <= i - C[tate][yoko] <= M and d[tate-1][yoko][i - C[tate][yoko]] == 1:
                d[tate][yoko][i] = 1
            if 0 <= C[tate][yoko] - i <= M and d[tate-1][yoko][C[tate][yoko] - i] == 1:
                d[tate][yoko][i] = 1
            if 0 <= i + C[tate][yoko] <= M and d[tate-1][yoko][i + C[tate][yoko]] == 1:
                d[tate][yoko][i] = 1
        
        if yoko >= 1:
            # 左から
            for i in range(M+1):
                if 0 <= i - C[tate][yoko] <= M and d[tate][yoko-1][i - C[tate][yoko]] == 1:
                    d[tate][yoko][i] = 1
                if 0 <= C[tate][yoko] - i <= M and d[tate][yoko-1][C[tate][yoko] - i] == 1:
                    d[tate][yoko][i] = 1
                if 0 <= i + C[tate][yoko] <= M and d[tate][yoko-1][i + C[tate][yoko]] == 1:
                    d[tate][yoko][i] = 1


# In[16]:


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


# In[ ]:

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[1]:


import numpy as np


# In[2]:


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


# In[3]:


A = [[int(i) for i in input().split()] for i in range(H)]


# In[4]:


B = [[int(i) for i in input().split()] for i in range(H)]


# In[5]:


A = np.array(A)
B = np.array(B)


# In[6]:


C = abs(A - B)


# In[7]:


A


# In[8]:


B


# In[9]:


C


# In[10]:


M = 100
d = [[[0 for _ in range(M+1)] for _ in range(W)] for _ in range(H)]


# 左上
d[0][0][C[0, 0]] = 1
# 上1行
for yoko in range(1, W):
    for i in range(M+1):
        if 0 <= i + C[0, yoko] <= M and d[0][yoko-1][i + C[0, yoko]] == 1:
            d[0][yoko][i] = 1
        if 0 <= i - C[0, yoko] <= M and d[0][yoko-1][i - C[0, yoko]] == 1:
            d[0][yoko][i] = 1
        if 0 <= C[0, yoko] - i <= M and d[0][yoko-1][C[0, yoko] - i] == 1:
            d[0][yoko][i] = 1

# # 左端
# for i in range(M+1):
#     # 上から
#     if 0 <= i - C[1, 0] <= M and d[0][0][i - C[1, 0]] == 1:
#         d[1][0][i] = 1
#     if 0 <= i + C[1, 0] <= M and d[0][0][i + C[1, 0]] == 1:
#         d[1][0][i] = 1
# 中1行
for tate in range(1, H):
    for yoko in range(0, W):
        # 上から
        for i in range(M+1):
            if 0 <= i - C[tate, yoko] <= M and d[tate-1][yoko][i - C[tate, yoko]] == 1:
                d[tate][yoko][i] = 1
            if 0 <= C[tate, yoko] - i <= M and d[tate-1][yoko][C[tate, yoko] - i] == 1:
                d[tate][yoko][i] = 1
            if 0 <= i + C[tate, yoko] <= M and d[tate-1][yoko][i + C[tate, yoko]] == 1:
                d[tate][yoko][i] = 1
        
        if yoko >= 1:
            # 左から
            for i in range(M+1):
                if 0 <= i - C[tate, yoko] <= M and d[tate][yoko-1][i - C[tate, yoko]] == 1:
                    d[tate][yoko][i] = 1
                if 0 <= C[tate, yoko] - i <= M and d[tate][yoko-1][C[tate, yoko] - i] == 1:
                    d[tate][yoko][i] = 1
                if 0 <= i + C[tate, yoko] <= M and d[tate][yoko-1][i + C[tate, yoko]] == 1:
                    d[tate][yoko][i] = 1


# In[11]:


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


# In[ ]:

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[1]:


import numpy as np


# In[2]:


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


# In[3]:


A = [[int(i) for i in input().split()] for i in range(H)]


# In[4]:


B = [[int(i) for i in input().split()] for i in range(H)]


# In[5]:


A = np.array(A)
B = np.array(B)


# In[6]:


C = abs(A - B)


# In[7]:


A


# In[8]:


B


# In[9]:


C


# In[10]:


M = 1000
d = [[[0 for _ in range(M+1)] for _ in range(W)] for _ in range(H)]


# 左上
d[0][0][C[0, 0]] = 1
# 上1行
for yoko in range(1, W):
    for i in range(M+1):
        if 0 <= i + C[0, yoko] <= M and d[0][yoko-1][i + C[0, yoko]] == 1:
            d[0][yoko][i] = 1
        if 0 <= i - C[0, yoko] <= M and d[0][yoko-1][i - C[0, yoko]] == 1:
            d[0][yoko][i] = 1
        if 0 <= C[0, yoko] - i <= M and d[0][yoko-1][C[0, yoko] - i] == 1:
            d[0][yoko][i] = 1

# # 左端
# for i in range(M+1):
#     # 上から
#     if 0 <= i - C[1, 0] <= M and d[0][0][i - C[1, 0]] == 1:
#         d[1][0][i] = 1
#     if 0 <= i + C[1, 0] <= M and d[0][0][i + C[1, 0]] == 1:
#         d[1][0][i] = 1
# 中1行
for tate in range(1, H):
    for yoko in range(0, W):
        # 上から
        for i in range(M+1):
            if 0 <= i - C[tate, yoko] <= M and d[tate-1][yoko][i - C[tate, yoko]] == 1:
                d[tate][yoko][i] = 1
            if 0 <= C[tate, yoko] - i <= M and d[tate-1][yoko][C[tate, yoko] - i] == 1:
                d[tate][yoko][i] = 1
            if 0 <= i + C[tate, yoko] <= M and d[tate-1][yoko][i + C[tate, yoko]] == 1:
                d[tate][yoko][i] = 1
        
        if yoko >= 1:
            # 左から
            for i in range(M+1):
                if 0 <= i - C[tate, yoko] <= M and d[tate][yoko-1][i - C[tate, yoko]] == 1:
                    d[tate][yoko][i] = 1
                if 0 <= C[tate, yoko] - i <= M and d[tate][yoko-1][C[tate, yoko] - i] == 1:
                    d[tate][yoko][i] = 1
                if 0 <= i + C[tate, yoko] <= M and d[tate][yoko-1][i + C[tate, yoko]] == 1:
                    d[tate][yoko][i] = 1


# In[11]:


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


# In[ ]:

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[3]:


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


# In[4]:


A = [[int(i) for i in input().split()] for i in range(H)]


# In[5]:


B = [[int(i) for i in input().split()] for i in range(H)]


# In[10]:


C = [[0 for _ in range(W)] for _ in range(H)]
for tate in range(H):
    for yoko in range(W):
        C[tate][yoko] = abs(A[tate][yoko] - B[tate][yoko])


# In[11]:


A


# In[12]:


B


# In[13]:


C


# In[15]:


M = 10000
d = [[[0 for _ in range(M+1)] for _ in range(W)] for _ in range(H)]


# 左上
d[0][0][C[0][0]] = 1
# 上1行
for yoko in range(1, W):
    for i in range(M+1):
        if 0 <= i + C[0][yoko] <= M and d[0][yoko-1][i + C[0][yoko]] == 1:
            d[0][yoko][i] = 1
        if 0 <= i - C[0][yoko] <= M and d[0][yoko-1][i - C[0][yoko]] == 1:
            d[0][yoko][i] = 1
        if 0 <= C[0][yoko] - i <= M and d[0][yoko-1][C[0][yoko] - i] == 1:
            d[0][yoko][i] = 1

# # 左端
# for i in range(M+1):
#     # 上から
#     if 0 <= i - C[1][0] <= M and d[0][0][i - C[1][0]] == 1:
#         d[1][0][i] = 1
#     if 0 <= i + C[1][0] <= M and d[0][0][i + C[1][0]] == 1:
#         d[1][0][i] = 1
# 中1行
for tate in range(1, H):
    for yoko in range(0, W):
        # 上から
        for i in range(M+1):
            if 0 <= i - C[tate][yoko] <= M and d[tate-1][yoko][i - C[tate][yoko]] == 1:
                d[tate][yoko][i] = 1
            if 0 <= C[tate][yoko] - i <= M and d[tate-1][yoko][C[tate][yoko] - i] == 1:
                d[tate][yoko][i] = 1
            if 0 <= i + C[tate][yoko] <= M and d[tate-1][yoko][i + C[tate][yoko]] == 1:
                d[tate][yoko][i] = 1
        
        if yoko >= 1:
            # 左から
            for i in range(M+1):
                if 0 <= i - C[tate][yoko] <= M and d[tate][yoko-1][i - C[tate][yoko]] == 1:
                    d[tate][yoko][i] = 1
                if 0 <= C[tate][yoko] - i <= M and d[tate][yoko-1][C[tate][yoko] - i] == 1:
                    d[tate][yoko][i] = 1
                if 0 <= i + C[tate][yoko] <= M and d[tate][yoko-1][i + C[tate][yoko]] == 1:
                    d[tate][yoko][i] = 1


# In[16]:


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


# In[ ]:

Submission status: Time Limit Exceeded

Solution code:
import queue

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

q = queue.Queue()
q.put((0,1,abs(A[0][0] - B[0][0])))
q.put((1,0,abs(A[0][0] - B[0][0])))
while not q.empty():
  h,w,k = q.get()
  dp[h][w][k + abs(A[h][w] - B[h][w])] = True
  dp[h][w][abs(k - abs(A[h][w] - B[h][w]))] = True
  if h < H-1:
    q.put((h+1,w,k + abs(A[h][w] - B[h][w])))
    q.put((h+1,w,abs(k - abs(A[h][w] - B[h][w]))))
  if w < W-1:
    q.put((h,w+1,abs(k - abs(A[h][w] - B[h][w]))))
    q.put((h,w+1,k + abs(A[h][w] - B[h][w])))

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

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[1]:


import numpy as np


# In[2]:


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


# In[3]:


A = [[int(i) for i in input().split()] for i in range(H)]


# In[4]:


B = [[int(i) for i in input().split()] for i in range(H)]


# In[5]:


A = np.array(A)
B = np.array(B)


# In[6]:


C = abs(A - B)


# In[7]:


A


# In[8]:


B


# In[9]:


C


# In[10]:


M = 10000
d = [[[0 for _ in range(M+1)] for _ in range(W)] for _ in range(H)]


# 左上
d[0][0][C[0, 0]] = 1
# 上1行
for yoko in range(1, W):
    for i in range(M+1):
        if 0 <= i + C[0, yoko] <= M and d[0][yoko-1][i + C[0, yoko]] == 1:
            d[0][yoko][i] = 1
        if 0 <= i - C[0, yoko] <= M and d[0][yoko-1][i - C[0, yoko]] == 1:
            d[0][yoko][i] = 1
        if 0 <= C[0, yoko] - i <= M and d[0][yoko-1][C[0, yoko] - i] == 1:
            d[0][yoko][i] = 1

# # 左端
# for i in range(M+1):
#     # 上から
#     if 0 <= i - C[1, 0] <= M and d[0][0][i - C[1, 0]] == 1:
#         d[1][0][i] = 1
#     if 0 <= i + C[1, 0] <= M and d[0][0][i + C[1, 0]] == 1:
#         d[1][0][i] = 1
# 中1行
for tate in range(1, H):
    for yoko in range(0, W):
        # 上から
        for i in range(M+1):
            if 0 <= i - C[tate, yoko] <= M and d[tate-1][yoko][i - C[tate, yoko]] == 1:
                d[tate][yoko][i] = 1
            if 0 <= C[tate, yoko] - i <= M and d[tate-1][yoko][C[tate, yoko] - i] == 1:
                d[tate][yoko][i] = 1
            if 0 <= i + C[tate, yoko] <= M and d[tate-1][yoko][i + C[tate, yoko]] == 1:
                d[tate][yoko][i] = 1
        
        if yoko >= 1:
            # 左から
            for i in range(M+1):
                if 0 <= i - C[tate, yoko] <= M and d[tate][yoko-1][i - C[tate, yoko]] == 1:
                    d[tate][yoko][i] = 1
                if 0 <= C[tate, yoko] - i <= M and d[tate][yoko-1][C[tate, yoko] - i] == 1:
                    d[tate][yoko][i] = 1
                if 0 <= i + C[tate, yoko] <= M and d[tate][yoko-1][i + C[tate, yoko]] == 1:
                    d[tate][yoko][i] = 1


# In[11]:


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


# In[ ]:

Submission status: Time Limit Exceeded

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

def main():
    H, W = map(int, input().split())
    P = W+1
    A = [list(map(int, input().split())) for _ in range(H)]
    B = [list(map(int, input().split())) for _ in range(H)]
    L = 80*80
    dp = [[False]*(L+1) for _ in range((W+1)*(H+1))]
    for h in range(H+1):
        dp[h*P][0] = True
    for w in range(H+1):
        dp[w][0] = True
    
    for h in range(H):
        for w in range(W):
            c = abs(A[h][w] - B[h][w])
            for l in range(L+1):
                b1 = abs(l-c)
                b2 = abs(l+c)
                if b1 <= L:
                    dp[(h+1)*P+w+1][l] = dp[h*P+w+1][b1] or dp[(h+1)*P+w][b1]
                if b2 <= L:
                    dp[(h+1)*P+w+1][l] = dp[(h+1)*P+w+1][l] or dp[h*P+w+1][b2] or dp[(h+1)*P+w][b2]

    for l in range(L+1):
        if dp[H*P+W][l]:
            print(l)
            break


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

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

def main():
    H, W = map(int, input().split())
    P = W+1
    A = [list(map(int, input().split())) for _ in range(H)]
    B = [list(map(int, input().split())) for _ in range(H)]
    L = 80*80*2
    dp = [[False]*(L+1) for _ in range((W+1)*(H+1))]
    for h in range(H+1):
        dp[h*P][0] = True
    for w in range(H+1):
        dp[w][0] = True
    
    for h in range(H):
        for w in range(W):
            c = abs(A[h][w] - B[h][w])
            for l in range(L+1):
                b1 = abs(l-c)
                b2 = abs(l+c)
                if b1 <= L:
                    dp[(h+1)*P+w+1][l] = dp[h*P+w+1][b1] or dp[(h+1)*P+w][b1]
                if b2 <= L:
                    dp[(h+1)*P+w+1][l] = dp[(h+1)*P+w+1][l] or dp[h*P+w+1][b2] or dp[(h+1)*P+w][b2]

    for l in range(L+1):
        if dp[H*P+W][l]:
            print(l)
            break


if __name__ == "__main__":
    main()