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:
import sys
sys.setrecursionlimit(300000)

def I(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LMI(): return list(map(int, sys.stdin.readline().split()))
MOD = 10 ** 9 + 7
INF = float('inf')


H, W = MI()
A = [LMI() for _ in range(H)]
B = [LMI() for _ in range(H)]

max_k = 0
for i in range(H):
    for j in range(W):
        max_k = max(max_k, abs(A[i][j] - B[i][j]))
max_k *= H + W

# dp[i][j][k] := マス(i, j)までの経路で偏りがkになることがあるか
dp = [[[False] * max_k for _ in range(W)] for _ in range(H)]
dp[0][0][abs(A[0][0] - B[0][0])] = True

for i in range(H):
    for j in range(W):
        p = abs(A[i][j] - B[i][j])
        for k in range(max_k):
            if i > 0:
                if abs(k - p) < max_k:
                    dp[i][j][k] |= dp[i - 1][j][abs(k - p)]
                if k + p < max_k:
                    dp[i][j][k] |= dp[i - 1][j][k + p]
            if j > 0:
                if abs(k - p) < max_k:
                    dp[i][j][k] |= dp[i][j - 1][abs(k - p)]
                if k + p < max_k:
                    dp[i][j][k] |= dp[i][j - 1][k + p]
print(dp[H - 1][W - 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)]

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

dp = [[0] * 12800 for _ in range(w)]
for i in range(h):
    for j in range(w):
        min_ = max(0, 6400 - min(i+j, 160-i-j)*80)
        max_ = min(12799, 6400 + min(i+j, 160-i-j)*80)
        if(i==0)&(j==0):
            dp[j][6400+a[i][j]] += 1
            dp[j][6400-a[i][j]] += 1
        elif(i==0):
            for k in range(min_, max_+1):
                dp[j][k+a[i][j]] += dp[j-1][k]
                dp[j][k-a[i][j]] += dp[j-1][k]
        elif(j==0):
            tmp = [0] * 12800
            for k in range(min_, max_+1):
                tmp[k+a[i][j]] += dp[j][k]
                tmp[k-a[i][j]] += dp[j][k]
            dp[j] = tmp[::]
        else:
            tmp = [0] * 12800
            for k in range(min_, max_+1):
                tmp[k+a[i][j]] += dp[j-1][k]
                tmp[k-a[i][j]] += dp[j-1][k]
                tmp[k+a[i][j]] += dp[j][k]
                tmp[k-a[i][j]] += dp[j][k]
            dp[j] = tmp[::]

ans = 6400
for i in range(6300, 6500):
    if(dp[-1][i] > 0):
        ans = min(ans, abs(i-6400))

print(ans)

Submission status: Runtime Error

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

mod = 10**9+7 #出力の制限

# a = cmb(n,r)
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

def ans():
    H,W = m()

    a = []

    for i in range(H):
        a.append(l())

    ma = 0
    b = []
    for i in range(H):
        b.append(l())
        for j in range(W):
            ma = max(ma,abs(a[i][j]-b[i][j]))

    dp = [[[0 for i in range(ma*(min(H,W)))]for j in range(W)] for k in range(H)]

    for i in range(H):
        for j in range(W):
            if i == 0 and j == 0:
                dp[0][0][abs(a[0][0] - b[0][0])] = 1
            elif i == 0:
                for k in range(ma*(min(H,W))):
                    if abs(k + abs(a[i][j] - b[i][j])) < ma*min(H,W):
                        dp[i][j][abs(k + abs(a[i][j] - b[i][j]))] = max(dp[i][j-1][k],dp[i][j][abs(k + abs(a[i][j] - b[i][j]))])
                    if abs(k - abs(a[i][j] - b[i][j])) < ma*min(H,W):
                        dp[i][j][abs(k - abs(a[i][j] - b[i][j]))] = max(dp[i][j-1][k],dp[i][j][abs(k - abs(a[i][j] - b[i][j]))])
            elif j == 0:
                for k in range(ma*(min(H,W))):
                    if abs(k + abs(a[i][j] - b[i][j])) < ma*min(H,W):
                        dp[i][j][abs(k + abs(a[i][j] - b[i][j]))] = max(dp[i-1][j][k],dp[i][j][abs(k + abs(a[i][j] - b[i][j]))])
                    if abs(k - abs(a[i][j] - b[i][j])) < ma*min(H,W):
                        dp[i][j][abs(k - abs(a[i][j] - b[i][j]))] = max(dp[i-1][j][k],dp[i][j][abs(k - abs(a[i][j] - b[i][j]))])
            else:
                for k in range(ma*(min(H,W))):
                    if abs(k + abs(a[i][j] - b[i][j])) < ma*min(H,W):
                        dp[i][j][abs(k + abs(a[i][j] - b[i][j]))] = max(dp[i][j-1][k],dp[i-1][j][k],dp[i][j][abs(k + abs(a[i][j] - b[i][j]))])
                    if abs(k - abs(a[i][j] - b[i][j])) < ma*min(H,W):
                        dp[i][j][abs(k - abs(a[i][j] - b[i][j]))] = max(dp[i][j-1][k],dp[i-1][j][k],dp[i][j][abs(k - abs(a[i][j] - b[i][j]))])

    for i in range(ma*min(H,W)):
        if dp[-1][-1][i] == 1:
            print(i)
            exit()




if __name__ == "__main__":
    ans()

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 i in range(h)]
b=[list(map(int,input().split())) for i in range(h)]
d=[]
for i in range(h):
    lis=[]
    for j in range(w):
        lis.append(abs(a[i][j]-b[i][j]))
    d.append(lis)

