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:
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)]
D = [[abs(A[h][w]-B[h][w]) for w in range(W)] for h in range(H)]
dp = [[set() for _ in range(W)] for _ in range(H)]
dp[0][0].add(D[0][0])
for h in range(H):
    for w in range(W):
        if 0 < h:
            for v in dp[h-1][w]:
                v1 = v+D[h][w]
                v2 = v-D[h][w] if v>=D[h][w] else -v+D[h][w]
                dp[h][w].add(v1)
                dp[h][w].add(v2)
        if 0 < w:
            for v in dp[h][w-1]:
                v1 = v+D[h][w]
                v2 = v-D[h][w] if v>=D[h][w] else -v+D[h][w]
                dp[h][w].add(v1)
                dp[h][w].add(v2)
                
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 _ in range(H)]
B = [list(map(int,input().split())) for _ in range(H)]
D = [[abs(A[h][w]-B[h][w]) for w in range(W)] for h in range(H)]
dp = [[{} for _ in range(W)] for _ in range(H)]
dp[0][0][D[0][0]] = 0
for h in range(H):
    for w in range(W):
        if 0 < h:
            for v in dp[h-1][w]:
                v1 = v+D[h][w]
                v2 = v-D[h][w] if v>=D[h][w] else -v+D[h][w]
                dp[h][w][v1] = 0
                dp[h][w][v2] = 0
        if 0 < w:
            for v in dp[h][w-1]:
                v1 = v+D[h][w]
                v2 = v-D[h][w] if v>=D[h][w] else -v+D[h][w]
                dp[h][w][v1] = 0
                dp[h][w][v2] = 0
                
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 _ in range(H)]
B = [list(map(int,input().split())) for _ in range(H)]
D = [[abs(A[h][w]-B[h][w]) for w in range(W)] for h in range(H)]
dp = [[{} for _ in range(W)] for _ in range(H)]
dp[0][0][D[0][0]] = 0
for h in range(H):
    for w in range(W):
        for v in dp[h][w]:
            if w != W-1:
                v1 = v+D[h][w+1]
                v2 = v-D[h][w+1]
                if v1<0:
                    v1 *= -1
                if v2<0:
                    v2 *= -1
                dp[h][w+1][v1] = 0
                dp[h][w+1][v2] = 0
            if h != H-1:
                v1 = v+D[h+1][w]
                v2 = v-D[h+1][w]
                if v1<0:
                    v1 *= -1
                if v2<0:
                    v2 *= -1
                dp[h+1][w][v1] = 0
                dp[h+1][w][v2] = 0
ans = min(dp[H-1][W-1])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def solve():
    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[h][w]-B[h][w]) for w in range(W)] for h in range(H)]
    dp = [[{} for _ in range(W)] for _ in range(H)]
    dp[0][0][D[0][0]] = 0
    for h in range(H):
        for w in range(W):
            for v in dp[h][w]:
                if w != W-1:
                    v1 = v+D[h][w+1]
                    v2 = v-D[h][w+1]
                    if v1<0:
                        v1 *= -1
                    if v2<0:
                        v2 *= -1
                    dp[h][w+1][v1] = 0
                    dp[h][w+1][v2] = 0
                if h != H-1:
                    v1 = v+D[h+1][w]
                    v2 = v-D[h+1][w]
                    if v1<0:
                        v1 *= -1
                    if v2<0:
                        v2 *= -1
                    dp[h+1][w][v1] = 0
                    dp[h+1][w][v2] = 0
    ans = min(dp[H-1][W-1])
    return ans
