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 i in range(h)]
b = [list(map(int,input().split())) for i in range(h)]
kanousei = [[[0] * (100 ** 2) for i in range(w)] for j in range(h)]
kanousei[0][0][abs(a[0][0]-b[0][0])] = 1
kanousei[0][0][abs(b[0][0]-a[0][0])] = 1
for RangeFromZero in range(1,h+w-1):
  #     print(RangeFromZero)
    for x in range(RangeFromZero,-1,-1):
        y = RangeFromZero-x
  #         print(x,y)
        if x < w and y < h:
            if 0 < x:
                for i in range(100 ** 2):
                    if kanousei[y][x-1][i] == 1:
                        kanousei[y][x][i+abs(a[y][x]-b[y][x])] = 1
                        kanousei[y][x][abs(i-abs(a[y][x]-b[y][x]))] = 1
            if 0 < y:
                for i in range(100 ** 2):
                    if kanousei[y-1][x][i] == 1:
                        kanousei[y][x][i+abs(a[y][x]-b[y][x])] = 1
                        kanousei[y][x][abs(i-abs(a[y][x]-b[y][x]))] = 1
print(kanousei[h-1][w-1].index(1))

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)]
kanousei = [[[0] * (100 ** 3) for i in range(w)] for j in range(h)]
kanousei[0][0][abs(a[0][0]-b[0][0])] = 1
kanousei[0][0][abs(b[0][0]-a[0][0])] = 1
for RangeFromZero in range(1,h+w-1):
  #     print(RangeFromZero)
    for x in range(RangeFromZero,-1,-1):
        y = RangeFromZero-x
  #         print(x,y)
        if x < w and y < h:
            if 0 < x:
                for i in range(100 ** 2):
                    if kanousei[y][x-1][i] == 1:
                        kanousei[y][x][i+abs(a[y][x]-b[y][x])] = 1
                        kanousei[y][x][abs(i-abs(a[y][x]-b[y][x]))] = 1
            if 0 < y:
                for i in range(100 ** 2):
                    if kanousei[y-1][x][i] == 1:
                        kanousei[y][x][i+abs(a[y][x]-b[y][x])] = 1
                        kanousei[y][x][abs(i-abs(a[y][x]-b[y][x]))] = 1
print(kanousei[h-1][w-1].index(1))

Submission status: Time Limit Exceeded

Solution code:
h, w = map(int, input().split())
mat = []
for i in range(h):
  a = list(map(int, input().split()))
  mat.append(a)
for i in range(h):
  b = list(map(int, input().split()))
  for j in range(w):
    mat[i][j] = abs(mat[i][j]-b[j])

dp = [[[False]*(80*(h+w)) for i in range(w)] for k in range(h)]
dp[0][0][mat[0][0]] = True
for y in range(h):
  for x in range(w):
      for k in range(50*(x+y)):
        if x > 0 and dp[y][x-1][k] or y > 0 and dp[y-1][x][k]:
          d = dp[y][x]
          d[k+mat[y][x]] = True
          d[abs(k-mat[y][x])] = True

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

Submission status: Time Limit Exceeded

Solution code:
h, w = map(int, input().split())
mat = []
for i in range(h):
  a = list(map(int, input().split()))
  mat.append(a)
for i in range(h):
  b = list(map(int, input().split()))
  for j in range(w):
    mat[i][j] = abs(mat[i][j]-b[j])

dp = [[[False]*(80*(h+w)) for i in range(w)] for k in range(h)]
dp[0][0][mat[0][0]] = True
for y in range(h):
  for x in range(w):
      for k in range(80*(x+y)):
        if x > 0 and dp[y][x-1][k] or y > 0 and dp[y-1][x][k]:
          d = dp[y][x]
          d[k+mat[y][x]] = True
          d[abs(k-mat[y][x])] = True

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

Submission status: Time Limit Exceeded

Solution code:
h, w = map(int, input().split())
mat = []
for i in range(h):
  a = list(map(int, input().split()))
  mat.append(a)
for i in range(h):
  b = list(map(int, input().split()))
  for j in range(w):
    mat[i][j] = abs(mat[i][j]-b[j])

