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())
diff = [list(map(int, input().split())) for _ in range(h)]
for i in range(h):
    for j, b in enumerate(list(map(int, input().split()))):
        diff[i][j] = abs(diff[i][j] - b)
dp = [[set() for j in range(w+1)] for i in range(h+1)]
dp[1][1].add(diff[0][0])

for i in range(1, h+1):
    for j in range(1, w+1):
        for x in dp[i-1][j]:
            dp[i][j].add(abs(x+diff[i-1][j-1]))
            dp[i][j].add(abs(x-diff[i-1][j-1]))
        for x in dp[i][j-1]:
            dp[i][j].add(abs(x+diff[i-1][j-1]))
            dp[i][j].add(abs(x-diff[i-1][j-1]))

print(min([abs(x) for x in dp[h][w]]))

Submission status: Time Limit Exceeded

Solution code:
h, w = map(int, input().split())
diff = [list(map(int, input().split())) for _ in range(h)]
for i in range(h):
    for j, b in enumerate(list(map(int, input().split()))):
        diff[i][j] = abs(diff[i][j] - b)
dp = [[set() for j in range(w+1)] for i in range(h+1)]
dp[1][1].add(diff[0][0])
dp[1][1].add(-diff[0][0])

for i in range(1, h+1):
    for j in range(1, w+1):
        for x in dp[i-1][j]:
            dp[i][j].add(x+diff[i-1][j-1])
            dp[i][j].add(x-diff[i-1][j-1])
        for x in dp[i][j-1]:
            dp[i][j].add(x+diff[i-1][j-1])
            dp[i][j].add(x-diff[i-1][j-1])

print(min([abs(x) for x in dp[h][w]]))

Submission status: Time Limit Exceeded

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


X = [[0] * W for i in range(H)]
for h in range(H):
    for w in range(W):
        X[h][w] = abs(A[h][w] - B[h][w])


# dp[h][w][x] := (h, w)の時点で差をxにできるか？
M = 12800
dp = [[[False] * (M + 1) for j in range(W)] for i in range(H)]
dp[0][0][X[0][0]] = True


for h in range(H):
    for w in range(W):
        for m in range(M):
            if not dp[h][w][m]:
                continue

            x = X[h][w]
            if h + 1 < H and abs(m + X[h + 1][w]) <= M:
                dp[h + 1][w][abs(m + X[h + 1][w])] = True

            if h + 1 < H and abs(m - X[h + 1][w]) <= M:
                dp[h + 1][w][abs(m - X[h + 1][w])] = True

            if (w + 1 < W) and abs(m + X[h][w + 1]) <= M:
                dp[h][w + 1][abs(m + X[h][w + 1])] = True

            if (w + 1 < W) and abs(m - X[h][w + 1]) <= M:
                dp[h][w + 1][abs(m - X[h][w + 1])] = True


ans = float('inf')
for x in range(M + 1):
    if dp[H - 1][W - 1][x]:
        ans = min(ans, x)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
A = [[0] * (W+1) for _ in range(H+1)]
B = [[0] * (W+1) for _ in range(H+1)]
dp = [[set([]) for _ in range(W+1)] for _ in range(H+1)]
for h in range(1, H+1):
    A[h][1::] = list(map(int, input().split()))

for h in range(1, H+1):
    B[h][1::] = list(map(int, input().split()))

dp[0][1].add(0)

for h in range(1, H+1):
    for w in range(1, W+1):
        diff = A[h][w] - B[h][w]
        for k in dp[h-1][w]:
            dp[h][w] = dp[h][w].union({k+diff, k-diff})
        for k in dp[h][w-1]:
            dp[h][w] = dp[h][w].union({k+diff, k-diff})

print(min([abs(res) for res in dp[H][W]]))

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

C = [[] for _ in range(H)]
for i in range(H):
  for j in range(W):
    C[i].append(abs(A[i][j] - B[i][j]))
#print(C)

dp = [[[0 for _ in range((H + W) * 80 + 1)] for _ in range(W)] for _ in range(H)]
#print(dp)