print(solve())

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)]
D = [[abs(A[h][w]-B[h][w]) for w in range(W)] for h in range(H)]
dp = [[{} for _ in range(W)] for _ in range(H)]
dp[0][0][D[0][0]] = 0
for h in range(H):
    for w in range(W):
        for v in dp[h][w]:
            if w != W-1:
                v1 = v+D[h][w+1]
                v2 = v-D[h][w+1]
                if v1<0:
                    v1 *= -1
                if v2<0:
                    v2 *= -1
                dp[h][w+1][v1] = 0
                dp[h][w+1][v2] = 0
            if h != H-1:
                v1 = v+D[h+1][w]
                v2 = v-D[h+1][w]
                if v1<0:
                    v1 *= -1
                if v2<0:
                    v2 *= -1
                dp[h+1][w][v1] = 0
                dp[h+1][w][v2] = 0
ans = min(dp[H-1][W-1])
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)]
D = [[abs(A[h][w]-B[h][w]) for w in range(W)] for h in range(H)]
dp = [[{} for _ in range(W)] for _ in range(H)]
dp[0][0][abs(A[0][0]-B[0][0])] = 0
for h in range(H):
    for w in range(W):
        for v in dp[h][w]:
            if w != W-1:
                v1 = v+A[h][w+1]-B[h][w+1]
                v2 = v-A[h][w+1]+B[h][w+1]
                if v1<0:
                    v1 *= -1
                if v2<0:
                    v2 *= -1
                dp[h][w+1][v1] = 0
                dp[h][w+1][v2] = 0
            if h != H-1:
                v1 = v+A[h+1][w]-B[h+1][w]
                v2 = v-A[h+1][w]+B[h+1][w]
                if v1<0:
                    v1 *= -1
                if v2<0:
                    v2 *= -1
                dp[h+1][w][v1] = 0
                dp[h+1][w][v2] = 0
ans = min(dp[H-1][W-1])
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 _ in range(W)] for _ in range(H)]
dp[0][0][abs(A[0][0]-B[0][0])] = 0
for h in range(H):
    for w in range(W):
        for v in dp[h][w]:
            if w != W-1:
                dp[h][w+1][abs(v+A[h][w+1]-B[h][w+1])] = 0
                dp[h][w+1][abs(v-A[h][w+1]+B[h][w+1])] = 0
            if h != H-1:
                dp[h+1][w][abs(v+A[h+1][w]-B[h+1][w])] = 0
                dp[h+1][w][abs(v-A[h+1][w]+B[h+1][w])] = 0
ans = min(dp[H-1][W-1])
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 _ in range(W)] for _ in range(H)]
dp[0][0][abs(A[0][0]-B[0][0])] = 0
for h in range(H-1):
    for w in range(W-1):
        for v in dp[h][w]:
            dp[h][w+1][abs(v+A[h][w+1]-B[h][w+1])] = 0
            dp[h][w+1][abs(v-A[h][w+1]+B[h][w+1])] = 0
            dp[h+1][w][abs(v+A[h+1][w]-B[h+1][w])] = 0
            dp[h+1][w][abs(v-A[h+1][w]+B[h+1][w])] = 0
for v in dp[H-1][W-2]:
    dp[H-1][W-1][abs(v+A[H-1][W-1]-B[H-1][W-1])] = 0
    dp[H-1][W-1][abs(v-A[H-1][W-1]+B[H-1][W-1])] = 0
for v in dp[H-2][W-1]:
    dp[H-1][W-1][abs(v+A[H-1][W-1]-B[H-1][W-1])] = 0
    dp[H-1][W-1][abs(v-A[H-1][W-1]+B[H-1][W-1])] = 0
ans = min(dp[H-1][W-1])
print(ans)

Submission status: Time Limit Exceeded

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

dp = [[{} for _ in range(W)] for _ in range(H)]
t = A[0][0]-B[0][0]
if t < 0:
    dp[0][0][-t*2] = True
    dp[0][0][-t*2-1] = True
elif t > 0:
    dp[0][0][2*t-1] = True
    dp[0][0][2*t] = True
else:
    dp[0][0][0] = True