dp = [[[False]*(80*(h+w)) for i in range(w)] for k in range(h)]
dp[0][0][mat[0][0]] = True
for y in range(h):
  for x in range(w):
      for k in range(80*(h+w)):
        if x > 0 and dp[y][x-1][k] or y > 0 and dp[y-1][x][k]:
          d = dp[y][x]
          d[k+mat[y][x]] = True
          d[abs(k-mat[y][x])] = True

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

Submission status: Time Limit Exceeded

Solution code:
h, w = map(int, input().split())
mat = []
for i in range(h):
  a = list(map(int, input().split()))
  mat.append(a)
for i in range(h):
  b = list(map(int, input().split()))
  for j in range(w):
    mat[i][j] = abs(mat[i][j]-b[j])

dp = [[[False]*(80*(h+w)) for i in range(w)] for k in range(h)]
dp[0][0][mat[0][0]] = True
for y in range(h):
  for x in range(w):
      for k in range(80*(h+w)):
        if x > 0 and dp[y][x-1][k] or y > 0 and dp[y-1][x][k]:
          dp[y][x][k+mat[y][x]] = True
          dp[y][x][abs(k-mat[y][x])] = True

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

Submission status: Time Limit Exceeded

Solution code:
h, w = map(int, input().split())
mat = []
for i in range(h):
  a = list(map(int, input().split()))
  mat.append(a)
for i in range(h):
  b = list(map(int, input().split()))
  for j in range(w):
    mat[i][j] = abs(mat[i][j]-b[j])

dp = [[[False]*(80*(h+w)) for i in range(w)] for k in range(h)]
dp[0][0][mat[0][0]] = True
for y in range(h):
  for x in range(w):
    if x > 0:
      for k in range(80*(h+w)):
        if dp[y][x-1][k]:
          dp[y][x][k+mat[y][x]] = True
          dp[y][x][abs(k-mat[y][x])] = True
    if y > 0:
      for k in range(80*(h+w)):
        if dp[y-1][x][k]:
          dp[y][x][k+mat[y][x]] = True
          dp[y][x][abs(k-mat[y][x])] = True
for k in range(80*(h+w)):
  if dp[h-1][w-1][k]:
    print(k)
    exit()

Submission status: Time Limit Exceeded

Solution code:
h, w = map(int, input().split())
mat = []
for i in range(h):
  a = list(map(int, input().split()))
  mat.append(a)
for i in range(h):
  b = list(map(int, input().split()))
  for j in range(w):
    mat[i][j] = abs(mat[i][j]-b[j])

dp = [[[False]*(80*80*80) for i in range(w)] for k in range(h)]
dp[0][0][mat[0][0]] = True
for y in range(h):
  for x in range(w):
    if x > 0:
      for k in range(80**3):
        if dp[y][x-1][k]:
          dp[y][x][k+mat[y][x]] = True
          dp[y][x][abs(k-mat[y][x])] = True
    if y > 0:
      for k in range(80**3):
        if dp[y-1][x][k]:
          dp[y][x][k+mat[y][x]] = True
          dp[y][x][abs(k-mat[y][x])] = True
for k in range(80**3):
  if dp[h-1][w-1][k]:
    print(k)
    exit()

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

MAX_VAL = 80*80+5
dp = [[[False]*(MAX_VAL) for _ in range(w+1)] for _ in range(h+1)]

bias = MAX_VAL//2
icost = a[0][0] - b[0][0]
dp[1][1][icost + bias] = True

dp[1][1][-icost + bias] = True

for i in range(1,h+1):
    for j in range(1,w+1):
        cost = a[i-1][j-1] - b[i-1][j-1]
        for k in range(MAX_VAL):
            if dp[i-1][j][k]:
                if 0 <= k+cost < MAX_VAL:
                    dp[i][j][k+cost] = True
                if 0 <= k-cost < MAX_VAL:
                    dp[i][j][k-cost] = True
            if dp[i][j-1][k]:
                if 0 <= k+cost < MAX_VAL:
                    dp[i][j][k+cost] = True
                if 0 <= k-cost < MAX_VAL:
                    dp[i][j][k-cost] = True

ans = 10010001001
for i in range(MAX_VAL):
    if dp[h][w][i]:
        ans = min(ans,abs(i-bias))
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)]

