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

maxSa=(h+w)*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()

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)
    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:
                t = abs(A[0][0] - B[0][0])
                l[i][j][t] = 1
            else:
                t = abs(A[i][j] - B[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:
import sys
input = sys.stdin.readline
inf = float('inf')

H, W = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(H)]
B = [list(map(int,input().split())) for _ in range(H)]

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

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

ans = inf

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

print(ans)

Submission status: Time Limit Exceeded

Solution code:
h,w=map(int,input().split())
a=[[int(i) for i in input().split()] for i in range(h)]
b=[[int(i) for i in input().split()] for i in range(h)]
for i in range(h):
    if i==0:
        x=[set([]) for _ in range(w)]
    for j in range(w):
        if i==0 and j==0:
            x[0].add(abs(a[0][0]-b[0][0]))
        elif i==0 and j!=0:
            for k in x[j-1]:
                x[j].add(abs(k+(a[i][j]-b[i][j])))
                x[j].add(abs(k+(b[i][j]-a[i][j])))
        elif i%2:
            if j==0:
                for k in x[0]:
                    y[j].add(abs(k+(a[i][j]-b[i][j])))
                    y[j].add(abs(k+(b[i][j]-a[i][j])))
            else:
                for k in x[j]:
                    y[j].add(abs(k+(a[i][j]-b[i][j])))
                    y[j].add(abs(k+(b[i][j]-a[i][j])))
                for k in y[j-1]:
                    y[j].add(abs(k+(a[i][j]-b[i][j])))
                    y[j].add(abs(k+(b[i][j]-a[i][j])))
        else:
            if j==0:
                for k in y[0]:
                    x[j].add(abs(k+(a[i][j]-b[i][j])))
                    x[j].add(abs(k+(b[i][j]-a[i][j])))
            else:
                for k in y[j]:
                    x[j].add(abs(k+(a[i][j]-b[i][j])))
                    x[j].add(abs(k+(b[i][j]-a[i][j])))
                for k in x[j-1]:
                    x[j].add(abs(k+(a[i][j]-b[i][j])))
                    x[j].add(abs(k+(b[i][j]-a[i][j])))

    if i%2==0:
        y=[set([]) for _ in range(w)]
        ans=min(x[-1])
    else:
        x=[set([]) for _ in range(w)]
        ans=min(y[-1])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
def main():
  input = sys.stdin.readline
  h, w = map(int, input().split())
  A = []
  AB = []
  for i in range(h):
    row = list(map(int, input().split()))
    A.append(row)
  for i in range(h):
    row = [abs(A[i][j] - int(b)) for j, b in enumerate(input().split())]
    AB.append(row)
 
  dp = [[set([]) for j in range(w+1)] for i in range(h+1)]
  dp[0][0].add(AB[0][0])
  for i in range(h):
    for j in range(w):
      dif1 = 0
      dif2 = 0
      if j < w - 1:
        dif1 = AB[i][j+1]
      if i < h - 1:
        dif2 = AB[i+1][j]
      for item in dp[i][j]:
        if j < w - 1:
          dp[i][j+1].add(item+dif1)
          dp[i][j+1].add(abs(item-dif1))
        if i < h - 1:
          dp[i+1][j].add(item+dif2)
          dp[i+1][j].add(abs(item-dif2))
  print(min(dp[h-1][w-1]))
if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  import sys
  input = sys.stdin.readline
  h, w = map(int, input().split())
  A = []
  AB = []
  for i in range(h):
    row = list(map(int, input().split()))
    A.append(row)
  for i in range(h):
    row = [abs(A[i][j] - int(b)) for j, b in enumerate(input().split())]
    AB.append(row)
 
  dp = [[set([]) for j in range(w+1)] for i in range(h+1)]
  dp[0][0].add(AB[0][0])
  for i in range(h):
    for j in range(w):
      dif1 = 0
      dif2 = 0
      if j < w - 1:
        dif1 = AB[i][j+1]
      if i < h - 1:
        dif2 = AB[i+1][j]
      for item in dp[i][j]:
        if j < w - 1:
          dp[i][j+1].add(item+dif1)
          dp[i][j+1].add(abs(item-dif1))
        if i < h - 1:
          dp[i+1][j].add(item+dif2)
          dp[i+1][j].add(abs(item-dif2))
  print(min(dp[h-1][w-1]))
if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  import sys
  input = sys.stdin.readline
  h, w = map(int, input().split())
  A = []
  AB = []
  for i in range(h):
    row = list(map(int, input().split()))
    A.append(row)
  for i in range(h):
    row = [abs(A[i][j] - int(b)) for j, b in enumerate(input().split())]
    AB.append(row)
 
  dp = [[{} for j in range(w+1)] for i in range(h+1)]
  dp[0][0][AB[0][0]] = 1
  for i in range(h):
    for j in range(w):
      dif1 = 0
      dif2 = 0
      if j < w - 1:
        dif1 = AB[i][j+1]
      if i < h - 1:
        dif2 = AB[i+1][j]
      for item in dp[i][j]:
        if j < w - 1:
          dp[i][j+1][item+dif1] = 1
          dp[i][j+1][abs(item-dif1)] = 1
        if i < h - 1:
          dp[i+1][j][item+dif2] = 1
          dp[i+1][j][abs(item-dif2)] = 1
  print(min(dp[h-1][w-1].keys()))
if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
from collections import defaultdict

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

dp = defaultdict(set)
dp[(0, 0)].add(abs(A[0][0] - B[0][0]))
for i in range(H):
    for j in range(W):
        d = abs(A[i][j] - B[i][j])
        if i > 0:
            for k in dp[(i - 1, j)]:
                dp[(i, j)].add(abs(k + d))
                dp[(i, j)].add(abs(k - d))
        if j > 0:
            for k in dp[(i, j - 1)]:
                dp[(i, j)].add(abs(k + d))
                dp[(i, j)].add(abs(k - d))
print(min(dp[(H - 1, W - 1)]))

Submission status: Time Limit Exceeded

Solution code:
def main():
  import sys
  input = sys.stdin.readline
  h, w = map(int, input().split())
  A = []
  B = []
  for i in range(h):
    row = list(map(int, input().split()))
    A.append(row)
  for i in range(h):
    row = list(map(int, input().split()))
    B.append(row)
 
  dp = [[{} for j in range(w+1)] for i in range(h+1)]
  dp[0][0][abs(A[0][0] - B[0][0])] = 1
  for i in range(h):
    for j in range(w):
      for item in dp[i][j]:
        if j < w - 1:
          dif = abs(A[i][j+1] - B[i][j+1])
          dp[i][j+1][item+dif] = 1
          dp[i][j+1][abs(item-dif)] = 1
        if i < h - 1:
          dif = abs(A[i+1][j] - B[i+1][j])
          dp[i+1][j][item+dif] = 1
          dp[i+1][j][abs(item-dif)] = 1
  print(min(dp[h-1][w-1].keys()))
if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  import sys
  input = sys.stdin.readline
  h, w = map(int, input().split())
  A = []
  B = []
  for i in range(h):
    row = list(map(int, input().split()))
    A.append(row)
  for i in range(h):
    row = list(map(int, input().split()))
    B.append(row)
 
  dp = [[[0 for k in range(12801)] for j in range(w+1)] for i in range(h+1)]
  dp[0][0][(abs(A[0][0] - B[0][0]))] = 1
  for i in range(h):
    for j in range(w):
      for item, f in enumerate(dp[i][j]):
        if not f:
          continue
        if j < w - 1:
          dif = abs(A[i][j+1] - B[i][j+1])
          dp[i][j+1][item+dif] = 1
          dp[i][j+1][abs(item-dif)] = 1
        if i < h - 1:
          dif = abs(A[i+1][j] - B[i+1][j])
          dp[i+1][j][item+dif] = 1
          dp[i+1][j][abs(item-dif)] = 1
  print(min([k for k, f in enumerate(dp[h-1][w-1]) if f]))
if __name__ == '__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
def main():
  import sys
  input = sys.stdin.readline
  h, w = map(int, input().split())
  A = []
  B = []
  for i in range(h):
    row = list(map(int, input().split()))
    A.append(row)
  for i in range(h):
    row = list(map(int, input().split()))
    B.append(row)
 
  dp = [[set([]) for j in range(w+1)] for i in range(h+1)]
  dp[0][0].add(abs(A[0][0] - B[0][0]))
  for i in range(h):
    for j in range(w):
      for item in dp[i][j]:
        if j < w - 1:
          dif = abs(A[i][j+1] - B[i][j+1])
          dp[i][j+1].add(item+dif)
          dp[i][j+1].add(abs(item-dif))
        if i < h - 1:
          dif = abs(A[i+1][j] - B[i+1][j])
          dp[i+1][j].add(item+dif)
          dp[i+1][j].add(abs(item-dif))
  print(min(dp[h-1][w-1]))
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())
  A = []
  B = []
  for i in range(h):
    row = list(map(int, input().split()))
    A.append(row)
  for i in range(h):
    row = list(map(int, input().split()))
    B.append(row)
 
  dp = [[set([]) for j in range(w+1)] for i in range(h+1)]
  dp[0][0].add(abs(A[0][0] - B[0][0]))
  for i in range(h):
    for j in range(w):
      for item in dp[i][j]:
        if j < w - 1:
          dif = abs(A[i][j+1] - B[i][j+1])
          dp[i][j+1].add(item+dif)
          dp[i][j+1].add(abs(item-dif))
        if i < h - 1:
          dif = abs(A[i+1][j] - B[i+1][j])
          dp[i+1][j].add(item+dif)
          dp[i+1][j].add(abs(item-dif))
  print(min(dp[h-1][w-1]))