dp[0][0][C[0][0]] = 1
for i in range(H):
  for j in range(W):
    for k in range((H+W)*80+1):
      if dp[i-1][j][k]:
        dp[i][j][k + C[i][j]] = 1
        dp[i][j][abs(k - C[i][j])] = 1
      if dp[i][j-1][k]:
        dp[i][j][k + C[i][j]] = 1
        dp[i][j][abs(k - C[i][j])] = 1

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import math
import datetime
import itertools
 
# 入力
#N = int(input())
#S = str(input())
#N, A, B = map(int, input().split())
 
H, W = map(int, input().split())
A = []
B = []
for i in range(H) :
  al = list(map(int, input().split()))
  A.append(al)
 
for i in range(H) :
  bl = list(map(int, input().split()))
  B.append(bl)
 
#print (A)
#print (B)
 
kmax = 80*80
#kmax = 10
 
dp = [[[False for _ in range(kmax)] for _ in range(W)] for _ in range(H)]
 
#print (dp)
 
for i in range(H) :
  for j in range(W) :
    #初期値
    if i == 0 and j == 0 :
      dp[i][j][abs(A[i][j] - B[i][j])] = True
    else :
    #初期値以降
      val = abs(A[i][j] - B[i][j])  #値の差
      
      #i=0のときはj-1のkの状態を参照する
      if i == 0 :
        for k in range(kmax) :
          if dp[i][j-1][k] == True :
              dp[i][j][k+val] = True
              dp[i][j][abs(k-val)] = True
        
      #j=0のときはi-1のkの状態を参照する
      elif j == 0 :
        for k in range(kmax) :
          if dp[i-1][j][k] == True :
              dp[i][j][k+val] = True
              dp[i][j][abs(k-val)] = True
      
      #i!=0, j!=0のときはi-1、j-1の両方のkの状態を参照する
      else :
        for k in range(kmax) :
          if dp[i-1][j][k] == True :
              dp[i][j][k+val] = True
              dp[i][j][abs(k-val)] = True
          if dp[i][j-1][k] == True :
              dp[i][j][k+val] = True
              dp[i][j][abs(k-val)] = True
 
#答え
for k in range(kmax) :
  if dp[H-1][W-1][k] == True :
    print (k)
    exit(0)
 
#print (dp[H-1][W-1])

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import math
import datetime
import itertools

# 入力
#N = int(input())
#S = str(input())
#N, A, B = map(int, input().split())

H, W = map(int, input().split())
A = []
B = []
for i in range(H) :
  al = list(map(int, input().split()))
  A.append(al)

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

#print (A)
#print (B)

kmax = 80*(H+W)
#kmax = 10

dp = [[[False for _ in range(kmax)] for _ in range(W)] for _ in range(H)]

#print (dp)

for i in range(H) :
  for j in range(W) :
    #初期値
    if i == 0 and j == 0 :
      dp[i][j][abs(A[i][j] - B[i][j])] = True
    else :
    #初期値以降
      val = abs(A[i][j] - B[i][j])  #値の差
      
      #i=0のときはj-1のkの状態を参照する
      if i == 0 :
        for k in range(kmax) :
          if dp[i][j-1][k] == True :
              dp[i][j][k+val] = True
              dp[i][j][abs(k-val)] = True
        
      #j=0のときはi-1のkの状態を参照する
      elif j == 0 :
        for k in range(kmax) :
          if dp[i-1][j][k] == True :
              dp[i][j][k+val] = True
              dp[i][j][abs(k-val)] = True
      
      #i!=0, j!=0のときはi-1、j-1の両方のkの状態を参照する
      else :
        for k in range(kmax) :
          if dp[i-1][j][k] == True :
              dp[i][j][k+val] = True
              dp[i][j][abs(k-val)] = True
          if dp[i][j-1][k] == True :
              dp[i][j][k+val] = True
              dp[i][j][abs(k-val)] = True

#答え
for k in range(kmax) :
  if dp[H-1][W-1][k] == True :
    print (k)
    exit(0)

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import math
import datetime
import itertools

# 入力
#N = int(input())
#S = str(input())
#N, A, B = map(int, input().split())

H, W = map(int, input().split())
A = []
B = []
for i in range(H) :
  al = list(map(int, input().split()))
  A.append(al)

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