MAX_VAL = 80*80*2+5
dp = [[[False]*(MAX_VAL) for _ in range(w+1)] for _ in range(h+1)]

bias = MAX_VAL//2
icost = a[0][0] - b[0][0]
dp[1][1][icost + bias] = True

dp[1][1][-icost + bias] = True

for i in range(1,h+1):
    for j in range(1,w+1):
        cost = a[i-1][j-1] - b[i-1][j-1]
        for k in range(MAX_VAL):
            if dp[i-1][j][k]:
                if 0 <= k+cost < MAX_VAL:
                    dp[i][j][k+cost] = True
                if 0 <= k-cost < MAX_VAL:
                    dp[i][j][k-cost] = True
            if dp[i][j-1][k]:
                if 0 <= k+cost < MAX_VAL:
                    dp[i][j][k+cost] = True
                if 0 <= k-cost < MAX_VAL:
                    dp[i][j][k-cost] = True

ans = 10010001001
for i in range(MAX_VAL):
    if dp[h][w][i]:
        ans = min(ans,abs(i-bias))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
h,w = map(int, input().split())
a = [list(map(int, input().split()))+[0] for i in range(h)] + [[0]*(w+1)]
b = [list(map(int, input().split()))+[0] for i in range(h)] + [[0]*(w+1)]
diff = [[x-y for x,y in zip(i,j)] for i,j in zip(a,b)]
diff_set = [[set() for j in range(w+1)] for i in range(h+1)]
diff_set[0][0].add(diff[0][0])
for i in range(h):
  for j in range(w):
    for x in diff_set[i][j]:
      diff_set[i][j+1].add(abs(x+diff[i][j+1]))
      diff_set[i][j+1].add(abs(x-diff[i][j+1]))
      diff_set[i+1][j].add(abs(x+diff[i+1][j]))
      diff_set[i+1][j].add(abs(x-diff[i+1][j]))
ans = min(diff_set[h-1][w-1])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
sys.setrecursionlimit(100000000)
input = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = 10 ** 15

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

    dp = [[[0]*12800 for _ in range(W)] for _ in range(H)]
    dp[0][0][6400] = 1
    for i in range(H):
        for j in range(W):
            if i == H - 1 and j == W - 1:
                break
            a,b = A[i][j],B[i][j]
            for k in range(12800):
                if dp[i][j][k] == 0:
                    continue
                if 0 <= k + a - b < 12800:
                    if i + 1 < H:
                        dp[i + 1][j][k + a - b] |= dp[i][j][k]
                    if j + 1  < W:
                        dp[i][j + 1][k + a - b] |= dp[i][j][k]
                if 0 <= k - a + b < 12800:
                    if i + 1 < H:
                        dp[i + 1][j][k - a + b] |= dp[i][j][k]
                    if j + 1 < W:
                        dp[i][j + 1][k - a + b] |= dp[i][j][k]
    
    ans = INF
    a,b = A[-1][-1],B[-1][-1]
    for i in range(12800):
        if (0 <= i + a - b < 12800 and dp[-1][-1][i + a - b]) or (0 <= i - a + b < 12800 and dp[-1][-1][i - a + b]):
            ans = min(ans,abs(i - 6400))

    print(ans)                
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# E
# dp[[i][j]で、(i,j)で可能な偏りをリストとして格納していく

import sys
# 再起回数上限変更
# sys.setrecursionlimit(1000000)

H, W = map(int, input().split())
a = []
x = []

for i in range(H):
    a.append(list(map(int, input().split())))
for i in range(H):
    x.append(list(map(int, input().split())))
    for j in range(W):
        x[i][j] = abs(x[i][j] - a[i][j])


dp = [[set([]) for _ in range(W)] for _ in range(H)]
dp[0][0].add(x[0][0])

for i in range(H):
    for j in range(W):
        if i > 0:
            for num in dp[i-1][j]:
                dp[i][j].add(num + x[i][j])
                dp[i][j].add(abs(num - x[i][j]))
        if j > 0:
            for num in dp[i][j-1]:
                dp[i][j].add(num + x[i][j])
                dp[i][j].add(abs(num - x[i][j]))