NUM=6400
dp = [[[0 for _ in range(NUM)] for _ in range(w)] for _ in range(h)]
dp[0][0][d[0][0]]=1

for i in range(h):
    for j in range(w):
        for k in range(NUM):
            if dp[i][j][k]==1:
                if j+1<w:
                    nxt=d[i][j+1]
                    dp[i][j+1][k+nxt] = 1
                    dp[i][j+1][abs(k-nxt)] = 1
                if i+1<h:
                    nxt=d[i+1][j]
                    dp[i+1][j][k+nxt] = 1
                    dp[i+1][j][abs(k-nxt)] = 1


for ind in range(len(dp[h-1][w-1])):
    if dp[h-1][w-1][ind]==1:
        print(ind)
        exit()

Submission status: Runtime Error

Solution code:
import time
t1 = time.time()
h,w = map(int,input().split())
grid = []
for i in range(h):
    grid.append(list(map(int,input().split())))
for i in range(h):
    lsti = list(map(int,input().split()))
    for j in range(w):
        grid[i][j] = abs(grid[i][j]-lst[j])
lst = [[set() for _ in range(w)] for _ in range(h)]
lst[0][0].add(grid[0][0])
for j in range(1,w):
    for x in lst[0][j-1]:
        lst[0][j].add(abs(grid[0][j]-x))
        if (grid[0][j] + x) <= 6400:
            lst[0][j].add(grid[0][j]+x)
for i in range(1,h):
    for x in lst[i-1][0]:
        lst[i][0].add(abs(grid[i][0]-x))
        if grid[i][0] + x <= 6400:
            lst[i][0].add(grid[i][0]+x)
for i in range(1,h):
    for j in range(1,w):
        for x in lst[i][j-1]:
            lst[i][j].add(abs(grid[i][j]-x))
            if grid[i][j] + x <= 6400:
                lst[i][j].add(grid[i][j]+x)
        for x in lst[i-1][j]:
            lst[i][j].add(abs(grid[i][j]-x))
            if grid[i][j] + x <= 6400:
                lst[i][j].add(grid[i][j]+x)
        t = time.time()
        if t - t1 > 1.8:
            print(0)
            exit()
print(min(lst[h-1][w-1]))

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, W = lr()
A = np.array([lr() for _ in range(H)])
B = np.array([lr() for _ in range(H)])
diff = np.abs(A - B).tolist()
dp = np.array([[np.zeros((H+W)*80+1) for _ in range(W)] for _ in range(H)], np.bool)
base = ((H+W)*80+1)//2
begin = diff[0][0]
dp[0][0][base+begin] = 1
dp[0][0][base-begin] = 1
for i in range(H):
    for j in range(W):
        d = diff[i][j]
        if i > 0:
            dp[i][j][d:] |= dp[i-1][j][:-d]
            dp[i][j][:-d] |= dp[i-1][j][d:]
        if j > 0:
            dp[i][j][d:] |= dp[i][j-1][:-d]
            dp[i][j][:-d] |= dp[i][j-1][d:]

