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: Accepted

Solution code:
def chk(a):
    L = []
    for i in range(13000):
        if a >> i & 1:
            L.append(i)
    return L
H, W = map(int, input().split())
A = [[int(a) for a in input().split()] for _ in range(H)]
B = [[int(a) for a in input().split()] for _ in range(H)]
C = [[A[i][j] - B[i][j] for j in range(W)] for i in range(H)]
m = (1 << 12800) - 1
X = [[0] * (W) for _ in range(H)]
X[0][0] = (1 << 80 + C[0][0]) | (1 << 80 - C[0][0])
for i in range(H):
    for j in range(W):
        if i:
            X[i][j] |= (X[i-1][j] << 80 + C[i][j]) | (X[i-1][j] << 80 - C[i][j])
        if j:
            X[i][j] |= (X[i][j-1] << 80 + C[i][j]) | (X[i][j-1] << 80 - C[i][j])
        X[i][j] &= m
s = bin(X[-1][-1] + (1<<13000))
mi = 1<<100
for i in range(len(s)):
    if s[-i-1] == "1":
        mi = min(mi, abs(i-(H+W-1)*80))
print(mi)

Submission status: Accepted

Solution code:
n, m = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(n)]
b = [list(map(int, input().split())) for _ in range(n)]
dp = [[0 for _ in range(m)] for _ in range(n)]
dp[0][0] = (1 << (a[0][0] - b[0][0] + 12880)) | 1 << (b[0][0] - a[0][0] + 12880)
mxs = (1 << (12880 * 2 + 1)) - 1
for i in range(n):
	for j in range(m):
		if not i and not j:
			continue
		d = abs(a[i][j] - b[i][j])
		if i:
			u = (dp[i - 1][j] << d) & mxs
			v = (dp[i - 1][j] >> d)
			dp[i][j] |= u
			dp[i][j] |= v
		if j:
			u = (dp[i][j - 1] << d) & mxs
			v = (dp[i][j - 1] >> d) & mxs
			dp[i][j] |= u
			dp[i][j] |= v

c = dp[n - 1][m - 1] >> 12880
for i in range(12880):
	if c & 1:
		print(i)
		break
	c >>= 1

Submission status: Accepted

Solution code:
def main():
    h, w = map(int, raw_input().split())
    aa = [map(int, raw_input().split()) for _ in xrange(h)]
    bb = [map(int, raw_input().split()) for _ in xrange(h)]
    dp = [{aa[0][0] - bb[0][0]}]
    for x in xrange(1, w):
        a = aa[0][x] - bb[0][x]
        ab = set()
        for c in dp[-1]:
            ab.add(abs(c + a))
            ab.add(abs(c - a))
        dp.append(ab)
    dp = [dp]
    for y in xrange(1, h):
        a = aa[y][0] - bb[y][0]
        ab = set()
        for c in dp[y - 1][0]:
            ab.add(abs(c + a))
            ab.add(abs(c - a))
        dp.append([ab])
        for x in xrange(1, w):
            a = aa[y][x] - bb[y][x]
            ab = set()
            for c in dp[y - 1][x]:
                ab.add(abs(c + a))
                ab.add(abs(c - a))
            for c in dp[y][x - 1]:
                ab.add(abs(c + a))
                ab.add(abs(c - a))
            dp[-1].append(ab)
    print min(abs(c) for c in dp[-1][-1])


main()

Submission status: Accepted

Solution code:

# coding: utf-8
# Your code here!
import sys
read = sys.stdin.read
readline = sys.stdin.readline 
readlines = sys.stdin.readlines

#n = int(input())
h,w = [int(i) for i in readline().split()]

a = [[int(i) for i in readline().split()] for _ in range(h)]
b = [[int(i) for i in readline().split()] for _ in range(h)]


dp = [[0] for _ in range(w)]
dp[0][0] = 1