# print(dp)

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# E
# dp[[i][j]で、(i,j)で可能な偏りをリストとして格納していく

import sys
# 再起回数上限変更
# sys.setrecursionlimit(1000000)

H, W = map(int, input().split())
a = []
x = []

for i in range(H):
    a.append(list(map(int, input().split())))
for i in range(H):
    x.append(list(map(int, input().split())))
    for j in range(W):
        x[i][j] = abs(x[i][j] - a[i][j])


dp = [[[] for _ in range(W)] for _ in range(H)]
dp[0][0].append(x[0][0])
for i in range(H):
    for j in range(W):
        candidate = []
        if i > 0:
            candidate += dp[i-1][j]
        if j > 0:
            candidate += dp[i][j-1]

        if len(candidate) > 0:
            candidate = list(set(candidate))

        for num in candidate:
            dp[i][j].append(num + x[i][j])
            dp[i][j].append(abs(num - x[i][j]))

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# E
# dp[[i][j]で、(i,j)で可能な偏りをリストとして格納していく

import sys
# 再起回数上限変更
# sys.setrecursionlimit(1000000)

H, W = map(int, input().split())
a = []
x = []

for i in range(H):
    a.append(list(map(int, input().split())))
for i in range(H):
    x.append(list(map(int, input().split())))
    for j in range(W):
        x[i][j] = abs(x[i][j] - a[i][j])


dp = [[[] for _ in range(W)] for _ in range(H)]
dp[0][0].append(x[0][0])
for i in range(H):
    for j in range(W):
        if i > 0:
            for num in dp[i-1][j]:
                dp[i][j].append(num + x[i][j])
                dp[i][j].append(abs(num - x[i][j]))
        if j > 0:
            for num in dp[i][j-1]:
                dp[i][j].append(num + x[i][j])
                dp[i][j].append(abs(num - x[i][j]))
        dp[i][j] = list(set(dp[i][j]))

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

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
# E

import sys
# 再起回数上限変更
# sys.setrecursionlimit(1000000)

H, W = map(int, input().split())
a = []
x = []

for i in range(H):
    a.append(list(map(int, input().split())))
for i in range(H):
    x.append(list(map(int, input().split())))
    for j in range(W):
        x[i][j] = abs(x[i][j] - a[i][j])

# print(x)

dp = [[[] for _ in range(W)] for _ in range(H)]
# print(dp[0][0])

dp[0][0].append(x[0][0])
# print(dp)

for i in range(H):
    for j in range(W):
        if i > 0:
            for num in dp[i-1][j]:
                # print(num)
                dp[i][j].append(num + x[i][j])
                dp[i][j].append(abs(num - x[i][j]))
        if j > 0:
            for num in dp[i][j-1]:
                # print(num)
                dp[i][j].append(num + x[i][j])
                dp[i][j].append(abs(num - x[i][j]))

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

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)]
  M = 100*(h*w)
  dp = [[0]*w for i in range(h)]
  d = abs(A[0][0] - B[0][0])
  mask = (1 << (2*M)) - 1
  dp[0][0] |= 1 << (M-d)
  dp[0][0] |= 1 << (M+d)
  for i in range(h):
    for j in range(w):
      d = abs(A[i][j] - B[i][j])
      if i:
        dp[i][j] |= (dp[i-1][j] << d) & mask
        dp[i][j] |= dp[i-1][j] >> d
      if j:
        dp[i][j] |= (dp[i][j-1] << d) & mask
        dp[i][j] |= dp[i][j-1] >> d
  t = dp[-1][-1] >> M
  ans = (t & (-t)).bit_length() - 1
  print(ans)
  
if __name__ == "__main__":
  main()

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)]
M = 100*(h*w)
dp = [[0]*w for i in range(h)]
d = abs(A[0][0] - B[0][0])
dp[0][0] |= 1 << (M-d)
dp[0][0] |= 1 << (M+d)
for i in range(h):
  for j in range(w):
    d = abs(A[i][j] - B[i][j])
    if i:
      dp[i][j] |= (dp[i-1][j] << d) & ((1 << (2*M)) - 1)
      dp[i][j] |= dp[i-1][j] >> d
    if j:
      dp[i][j] |= (dp[i][j-1] << d) & ((1 << (2*M)) - 1)
      dp[i][j] |= dp[i][j-1] >> d