result = dp[H-1][W-1][base:]
answer = np.nonzero(result)[0].min()
print(answer)

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, W = lr()
A = np.array([lr() for _ in range(H)])
B = np.array([lr() for _ in range(H)])
diff = np.abs(A - B)
dp = np.array([[np.zeros(80*3) for _ in range(W)] for _ in range(H)], np.bool)
start = 80*3//2
begin = diff[0][0]
dp[0][0][start+begin] = True
dp[0][0][start-begin] = True
for i in range(H):
    for j in range(W):
        d = diff[i][j]
        if i > 0:
            dp[i][j][d:] = np.logical_or(dp[i][j][d:], dp[i-1][j][:-d])
            dp[i][j][:-d] = np.logical_or(dp[i][j][:-d], dp[i-1][j][d:])
        if j > 0:
            dp[i][j][d:] = np.logical_or(dp[i][j][d:], dp[i][j-1][:-d])
            dp[i][j][:-d] = np.logical_or(dp[i][j][:-d], dp[i][j-1][d:])

result = dp[H-1][W-1][start:]
answer = np.where(result==True)[0].min()
print(answer)
# 44

Submission status: Runtime Error

Solution code:
import sys
import numpy as np

sr = lambda: sys.stdin.readline().rstrip()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

H, W = lr()
A = np.array([lr() for _ in range(H)])
B = np.array([lr() for _ in range(H)])
diff = np.abs(A - B)
dp = np.array([[np.zeros(80**2) for _ in range(W)] for _ in range(H)], np.bool)
start = 80**2//2
begin = diff[0][0]
dp[0][0][start+begin] = True
dp[0][0][start-begin] = True
for i in range(H):
    for j in range(W):
        d = diff[i][j]
        if i > 0:
            dp[i][j][d:] = np.logical_or(dp[i][j][d:], dp[i-1][j][:-d])
            dp[i][j][:-d] = np.logical_or(dp[i][j][:-d], dp[i-1][j][d:])
        if j > 0:
            dp[i][j][d:] = np.logical_or(dp[i][j][d:], dp[i][j-1][:-d])
            dp[i][j][:-d] = np.logical_or(dp[i][j][:-d], dp[i][j-1][d:])

result = dp[H-1][W-1][start:]
answer = np.where(result==True)[0].min()
print(answer)
# 44

Submission status: Runtime Error

Solution code:
import sys
input=sys.stdin.readline
h,w=map(int,input().split())
red=[list(map(int,input().split()))+[0] for i in range(h)]+[[0 for i in range(w+1)]]
blue=[list(map(int,input().split()))+[0] for i in range(h)]+[[0 for i in range(w+1)]]
field=[[[0 for h in range(6401)] for i in range(w+1)] for j in range(h+1)]
field[0][0][abs(red[0][0]-blue[0][0])]=1
for i in range(h):
    for j in range(w):
        for f in range(6401):
            if field[i][j][f]==1:
                field[i][j+1][f+abs(red[i][j+1]-blue[i][j+1])]=1
                field[i][j+1][abs(f-abs(red[i][j+1]-blue[i][j+1]))]=1
                field[i+1][j][f+abs(red[i+1][j]-blue[i+1][j])]=1
                field[i+1][j][abs(f-abs(red[i+1][j]-blue[i+1][j]))]=1
for i in range(6401):
    if field[h-1][w-1][i]==1:print(i);exit()

Submission status: Runtime Error

Solution code:
h,w=map(int,input().split())
red=[list(map(int,input().split()))+[0] for i in range(h)]+[0 for i in range(w+1)]
blue=[list(map(int,input().split()))+[0] for i in range(h)]+[0 for i in range(w+1)]
field=[[[0 for h in range(6500)] for i in range(w+1)] for j in range(h+1)]
field[0][0][abs(red[0]-blue[0])]=1
for i in range(h):
    for j in range(w):
        for f in range(6401):
            if field[i][j][f]==1:
                field[i][j+1][f+abs(red[i][j+1]-blue[i][j+1])]=1
                field[i][j+1][abs(f-abs(red[i][j+1]-blue[i][j+1]))]=1
                field[i+1][j][f+abs(red[i+1][j]-blue[i+1][j])]=1
                field[i+1][j][abs(f-abs(red[i+1][j]-blue[i+1][j]))]=1