#print(dp)
N = 6401
for i in range(h):
    ndp = [[0]*N for _ in range(w)]
    for j in range(w):
        v = abs(a[i][j] - b[i][j])
        #print(v)
        for ii,c in enumerate(dp[j]):
            if ii+v < N:
                ndp[j][abs(ii+v)] |= c
            ndp[j][abs(ii-v)] |= c
        if j:
            for ii,c in enumerate(ndp[j-1]):
                if ii+v < N:
                    ndp[j][abs(ii+v)] |= c
                ndp[j][abs(ii-v)] |= c

    dp = ndp
    #print(dp)
    
c = [i for i,c in enumerate(dp[j]) if c]

#print(c)
print(min(c))

Submission status: Accepted

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


def reverse(d, x):
    res = 0
    for i in range(x+1):
        res <<= 1
        if (1<<i) & d:
            res |= 1
    return res

H, W = map(int, readline().split())

A = [list(map(int, readline().split())) for _ in range(H)]
B = [list(map(int, readline().split())) for _ 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])

dp = [[0]*W for _ in range(H)]
dp[0][0] = 1<<G[0][0]
for s in range(H+W-1):
    for h in range(max(0, s-(W-1)), 1+min(s, H-1)):
        w = s - h
        d = dp[h][w]
        if w != W-1:
            g = G[h][w+1]
            dp[h][w+1] |= d<<g | d>>g | reverse(d, g)
        if h != H-1:
            g = G[h+1][w]
            dp[h+1][w] |= d<<g | d>>g | reverse(d, g)
ans = dp[H-1][W-1]
ans = (-ans&ans).bit_length() - 1
print(ans)

Submission status: Runtime Error

Solution code:
H,W=map(int,input().split())
A=[[int(i) for i in input().split()] for j in range(H)]
B=[[int(i) for i in input().split()] for j in range(H)]
dp=[[0 for j in range(W)] for i in range(H)]
M=161*161
dp[0][0]|=1<<(A[0][0]-B[0][0]+M)
dp[0][0]|=1<<(-A[0][0]+B[0][0]+M)

for i in range(H):
    for j in range(W):
        dij=abs(A[i][j]-B[i][j])
        if i>0:
            dp[i][j]|=dp[i-1][j]<<dij
            dp[i][j]|=dp[i-1][j]>>dij
        if j>0:
            dp[i][j]|=dp[i][j-1]>>dij
            dp[i][j]|=dp[i][j-1]<<dij
ans=[]
for i in range(M):
    if (1<<i)&dp[-1][-1]!=0:
        ans.append(abs(i-M))
print(min(ans))

Submission status: Runtime Error

Solution code:
H,W=map(int,input().split())
A=[[int(i) for i in input().split()] for j in range(H)]
B=[[int(i) for i in input().split()] for j in range(H)]
dp=[[0 for j in range(W)] for i in range(H)]
M=80*80*2+1
dp[0][0]|=1<<(A[0][0]-B[0][0]+M)
dp[0][0]|=1<<(-A[0][0]+B[0][0]+M)

for i in range(H):
    for j in range(W):
        dij=abs(A[i][j]-B[i][j])
        if i>0:
            dp[i][j]|=dp[i-1][j]<<dij
            dp[i][j]|=dp[i-1][j]>>dij
        if j>0:
            dp[i][j]|=dp[i][j-1]>>dij
            dp[i][j]|=dp[i][j-1]<<dij
ans=[]
for i in range(M):
    if (1<<i)&dp[-1][-1]!=0:
        ans.append(abs(i-M))
print(min(ans))

Submission status: Runtime Error

Solution code:
H,W=map(int,input().split())
A=[[int(i) for i in input().split()] for j in range(H)]
B=[[int(i) for i in input().split()] for j in range(H)]
dp=[[0 for j in range(W)] for i in range(H)]
M=80*80*2+1
dp[0][0]=1<<(abs(A[0][0]-B[0][0])+M)

for i in range(H):
    for j in range(W):
        dij=abs(A[i][j]-B[i][j])
        if i>0:
            dp[i][j]|=dp[i-1][j]<<dij
            dp[i][j]|=dp[i-1][j]>>dij
        if j>0:
            dp[i][j]|=dp[i][j-1]>>dij
            dp[i][j]|=dp[i][j-1]<<dij
