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: Runtime Error

Solution code:
HW = input().split()
H = int(HW[0])
W = int(HW[1])
A_s = []
for h in range(H):
  A = input().split()
  A_s.append([int(a) for a in A])
B_s = []
for h in range(H):
  B = input().split()
  B_s.append([int(a) for a in B])

import numpy as np
from collections import defaultdict
A_s = np.array(A_s)
B_s = np.array(B_s)
difference = abs(A_s-B_s)

possibility = defaultdict(dict)
D = H if H > W else W
Md = H if H< W else W

for d in range(D+1):
  for i in range(d+1):
    w = d-i
    if i==0 and w==0:
      possibility[0][0] = [difference[0][0]]
    elif i < H and w < W:
      tmp = []
      if i>0:
        tmp += possibility[i-1][w]
      if w>0:
        tmp += possibility[i][w-1]
      tmp = np.array(tmp)
      plus = abs(tmp + difference[i][w])
      minus = abs(tmp - difference[i][w])
      tmp = plus.tolist()+minus.tolist()
      tmp = list(set(tmp))
      possibility[i][w] = tmp

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

Submission status: Runtime Error

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)]
M = 0
for h in range(H):
    for w in range(W):
        M = max(abs(A[h][w]-B[h][w]),M)
M *= H+W
dp = [[[False]*M for _ in range(W)] for _ in range(H)]

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

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

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

print(ans)

Submission status: Runtime Error

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)]
M = 0
for h in range(H):
    for w in range(W):
        M += abs(A[h][w]-B[h][w])
dp = [[[False]*M for _ in range(W)] for _ in range(H)]

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

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

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

print(ans)

Submission status: Runtime Error

Solution code:
H,W = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(H)]
B = [list(map(int,input().split())) for _ in range(H)]
dp1 = [[False]*((H+W)*80) for _ in range(W)]
dp2 = [[False]*((H+W)*80) for _ in range(W)]
for w in range(W):
    dp1[w][0] = True

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

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

print(ans)

Submission status: Runtime Error

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


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


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


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

Submission status: Runtime Error

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


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


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


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

Submission status: Runtime Error

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 i in range(h)]
for i in range(h):
    for j in range(w):
        C[i][j]=abs(A[i][j]-B[i][j])
    
dp=[[0]*w for i in range(h)]
m=80
dp[0][0] = (1<<m+C[0][0]) | (1<<m-C[i][j])

for i in range(h):
    for j in range(w):
        if i>=1:
            dp[i][j] |= (dp[i-1][j]<<C[i][j]) | (dp[i-1][j]>>C[i][j])
        if j>=1:
            dp[i][j] |= (dp[i][j-1]<<C[i][j]) | (dp[i][j-1]>>C[i][j])
        
b=dp[h-1][w-1]
for i in range(m):
    if (b>>m+i)&1==1:
        ans=i
        break
for i in range(m):
    if (b>>m-i)&1==1:
        ans=min(ans,i)
        break

print(ans)

Submission status: Runtime Error

Solution code:
for xy in range(h*w):
    i=xy//w
    j=xy%w
    z=a[i][j]-b[i][j]
    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]:
            if abs(k+z)<81: x[j].add(abs(k+z))
            elif abs(k-z)<81: x[j].add(abs(k-z))
    elif i%2:
        if j==0:
            for k in x[0]:
                if abs(k+z)<81: y[0].add(abs(k+z))
                if abs(k-z)<81: y[0].add(abs(k-z))
        else:
            for k in x[j]:
                if abs(k+z)<81: y[j].add(abs(k+z))
                if abs(k-z)<81: y[j].add(abs(k-z))
            for k in y[j-1]:
                if abs(k+z)<81: y[j].add(abs(k+z))
                if abs(k-z)<81: y[j].add(abs(k-z))
    else:
        if j==0:
            for k in y[0]:
                if abs(k+z)<81: x[0].add(abs(k+z))
                if abs(k-z)<81: x[0].add(abs(k-z))
        else:
            for k in y[j]:
                if abs(k+z)<81: x[j].add(abs(k+z))
                if abs(k-z)<81: x[j].add(abs(k-z))
            for k in x[j-1]:
                if abs(k+z)<81: x[j].add(abs(k+z))
                if abs(k-z)<81: x[j].add(abs(k-z))
    if j==w-1 and i%2==0:
        y=[set([]) for _ in range(w)]
        ans=min(x[-1])
    elif j==w-1:
        x=[set([]) for _ in range(w)]
        ans=min(y[-1])