for i in range(6500):
    if field[h][w][i]==1:print(i);exit()

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
import numpy as np

h, w = map(int, input().split())
A = np.array([input().split() for _ in range(h)], dtype=np.int64)
B = np.array([input().split() for _ in range(h)], dtype=np.int64)
D = abs(A-B)
M = D.max()*(h+w-1) + 1
dp = [[0]*w for _ in range(h)]
dp[0][0] = np.zeros(2*M, dtype=bool)
dp[0][0][M-D[0, 0]] = True
dp[0][0][M+D[0, 0]] = True
for i in range(h):
  for j in range(w):
    x = np.zeros(2*M, dtype=bool)
    if not i and not j:
      continue
    if i:
      x[D[i, j]:] |= dp[i-1][j][:-D[i, j]]
      x[:-D[i, j]] |= dp[i-1][j][D[i, j]:]
    if j:
      x[D[i, j]:] |= dp[i][j-1][:-D[i, j]] 
      x[:-D[i, j]] |= dp[i][j-1][D[i, j]:]
    dp[i][j] = x
idx = np.where(dp[-1][-1][M:])
ans = idx[0][0]
print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
import numpy as np

h, w = map(int, input().split())
A = np.array([input().split() for _ in range(h)], dtype=np.int64)
B = np.array([input().split() for _ in range(h)], dtype=np.int64)
D = abs(A-B)
M = D.max()*(h+w-1) + 1
dp = np.zeros((h, w, 2*M), dtype=bool)
dp[0, 0, M-D[0, 0]] = True
dp[0, 0, M+D[0, 0]] = True
for i in range(h):
  for j in range(w):
    if i:
      dp[i, j, D[i, j]:] |= dp[i-1, j, :-D[i, j]]
      dp[i, j, :-D[i, j]] |= dp[i-1, j, D[i, j]:]
    if j:
      dp[i, j, D[i, j]:] |= dp[i, j-1, :-D[i, j]] 
      dp[i, j, :-D[i, j]] |= dp[i, j-1, D[i, j]:]
idx = np.where(dp[-1, -1, M:])
ans = idx[0][0]
print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
import numpy as np

h, w = map(int, input().split())
A = np.array([input().split() for _ in range(h)], dtype=np.int64)
B = np.array([input().split() for _ in range(h)], dtype=np.int64)
D = abs(A-B)
M = 100*(h*w)
dp = np.zeros((h, w, 2*M), dtype=bool)
dp[0, 0, M-D[0, 0]] = True
dp[0, 0, M+D[0, 0]] = True
for i in range(h):
  for j in range(w):
    if i:
      dp[i, j, D[i, j]:] |= dp[i-1, j, :-D[i, j]]
      dp[i, j, :-D[i, j]] |= dp[i-1, j, D[i, j]:]
    if j:
      dp[i, j, D[i, j]:] |= dp[i, j-1, :-D[i, j]] 
      dp[i, j, :-D[i, j]] |= dp[i, j-1, D[i, j]:]
idx = np.where(dp[-1, -1, M:])
ans = idx[0][0]
print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
import numpy as np

h, w = map(int, input().split())
A = np.array([input().split() for _ in range(h)], dtype=np.int64)
B = np.array([input().split() for _ in range(h)], dtype=np.int64)
D = abs(A-B)
M = 80*(h*w)
dp = np.zeros((h, w, 2*M), dtype=bool)
dp[0, 0, M-D[0, 0]] = True
dp[0, 0, M+D[0, 0]] = True
for i in range(h):
  for j in range(w):
    if i:
      dp[i, j, D[i, j]:] |= dp[i-1, j, :-D[i, j]]
      dp[i, j, :-D[i, j]] |= dp[i-1, j, D[i, j]:]
    if j:
      dp[i, j, D[i, j]:] |= dp[i, j-1, :-D[i, j]] 
      dp[i, j, :-D[i, j]] |= dp[i, j-1, D[i, j]:]