#print (A)
#print (B)

kmax = 80*H*W
#kmax = 10

dp = [[[False for _ in range(kmax)] for _ in range(W)] for _ in range(H)]

#print (dp)

for i in range(H) :
  for j in range(W) :
    #初期値
    if i == 0 and j == 0 :
      dp[i][j][abs(A[i][j] - B[i][j])] = True
    else :
    #初期値以降
      val = abs(A[i][j] - B[i][j])  #値の差
      
      #i=0のときはj-1のkの状態を参照する
      if i == 0 :
        for k in range(kmax) :
          if dp[i][j-1][k] == True :
              dp[i][j][k+val] = True
              dp[i][j][abs(k-val)] = True
        
      #j=0のときはi-1のkの状態を参照する
      elif j == 0 :
        for k in range(kmax) :
          if dp[i-1][j][k] == True :
              dp[i][j][k+val] = True
              dp[i][j][abs(k-val)] = True
      
      #i!=0, j!=0のときはi-1、j-1の両方のkの状態を参照する
      else :
        for k in range(kmax) :
          if dp[i-1][j][k] == True :
              dp[i][j][k+val] = True
              dp[i][j][abs(k-val)] = True
        for k in range(kmax) :
          if dp[i][j-1][k] == True :
              dp[i][j][k+val] = True
              dp[i][j][abs(k-val)] = True

#答え
for k in range(kmax) :
  if dp[H-1][W-1][k] == True :
    print (k)
    exit(0)

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

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

def run():
  H, W = map(int, input().split())
  df = np.zeros((H, W)).astype(int)
  M = min(H, W)
  dp = [[[] for w in range(W)] for h in range(H)]
  for h in range(H):
      for w, v in enumerate(map(int, input().split())):
          df[h][w] = v
  for h in range(H):
      for w, v in enumerate(map(int, input().split())):
          df[h][w] = abs(df[h][w]-v)
  for h in range(H):
      for w in range(W):
          diff = df[h][w]
          if h == w and h == 0:
              dp[h][w] = [diff]
              continue
          temp = []
          for x, y in [[h-1, w], [h, w-1]]:
              if 0 > min(x, y): continue
              for v in dp[x][y]:
                  temp.append(abs(v + diff))
                  temp.append(abs(v - diff))
          dp[h][w] = set(temp)
  print(min(dp[-1][-1]))
  
if __name__ == '__main__':
  run()

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
AB = []
max_ab = 0
for i in range(H):
  AB.append(list(map(int, input().split())))
  max_ab = max(max_ab, max(AB[-1]))

for i in range(H):
  AB[i] = [abs(AB[i][j] - int(b)) for j, b in enumerate(input().split())]

K = max_ab*(H + W) + 1
dp = [[[False]*K for w in range(W+1)] for h in range(H+1)]
dp[0][0][0] = True
dp[0][1][0] = True
dp[1][0][0] = True

for h in range(H):
  for w in range(W):
    ab = AB[h][w]
    for i, tp in enumerate(dp[h][w+1]):
      if not tp:
        continue
        
      dp[h+1][w+1][abs(i+ab)] = True
      dp[h+1][w+1][abs(i-ab)] = True
      
    for i, tp in enumerate(dp[h+1][w]):
      if not tp:
        continue
      
      dp[h+1][w+1][abs(i+ab)] = True
      dp[h+1][w+1][abs(i-ab)] = True

for k in range(K):
  if dp[H][W][k]:
    print(k)
    break

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
AB = []
for i in range(H):
  AB.append(list(map(int, input().split())))

for i in range(H):
  AB[i] = [abs(AB[i][j] - int(b)) for j, b in enumerate(input().split())]

K = 80*(H + W) + 1
dp = [[[False]*K for w in range(W+1)] for h in range(H+1)]
dp[0][0][0] = True
dp[0][1][0] = True
dp[1][0][0] = True