for i in range(H):
    for j in range(W):
        if i > 0:
            for k in dp[i-1][j]:
                if k % 2 == 0:
                    num = -k//2
                else:
                    num = (k+1)//2
                num += A[i][j]-B[i][j]
                if num <= 0:
                    idx = -num*2
                else:
                    idx = num*2-1
                dp[i][j][idx] = True
                num += (-A[i][j]+B[i][j])*2
                if num <= 0:
                    idx = -num*2
                else:
                    idx = num*2-1
                dp[i][j][idx] = True
        if j > 0:
            for k in dp[i][j-1]:
                if k % 2 == 0:
                    num = -k//2
                else:
                    num = (k+1)//2
                num += A[i][j]-B[i][j]
                if num <= 0:
                    idx = -num*2
                else:
                    idx = num*2-1
                dp[i][j][idx] = True
                num += (-A[i][j]+B[i][j])*2
                if num <= 0:
                    idx = -num*2
                else:
                    idx = num*2-1
                dp[i][j][idx] = True
                    
ans = 10**9
for k in dp[H-1][W-1]:
    ans = min(ans, k//2+k%2)
        
print(ans)

Submission status: Time Limit Exceeded

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

dp = [[[False]*12802 for _ in range(W)] for _ in range(H)]
t = A[0][0]-B[0][0]
if t < 0:
    dp[0][0][-t*2] = True
    dp[0][0][-t*2-1] = True
elif t > 0:
    dp[0][0][2*t-1] = True
    dp[0][0][2*t] = True
else:
    dp[0][0][0] = True

for i in range(H):
    for j in range(W):
        if i > 0:
            for k in range(12802):
                if dp[i-1][j][k]:
                    if k % 2 == 0:
                        num = -k//2
                    else:
                        num = (k+1)//2
                    num += A[i][j]-B[i][j]
                    if num <= 0:
                        idx = -num*2
                    else:
                        idx = num*2-1
                    dp[i][j][idx] = True
                    num -= (-A[i][j]+B[i][j])*2
                    if num <= 0:
                        idx = -num*2
                    else:
                        idx = num*2-1
                    dp[i][j][idx] = True
        if j > 0:
            for k in range(12802):
                if dp[i][j-1][k]:
                    if k % 2 == 0:
                        num = -k//2
                    else:
                        num = (k+1)//2
                    num += A[i][j]-B[i][j]
                    if num <= 0:
                        idx = -num*2
                    else:
                        idx = num*2-1
                    dp[i][j][idx] = True
                    num += (-A[i][j]+B[i][j])*2
                    if num <= 0:
                        idx = -num*2
                    else:
                        idx = num*2-1
                    dp[i][j][idx] = True
                    
for k in range(12802):
    if dp[H-1][W-1][k]:
        ans = k//2 + k%2
        break
        
print(ans)

Submission status: Time Limit Exceeded

Solution code:
H,W = list(map(int,input().split()))
import numpy as np
A = np.array([list(map(int,input().split())) for i in range(H)])
B = np.array([list(map(int,input().split())) for i in range(H)])
C = np.abs(A-B)
from collections import defaultdict
res = defaultdict(set)
for h in range(H):
  for w in range(W):
    c = C[h,w]
    if h+w==0:
      rs = set([c,-c])
    else:
      rs = set()
      if h>0:
        rs |= set(s+c for s in res[(h-1,w)]) | set(s-c for s in res[(h-1,w)])
      if w>0:
        rs |= set(s+c for s in res[(h,w-1)]) | set(s-c for s in res[(h,w-1)])
    res[(h,w)] = rs

print(min(map(abs,rs)))

Submission status: Time Limit Exceeded

Solution code:
def main():
    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)]
    M = (H + W) * 80 + 1

    u = [[False] * M for _ in range(W)]
    v = [[False] * M for _ in range(W)]
    v[0][0] = True
    for h in range(H):
        v, u = u, v
        for w in range(W):
            a, b = A[h][w], B[h][w]
            d = a - b if a > b else b - a
            for i in range(M):
                t = False
                if 0 <= i + d < M:
                    t = t or u[w][i + d] or (w > 0 and v[w - 1][i + d])
                if 0 <= i - d < M:
                    t = t or u[w][i - d] or (w > 0 and v[w - 1][i - d])
                if 0 <= d - i < M:
                    t = t or u[w][d - i] or (w > 0 and v[w - 1][d - i])
                v[w][i] = t
    vv = v[-1]
    for i in range(M):
        if vv[i]:
            print(i)
            return
    raise