print(ans)

Submission status: Runtime Error

Solution code:
import sys
def main():
  input = sys.stdin.readline
  h, w = map(int, input().split())
  A = []
  AB = []
  A_append = A.append
  AB_append = AB.append
  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_cur = [set([]) for _ in range(w)]
  dp_cur[0].add(AB[0][0])
  for i in range(h):
    dp_next = [set([]) for _ in range(w)]
    for j in range(w):
      if j < w - 1:
        add1 = dp_cur[j+1].add
        dif1 = AB[i][j+1]
      if i < h - 1:
        add2 = dp_next[j].add
        dif2 = AB[i+1][j]
      for item in dp_cur[j]:
        if j < w - 1:
          add1(item+dif1)
          add1(abs(item-dif1))
        if i < h - 1:
          add2(item+dif2)
          add2(abs(item-dif2))
    dp_cur = dp_next
  print(min(dp_cur[w-1]))
if __name__ == '__main__':
  main()

Submission status: Runtime Error

Solution code:
import sys
def main():
  input = sys.stdin.readline
  h, w = map(int, input().split())
  A = []
  AB = []
  A_append = A.append
  AB_append = AB.append
  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:
        add1 = dp[i][j+1].add
        dif1 = AB[i][j+1]
      if i < h - 1:
        add2 = dp[j+1][i].add
        dif2 = AB[i+1][j]
      for item in dp[i][j]:
        if j < w - 1:
          add1(item+dif1)
          add1(abs(item-dif1))
        if i < h - 1:
          add2(item+dif2)
          add2(abs(item-dif2))
  print(min(dp[h-1][w-1]))
if __name__ == '__main__':
  main()

Submission status: Runtime Error

Solution code:
import sys
def main():
  input = sys.stdin.readline
  h, w = map(int, input().split())
  A = []
  AB = []
  A_append = A.append
  AB_append = AB_append
  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:
        add1 = dp[i][j+1].add
        dif1 = AB[i][j+1]
      if i < h - 1:
        add2 = dp[j+1][i].add
        dif2 = AB[i+1][j]
      for item in dp[i][j]:
        if j < w - 1:
          add1(item+dif1)
          add1(abs(item-dif1))
        if i < h - 1:
          add2(item+dif2)
          add2(abs(item-dif2))
  print(min(dp[h-1][w-1]))
if __name__ == '__main__':
  main()

Submission status: Runtime Error

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][abs(A[0][0] - B[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: Runtime Error

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 key 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: Runtime Error

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].add(abs(A[0][0] - B[0][0]))
  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].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: Runtime Error

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
    for k in range(l):
      if dp[i*(w+1)+j]:
        if (i < h):
          c1 = abs(a[i+1][j]-b[i+1][j])
          dp[(i+1)*(w+1)+j][abs(k-c1)]=True
          dp[(i+1)*(w+1)+j][k+c1]=True
        if (j < w):
          c2 = abs(a[i][j+1]-b[i][j+1])
          dp[i*(w+1)+j+1][abs(k-c1)]=True
          dp[i*(w+1)+j+1][k+c1]=True
for i in range(l):
  if dp[-1][i]:print(i);exit()

Submission status: Runtime Error

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
    for k in range l:
      if dp[i*(w+1)+j]=True:
        if (i < h):
          c1 = abs(a[i+1][j]-b[i+1][j])
          dp[(i+1)*(w+1)+j][abs(k-c1)]=True
          dp[(i+1)*(w+1)+j][k+c1]=True
        if (j < w):
          c2 = abs(a[i][j+1]-b[i][j+1])
          dp[i*(w+1)+j+1][abs(k-c1)]=True
          dp[i*(w+1)+j+1][k+c1]=True
for i in range(l):
  if dp[-1][i]:print(i);exit()

Submission status: Runtime Error

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] * 10001 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(10000):
            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: Runtime Error