t = dp[-1][-1] >> M
ans = (t & (-t)).bit_length() - 1
print(ans)

Submission status: Time Limit Exceeded

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

h,w = map(int,input().split())
A = []
for i in range(h):
    A.append(list(map(int,input().split())))
B = []
for i in range(h):
    B.append(list(map(int,input().split())))
l = 80*80 + 1
dp = [[[0]*l for _ in range(w+1)] for _ in range(h+1)]

for i in range(h):
    for j in range(w):
        d = abs(A[i][j] - B[i][j])
        if i == 0 and j == 0:
            dp[0][0][d] = 1
        for k in range(l):
            if k+d < l:
                dp[i+1][j][k+d] += dp[i][j][k]
                dp[i][j+1][k+d] += dp[i][j][k]
            dp[i+1][j][abs(k-d)] += dp[i][j][k]
            dp[i][j+1][abs(k-d)] += dp[i][j][k]
for i in range(l+1):
    if dp[h][w-1][i] > 0:
        print(i)
        exit(0)

Submission status: Time Limit Exceeded

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

h,w = map(int,input().split())
A = []
for i in range(h):
    A.append(list(map(int,input().split())))
B = []
for i in range(h):
    B.append(list(map(int,input().split())))
l = 160*80 + 1
dp = [[[0]*l for _ in range(w+1)] for _ in range(h+1)]

for i in range(h):
    for j in range(w):
        d = abs(A[i][j] - B[i][j])
        if i == 0 and j == 0:
            dp[0][0][d] = 1
        for k in range(l):
            if k+d < l:
                dp[i+1][j][k+d] += dp[i][j][k]
                dp[i][j+1][k+d] += dp[i][j][k]
            dp[i+1][j][abs(k-d)] += dp[i][j][k]
            dp[i][j+1][abs(k-d)] += dp[i][j][k]
for i in range(l+1):
    if dp[h][w-1][i] > 0:
        print(i)
        exit(0)

Submission status: Time Limit Exceeded

Solution code:
H,W=map(int,input().split())

amat=[[int(x) for x in input().split()] for _ in range(H)]
bmat=[[int(x) for x in input().split()] for _ in range(H)]

mat=[[None for _ in range(W)] for _ in range(H)]
for i in range(H):
  for j in range(W):
    mat[i][j]=abs(amat[i][j]-bmat[i][j])
#print(mat)

dp=[[set() for _ in range(W)] for _ in range(H)]
dp[0][0]={mat[0][0]}

for i in range(1,H):
  for x in dp[i-1][0]:
    dp[i][0].add(abs(x+mat[i][0]))
    dp[i][0].add(abs(x-mat[i][0]))

for j in range(1,W):
  for x in dp[0][j-1]:
    dp[0][j].add(abs(x+mat[0][j]))
    dp[0][j].add(abs(x-mat[0][j]))
    
for i in range(1,H):
  for j in range(1,W):
    for x in dp[i-1][j]:
      if x+mat[i][j]<=6400:
        dp[i][j].add(abs(x+mat[i][j]))
      dp[i][j].add(abs(x-mat[i][j]))
    for x in dp[i][j-1]:
      if x+mat[i][j]<=6400:
        dp[i][j].add(abs(x+mat[i][j]))
      dp[i][j].add(abs(x-mat[i][j]))
      
#print(dp)
print(min(dp[-1][-1]))

Submission status: Time Limit Exceeded

Solution code:
H,W=map(int,input().split())

amat=[[int(x) for x in input().split()] for _ in range(H)]
bmat=[[int(x) for x in input().split()] for _ in range(H)]

mat=[[None for _ in range(W)] for _ in range(H)]
for i in range(H):
  for j in range(W):
    mat[i][j]=abs(amat[i][j]-bmat[i][j])
#print(mat)

dp=[[set() for _ in range(W)] for _ in range(H)]
dp[0][0]={mat[0][0]}

for i in range(1,H):
  for x in dp[i-1][0]:
    dp[i][0].add(abs(x+mat[i][0]))
    dp[i][0].add(abs(x-mat[i][0]))