main()

Submission status: Time Limit Exceeded

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

A = np.array(A, dtype=np.int)
B = np.array(B, dtype=np.int)

dp = np.zeros([H, W, 80**2+1], dtype=bool)
dp[0][0][abs(A[0][0] - B[0][0])] = True

for i in range(H):
    for j in range(W):
        diff = A[i][j] - B[i][j]
        if i > 0:
            idx = np.where(dp[i - 1][j][:])[0]
            dp[i][j][np.clip(np.abs(idx - diff), 0, 80**2)] = True
            dp[i][j][np.clip(np.abs(idx + diff), 0, 80**2)] = True

        if j > 0:
            idx = np.where(dp[i][j - 1][:])[0]
            dp[i][j][np.clip(np.abs(idx - diff), 0, 80**2)] = True
            dp[i][j][np.clip(np.abs(idx + diff), 0, 80**2)] = True

print(dp[H - 1][W - 1][:].nonzero()[0].min())

Submission status: Time Limit Exceeded

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 = False * np.ones([H, W, 80**2+1], dtype=bool)
dp[0][0][abs(A[0][0] - B[0][0])] = True

for i in range(H):
    for j in range(W):
        diff = A[i][j] - B[i][j]
        if i > 0:
            idx = np.where(dp[i - 1][j][:])[0]
            dp[i][j][np.clip(np.abs(idx - diff), 0, 80**2)] = True
            dp[i][j][np.clip(np.abs(idx + diff), 0, 80**2)] = True

        if j > 0:
            idx = np.where(dp[i][j - 1][:])[0]
            dp[i][j][np.clip(np.abs(idx - diff), 0, 80**2)] = True
            dp[i][j][np.clip(np.abs(idx + diff), 0, 80**2)] = True

print(dp[H - 1][W - 1][:].nonzero()[0].min())

Submission status: Time Limit Exceeded

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])
for i in range(H):
  for j in range(W):
    x=80*(H+W-i-j)
    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: 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)]
dp1 = [[False]*((H+W)*80) for _ in range(W)]
dp2 = [[False]*((H+W)*80) for _ in range(W)]
for w in range(W):
    dp1[w][0] = True

for h in range(H):
    for w in range(W):
        va = A[h][w]
        vb = B[h][w]
        for v in range((H+W)*80):
            if dp1[w][v]:
                dp2[w][abs(v+va-vb)] = True
                dp2[w][abs(v-va+vb)] = True
        if w!=0:
            for v in range((H+W)*80):
                if dp2[w-1][v]:
                    dp2[w][abs(v+va-vb)] = True
                    dp2[w][abs(v-va+vb)] = True
    dp1 = dp2
    dp2 = [[False]*((H+W)*80) for _ in range(W)]

for v in range((H+W)*80):
    if dp1[W-1][v]:
        ans = v
        break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
H,W=map(int,input().split())
a = [[] for _ in range(H)]
for i in range(H):
    a[i] = list(map(int, input().split()))
b = [[] for _ in range(H)]
for i in range(H):
    b[i] = list(map(int, input().split()))
#print(a)
#print(b)
for i in range(H):
    for j in range(W):
        a[i][j] =abs(a[i][j]-b[i][j])