if __name__ == '__main__':
  main()

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):
  row = list(map(int, input().split()))
  row.append(0)
  A.append(row)
A.append([0 for i in range(w+1)])

for i in range(h):
  row = list(map(int, input().split()))
  row.append(0)
  B.append(row)
B.append([0 for i in range(w+1)])

dp = [[set([]) for j in range(w+1)] for i in range(h+1)]
dp[0][0].add(abs(A[0][0] - B[0][0]))
for i in range(h):
  for j in range(w):
    for item in dp[i][j]:
      dif = abs(A[i][j+1] - B[i][j+1])
      dp[i][j+1].add(item+dif)
      dp[i][j+1].add(abs(item-dif))
      dif = abs(A[i+1][j] - B[i+1][j])
      dp[i+1][j].add(item+dif)
      dp[i+1][j].add(abs(item-dif))
print(min(dp[h-1][w-1]))

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

AB = [list(map(lambda x, y: abs(x - y), a, b)) for a, b in zip(A, B)]
dp = [[[False] * 10001 for _ in range(W + 1)] for _ in range(H + 1)]
dp[0][0][0] = True

for h in range(H):
    for w in range(W):
        ab = AB[h][w]
        for i in range(10000):
            if dp[h][w][i]:
                dp[h+1][w][i+ab] = True
                dp[h+1][w][abs(i-ab)] = True
                dp[h][w+1][i+ab] = True
                dp[h][w+1][abs(i-ab)] = True

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

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