ans=[]
for i in range(M):
    if (1<<i)&dp[-1][-1]!=0:
        ans.append(abs(i-M))
print(min(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)]
diffs = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]
max_diff = 0
for diff in diffs:
    max_diff= max(max_diff, max(diff))
r_k = max_diff*(H+W)
dp = [[[False] * r_k for _ in range(W)] for _ in range(H)]
dp[0][0][0] = True
for i in range(H):
    for j in range(W):
        diff = diffs[i][j]
        for k in range(r_k):
            if dp[i][j][k] == True:
                for i_, j_ in [(i + 1, j), (i, j + 1)]:
                    for k_ in [k+diff, abs(k-diff)]:
                        try:
                            dp[i_][j_][k_] = True
                        except IndexError:
                            pass
ans = pow(80, 3)
diff = diffs[H-1][W-1]
for k in range(r_k):
    if dp[H - 1][W - 1][k]:
        ans = min(ans, abs(k-diff), k+diff)
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)]
diffs = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]
max_diff = 0
for diff in diffs:
    max_diff= max(max_diff, max(diff))
r_k = max_diff*(H+W)
dp = [[[False] * r_k for _ in range(W + 1)] for _ in range(H + 1)]
dp[0][0][0] = True
dp[1][0][0] = True
dp[0][1][0] = True
for i in range(H):
    for j in range(W):
        diff = diffs[i][j]
        for k in range(r_k):
            dp[i + 1][j + 1][abs(k - diff)] = dp[i + 1][j][k] or dp[i][j +
                                                                       1][k] or dp[i + 1][j + 1][abs(k - diff)]
            if k + diff < r_k:
                dp[i + 1][j + 1][k + diff] = dp[i + 1][j][k] or dp[i][j +
                                                                      1][k] or dp[i + 1][j + 1][k+diff]

for k in range(r_k):
    if dp[H][W][k]:
        print(k)
        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)]
diffs = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]
max_diff = 0
for diff in diffs:
    max_diff= max(max_diff, max(diff))
r_k = max_diff*(H+W)
dp = [[[False] * r_k for _ in range(W+1)] for _ in range(H+1)]
for i in range(H+1):
    dp[i][0][0] = True
for j in range(W+1):
    dp[0][j][0] = True
for i in range(H):
    for j in range(W):
        diff = diffs[i][j]
        for k in range(r_k):
            dp[i + 1][j + 1][abs(k - diff)] = dp[i + 1][j][k] or dp[i][j +
                                                                       1][k] or dp[i + 1][j + 1][abs(k - diff)]
            if k + diff < r_k:
                dp[i + 1][j + 1][k + diff] = dp[i + 1][j][k] or dp[i][j +
                                                                      1][k] or dp[i + 1][j + 1][k+diff]

for k in range(r_k):
    if dp[H][W][k]:
        print(k)
        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)]
r_k = 80*80
diffs = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]
dp = [[[False] * r_k for _ in range(W+1)] for _ in range(H+1)]
for i in range(H+1):
    dp[i][0][0] = True
for j in range(H+1):
    dp[0][j][0] = True
for i in range(H):
    for j in range(W):
        diff = diffs[i][j]
        for k in range(r_k):
            dp[i + 1][j + 1][abs(k - diff)] = dp[i + 1][j][k] or dp[i][j + 1][k] or dp[i + 1][j + 1][abs(k - diff)]
            if k + diff < r_k:
                dp[i + 1][j + 1][k + diff] = dp[i + 1][j][k] or dp[i][j + 1][k] or dp[i + 1][j + 1][k+diff]

for k in range(r_k):
    if dp[H][W][k]:
        print(k)
        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)]
r_k = 80*3
diffs = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]
dp = [[[False] * r_k for _ in range(W+1)] for _ in range(H+1)]
for i in range(H+1):
    dp[i][0][0] = True
for j in range(H+1):
    dp[0][j][0] = True