for h in range(H):
  for w in range(W):
    ab = AB[h][w]
    for i, tp in enumerate(dp[h][w+1]):
      if not tp:
        continue
        
      dp[h+1][w+1][abs(i+ab)] = True
      dp[h+1][w+1][abs(i-ab)] = True
      
    for i, tp in enumerate(dp[h+1][w]):
      if not tp:
        continue
      
      dp[h+1][w+1][abs(i+ab)] = True
      dp[h+1][w+1][abs(i-ab)] = True

for k in range(K):
  if dp[H][W][k]:
    print(k)
    break

Submission status: Time Limit Exceeded

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


X = [[0] * W for i in range(H)]
for h in range(H):
    for w in range(W):
        X[h][w] = abs(A[h][w] - B[h][w])


# dp[h][w][x] := (h, w)の時点で差をxにできるか？
M = 3200
dp = [[[False] * (M + 1) for j in range(W)] for i in range(H)]
dp[0][0][X[0][0]] = True


for h in range(H):
    for w in range(W):
        for m in range(M):
            x = X[h][w]
            if (0 <= h - 1) and abs(m + X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h - 1][w][abs(m + x)]

            if (0 <= h - 1) and abs(m - X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h - 1][w][abs(m - x)]

            if (0 <= w - 1) and abs(m + X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h][w - 1][abs(m + x)]

            if (0 <= w - 1) and abs(m + X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h][w - 1][abs(m - x)]


ans = float('inf')
for x in range(M + 1):
    if dp[H - 1][W - 1][x]:
        ans = min(ans, x)

print(ans)

Submission status: Time Limit Exceeded

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


X = [[0] * W for i in range(H)]
for h in range(H):
    for w in range(W):
        X[h][w] = abs(A[h][w] - B[h][w])


# dp[h][w][x] := (h, w)の時点で差をxにできるか？
M = max(H, W) * 80
dp = [[[False] * (M + 1) for j in range(W)] for i in range(H)]
dp[0][0][X[0][0]] = True


for h in range(H):
    for w in range(W):
        for m in range(M):
            nx = X[h][w]
            if (0 <= h - 1) and abs(m + X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h - 1][w][abs(m + X[h][w])]

            if (0 <= h - 1) and abs(m - X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h - 1][w][abs(m - X[h][w])]

            if (0 <= w - 1) and abs(m + X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h][w - 1][abs(m + X[h][w])]

            if (0 <= w - 1) and abs(m + X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h][w - 1][abs(m - X[h][w])]


ans = float('inf')
for x in range(M + 1):
    if dp[H - 1][W - 1][x]:
        ans = min(ans, x)

print(ans)

Submission status: Time Limit Exceeded

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


X = [[0] * W for i in range(H)]
for h in range(H):
    for w in range(W):
        X[h][w] = abs(A[h][w] - B[h][w])


# dp[h][w][x] := (h, w)の時点で差をxにできるか？
M = H * W * 80
dp = [[[False] * (M + 1) for j in range(W)] for i in range(H)]
dp[0][0][X[0][0]] = True


for h in range(H):
    for w in range(W):
        for m in range(M):
            nx = X[h][w]
            if (0 <= h - 1) and abs(m + X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h - 1][w][abs(m + X[h][w])]

            if (0 <= h - 1) and abs(m - X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h - 1][w][abs(m - X[h][w])]

            if (0 <= w - 1) and abs(m + X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h][w - 1][abs(m + X[h][w])]

            if (0 <= w - 1) and abs(m + X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h][w - 1][abs(m - X[h][w])]


ans = float('inf')
for x in range(M + 1):
    if dp[H - 1][W - 1][x]:
        ans = min(ans, x)

print(ans)

Submission status: Time Limit Exceeded

Solution code:
h, w = list(map(int, input().split()))

aa = [list(map(int, input().split())) for _ in range(h)]
bb = [list(map(int, input().split())) for _ in range(h)]

dif = []
for i in range(h):
    dif.append([int(abs(a - b)) for a, b in zip(aa[i], bb[i])])

dp = [[set() for _ in range(w + 2)] for _ in range(h + 2)]

dp[1][0] = set([0])

lim = [[0 for _ in range(w + 2)] for _ in range(h + 2)]
for i in reversed(range(1, h + 1)):
    for j in reversed(range(1, w + 1)):
        lim[i][j] = max(lim[i + 1][j], lim[i][j + 1]) + dif[i - 1][j - 1]