for j in range(1,W):
  for x in dp[0][j-1]:
    dp[0][j].add(abs(x+mat[0][j]))
    dp[0][j].add(abs(x-mat[0][j]))
    
for i in range(1,H):
  for j in range(1,W):
    for x in dp[i-1][j]:
      dp[i][j].add(abs(x+mat[i][j]))
      dp[i][j].add(abs(x-mat[i][j]))
    for x in dp[i][j-1]:
      dp[i][j].add(abs(x+mat[i][j]))
      dp[i][j].add(abs(x-mat[i][j]))
      
#print(dp)
print(min(dp[-1][-1]))

Submission status: Time Limit Exceeded

Solution code:
H,W=map(int,input().split())

amat=[[int(x) for x in input().split()] for _ in range(H)]
bmat=[[int(x) for x in input().split()] for _ in range(H)]

mat=[[None for _ in range(W)] for _ in range(H)]
for i in range(H):
  for j in range(W):
    mat[i][j]=abs(amat[i][j]-bmat[i][j])
#print(mat)

dp=[[set() for _ in range(W)] for _ in range(H)]
dp[0][0]={mat[0][0],-mat[0][0]}

for i in range(1,H):
  for x in dp[i-1][0]:
    dp[i][0].add(x+mat[i][0])
    dp[i][0].add(x-mat[i][0])

for j in range(1,W):
  for x in dp[0][j-1]:
    dp[0][j].add(x+mat[0][j])
    dp[0][j].add(x-mat[0][j])
    
for i in range(1,H):
  for j in range(1,W):
    for x in dp[i-1][j]:
      dp[i][j].add(x+mat[i][j])
      dp[i][j].add(x-mat[i][j])
    for x in dp[i][j-1]:
      dp[i][j].add(x+mat[i][j])
      dp[i][j].add(x-mat[i][j])
      
#print(dp)
#print(dp[-1][-1])
answer=float("inf")
for x in dp[-1][-1]:
  answer=min(answer,abs(x))
  
print(answer)

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


dp=[[set() for j in range(w)]for i in range(h)]
sub=a[0][0]-b[0][0]
dp[0][0].add(sub)
dp[0][0].add(-sub)

for c in range(h):
    for l in range(w):
        sub=a[c][l]-b[c][l]
        if c!=0:
            for p in dp[c-1][l]:
                dp[c][l].add(p+sub)
                dp[c][l].add(p-sub)
        if l!=0:
            for p in dp[c][l-1]:
                dp[c][l].add(p+sub)
                dp[c][l].add(p-sub)
st=dp[-1][-1]
for i in range(1000000):
    if (i in st)or(-i in st):
        print(i)
        break

Submission status: Time Limit Exceeded

Solution code:
h, w = map(int, input().split())
grid = []
for _ in range(h):
    grid.append(list(map(int, input().split())))
for i in range(h):
    l = list(map(int, input().split()))
    for j in range(w):
        grid[i][j] = grid[i][j] - l[j]

unba = []

# 0行目
l = [{grid[0][0], -grid[0][0]}]
for i in range(1, w):
    l1 = {j + grid[0][i] for j in l[i-1]}
    l2 = {j - grid[0][i] for j in l[i-1]}
    l.append(l1 | l2)
unba.append(l)

# 1~h-1行目
for i in range(1, h):

    # i行0列
    l1 = {j + grid[i][0] for j in unba[i-1][0]}
    l2 = {j - grid[i][0] for j in unba[i-1][0]}
    l = [l1 | l2]

    # i行j列
    for j in range(1, w):
        l1 = {k + grid[i][j] for k in l[j-1]}
        l2 = {k - grid[i][j] for k in l[j-1]}
        l3 = {k + grid[i][j] for k in unba[i-1][j]}
        l4 = {k - grid[i][j] for k in unba[i-1][j]}
        l.append(l1 | l2 | l3 | l4)
    unba.append(l)

ans = abs(unba[h-1][w-1].pop())
for i in unba[h-1][w-1]:
    if abs(i) < ans:
        ans = abs(i)
print(ans)

Submission status: Time Limit Exceeded

Solution code:
h, w = map(int, input().split())
grid = []
for _ in range(h):
    grid.append(list(map(int, input().split())))