Solution code:
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][w]))

Submission status: Runtime Error

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*40+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: Runtime Error

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] * 10001 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(10000):
            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(10000):
    if dp[H][W-1][i] == 1:
        print(i)
        break

Submission status: Runtime Error

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] * 6401 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(6400):
            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(6400):
    if dp[H][W-1][i] == 1:
        print(i)
        break

Submission status: Runtime Error

Solution code:
R, C = map(int, input().split())

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

dp = [0] * R
dp[0] = s = 1 << 6400

for row_a, row_b in zip(A, B):
    for i, (a, b) in enumerate(zip(row_a, row_b)):
        d = abs(a - b)
        if i > 0:
            dp[i] |= dp[i - 1]
        dp[i] = (dp[i] << d) | (dp[i] >> d)

g = dp[-1]
for i in range(6401):
    if g & (s << i) or g & (s >> i):
        print(i)
        quit()

Submission status: Runtime Error

Solution code:
R, C = map(int, input().split())

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

dp = [0] * R
dp[0] = s = 1 << 6400

for row_a, row_b in zip(A, B):
    for i, (a, b) in enumerate(zip(row_a, row_b)):
        d = abs(a - b)
        if i > 0:
            dp[i] |= dp[i - 1]
        dp[i] <<= d
        dp[i] >>= d

g = dp[-1]
for i in range(6401):
    if g & (s << i) or g & (s >> i):
        print(i)
        quit()

Submission status: Runtime Error

Solution code:
r, c = map(int, input().split())

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

dp_table = [[[False] * 6400 for _2 in range(c)] for _1 in range(r)]

dp_table[0][0][abs(A[0][0] - B[0][0])] = True

for i in range(1, r):
    temp = A[i][0] - B[i][0]
    up_list = dp_table[i - 1][0]
    for idx, up in enumerate(up_list):
        if up:
            dp_table[i][0][abs(idx + temp)] = True
            dp_table[i][0][abs(idx - temp)] = True

for j in range(1, c):
    temp = A[0][j] - B[0][j]
    left_list = dp_table[0][j]
    for idx, left in enumerate(left_list):
        if left:
            dp_table[0][j][abs(idx + temp)] = True
            dp_table[0][j][abs(idx - temp)] = True

for i in range(1, r):
    for j in range(1, c):
        temp = A[i][j] - B[i][j]
        up_list = dp_table[i - 1][j]
        for idx, up in enumerate(up_list):
            if up:
                dp_table[i][j][abs(idx + temp)] = True
                dp_table[i][j][abs(idx - temp)] = True
        left_list = dp_table[i][j - 1]
        for idx, left in enumerate(left_list):
            if left:
                dp_table[i][j][abs(idx + temp)] = True
                dp_table[i][j][abs(idx - temp)] = True

print(dp_table[r - 1][c - 1].index(True))

Submission status: Runtime Error

Solution code:
H,W=map(int,input().split())
A=[list(map(int,input().split())) for _ in range(H)]
B=[list(map(int,input().split())) for _ in range(H)]
dp=[[[] for i in range(W)] for j in range(H)]
g=[[0]*W for _ in range(H)]
for i in range(H):
    for j in range(W):
        g[i][j]=abs(A[i][j]-B[i][j])