for i in range(H):
    for j in range(W):
        diff = diffs[i][j]
        for k in range(r_k):
            dp[i + 1][j + 1][abs(k - diff)] = dp[i + 1][j][k] or dp[i][j + 1][k] or dp[i + 1][j + 1][abs(k - diff)]
            if k + diff < r_k:
                dp[i + 1][j + 1][k + diff] = dp[i + 1][j][k] or dp[i][j + 1][k] or dp[i + 1][j + 1][k+diff]

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

Submission status: Runtime Error

Solution code:
import numpy as np

h, w = map(int, input().split())
a = np.array([list(map(int, input().split())) for _ in range(h)])
b = np.array([list(map(int, input().split())) for _ in range(h)])

arr = np.abs(a - b)
dp = np.zeros([h + 1, w + 1, arr.sum() + 1], dtype=np.int64)

dp[:, 0, 0] = 1
dp[0, :, 0] = 1

for r in range(1, h + 1):
    for c in range(1, w + 1):
        move = arr[r - 1, c - 1]
        if move == 0:
            dp[r, c, :] |= dp[r - 1, c, :]
            dp[r, c, :] |= dp[r, c - 1, :]
        else:
            dp[r, c, move:] |= dp[r - 1, c, :-move]
            dp[r, c, :-move] |= dp[r - 1, c, move:]
            dp[r, c, move:] |= dp[r, c - 1, :-move]
            dp[r, c, :-move] |= dp[r, c - 1, move:]

print(np.where(dp[r, c] == 1)[0].min())

Submission status: Runtime Error

Solution code:
import numpy as np

h, w = map(int, input().split())
a = np.array([list(map(int, input().split())) for _ in range(h)])
b = np.array([list(map(int, input().split())) for _ in range(h)])

arr = np.abs(a - b)
dp = np.zeros([h + 1, w + 1, 100000000], dtype=np.int64)

dp[:, 0, 0] = 1
dp[0, :, 0] = 1

for r in range(1, h + 1):
    for c in range(1, w + 1):
        move = arr[r - 1, c - 1]
        if move == 0:
            dp[r, c, :] |= dp[r - 1, c, :]
            dp[r, c, :] |= dp[r, c - 1, :]
        else:
            dp[r, c, move:] |= dp[r - 1, c, :-move]
            dp[r, c, :-move] |= dp[r - 1, c, move:]
            dp[r, c, move:] |= dp[r, c - 1, :-move]
            dp[r, c, :-move] |= dp[r, c - 1, move:]

print(np.where(dp[r, c] == 1)[0].min())

Submission status: Runtime Error

Solution code:
import numpy as np

h, w = map(int, input().split())
a = np.array([list(map(int, input().split())) for _ in range(h)])
b = np.array([list(map(int, input().split())) for _ in range(h)])

arr = np.abs(a - b)
dp = np.zeros([h + 1, w + 1, 1000000], dtype=np.int64)

dp[:, 0, 0] = 1
dp[0, :, 0] = 1

for r in range(1, h + 1):
    for c in range(1, w + 1):
        move = arr[r - 1, c - 1]
        if move == 0:
            dp[r, c, :] |= dp[r - 1, c, :]
            dp[r, c, :] |= dp[r, c - 1, :]
        else:
            dp[r, c, move:] |= dp[r - 1, c, :-move]
            dp[r, c, :-move] |= dp[r - 1, c, move:]
            dp[r, c, move:] |= dp[r, c - 1, :-move]
            dp[r, c, :-move] |= dp[r, c - 1, move:]

print(np.where(dp[r, c] == 1)[0].min())

Submission status: Runtime Error

Solution code:
h,w = map(int, input().split())
al = []
bl = []

for _ in range(h):
    row = list(map(int, input().split()))
    al.append(row)

for _ in range(h):
    row = list(map(int, input().split()))
    bl.append(row)


cl = [ [0]*w for _ in range(h) ]
for i in range(h):
    for j in range(w):
        cl[i][j] = abs(al[i][j] - bl[i][j]) 

INF = 10**9
lim = (h+w)*80//2+1
# dp = [ [INF]*6400 for _ in range(h) ]
dp = [ [ [False]*(lim+1)  for _ in range(w) ] for _ in range(h) ]