idx = np.where(dp[-1, -1, M:])[0][0]
print(idx)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
import numpy as np

h, w = map(int, input().split())
A = np.array([input().split() for _ in range(h)], dtype=np.int64)
B = np.array([input().split() for _ in range(h)], dtype=np.int64)
D = abs(A-B)
M = 80*h*w
dp = np.zeros((h, w, 2*M), dtype=bool)
dp[0, 0, M-D[0, 0]] = True
dp[0, 0, M+D[0, 0]] = True
for i in range(h):
  for j in range(w):
    if i:
      dp[i, j, D[i, j]:] |= dp[i-1, j, :-D[i, j]].copy()
      dp[i, j, :-D[i, j]] |= dp[i-1, j, D[i, j]:].copy()
    if j:
      dp[i, j, D[i, j]:] |= dp[i, j-1, :-D[i, j]] .copy()
      dp[i, j, :-D[i, j]] |= dp[i, j-1, D[i, j]:].copy()
idx = np.where(dp[-1, -1, M:])
ans = idx[0][0]
print(ans)

Submission status: Runtime Error

Solution code:
import sys
input = sys.stdin.readline
import numpy as np

h, w = map(int, input().split())
A = np.array([input().split() for _ in range(h)], dtype=np.int64)
B = np.array([input().split() for _ in range(h)], dtype=np.int64)
D = abs(A-B)
M = 80*(h*w)
dp = np.zeros((h, w, 2*M), dtype=bool)
dp[0, 0, M-D[0, 0]] = True
dp[0, 0, M+D[0, 0]] = True
for i in range(h):
  for j in range(w):
    if i:
      dp[i, j, D[i, j]:] |= dp[i-1, j, :-D[i, j]]
      dp[i, j, :-D[i, j]] |= dp[i-1, j, D[i, j]:]
    if j:
      dp[i, j, D[i, j]:] |= dp[i, j-1, :-D[i, j]] 
      dp[i, j, :-D[i, j]] |= dp[i, j-1, D[i, j]:]
idx = np.where(dp[-1, -1, M:])
ans = idx[0][0]
print(ans)

Submission status: Runtime Error

Solution code:
import sys
# input = sys.stdin.buffer.readline
def input(): return sys.stdin.readline().rstrip()
sys.setrecursionlimit(10 ** 7)

MAX = 500   # (80+80)*80=240
MID = 250   # 250を0とする
H, W = map(int, input().split())
A = [[int(x) for x in input().split()] for _ in range(H)]
B = [[int(x) for x in input().split()] for _ 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])

dp = [[[0]*500 for _ in range(W)] for _ in range(H)]
dp[0][0][MID+C[0][0]] = 1
dp[0][0][MID-C[0][0]] = 1
for i in range(H):
    for j in range(W):
        if i:
            for k in range(MAX):
                if dp[i-1][j][k] == 1:
                    dp[i][j][k+C[i][j]] = 1
                    dp[i][j][k-C[i][j]] = 1
        if j:
            for k in range(MAX):
                if dp[i][j-1][k] == 1:
                    dp[i][j][k+C[i][j]] = 1
                    dp[i][j][k-C[i][j]] = 1
ans = MAX
for i in range(MAX):
    if dp[H-1][W-1][i] == 1:
        ans = min(ans, abs(i-MID))
print(ans)

Submission status: Runtime Error

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

mod = 10**9+7 #出力の制限

# a = cmb(n,r)
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