for i in range(1, h + 1):
    for j in range(1, w + 1):
        origin = dp[i - 1][j] | dp[i][j - 1]
        cand = {x + dif[i - 1][j - 1] for x in origin} | {x - dif[i - 1][j - 1] for x in origin}
        dp[i][j] = {x for x in cand if x <= lim[i][j]} | {min({10 ** 10}.union({x for x in cand if x > lim[i][j]}))}

print(min([abs(x) for x in dp[h][w]]))

Submission status: Time Limit Exceeded

Solution code:
h, w = list(map(int, input().split()))

aa = [list(map(int, input().split())) for _ in range(h)]
bb = [list(map(int, input().split())) for _ in range(h)]

dif = []
for i in range(h):
    dif.append([int(abs(a - b)) for a, b in zip(aa[i], bb[i])])

dp = [[set() for _ in range(w + 2)] for _ in range(h + 2)]

dp[1][0] = [0]

lim = [[0 for _ in range(w + 2)] for _ in range(h + 2)]
for i in reversed(range(1, h + 1)):
    for j in reversed(range(1, w + 1)):
        lim[i][j] = max(lim[i + 1][j], lim[i][j + 1]) + dif[i - 1][j - 1]



for i in range(1, h + 1):
    for j in range(1, w + 1):
        cand = list(set([x + dif[i - 1][j - 1] for x in dp[i - 1][j]]
                          + [x - dif[i - 1][j - 1] for x in dp[i - 1][j]]
                          + [x + dif[i - 1][j - 1] for x in dp[i][j - 1]]
                          + [x - dif[i - 1][j - 1] for x in dp[i][j - 1]]))
        dp[i][j] = [x for x in cand if x <= lim[i][j]] + [min([x for x in cand if x > lim[i][j]] + [10 ** 10])]

print(min([abs(x) for x in dp[h][w]]))

Submission status: Time Limit Exceeded

Solution code:
h, w = list(map(int, input().split()))

aa = [list(map(int, input().split())) for _ in range(h)]
bb = [list(map(int, input().split())) for _ in range(h)]

dif = []
for i in range(h):
    dif.append([int(abs(a - b)) for a, b in zip(aa[i], bb[i])])

dp = [[set() for _ in range(w + 1)] for _ in range(h + 1)]

dp[1][0] = set([0])

for i in range(1, h + 1):
    for j in range(1, w + 1):
        dp[i][j] = list(set([x + dif[i - 1][j - 1] for x in dp[i - 1][j]]
                          + [x - dif[i - 1][j - 1] for x in dp[i - 1][j]]
                          + [x + dif[i - 1][j - 1] for x in dp[i][j - 1]]
                          + [x - dif[i - 1][j - 1] for x in dp[i][j - 1]]))

print(min([abs(x) for x in dp[h][w]]))

Submission status: Time Limit Exceeded

Solution code:
import numpy as np

h, w = list(map(int, input().split()))

aa = [list(map(int, input().split())) for _ in range(h)]
bb = [list(map(int, input().split())) for _ in range(h)]

dif = []
for i in range(h):
    dif.append([int(abs(a - b)) for a, b in zip(aa[i], bb[i])])

dp = [[set() for _ in range(w + 1)] for _ in range(h + 1)]

dp[1][0] = set([0])

for i in range(1, h + 1):
    for j in range(1, w + 1):
        dp[i][j] = list(set([x + dif[i - 1][j - 1] for x in dp[i - 1][j]]
                          + [x - dif[i - 1][j - 1] for x in dp[i - 1][j]]
                          + [x + dif[i - 1][j - 1] for x in dp[i][j - 1]]
                          + [x - dif[i - 1][j - 1] for x in dp[i][j - 1]]))

print(min([abs(x) for x in dp[h][w]]))

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

k_max = 81 * (h + w)
dp = [[[0] * (k_max+1) for _ in range(w+1)] for _ in range(h+1)]
for i in range(h + 1):
    dp[i][0][0] = 1

for j in range(w + 1):
    dp[0][j][0] = 1