#print(a)
dp = [[set() for _ in range(W)] for _ in range(H)]
dp[0][0].add(a[0][0])
for i in range(H):
    for j in range(W):
        t = a[i][j]
        tmp = set()
        if i!=0:
            for k in dp[i-1][j]:
                tmp.add(abs(k+t))
                tmp.add(abs(k-t))
        if j!=0:
            for k in dp[i][j-1]:
                tmp.add(abs(k+t))
                tmp.add(abs(k-t))
        if i>0 or j>0:
            dp[i][j] = tmp
        #print(tmp)
#print(dp[H-1][W-1])
ls = list(dp[H-1][W-1])
#res = np.abs(np.asarray(ls)).argmin()
print(min(ls))

Submission status: Time Limit Exceeded

Solution code:
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]))
print(min(dp[H-1][W-1]))

Submission status: Time Limit Exceeded

Solution code:
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])
for i in range(H):
  for j in range(W):
    x=80*(H+W-i-j)
    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]))
print(min(dp[H-1][W-1]))

Submission status: Time Limit Exceeded

Solution code:
H,W=[int(s) for s in input().split()]
M=80*(H+W)
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=[[[0 for _ in range(M)] for _ in range(W)] for _ in range(H)]
dp[0][0][inls[0][0]]=1
for i in range(H):
  for j in range(W):
    x=inls[i][j]
    for k in range(M):
      if dp[i-1][j][k]==1:
        dp[i][j][k+x]=1
        dp[i][j][abs(k-x)]=1
      if dp[i][j-1][k]==1:
        dp[i][j][k+x]=1
        dp[i][j][abs(k-x)]=1
for k in range(M):
  if dp[i][j][k]==1:
    print(k)
    break

Submission status: Time Limit Exceeded

Solution code:
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]-=x[j]
dp=[[set() for _ in range(W)] for _ in range(H)]
dp[0][0].add(abs(inls[0][0]))
for i in range(H):
  for j in range(W):
    for e in dp[i-1][j]:
      dp[i][j].add(abs(e+inls[i][j]))
      dp[i][j].add(abs(e-inls[i][j]))
    for e in dp[i][j-1]:
      dp[i][j].add(abs(e+inls[i][j]))
      dp[i][j].add(abs(e-inls[i][j]))
print(min(dp[H-1][W-1]))

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor
from operator import mul
from functools import reduce


sys.setrecursionlimit(2147483647)
INF = 10 ** 13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007



h, w = LI()
A = LIR(h)
B = LIR(h)
dp = [[[0] * 80 ** 2 * 2 for _ in range(w)] for _ in range(h)]
dp[0][0][abs(A[0][0] - B[0][0])] = 1
for y in range(h):
    for x in range(w):
        for i in range(80 ** 2 * 2):
            if dp[y][x][i]:
                if x + 1 < w:
                    dp[y][x + 1][abs(i + A[y][x + 1] - B[y][x + 1])] = 1
                    dp[y][x + 1][abs(i + B[y][x + 1] - A[y][x + 1])] = 1
                if y + 1 < h:
                    dp[y + 1][x][abs(i + A[y + 1][x] - B[y + 1][x])] = 1
                    dp[y + 1][x][abs(i + B[y + 1][x] - A[y + 1][x])] = 1


for j in range(80 ** 2 * 2):
    if dp[h - 1][w - 1][j]:
        print(j)
        exit()

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 = [[[False]*((H+W)*80) for _ in range(W)] for _ in range(H)]

dp[0][0][abs(A[0][0]-B[0][0])] = True
for h in range(H-1):
    for w in range(W-1):
        v1 = abs(A[h+1][w]-B[h+1][w])
        v2 = abs(A[h][w+1]-B[h][w+1])
        for v in range((H+W)*80):
            if dp[h][w][v]:
                dp[h+1][w][v1+v] = True
                dp[h+1][w][abs(v1-v)] = True
                dp[h][w+1][v2+v] = True
                dp[h][w+1][abs(v2-v)] = True