dp[0][0][cl[0][0]] = True

for i in range(h):
    for j in range(w):
        for k in range(0,lim+1):
            if dp[i][j][k] == True:
                if i+1 < h:
                    v = cl[i+1][j]
                    dp[i+1][j][k+v] = True
                    dp[i+1][j][abs(k-v)] = True
                if j+1 < w:
                    v = cl[i][j+1]
                    dp[i][j+1][k+v] = True
                    dp[i][j+1][abs(k-v)] = True


for i,v in enumerate(dp[-1][-1]):
    if v == True:
        print(i)
        break

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

ab = [[0]*w for i in range(h)]
for ch in range(h):
    for cw in range(w):
        ab[ch][cw] = abs(a[ch][cw] - b[ch][cw])

ab0 = ab[0][0]
size = 80*(h+w)
dp = [[[False]*(1+size) for ww in range(w)] for hh in range(h)]
dp[0][0][size//2 + ab0] = True # size//2が0に相当
dp[0][0][size//2 - ab0] = True
for ch in range(h):
    for cw in range(w):
        for ck in range(1 + size): 
            if dp[ch][cw][ck]:
                if ch < h-1:
                    dp[ch+1][cw][ck + ab[ch+1][cw]] = True
                    dp[ch+1][cw][ck - ab[ch+1][cw]] = True
                if cw < w-1:
                    dp[ch][cw+1][ck + ab[ch][cw+1]] = True
                    dp[ch][cw+1][ck - ab[ch][cw+1]] = True

for i in range(1 + size//2):
    for j in [1, -1]:
        if dp[h-1][w-1][size//2 + i*j]:
            print(i)
            exit()

Submission status: Runtime Error

Solution code:
import numpy as np

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

a = np.array(a)
b = np.array(b)
ab = abs(a - b)
ab0 = ab[0][0]
size = 80*(h+w)
dp = np.zeros((h, w, 1 + size), dtype='bool')
dp[0][0][size//2 + ab0] = True # size//2が0に相当
dp[0][0][size//2 - ab0] = True
for ch in range(h):
    for cw in range(w):
        for ck in range(1 + size): 
            if dp[ch][cw][ck]:
                if ch < h-1:
                    dp[ch+1][cw][ck + ab[ch+1][cw]] = True
                    dp[ch+1][cw][ck - ab[ch+1][cw]] = True
                if cw < w-1:
                    dp[ch][cw+1][ck + ab[ch][cw+1]] = True
                    dp[ch][cw+1][ck - ab[ch][cw+1]] = True

for i in range(1 + size//2):
    for j in [1, -1]:
        if dp[h-1][w-1][size//2 + i*j]:
            print(i)
            exit()

Submission status: Runtime Error

Solution code:
import numpy as np
H, W = map(int, input().split())
red_map = np.array([input().split() for i in range(H)], dtype="int")
blue_map = np.array([input().split() for i in range(H)], dtype="int")
sub_map = abs(red_map - blue_map)
dp = [[[] for i in range(W)] for i in range(H)]
 
for i in range(H):
  for j in range(W):
    if not i and not j:
      dp[i][j].append(sub_map[i][j])
      dp[i][j].append(-sub_map[i][j])
    if i > 0 and j > 0:
      for k in dp[i-1][j]:
        dp[i][j].append(k + sub_map[i][j])
        dp[i][j].append(k - sub_map[i][j])
      for l in dp[i][j-1]:
        dp[i][j].append(l + sub_map[i][j])
        dp[i][j].append(l - sub_map[i][j])
    if i > 0 and not j:
      for m in dp[i-1][j]:
        dp[i][j].append(m + sub_map[i][j])
        dp[i][j].append(m - sub_map[i][j])
    if j > 0 and not i:
      for n in dp[i][j-1]:
        dp[i][j].append(n + sub_map[i][j])
        dp[i][j].append(n - sub_map[i][j])

print(min(np.abs(dp[H-1][W-1])))

Submission status: Runtime Error

Solution code:
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

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

    ablst=[[0]*w for i in range(h)]
    ma=0
    for i in range(h):
        for j in range(w):
            ablst[i][j]=abs(a[i][j]-b[i][j])
            ma=max(ma,ablst[i][j])
    lst1=[0]*ma*(h+w+10)

    lst=[[lst1[:] for i in range(w)] for i in range(h)]
    lst[0][0][abs(a[0][0]-b[0][0])]=1


    # print(lst)
    for i in range(h):
        for j in range(w):
            if i+1<h:
                for k in range(len(lst[i][j])):
                    if lst[i][j][k]==1:
                        lst[i+1][j][k+ablst[i+1][j]]=1
                        lst[i+1][j][abs(k-ablst[i+1][j])]=1
            if j+1<w:
                for k in range(len(lst[i][j])):
                    if lst[i][j][k]==1:
                        lst[i][j+1][k+ablst[i][j+1]]=1
                        lst[i][j+1][abs(k-ablst[i][j+1])]=1
        # print(d)
            # print(lst[i][j])
    # anslst=list(lst[-1][-1].keys())
    # anslst.sort(key=lambda x: abs(x))
    # print(lst)
    for i in range(len(lst[-1][-1])):
        if lst[-1][-1][i]==1:
            print(i)
            break

main()

Submission status: Runtime Error

Solution code:
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-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)]

ablst=[[0]*w for i in range(h)]
ma=0
for i in range(h):
    for j in range(w):
        ablst[i][j]=abs(a[i][j]-b[i][j])
        ma=max(ma,ablst[i][j])
lst1=[0]*ma*(h+w+10)

lst=[[lst1[:] for i in range(w)] for i in range(h)]
lst[0][0][abs(a[0][0]-b[0][0])]=1


# print(lst)
for i in range(h):
    for j in range(w):
        if i+1<h:
            for k in range(len(lst[i][j])):
                if lst[i][j][k]==1:
                    lst[i+1][j][k+ablst[i+1][j]]=1
                    lst[i+1][j][abs(k-ablst[i+1][j])]=1
        if j+1<w:
             for k in range(len(lst[i][j])):
                if lst[i][j][k]==1:
                    lst[i][j+1][k+ablst[i][j+1]]=1
                    lst[i][j+1][abs(k-ablst[i][j+1])]=1
    # print(d)
        # print(lst[i][j])
# anslst=list(lst[-1][-1].keys())
# anslst.sort(key=lambda x: abs(x))
# print(lst)
for i in range(len(lst[-1][-1])):
    if lst[-1][-1][i]==1:
        print(i)
        break

Submission status: Runtime Error

Solution code:
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-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)]

ablst=[[0]*w for i in range(h)]
ma=0
for i in range(h):
    for j in range(w):
        ablst[i][j]=abs(a[i][j]-b[i][j])
        ma=max(ma,ablst[i][j])
lst1=[0]*ma*(h*w+10)

lst=[[lst1[:] for i in range(w)] for i in range(h)]
lst[0][0][abs(a[0][0]-b[0][0])]=1


# print(lst)
for i in range(h):
    for j in range(w):
        if i+1<h:
            for k in range(len(lst[i][j])):
                if lst[i][j][k]==1:
                    lst[i+1][j][k+ablst[i+1][j]]=1
                    lst[i+1][j][abs(k-ablst[i+1][j])]=1
        if j+1<w:
             for k in range(len(lst[i][j])):
                if lst[i][j][k]==1:
                    lst[i][j+1][k+ablst[i][j+1]]=1
                    lst[i][j+1][abs(k-ablst[i][j+1])]=1
    # print(d)
        # print(lst[i][j])
# anslst=list(lst[-1][-1].keys())
# anslst.sort(key=lambda x: abs(x))
# print(lst)
for i in range(len(lst[-1][-1])):
    if lst[-1][-1][i]==1:
        print(i)
        break

Submission status: Runtime Error

Solution code:
def main():
    B_MAX = (80 + 80 - 1) * 80
    H, W = list(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 = [[[0 for _ in range(B_MAX + 1)] for _ in range(W)] for _ in range(H)]
    dp[0][0][abs(A[0][0] - B[0][0])] = 1
    max_b = abs(A[0][0] - B[0][0])
    for h in range(H):
        for w in range(W):
            if h == 0 and w == 0:
                continue
            d = abs(A[h][w] - B[h][w])
            max_b += d
            for b in range(max_b + 1):
                v = 0
                if h > 0:
                    v = max(v, dp[h - 1][w][abs(b - d)])
                    if b + d <= B_MAX:
                        v = max(v, dp[h - 1][w][b + d])
                if w > 0:
                    v = max(v, dp[h][w - 1][abs(b - d)])
                    if b + d <= B_MAX:
                        v = max(v, dp[h][w - 1][b + d])
                dp[h][w][b] = v
    ans = 0
    while dp[H - 1][W - 1][ans] == 0:
        ans += 1
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
def main():
    B_MAX = (80 + 80 - 1) * 80 // 2
    H, W = list(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 = [[[0 for _ in range(B_MAX + 1)] for _ in range(W)] for _ in range(H)]
    dp[0][0][abs(A[0][0] - B[0][0])] = 1
    max_b = abs(A[0][0] - B[0][0])
    for h in range(H):
        for w in range(W):
            if h == 0 and w == 0:
                continue
            d = abs(A[h][w] - B[h][w])
            max_b += d
            for b in range(max_b + 1):
                v = 0
                if h > 0:
                    v = max(v, dp[h - 1][w][abs(b - d)])
                    if b + d <= B_MAX:
                        v = max(v, dp[h - 1][w][b + d])
                if w > 0:
                    v = max(v, dp[h][w - 1][abs(b - d)])
                    if b + d <= B_MAX:
                        v = max(v, dp[h][w - 1][b + d])
                dp[h][w][b] = v
    ans = 0
    while dp[H - 1][W - 1][ans] == 0:
        ans += 1
    print(ans)


if __name__ == '__main__':
    main()

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)]
num=0
for i in range(H):
  for j in range(W):
    num+=abs(A[i][j]-B[i][j])
    
#dp[i][j]=マス(i,j)における可能なポイントのリスト
dp=[[[False]*(num) for _ in range(W)] for _ in range(H)]
dp[0][0][0]=True

for i in range(W+H-1):
  I,J=0,0
  if i < H:
    I=i
    J=0
  else:
    I=H-1
    J=i-H+1
  while I>=0 and J<W:
    sa = abs(A[I][J] - B[I][J])
    for n in range(num):
      if not dp[I][J][n]:
        continue
      if I!=H-1:
        dp[I+1][J][n+sa]=True
        dp[I+1][J][abs(n-sa)]=True
      if J!=W-1:
        dp[I][J+1][n+sa]=True
        dp[I][J+1][abs(n-sa)]=True
    I-=1
    J+=1

sa = abs(A[H-1][W-1] - B[H-1][W-1])
ans=float("inf")
for n in range(num):
  if not dp[-1][-1][n]:
    continue
  ans = min(ans,abs(n-sa),n+sa)
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
H,W=map(int,input().split())

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

#dp[i][j]=マス(i,j)における可能なポイントのリスト
dp=[[[] for _ in range(W)] for _ in range(H)]
dp[0][0].append(0)

for i in range(W+H-1):
  I,J=0,0
  if i < H:
    I=i
    J=0
  else:
    I=H-1
    J=i-H+1
  while I>=0 and J<W:
    sa = A[I][J] - B[I][J]
    for point in dp[I][J]:
      if I!=H-1:
        dp[I+1][J].append(point+sa)
        dp[I+1][J].append(point-sa)
      if J!=W-1:
        dp[I][J+1].append(point+sa)
        dp[I][J+1].append(point-sa)
    I-=1
    J+=1

ans=dp[H-1][W-1]
sa = A[H-1][W-1] - B[H-1][W-1]
res=float("inf")
for p in ans:
  res=min(res,abs(p+sa),abs(p-sa))
print(res)
print(dp[0][1])

Submission status: Runtime Error

Solution code:
H,W=map(int,input().split())
red=[]
blue=[]
for i in range(H):
  A=list(map(int,input().split()))
  red.append(A)
for i in range(H):
  B=list(map(int,input().split()))
  blue.append(B)
list=[[[0 for i in range(80*(j+k+1))] for j in range(W)] for k in range(H)]
list[0][0][abs(red[0][0]-blue[0][0])]=1
for i in range(H):
  for j in range(W):
    for k in range(len(list[i][j])):
      if list[i][j][k]==1:
        if i+1<H:
          if abs(k-abs(red[i+1][j]-blue[i+1][j]))<12801: 
            list[i+1][j][abs(k-abs(red[i+1][j]-blue[i+1][j]))]=1
          if abs(k+abs(red[i+1][j]-blue[i+1][j]))<12801: 
            list[i+1][j][abs(k+abs(red[i+1][j]-blue[i+1][j]))]=1
        if j+1<W:
          if abs(k-abs(red[i][j+1]-blue[i][j+1]))<12801: 
            list[i][j+1][abs(k-abs(red[i][j+1]-blue[i][j+1]))]=1
          if abs(k+abs(red[i][j+1]-blue[i][j+1]))<12801: 
            list[i][j+1][abs(k+abs(red[i][j+1]-blue[i][j+1]))]=1           
for i in range(12801):
  if list[H-1][W-1][i]==1:
    print(i)
    break

Submission status: Runtime Error

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)]
    
    ofset = 80*164
    #mask = (1 << (ofset*2)) - 1
    dp=[[0 for i in range(w)] for j in range(h)]
    dp[0][0] |= 1 << (a[0][0] - b[0][0] + ofset)
    dp[0][0] |= 1 << (b[0][0] - a[0][0] + ofset)
    """
    for i in range(h):
        for j in range(w):
            if i < h - 1:
                A = abs(a[i+1][j] - b[i+1][j])
                dp[i+1][j] |= (dp[i][j] << A)
                dp[i+1][j] |= (dp[i][j] >> A)
            if j < w - 1:
                A = abs(a[i][j+1] - b[i][j+1])
                dp[i][j+1] |= (dp[i][j] << A)
                dp[i][j+1] |= (dp[i][j] >> A)
    ans = 10000000000
    """
    for i in range(ofset, 80*330):
        if (dp[h-1][w-1]>>i)&1:
            ans = i - ofset
            break
    for i in reversed(range(ofset)):
        if (dp[h-1][w-1]>>i)&1:
            ans = min(ans, ofset - i)
            break
    print(ans)
    
if __name__=='__main__':
  main()

Submission status: Runtime Error

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

def main():
    H,W = map(int,input().split())
    point = [[0]*(W+1) for _ in range(H+1)]
    
    for i in range(H):
        a = list(map(int,input().split()))
        for j in range(W):
            point[i][j] += a[j]
    
    cnt = 0
    for i in range(H):
        b = list(map(int,input().split()))
        for j in range(W):
            point[i][j] = abs(point[i][j]-b[j])
            cnt += point[i][j]

    dp = [[[False]*(cnt+1) for _ in range(W+1)] for _ in range(H+1)]
    dp[0][0][point[0][0]] = True
    for i in range(H):
        for j in range(W):
            for k in range(cnt):
                if k+point[i+1][j] <= cnt:
                    dp[i+1][j][k+point[i+1][j]] = dp[i+1][j][k+point[i+1][j]] or dp[i][j][k]
                if k+point[i][j+1] <= cnt:
                    dp[i][j+1][k+point[i][j+1]] = dp[i][j+1][k+point[i][j+1]] or dp[i][j][k]
                dp[i+1][j][abs(k-point[i+1][j])] = dp[i+1][j][abs(k-point[i+1][j])] or dp[i][j][k]
                dp[i][j+1][abs(k-point[i][j+1])] = dp[i][j+1][abs(k-point[i][j+1])] or dp[i][j][k]
    
    print(dp[H-1][W-1].index(True))
    
if __name__ == "__main__":
    main()