for i in range(1, h + 1):
    for j in range(1, w + 1):
        ab = abs(a[i-1][j-1] - b[i-1][j-1])
        for k in range(k_max + 1):
            d = dp[i-1][j][abs(k-ab)]
            r = dp[i][j-1][abs(k-ab)]
            if k + ab <= k_max:
                d |= dp[i-1][j][k+ab]
                r |= dp[i][j-1][k+ab]
            dp[i][j][k] = d or r

ans = 0
for k in range(k_max + 1):
    if dp[h][w][k]:
        ans = k
        break

print(ans)

Submission status: Time Limit Exceeded

Solution code:
h, w = map(int, input().split())
lsa = [list(map(int, input().split())) for i in range(h)]
lsb = [list(map(int, input().split())) for i in range(h)]
ls = [[abs(lsa[i][j] - lsb[i][j]) for j in range(w)] for i in range(h)]


dp = [[set() for _ in range(w)] for _ in range(h)]

dp[0][0].add(ls[0][0])

for i in range(h):
    for j in range(w):

        lsap = set()  
        if j != 0:  
            for k in dp[i][j-1]:
                lsap.add(k+ls[i][j])
                lsap.add(abs(k-ls[i][j]))

        if i != 0:
            for k in dp[i-1][j]:
                lsap.add(k+ls[i][j])
                lsap.add(abs(k-ls[i][j]))
        
        if lsap:
            dp[i][j] = lsap

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 = [tuple(map(int, input().split())) for _ in range(h)]
b = [tuple(map(int, input().split())) for _ in range(h)]
c = [[a[i][j] - b[i][j] for j in range(w)] for i in range(h)]

dp = [[set() for _ in range(w)] for _ in range(h)]
dp[0][0].add(c[0][0])

for i in range(h):
    for j in range(w):
        for x in dp[i][j]:
            if i < h - 1:
                dp[i + 1][j].add(abs(x + c[i + 1][j]))
                dp[i + 1][j].add(abs(x - c[i + 1][j]))

            if j < w - 1:
                dp[i][j + 1].add(abs(x + c[i][j + 1]))
                dp[i][j + 1].add(abs(x - c[i][j + 1]))

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 = [tuple(map(int, input().split())) for _ in range(h)]
b = [tuple(map(int, input().split())) for _ in range(h)]
c = [[a[i][j] - b[i][j] for j in range(w)] for i in range(h)]

MAX_K = 80 * (h + w) + 5
dp = [[[False] * (MAX_K + 80) for _ in range(w)] for _ in range(h)]
dp[0][0][abs(c[0][0])] = True

for i in range(h):
    for j in range(w):
        for k in range(MAX_K):
            if dp[i][j][k]:
                if i < h - 1:
                    dp[i + 1][j][abs(k + c[i + 1][j])] = True
                    dp[i + 1][j][abs(k - c[i + 1][j])] = True

                if j < w - 1:
                    dp[i][j + 1][abs(k + c[i][j + 1])] = True
                    dp[i][j + 1][abs(k - c[i][j + 1])] = True

for k in range(MAX_K):
    if dp[h - 1][w - 1][k]:
        ans = k
        break
print(ans)

Submission status: Time Limit Exceeded

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

h, w = map(int, input().split())
a = [tuple(map(int, input().split())) for _ in range(h)]
b = [tuple(map(int, input().split())) for _ in range(h)]
c = [[a[i][j] - b[i][j] for j in range(w)] for i in range(h)]

MAX_K = 12805
dp = [[[False] * (MAX_K + 80) for _ in range(w)] for _ in range(h)]
dp[0][0][abs(c[0][0])] = True

for i in range(h):
    for j in range(w):
        for k in range(MAX_K):
            if dp[i][j][k]:
                if i < h - 1:
                    dp[i + 1][j][abs(k + c[i + 1][j])] = True
                    dp[i + 1][j][abs(k - c[i + 1][j])] = True

                if j < w - 1:
                    dp[i][j + 1][abs(k + c[i][j + 1])] = True
                    dp[i][j + 1][abs(k - c[i][j + 1])] = True