v0 = abs(A[H-1][W-1]-B[H-1][W-1])
for v in range((H+W)*80):
    if dp[H-1][W-2][v]:
        dp[H-1][W-1][v0+v] = True
        dp[H-1][W-1][abs(v0-v)] = True
    if dp[H-2][W-1][v]:
        dp[H-1][W-1][v0+v] = True
        dp[H-1][W-1][abs(v0-v)] = True

for v in range((H+W)*80):
    if dp[H-1][W-1][v]:
        ans = v
        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 = [[[False]*((H+W)*80) for _ in range(W)] for _ in range(H)]

dp[0][0][abs(A[0][0]-B[0][0])] = True
for h in range(H-1):
    for w in range(W-1):
        for v in range((H+W)*80):
            v1 = abs(A[h+1][w]-B[h+1][w])
            v2 = abs(A[h][w+1]-B[h][w+1])
            if dp[h][w][v]:
                dp[h+1][w][v1+v] = True
                dp[h+1][w][abs(v1-v)] = True
                dp[h][w+1][v2+v] = True
                dp[h][w+1][abs(v2-v)] = True

v0 = abs(A[H-1][W-1]-B[H-1][W-1])
for v in range((H+W)*80):
    if dp[H-1][W-2][v]:
        dp[H-1][W-1][v0+v] = True
        dp[H-1][W-1][abs(v0-v)] = True
    if dp[H-2][W-1][v]:
        dp[H-1][W-1][v0+v] = True
        dp[H-1][W-1][abs(v0-v)] = True

for v in range((H+W)*80):
    if dp[H-1][W-1][v]:
        ans = v
        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 = [[[False]*((H+W)*80) for _ in range(W)] for _ in range(H)]

dp[0][0][abs(A[0][0]-B[0][0])] = True
for h in range(H-1):
    for w in range(W-1):
        for v in range((H+W)*80):
            v1 = abs(A[h+1][w]-B[h+1][w])
            v2 = abs(A[h][w+1]-B[h][w+1])
            if dp[h][w][v]:
                dp[h+1][w][v1+v] = True
                dp[h+1][w][abs(v1-v)] = True
                dp[h][w+1][v2+v] = True
                dp[h][w+1][abs(v2-v)] = True

v0 = abs(A[H-1][W-1]-B[H-1][W-1])
for v in range((H+W)*80):
    if dp[H-1][W-2][v]:
        dp[H-1][W-1][v0+v] = True
        dp[H-1][W-1][abs(v0-v)] = True
    if dp[H-2][W-1][v]:
        dp[H-1][W-1][v0+v] = True
        dp[H-1][W-1][abs(v0-v)] = True

for v in range((H+W)*80):
    if dp[H-1][W-1][v]:
        ans = v
        break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