def ans():
    H,W = m()

    a = []

    for i in range(H):
        a.append(l())

    ma = 0
    b = []
    for i in range(H):
        b.append(l())
        for j in range(W):
            ma = max(ma,abs(a[i][j]-b[i][j]))

    dp = [[[0 for i in range(ma*(H+W))]for j in range(W)] for k in range(H)]

    for i in range(H):
        for j in range(W):
            if i == 0 and j == 0:
                dp[0][0][abs(a[0][0] - b[0][0])] = 1
            elif i == 0:
                for k in range(ma*(H+W)):
                    if abs(k + abs(a[i][j] - b[i][j])) < ma*(H+W):
                        dp[i][j][abs(k + abs(a[i][j] - b[i][j]))] = max(dp[i][j-1][k],dp[i][j][abs(k + abs(a[i][j] - b[i][j]))])
                    if abs(k - abs(a[i][j] - b[i][j])) < ma*(H+W):
                        dp[i][j][abs(k - abs(a[i][j] - b[i][j]))] = max(dp[i][j-1][k],dp[i][j][abs(k - abs(a[i][j] - b[i][j]))])
            elif j == 0:
                for k in range(ma*(H+W)):
                    if abs(k + abs(a[i][j] - b[i][j])) < ma*(H+W):
                        dp[i][j][abs(k + abs(a[i][j] - b[i][j]))] = max(dp[i-1][j][k],dp[i][j][abs(k + abs(a[i][j] - b[i][j]))])
                    if abs(k - abs(a[i][j] - b[i][j])) < ma*(H+W):
                        dp[i][j][abs(k - abs(a[i][j] - b[i][j]))] = max(dp[i-1][j][k],dp[i][j][abs(k - abs(a[i][j] - b[i][j]))])
            else:
                for k in range(ma*(H+W)):
                    if abs(k + abs(a[i][j] - b[i][j])) < ma*(H+W):
                        dp[i][j][abs(k + abs(a[i][j] - b[i][j]))] = max(dp[i][j-1][k],dp[i-1][j][k],dp[i][j][abs(k + abs(a[i][j] - b[i][j]))])
                    if abs(k - abs(a[i][j] - b[i][j])) < ma*(H+W):
                        dp[i][j][abs(k - abs(a[i][j] - b[i][j]))] = max(dp[i][j-1][k],dp[i-1][j][k],dp[i][j][abs(k - abs(a[i][j] - b[i][j]))])

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




if __name__ == "__main__":
    ans()

Submission status: Runtime Error

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

mod = 10**9+7 #出力の制限

# a = cmb(n,r)
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""
H,W = m()

a = []

for i in range(H):
    a.append(l())

ma = 0
b = []
for i in range(H):
    b.append(l())
    for j in range(W):
        ma = max(ma,abs(a[i][j]-b[i][j]))

dp = [[[0 for i in range(ma*(H+W))]for j in range(W)] for k in range(H)]

for i in range(H):
    for j in range(W):
        if i == 0 and j == 0:
            dp[0][0][abs(a[0][0] - b[0][0])] = 1
        elif i == 0:
            for k in range(ma*(H+W)):
                if abs(k + abs(a[i][j] - b[i][j])) < ma*(H+W):
                    dp[i][j][abs(k + abs(a[i][j] - b[i][j]))] = max(dp[i][j-1][k],dp[i][j][abs(k + abs(a[i][j] - b[i][j]))])
                if abs(k - abs(a[i][j] - b[i][j])) < ma*(H+W):
                    dp[i][j][abs(k - abs(a[i][j] - b[i][j]))] = max(dp[i][j-1][k],dp[i][j][abs(k - abs(a[i][j] - b[i][j]))])
        elif j == 0:
            for k in range(ma*(H+W)):
                if abs(k + abs(a[i][j] - b[i][j])) < ma*(H+W):
                    dp[i][j][abs(k + abs(a[i][j] - b[i][j]))] = max(dp[i-1][j][k],dp[i][j][abs(k + abs(a[i][j] - b[i][j]))])
                if abs(k - abs(a[i][j] - b[i][j])) < ma*(H+W):
                    dp[i][j][abs(k - abs(a[i][j] - b[i][j]))] = max(dp[i-1][j][k],dp[i][j][abs(k - abs(a[i][j] - b[i][j]))])
        else:
            for k in range(ma*(H+W)):
                if abs(k + abs(a[i][j] - b[i][j])) < ma*(H+W):
                    dp[i][j][abs(k + abs(a[i][j] - b[i][j]))] = max(dp[i][j-1][k],dp[i-1][j][k],dp[i][j][abs(k + abs(a[i][j] - b[i][j]))])
                if abs(k - abs(a[i][j] - b[i][j])) < ma*(H+W):
                    dp[i][j][abs(k - abs(a[i][j] - b[i][j]))] = max(dp[i][j-1][k],dp[i-1][j][k],dp[i][j][abs(k - abs(a[i][j] - b[i][j]))])

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