gmax=max(max(g[i] for i in range(H)))
saidai=(H+W)*gmax
#print(saidai)
dp[0][0].append(abs(A[0][0]-B[0][0]))
for i in range(1,W):
    for j in dp[0][i-1]:
        if j+abs(A[0][i]-B[0][i])<saidai:
            dp[0][i].append(j+abs(A[0][i]-B[0][i]))
        if abs(j-abs(A[0][i]-B[0][i]))<saidai:
            dp[0][i].append(abs(j-abs(A[0][i]-B[0][i])))
for i in range(1,H):
    for j in dp[i-1][0]:
        if j+abs(A[i][0]-B[i][0])<saidai:
            dp[i][0].append(j+abs(A[i][0]-B[i][0]))
        if abs(j-abs(A[i][0]-B[i][0]))<saidai:
            dp[i][0].append(abs(j-abs(A[i][0]-B[i][0])))
for i in range(1,H):
    for j in range(1,W):
        abij=abs(A[i][j]-B[i][j])
        for k in dp[i][j-1]:
            if k+abij<saidai:
                dp[i][j].append(k+abij)
            if abs(k-abij)<saidai:
                dp[i][j].append(abs(k-abij))
        for k in dp[i-1][j]:
            if k+abij<saidai:
                dp[i][j].append(k+abij)
            if abs(k-abij)<saidai:
                dp[i][j].append(abs(k-abij))

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

Submission status: Runtime Error

Solution code:
def main():
    import numpy as np

    H, W = map(int, input().split())
    A = np.array([[int(x) for x in input().split()] for _ in range(H)])
    B = np.array([[int(x) for x in input().split()] for _ in range(H)])
    D = np.abs(A - B)

    MAX_BIAS_PER_CELL = 80
    UPPER_BOUND_TOTAL_BIAS = MAX_BIAS_PER_CELL * (H + W - 1) + 1

    dp = np.zeros(shape=(H, W, UPPER_BOUND_TOTAL_BIAS * 2 + 1), dtype=np.bool_)
    d = D[0][0]
    dp[0, 0, UPPER_BOUND_TOTAL_BIAS - d] = 1
    dp[0, 0, UPPER_BOUND_TOTAL_BIAS + d] = 1

    for r in range(H):
        for c in range(W):
            d = D[r][c]
            if r > 0:
                dp[r, c, d:] |= dp[r - 1, c, :-d]
                dp[r, c, :-d] |= dp[r - 1, c, d:]
            if c > 0:
                dp[r, c, d:] |= dp[r, c - 1, :-d]
                dp[r, c, :-d] |= dp[r, c - 1, d:]

    t = dp[H - 1, W - 1, :]
    indices = np.where(t == 1)[0]  # 一次元
    ret = np.abs(indices - UPPER_BOUND_TOTAL_BIAS)
    print(np.min(ret))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def main():
    import numpy as np

    H, W = map(int, input().split())
    A = np.array([[int(x) for x in input().split()] for _ in range(H)])
    B = np.array([[int(x) for x in input().split()] for _ in range(H)])
    D = np.abs(A - B)

    MAX_BIAS_PER_CELL = 80
    UPPER_BOUND_TOTAL_BIAS = MAX_BIAS_PER_CELL * (H + W - 1) + 1

    dp = np.zeros(shape=(H, W, UPPER_BOUND_TOTAL_BIAS * 2 + 1), dtype=np.uint8)
    d = D[0][0]
    dp[0, 0, UPPER_BOUND_TOTAL_BIAS - d] = 1
    dp[0, 0, UPPER_BOUND_TOTAL_BIAS + d] = 1

    for r in range(H):
        for c in range(W):
            d = D[r][c]
            if r > 0:
                dp[r, c, d:] |= dp[r - 1, c, :-d]
                dp[r, c, :-d] |= dp[r - 1, c, d:]
            if c > 0:
                dp[r, c, d:] |= dp[r, c - 1, :-d]
                dp[r, c, :-d] |= dp[r, c - 1, d:]

    indices = np.where(dp[H - 1, W - 1, :] == 1)
    t = tuple(abs(idx - UPPER_BOUND_TOTAL_BIAS) for idx in indices)
    print(np.min(t))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def main():
    import numpy as np

    H, W = map(int, input().split())
    A = np.array([[int(x) for x in input().split()] for _ in range(H)])
    B = np.array([[int(x) for x in input().split()] for _ in range(H)])
    D = np.abs(A - B)

    MAX_BIAS_PER_CELL = 80
    UPPER_BOUND_TOTAL_BIAS = MAX_BIAS_PER_CELL * (H + W - 1) + 1

    dp = np.zeros(shape=(H, W, UPPER_BOUND_TOTAL_BIAS * 2 + 1), dtype=np.bool_)
    d = D[0][0]
    dp[0, 0, UPPER_BOUND_TOTAL_BIAS - d] = 1
    dp[0, 0, UPPER_BOUND_TOTAL_BIAS + d] = 1

    for r in range(H):
        for c in range(W):
            d = D[r][c]
            if r > 0:
                dp[r, c, d:] |= dp[r - 1, c, :-d]
                dp[r, c, :-d] |= dp[r - 1, c, d:]
            if c > 0:
                dp[r, c, d:] |= dp[r, c - 1, :-d]
                dp[r, c, :-d] |= dp[r, c - 1, d:]

    indices = np.where(dp[H - 1, W - 1, :] == 1)
    t = tuple(abs(idx - UPPER_BOUND_TOTAL_BIAS) for idx in indices)
    print(np.min(t))


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import sys
def LI(): return([int(x) for x in sys.stdin.readline().split()])
def main():
    H,W = LI()
    A = [LI() for _ in range(H)]
    B = [LI() for _ in range(H)]
    D = [[abs(A[h][w]-B[h][w]) for w in range(W)] for h in range(H)]
    dp=[[0]*W for _ in range(H)]
    dp[0][0]=1<<D[0][0]<<20000

    for h in range(H):
        for w in range(W):
            if(h>0):
                dp[h][w] |= dp[h-1][w]<<D[h][w]
                dp[h][w] |= dp[h-1][w]>>D[h][w]
            if(w>0):
                dp[h][w] |= dp[h][w-1]<<D[h][w]
                dp[h][w] |= dp[h][w-1]>>D[h][w]
    for i in range(81):
        bit = 1<<(20000+i)
        bit |= 1<<(20000-i)
        if dp[H-1][W-1]&bit
            return(i)
 