def main():
    input = sys.stdin.readline
    H, W = map(int, input().split())
    A = []
    B = []
    M = 80 * (H + W)
    for _ in range(H):
        a = tuple(map(int, input().split()))
        A.append(a)
    for _ in range(H):
        b = tuple(map(int, input().split()))
        B.append(b)
    AB = [[0] * W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            AB[i][j] = abs(A[i][j] - B[i][j])
    l = [[set([]) for _ in range(M) for _ in range(W)] for _ in range(H)]
    for i in range(H):
        for j in range(W):
            if i == 0 and j == 0:
                l[i][j].add(AB[0][0])
            else:
                t = AB[i][j]
                if i > 0:
                    for k in l[i-1][j]:
                        l[i][j].add(k+t)
                        l[i][j].add(abs(k-t))
                if j > 0:
                    for k in l[i][j-1]:
                        l[i][j].add(k+t)
                        l[i][j].add(abs(k-t))
    return min(l[i][j])
print(main())

Submission status: Time Limit Exceeded

Solution code:
def main():
    H, W = map(int, input().split())
    A = []
    B = []
    M = 80 * (H + W)
    for _ in range(H):
        a = tuple(map(int, input().split()))
        A.append(a)
    for _ in range(H):
        b = tuple(map(int, input().split()))
        B.append(b)
    AB = [[0] * W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            AB[i][j] = abs(A[i][j] - B[i][j])
    l = [[[0] * (M+1) for _ in range(W)] for _ in range(H)]
    for i in range(H):
        for j in range(W):
            if i == 0 and j == 0:
                l[i][j][AB[0][0]] = 1
            else:
                t = AB[i][j]
                for k in range(M+1):
                    if i > 0 and l[i-1][j][k] == 1:
                        l[i][j][k+t] = 1
                        l[i][j][abs(k-t)] = 1
                    if j > 0 and l[i][j-1][k] == 1:
                        l[i][j][k+t] = 1
                        l[i][j][abs(t-k)] = 1
    for k in range(M):
        if l[-1][-1][k] == 1:
            return k
print(main())

Submission status: Time Limit Exceeded

Solution code:
def main():
    H, W = map(int, input().split())
    A = []
    B = []
    M = 80 * (H + W)
    for _ in range(H):
        a = tuple(map(int, input().split()))
        A.append(a)
    for _ in range(H):
        b = tuple(map(int, input().split()))
        B.append(b)
    AB = [[0] * W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            AB[i][j] = abs(A[i][j] - B[i][j])
    l = [[[0] * (M+1) for _ in range(W)] for _ in range(H)]
    for i in range(H):
        for j in range(W):
            if i == 0 and j == 0:
                l[i][j][AB[0][0]] = 1
            else:
                t = AB[i][j]
                for k in range(M+1):
                    if i > 0 and l[i-1][j][k] == 1:
                        if k + t <= M:
                            l[i][j][k+t] = 1
                        if k - t >= 0:
                            l[i][j][k-t] = 1
                        if t - k >= 0:
                            l[i][j][t-k] = 1
                    if j > 0 and l[i][j-1][k] == 1:
                        if k + t <= M:
                            l[i][j][k+t] = 1
                        if k - t >= 0:
                            l[i][j][k-t] = 1
                        if t - k >= 0:
                            l[i][j][t-k] = 1
    for k in range(M):
        if l[-1][-1][k] == 1:
            return k
print(main())

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

maxSa=(h-1+w-1)*80+5
dp = [[[0 for i in range(maxSa)]for j in range(w)] for k in range(h)]
dp[0][0][abs(a[0][0]-b[0][0])]=1

sa = [[0]*w for _ in range(h)]
for i in range(h):
	for j in range(w):
		sa[i][j] = abs(a[i][j] - b[i][j])


for i in range(h):
	for j in range(w):
		for k in range(maxSa):
			if dp[i][j][k] == 1:

				#　右
				if j!=w-1:
					dp[i][j+1][k+sa[i][j+1]]=1
					dp[i][j+1][abs(k-sa[i][j+1])]=1
				
				#　下
				if i != h-1:
					dp[i+1][j][k+sa[i+1][j]]=1
					dp[i+1][j][abs(k-sa[i+1][j])]=1

ans = 1234567890
for i in range(k):
	if dp[h-1][w-1][i]==1:
		print(i)
		exit()

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

maxSa=(h-1+w-1)*80+5
dp = [[[0 for i in range(maxSa)]for j in range(w)] for k in range(h)]
dp[0][0][abs(a[0][0]-b[0][0])]=1

for i in range(h):
	for j in range(w):
		for k in range(maxSa):
			if dp[i][j][k] == 1:

				#　右
				if j!=w-1:
					sa = abs(a[i][j+1]-b[i][j+1])
					dp[i][j+1][k+sa]=1
					dp[i][j+1][abs(k-sa)]=1
				
				#　下
				if i != h-1:
					sa = abs(a[i+1][j]-b[i+1][j])
					dp[i+1][j][k+sa]=1
					dp[i+1][j][abs(k-sa)]=1

ans = 1234567890
for i in range(k):
	if dp[h-1][w-1][i]==1:
		print(i)
		exit()