AB = [list(map(lambda x, y: abs(x - y), a, b)) for a, b in zip(A, B)]
dp = [[[0] * 15001 for _ in range(W + 1)] for _ in range(H + 1)]
dp[0][0][0] = 1

for h in range(H):
    for w in range(W):
        ab = AB[h][w]
        for i in range(12800):
            if dp[h][w][i] == 1:
                dp[h+1][w][i+ab] = 1
                dp[h+1][w][abs(i-ab)] = 1
                dp[h][w+1][i+ab] = 1
                dp[h][w+1][abs(i-ab)] = 1

for i in range(1000):
    if dp[H][W-1][i] == 1:
        print(i)
        break

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

AB = [list(map(lambda x, y: abs(x - y), a, b)) for a, b in zip(A, B)]
dp = [[[0] * 15001 for _ in range(W + 1)] for _ in range(H + 1)]
dp[0][0][0] = 1

for h in range(H):
    for w in range(W):
        ab = AB[h][w]
        for i in range(15000):
            if dp[h][w][i] == 1:
                dp[h+1][w][i+ab] = 1
                dp[h+1][w][abs(i-ab)] = 1
                dp[h][w+1][i+ab] = 1
                dp[h][w+1][abs(i-ab)] = 1

for i in range(15000):
    if dp[H][W-1][i] == 1:
        print(i)
        break

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

Submission status: 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=[]
ma=0
for i in range(h):
  d.append([])
  for j in range(w):
    d[-1].append(abs(a[i][j]-b[i][j]))
    ma=max(abs(a[i][j]-b[i][j]),ma)
if ma==0:print(0);exit()
l=(h+w-1)*ma*2+1
dp=[[l*[0]for _ in range(w)]for _ in range(h)]
mid=l//2
for i in range(h):
  for j in range(w):
    c=d[i][j]
    if i==j==0:dp[0][0][mid-c]=dp[0][0][mid+c]=1
    else:
      for k in range(l):
        if i!=0:
          if k-c>=0:dp[i][j][k-c]+=dp[i-1][j][k]
          if k+c<l:dp[i][j][k+c]+=dp[i-1][j][k]
        if j!=0:
          if k-c>=0:dp[i][j][k-c]+=dp[i][j-1][k]
          if k+c<l:dp[i][j][k+c]+=dp[i][j-1][k]
for i in range(mid):
  if dp[h-1][w-1][mid-i]or dp[h-1][w-1][mid+i]:print(i);exit()

Submission status: Time Limit Exceeded

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