if __name__ == "__main__":
    print(main())

Submission status: Runtime Error

Solution code:
import sys
import time
def input():
    return sys.stdin.readline()[:-1]
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)]
kata=[]
for i in range(h):
    tmp=[]
    for j in range(w):
        tmp.append(abs(a[i][j]-b[i][j]))
    kata.append(tmp)
# print(kata)
tmp=0
for i in range(h):
    tmp=max(tmp,max(kata[i]))
# print(tmp)

dp=[[[0 for i in range(tmp*(h+w)+1)] for j in range(w+1)] for k in range(h+1)]
dp[0][0][kata[0][0]]+=1
i=0
j=0
# print(dp)
while i+j!=h+w-2:
    # print(i,j)
    for k in range(tmp*(h+w)+1):
        if dp[i][j][k]!=0:
            if i+1!=h:
                dp[i+1][j][k+kata[i+1][j]]+=1
                dp[i+1][j][abs(k-kata[i+1][j])]+=1
            if j+1!=w:
                dp[i][j+1][k+kata[i][j+1]]+=1
                dp[i][j+1][abs(k-kata[i][j+1])]+=1
    i-=1
    j+=1
    if i<0:
        i=abs(i+j)+1
        j=0
    while i>=h:
        i-=1
        j+=1
    # time.sleep(1)
# print(dp[1][0])
# print(dp[0][1])
# print(dp[h-1][w-1])
for i in range(tmp*(h+w)+1):
    if dp[h-1][w-1][i]!=0:
        print(i)
        quit()