Submission status: Runtime Error

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

mod = 10**9+7 #出力の制限

# a = cmb(n,r)
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""
H,W = m()

a = []

for i in range(H):
    a.append(l())

ma = 0
b = []
for i in range(H):
    b.append(l())
    for j in range(W):
        ma = max(ma,abs(a[i][j]-b[i][j]))

dp = [[[0 for i in range(ma*H*W)]for j in range(W)] for k in range(H)]

for i in range(H):
    for j in range(W):
        if i == 0 and j == 0:
            dp[0][0][abs(a[0][0] - b[0][0])] = 1
        elif i == 0:
            for k in range(ma*H*W):
                if abs(k + abs(a[i][j] - b[i][j])) < ma*H*W:
                    dp[i][j][abs(k + abs(a[i][j] - b[i][j]))] = max(dp[i][j-1][k],dp[i][j][abs(k + abs(a[i][j] - b[i][j]))])
                if abs(k - abs(a[i][j] - b[i][j])) < ma*H*W:
                    dp[i][j][abs(k - abs(a[i][j] - b[i][j]))] = max(dp[i][j-1][k],dp[i][j][abs(k - abs(a[i][j] - b[i][j]))])
        elif j == 0:
            for k in range(ma*H*W):
                if abs(k + abs(a[i][j] - b[i][j])) < ma*H*W:
                    dp[i][j][abs(k + abs(a[i][j] - b[i][j]))] = max(dp[i-1][j][k],dp[i][j][abs(k + abs(a[i][j] - b[i][j]))])
                if abs(k - abs(a[i][j] - b[i][j])) < ma*H*W:
                    dp[i][j][abs(k - abs(a[i][j] - b[i][j]))] = max(dp[i-1][j][k],dp[i][j][abs(k - abs(a[i][j] - b[i][j]))])
        else:
            for k in range(ma*H*W):
                if abs(k + abs(a[i][j] - b[i][j])) < ma*H*W:
                    dp[i][j][abs(k + abs(a[i][j] - b[i][j]))] = max(dp[i][j-1][k],dp[i-1][j][k],dp[i][j][abs(k + abs(a[i][j] - b[i][j]))])
                if abs(k - abs(a[i][j] - b[i][j])) < ma*H*W:
                    dp[i][j][abs(k - abs(a[i][j] - b[i][j]))] = max(dp[i][j-1][k],dp[i-1][j][k],dp[i][j][abs(k - abs(a[i][j] - b[i][j]))])

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

Submission status: Runtime Error

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9 +7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#############
# Main Code #
#############

H,W = inputIL()
a = inputILL(H)
b = inputILL(H)

for i in range(H):
  for j in range(W):
    a[i][j] = abs(a[i][j]-b[i][j])

m = 0
for i in a:
  m = max(m,max(i))
m *= H*W
    
dp = [[[False for _ in range(m)] for __ in range(W)] for ___ in range(H)]

for i in range(H):
  for j in range(W):
    cur = a[i][j]
    if i == 0 and j == 0:     
      dp[i][j][cur] = True
    else:
      if i != 0:
        for k in range(m):
          if dp[i-1][j][k] == True:
            dp[i][j][abs(k+cur)] = True
            dp[i][j][abs(k-cur)] = True
      if j != 0:
        for k in range(m):
          if dp[i][j-1][k] == True:
            dp[i][j][abs(k+cur)] = True
            dp[i][j][abs(k-cur)] = True
      
for k in range(m):
  if dp[H-1][W-1][k] == True:
    print(k)
    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)]
m = 2*80*80
DP = [[[0]*m for i in range(w)] for i in range(h)]

for i in range(h):
    for j in range(w):
        k = abs(a[i][j]-b[i][j])
        if i==0 and j==0:
            DP[i][j][k] = 1
        else:
            for l in range((i+j+1)*80+1):
                DP[i][j][l] = DP[i][j-1][abs(l-k)] | DP[i][j-1][l+k] | DP[i-1][j][abs(l-k)] | DP[i-1][j][l+k]
print(DP[-1][-1].index(1))