for i in range(h):
    l = list(map(int, input().split()))
    for j in range(w):
        grid[i][j] = grid[i][j] - l[j]

unba = []

# 0行目
l = [[grid[0][0], -grid[0][0]]]
for i in range(1, w):
    l1 = [j + grid[0][i] for j in l[i-1]]
    l2 = [j - grid[0][i] for j in l[i-1]]
    l.append(l1 + l2)
unba.append(l)

# 1~h-1行目
for i in range(1, h):

    # i行0列
    l1 = [j + grid[i][0] for j in unba[i-1][0]]
    l2 = [j - grid[i][0] for j in unba[i-1][0]]
    l = [l1 + l2]

    # i行j列
    for j in range(1, w):
        l1 = [k + grid[i][j] for k in l[j-1]]
        l2 = [k - grid[i][j] for k in l[j-1]]
        l3 = [k + grid[i][j] for k in unba[i-1][j]]
        l4 = [k - grid[i][j] for k in unba[i-1][j]]
        l.append(l1 + l2 + l3 + l4)
    unba.append(l)

ans = abs(unba[h-1][w-1][0])
for i in unba[h-1][w-1]:
    if abs(i) < ans:
        ans = abs(i)
print(ans)

Submission status: Time Limit Exceeded

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

    mx=80*(H+W)
    C=[[abs(A[i][j]-B[i][j]) for j in range(W)] for i in range(H)]
    dp=[[0 for _ in range(W)] for __ in range(H)]
    dp[0][0]=1<<(mx+C[0][0]) | 1<<(mx-C[0][0])
    from collections import deque
    q=deque([(0,0)])
    while q:
        i,j=q.popleft()
        if i>0:
            dp[i][j] |= dp[i-1][j] << C[i][j]
            dp[i][j] |= dp[i-1][j] >> C[i][j]
        if j>0:
            dp[i][j] |= dp[i][j-1] << C[i][j]
            dp[i][j] |= dp[i][j-1] >> C[i][j]
        if i<H-1: q.append((i+1,j))
        if j<W-1: q.append((i,j+1))

    for ans in range(mx+1):
        if dp[H-1][W-1] & (1<<(mx+ans) | 1<<(mx-ans)):
            print(ans)
            break

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

    mx=80*(80+80)
    C=[[A[i][j]-B[i][j] for j in range(W)] for i in range(H)]
    dp=[[[0]*(mx*2+1) for _ in range(W)] for __ in range(H)]
    dp[0][0][C[0][0]+mx]=1
    dp[0][0][-C[0][0]+mx]=1
    from collections import deque
    q=deque([(0,0)])
    while q:
        i,j=q.popleft()
        for d in range(80*(i+j+1)*2):
            d=d-80*(i+j+1)+mx
            if i>0:
                if 0<=d-C[i][j]<=mx*2: dp[i][j][d] = max(dp[i][j][d], dp[i-1][j][d-C[i][j]])
                if 0<=d+C[i][j]<=mx*2: dp[i][j][d] = max(dp[i][j][d], dp[i-1][j][d+C[i][j]])
            if j>0:
                if 0<=d-C[i][j]<=mx*2: dp[i][j][d] = max(dp[i][j][d], dp[i][j-1][d-C[i][j]])
                if 0<=d+C[i][j]<=mx*2: dp[i][j][d] = max(dp[i][j][d], dp[i][j-1][d+C[i][j]])
        if i<H-1: q.append((i+1,j))
        if j<W-1: q.append((i,j+1))

    for ans in range(mx+1):
        if dp[H-1][W-1][mx+ans] or dp[H-1][W-1][mx-ans]:
            print(ans)
            break

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

    mx=80*(80+80)
    C=[[A[i][j]-B[i][j] for j in range(W)] for i in range(H)]
    dp=[[[0]*(mx*2+1) for _ in range(W)] for __ in range(H)]
    dp[0][0][C[0][0]+mx]=1
    dp[0][0][-C[0][0]+mx]=1
    from collections import deque
    q=deque([(0,0)])
    while q:
        i,j=q.popleft()
        for d in range(mx*2):
            if i>0:
                if 0<=d-C[i][j]<=mx*2: dp[i][j][d] = max(dp[i][j][d], dp[i-1][j][d-C[i][j]])
                if 0<=d+C[i][j]<=mx*2: dp[i][j][d] = max(dp[i][j][d], dp[i-1][j][d+C[i][j]])
            if j>0:
                if 0<=d-C[i][j]<=mx*2: dp[i][j][d] = max(dp[i][j][d], dp[i][j-1][d-C[i][j]])
                if 0<=d+C[i][j]<=mx*2: dp[i][j][d] = max(dp[i][j][d], dp[i][j-1][d+C[i][j]])
        if i<H-1: q.append((i+1,j))
        if j<W-1: q.append((i,j+1))

    for ans in range(mx+1):
        if dp[H-1][W-1][mx+ans] or dp[H-1][W-1][mx-ans]:
            print(ans)
            break