for k in range(MAX_K):
    if dp[h - 1][w - 1][k]:
        ans = k
        break
print(ans)

Submission status: Time Limit Exceeded

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

dp = [[set() for _ in range(w)] for _ in range(h)]
dp[0][0].add(c[0][0])

for i in range(h):
    for j in range(w):
        for x in dp[i][j]:
            if i < h - 1:
                dp[i + 1][j].add(abs(x + c[i + 1][j]))
                dp[i + 1][j].add(abs(x - c[i + 1][j]))

            if j < w - 1:
                dp[i][j + 1].add(abs(x + c[i][j + 1]))
                dp[i][j + 1].add(abs(x - c[i][j + 1]))

print(min(dp[h - 1][w - 1]))

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
AB = []
for i in range(H):
  AB.append(list(map(int, input().split())))

for i in range(H):
  AB[i] = [abs(AB[i][j] - int(b)) for j, b in enumerate(input().split())]

K = 80*(H + W)
dp = [[[False]*K for w in range(W)] for h in range(H)]
dp[0][0][abs(AB[0][0])] = True


for h in range(H):
  for w in range(W):
    if h == 0:
      if w == 0:
        continue
      else:
        for k in range(K):
          ab = AB[h][w]
          k1, k2 = abs(k - ab), k + ab
          if k1 < K and k2 < K:
            dp[h][w][k] = dp[h][w-1][k1] or dp[h][w-1][k2]
          elif k1 < K and k2 >= K:
            dp[h][w][k] = dp[h][w-1][k1]
          else:
            dp[h][w][k] = dp[h][w-1][k2]
    else:
      if w == 0:
        for k in range(K):
          ab = AB[h][w]
          k1, k2 = abs(k - ab), k + ab
          if k1 < K and k2 < K:
            dp[h][w][k] = dp[h-1][w][k1] or dp[h-1][w][k2]
          elif k1 < K and k2 >= K:
            dp[h][w][k] = dp[h-1][w][k1]
          else:
            dp[h][w][k] = dp[h-1][w][k2]
      else:
        for k in range(K):
          ab = AB[h][w]
          k1, k2 = abs(k - ab), k + ab
          if k1 < K and k2 < K:
            dp[h][w][k] = dp[h][w-1][k1] or dp[h][w-1][k2] or dp[h-1][w][k1] or dp[h-1][w][k2]
          elif k1 < K and k2 >= K:
            dp[h][w][k] = dp[h][w-1][k1] or dp[h-1][w][k1]
          else:
            dp[h][w][k] = dp[h][w-1][k2] or dp[h-1][w][k2]

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

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
AB = []
for i in range(H):
  AB.append(list(map(int, input().split())))

for i in range(H):
  AB[i] = [abs(AB[i][j] - int(b)) for j, b in enumerate(input().split())]

K = 80*(H + W)
dp = [[[False]*K for w in range(W)] for h in range(H)]
dp[0][0][abs(AB[0][0])] = True


for h in range(H):
  for w in range(W):
    if h == 0:
      if w == 0:
        continue
      else:
        for k in range(K):
          ab = AB[h][w]
          k1, k2 = abs(k - ab), k + ab
          if k1 < K and k2 < K:
            dp[h][w][k] = dp[h][w-1][k1] or dp[h][w-1][k2]
          elif k1 < K and k2 >= K:
            dp[h][w][k] = dp[h][w-1][k1]
          else:
            dp[h][w][k] = dp[h][w-1][k2]
    else:
      if w == 0:
        for k in range(K):
          ab = AB[h][w]
          k1, k2 = abs(k - ab), k + ab
          if k1 < K and k2 < K:
            dp[h][w][k] = dp[h-1][w][k1] or dp[h-1][w][k2]
          elif k1 < K and k2 >= K:
            dp[h][w][k] = dp[h-1][w][k1]
          else:
            dp[h][w][k] = dp[h-1][w][k2]
      else:
        for k in range(K):
          ab = AB[h][w]
          k1, k2 = abs(k - ab), k + ab
          if k1 < K and k2 < K:
            dp[h][w][k] = dp[h][w-1][k1] or dp[h][w-1][k2] or dp[h-1][w][k1] or dp[h-1][w][k2]
          elif k1 < K and k2 >= K:
            dp[h][w][k] = dp[h][w-1][k1] or dp[h-1][w][k1]
          else:
            dp[h][w][k] = dp[h][w-1][k2] or dp[h-1][w][k2]

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

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
AB = []
for i in range(H):
  AB.append(list(map(int, input().split())))