def main():
    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] * 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):
        td = abs(a[i+1][0]-b[i+1][0])
        for k in range(80*(i+1)):
            if k+td < n:
                dp[i+1][0][k] = dp[i][0][k+td] or dp[i][0][abs(k-td)]
            else:
                dp[i+1][0][k] = dp[i][0][abs(k-td)]

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

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

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

main()

Submission status: Time Limit Exceeded

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

def main():
    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 = 40*(h+w)
    dp = [[[False] * 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):
        td = abs(a[i+1][0]-b[i+1][0])
        for k in range(n):
            if k+td < n:
                dp[i+1][0][k] = dp[i][0][abs(k+td)] or dp[i][0][abs(k-td)]
            else:
                dp[i+1][0][k] = dp[i][0][abs(k-td)]

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

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

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

main()

Submission status: Time Limit Exceeded

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

def main():
    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 = 40*(h+w)
    dp = [[[False] * 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):
            tk1 = k+td
            tk2 = abs(k-td)
            if tk1 < n:
                dp[i+1][0][tk1] = dp[i+1][0][tk1] or dp[i][0][k]
            dp[i+1][0][tk2] = dp[i+1][0][tk2] 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):
            tk1 = k+td
            tk2 = abs(k-td)
            if tk1 < n:
                dp[0][j+1][tk1] = dp[0][j+1][tk1] or dp[0][j][k]
            dp[0][j+1][tk2] = dp[0][j+1][tk2] 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):
                tk1 = k+td
                tk2 = abs(k-td)
                if tk1 < n:
                    dp[i+1][j+1][tk1] = dp[i+1][j+1][tk1] or dp[i+1][j][k] or dp[i][j+1][k]
                dp[i+1][j+1][tk2] = dp[i+1][j+1][tk2] 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)

main()

Submission status: Time Limit Exceeded

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

def main():
    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 = 40*(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):
            tk1 = k+td
            tk2 = abs(k-td)
            if tk1 < n:
                dp[i+1][0][tk1] = dp[i+1][0][tk1] or dp[i][0][k]
            dp[i+1][0][tk2] = dp[i+1][0][tk2] 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):
            tk1 = k+td
            tk2 = abs(k-td)
            if tk1 < n:
                dp[0][j+1][tk1] = dp[0][j+1][tk1] or dp[0][j][k]
            dp[0][j+1][tk2] = dp[0][j+1][tk2] 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):
                tk1 = k+td
                tk2 = abs(k-td)
                if tk1 < n:
                    dp[i+1][j+1][tk1] = dp[i+1][j+1][tk1] or dp[i+1][j][k] or dp[i][j+1][k]
                dp[i+1][j+1][tk2] = dp[i+1][j+1][tk2] 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)

main()

Submission status: Time Limit Exceeded

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

def main():
    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):
            tk1 = k+td
            tk2 = abs(k-td)
            if tk1 < n:
                dp[i+1][0][tk1] = dp[i+1][0][tk1] or dp[i][0][k]
            dp[i+1][0][tk2] = dp[i+1][0][tk2] 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):
            tk1 = k+td
            tk2 = abs(k-td)
            if tk1 < n:
                dp[0][j+1][tk1] = dp[0][j+1][tk1] or dp[0][j][k]
            dp[0][j+1][tk2] = dp[0][j+1][tk2] 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):
                tk1 = k+td
                tk2 = abs(k-td)
                if tk1 < n:
                    dp[i+1][j+1][tk1] = dp[i+1][j+1][tk1] or dp[i+1][j][k] or dp[i][j+1][k]
                dp[i+1][j+1][tk2] = dp[i+1][j+1][tk2] 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)

main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    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):
            tk1 = k+td
            tk2 = abs(k-td)
            if tk1 < n:
                dp[i+1][0][tk1] = dp[i+1][0][tk1] or dp[i][0][k]
            dp[i+1][0][tk2] = dp[i+1][0][tk2] 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):
            tk1 = k+td
            tk2 = abs(k-td)
            if tk1 < n:
                dp[0][j+1][tk1] = dp[0][j+1][tk1] or dp[0][j][k]
            dp[0][j+1][tk2] = dp[0][j+1][tk2] 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):
                tk1 = k+td
                tk2 = abs(k-td)
                if tk1 < n:
                    dp[i+1][j+1][tk1] = dp[i+1][j+1][tk1] or dp[i+1][j][k] or dp[i][j+1][k]
                dp[i+1][j+1][tk2] = dp[i+1][j+1][tk2] 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)

main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    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]
            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]
            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]
                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)

main()

Submission status: Time Limit Exceeded

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

main()