if __name__ == '__main__':
    main()

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

C = [[0]*w for _ in range(h)]
for i in range(h):
    for j in range(w):
        C[i][j] = abs(A[i][j]-B[i][j])

import numpy as np
dp = [[0]*(w+1) for _ in range(h+1)]
import numpy as np
d = 80*(h*w)+1
D = 2*d+1
dp[0][0] = np.zeros(D, np.bool)
dp[0][0][C[0][0]+d] = 1
#dp[0][0][-C[0][0]+d] = 1
for i in range(h):
    for j in range(w):
        c = C[i][j]
        if i == 0 and j == 0:
            continue
        elif i == 0:
            b = np.zeros(D, np.bool)
            b[c:] |= dp[i][j-1][:D-c]
            b[:D-c] |= dp[i][j-1][c:]
        elif j == 0:
            b = np.zeros(D, np.bool)
            b[c:] |= dp[i-1][j][:D-c]
            b[:D-c] |= dp[i-1][j][c:]
        else:
            b = np.zeros(D, np.bool)
            b[c:] |= dp[i][j-1][:D-c]
            b[:D-c] |= dp[i][j-1][c:]
            b[c:] |= dp[i-1][j][:D-c]
            b[:D-c] |= dp[i-1][j][c:]
        dp[i][j] = b
#print(dp[h-1][w-1])
ans = float('inf')
for i in range(D):
    if dp[h-1][w-1][i]:
        #print(abs(i-d))
        ans = min(ans, abs(i-d))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
from collections import deque
def input(): return sys.stdin.readline().strip()


def main():
    H, W = map(int, input().split())
    A = []
    for _ in range(H):
        A.append(list(map(int, input().split())))
    B = []
    for _ in range(H):
        B.append(list(map(int, input().split())))
    
    """
    明らかなdp臭。
        dp[i][j] = (マス(0, 0)から(i, j)に進むときの偏りとしてありうるものの集合)
    とおく。(0<=i<=H-1, 0<=j<=W-1, 0<=d<=80)
    """

    dp = [[set([]) for _ in range(W)] for _ in range(H)]
    dp[0][0].add(abs(A[0][0] - B[0][0]))
    for i in range(1, H):
        diff = A[i][0] - B[i][0]
        for val in dp[i - 1][0]:
            if abs(val + diff) <= 1600: dp[i][0].add(abs(val + diff))
            if abs(val - diff) <= 1600: dp[i][0].add(abs(val - diff))
    for j in range(1, W):
        diff = A[0][j] - B[0][j]
        for val in dp[0][j - 1]:
            if abs(val + diff) <= 6400: dp[0][j].add(abs(val + diff))
            if abs(val - diff) <= 6400: dp[0][j].add(abs(val - diff))

    for i in range(1, H):
        for j in range(1, W):
            diff = A[i][j] - B[i][j]
            for val in dp[i - 1][j]:
                if abs(val + diff) <= 6400: dp[i][j].add(abs(val + diff))
                if abs(val - diff) <= 6400: dp[i][j].add(abs(val - diff))
            for val in dp[i][j - 1]:
                if abs(val + diff) <= 6400: dp[i][j].add(abs(val + diff))
                if abs(val - diff) <= 6400: dp[i][j].add(abs(val - diff))
    
    print(min(dp[H - 1][W - 1]))


if __name__ == "__main__":
    main()