for i in range(H):
  AB[i] = [abs(AB[i][j] - int(b)) for j, b in enumerate(input().split())]

K = 80*H*W
dp = [[[False]*K for w in range(W)] for h in range(H)]
dp[0][0][abs(AB[0][0])] = True


for h in range(H):
  for w in range(W):
    if h == 0:
      if w == 0:
        continue
      else:
        for k in range(K):
          ab = AB[h][w]
          k1, k2 = abs(k - ab), k + ab
          if k1 < K and k2 < K:
            dp[h][w][k] = dp[h][w-1][k1] or dp[h][w-1][k2]
          elif k1 < K and k2 >= K:
            dp[h][w][k] = dp[h][w-1][k1]
          else:
            dp[h][w][k] = dp[h][w-1][k2]
    else:
      if w == 0:
        for k in range(K):
          ab = AB[h][w]
          k1, k2 = abs(k - ab), k + ab
          if k1 < K and k2 < K:
            dp[h][w][k] = dp[h-1][w][k1] or dp[h-1][w][k2]
          elif k1 < K and k2 >= K:
            dp[h][w][k] = dp[h-1][w][k1]
          else:
            dp[h][w][k] = dp[h-1][w][k2]
      else:
        for k in range(K):
          ab = AB[h][w]
          k1, k2 = abs(k - ab), k + ab
          if k1 < K and k2 < K:
            dp[h][w][k] = dp[h][w-1][k1] or dp[h][w-1][k2] or dp[h-1][w][k1] or dp[h-1][w][k2]
          elif k1 < K and k2 >= K:
            dp[h][w][k] = dp[h][w-1][k1] or dp[h-1][w][k1]
          else:
            dp[h][w][k] = dp[h][w-1][k2] or dp[h-1][w][k2]

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

Submission status: Time Limit Exceeded

Solution code:
from queue import Queue
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)]
ab = []
for i in range(h):
    l = []
    for j in range(w):
        l.append(abs(a[i][j]-b[i][j]))
    ab.append(l)

q = Queue()
is_checked = [[False for _ in range(w)] for _ in range(h)]
dp = [[[False for _ in range(25601)] for _ in range(w)] for _ in range(h)]

# 幅優先的な
q.put((0, 0))

while True:
    if q.empty():
        break
    else:
        (x, y) = q.get()
        v = ab[y][x]
        if x == 0 and y == 0:
            dp[y][x][12800+v] = True
            dp[y][x][12800-v] = True
        if x > 0:
            l = []
            for i in range(25601):
                if dp[y][x-1][i]:
                    l.append(i)
            for i in l:
                dp[y][x][i+v] = True
                dp[y][x][i-v] = True
        if y > 0:
            l = []
            for i in range(25601):
                if dp[y-1][x][i]:
                    l.append(i)
            for i in l:
                dp[y][x][i+v] = True
                dp[y][x][i-v] = True
        if x < w - 1:
            if not is_checked[y][x+1]:
                q.put((x+1, y))
                is_checked[y][x+1] = True
        if y < h - 1:
            if not is_checked[y+1][x]:
                q.put((x, y+1))
                is_checked[y+1][x] = True

v = ab[h-1][w-1]
l = []
for i in range(25601):
    if dp[h-1][w-2][i]:
        l.append(i)
for i in l:
    dp[h-1][w-1][i+v] = True
    dp[h-1][w-1][i-v] = True

l = []
for i in range(25601):
    if dp[h-2][w-1][i]:
        l.append(i)
for i in l:
    dp[h-1][w-1][i+v] = True
    dp[h-1][w-1][i-v] = True

ans = 10 ** 9
for i in range(25601):
    if dp[h-1][w-1][i]:
        ans = min(ans, abs(12800 - i))

print(ans)