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:
import sys
input = sys.stdin.readline

H,W = map(int,input().split())
#差分だけで良い，これを*1 or -1していく
diff = [list(map(int, input().split())) for _ in range(H)]

for i in range(H):
    B = list(map(int,input().split()))
    for j in range(W):
        diff[i][j] = abs(diff[i][j] - B[j])
        
 # i,jマスにおいて，偏りがkになれるかどうかでDP
#dp[i][j]にはkという数字にたどり着けるかどうかを示す2進数を入れる

dp = [[False]*W for _ in range(H)]
ofs = 80*(H+W)


dp[0][0]=(1<<(ofs+diff[0][0]) | 1>>(ofs-diff[0][0]))
 
for i in range(H):
    for j in range(W):
        if i>0:
            dp[i][j]|=(dp[i-1][j]<<diff[i][j] | dp[i-1][j]>>diff[i][j])
        if j>0:
            dp[i][j]|=(dp[i][j-1]<<diff[i][j] | dp[i][j-1]>>diff[i][j])


#0のところから見て，最初に1が出てきたところが答え
for k in range(ofs):
    if (dp[-1][-1]>>(k+ofs)&1):
        print(k)
        break

Submission status: Accepted

Solution code:
tdlist = lambda i, j, value=0: [[value] * j for _ in range(i)]

h, w = map(int, input().split())
D = 160*80
D2 = D*2+1

diff = [list(map(int, input().split())) for _ in range(h)]
for i in range(h):
    for j, b in enumerate(list(map(int, input().split()))):
        diff[i][j] = abs(diff[i][j] - b)

dp = tdlist(h, w)
dp[0][0] |= 1 << D+diff[0][0]
dp[0][0] |= 1 << D-diff[0][0]

for i in range(h):
    for j in range(w):
        if j:
            dp[i][j] |= dp[i][j-1] << diff[i][j]
            dp[i][j] |= dp[i][j-1] >> diff[i][j]
        if i:
            dp[i][j] |= dp[i-1][j] << diff[i][j]
            dp[i][j] |= dp[i-1][j] >> diff[i][j]

ans = float("inf")
for i in range(D2):
    if dp[h-1][w-1] >> i & 1:
        ans = min(ans, abs(i-D))
print(ans)

Submission status: Accepted

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

H,W=map(int, sys.stdin.readline().split())
A=[ map(int, sys.stdin.readline().split()) for _ in range(H) ]
B=[ map(int, sys.stdin.readline().split()) for _ in range(H) ]

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

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

Center=80*80      #中心地のセット マイナスの値もセットできるようにbitsetのゼロを80*80+1桁目に置く

#初期化
r,c=0,0
val=A[r][c]
dp[r][c]=1<<val+Center

D=[(1,0),(0,1)]

for r in range(H):
    for c in range(W):
        for dr,dc in D: #下に1つ移動したマス、右に1つ移動したマス
            if r+dr<H and c+dc<W:   #下のマス or 右のマスが存在するならばif文内の処理を行う
                next_r=r+dr
                next_c=c+dc
                next_val=A[next_r][next_c]
                dp[next_r][next_c]|=dp[r][c]<<next_val  #次のマスの値が+の場合は、bitshift全体を左にシフト
                dp[next_r][next_c]|=dp[r][c]>>next_val  #次のマスの値が-の場合は、bitshift全体を右にシフト

ans=float("inf")
for i in range(Center*2):
    bit=dp[H-1][W-1]>>i&1
    if bit==1:
        ans=min(ans,abs(i-Center))

print ans

Submission status: Accepted

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

H,W=map(int, sys.stdin.readline().split())
A=[ map(int, sys.stdin.readline().split()) for _ in range(H) ]
B=[ map(int, sys.stdin.readline().split()) for _ in range(H) ]

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

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

Center=80*80      #中心地のセット マイナスの値もセットできるようにbitsetのゼロを80*80+1桁目に置く

#初期化
r,c=0,0
val=A[r][c]
dp[r][c]=1<<val+Center

D=[(1,0),(0,1)]

for r in range(H):
    for c in range(W):
        for dr,dc in D: #下に1つ移動したマス、右に1つ移動したマス
            if r+dr<H and c+dc<W:   #下のマスor右のマスが存在するならばif文内の処理を行う
                next_r=r+dr
                next_c=c+dc
                next_val=A[next_r][next_c]
                dp[next_r][next_c]|=dp[r][c]<<next_val  #次のマスの値が+の場合は、bitshift全体を左にシフト
                dp[next_r][next_c]|=dp[r][c]>>next_val  #次のマスの値が-の場合は、bitshift全体を右にシフト

ans=float("inf")
for i in range(Center*2):
    bit=dp[H-1][W-1]>>i&1
    if bit==1:
        ans=min(ans,abs(i-Center))

print ans

Submission status: Accepted

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

H,W=map(int, sys.stdin.readline().split())
A=[ map(int, sys.stdin.readline().split()) for _ in range(H) ]
B=[ map(int, sys.stdin.readline().split()) for _ in range(H) ]

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

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

Center=80*80      #中心地のセット マイナスの値もセットできるようにbitsetのゼロを80*80+1桁目に置く

#初期化
r,c=0,0
val=A[r][c]
dp[r][c]=1<<val+Center

D=[(1,0),(0,1)]

for r in range(H):
    for c in range(W):
        for dr,dc in D: #横に1つ移動か、下に1つ移動
            if r+dr<H and c+dc<W:   #移動しようとするマスが存在するならばif文内の処理を行う
                next_r=r+dr
                next_c=c+dc
                next_val=A[next_r][next_c]
                dp[next_r][next_c]|=dp[r][c]<<next_val  #次のマスの値が+の場合は、bitshift全体を左にシフト
                dp[next_r][next_c]|=dp[r][c]>>next_val  #次のマスの値が-の場合は、bitshift全体を右にシフト

ans=float("inf")
for i in range(Center*2):
    bit=dp[H-1][W-1]>>i&1
    if bit==1:
        ans=min(ans,abs(i-Center))

print ans

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
from collections import deque,defaultdict

H,W=map(int, sys.stdin.readline().split())
A=[ map(int, sys.stdin.readline().split()) for _ in range(H) ]
B=[ map(int, sys.stdin.readline().split()) for _ in range(H) ]

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

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

Center=80*80      #中心地のセット マイナスの値もセットできるようにbitsetのゼロを80*80+1桁目に置く

#初期化
r,c=0,0
val=A[r][c]
dp[r][c]=1<<val+Center

q=deque()
q.append((0,0))
D=[(1,0),(0,1)]
Visit=defaultdict(lambda: 0)     #訪問済みの頂点にフラグ1を立てる
Visit[(0,0)]=1
D=[(1,0),(0,1)]

for r in range(H):
    for c in range(W):
        for dr,dc in D: #横に1つ移動か、下に1つ移動
            if r+dr<H and c+dc<W:   #移動しようとするマスが存在するならばif文内の処理を行う
                next_r=r+dr
                next_c=c+dc
                next_val=A[next_r][next_c]
                dp[next_r][next_c]|=dp[r][c]<<next_val  #次のマスの値が+の場合は、bitshift全体を左にシフト
                dp[next_r][next_c]|=dp[r][c]>>next_val  #次のマスの値が-の場合は、bitshift全体を右にシフト
                q.append((next_r,next_c))

ans=float("inf")
for i in range(Center*2):
    bit=dp[H-1][W-1]>>i&1
    if bit==1:
        ans=min(ans,abs(i-Center))

print ans

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
h, w = map(int, input().split())
grid = []
bias_inf = 2000
for i in range(h):
    k = list(map(int, input().split()))
    grid.append(k)
for i in range(h):
    k = list(map(int, input().split()))
    for j in range(w):
        grid[i][j] = abs(grid[i][j] - k[j])

dp = [[[0 for i in range(-bias_inf,bias_inf)] for j in range(w)] for k in range(h)]
dp[0][0][0 - grid[0][0]] = 1
dp[0][0][0 + grid[0][0]] = 1
for i in range(h):
    for j in range(w):
        for k in range(-bias_inf,bias_inf):
            if i == h-1 and j == w-1:
                break
            if dp[i][j][k]:
                if i != h-1 and j != w-1:
                    dp[i][j+1][k + grid[i][j+1]] = 1
                    dp[i+1][j][k + grid[i+1][j]] = 1
                    dp[i][j+1][k - grid[i][j+1]] = 1
                    dp[i+1][j][k - grid[i+1][j]] = 1
                elif j == w-1:
                    dp[i+1][j][k - grid[i+1][j]] = 1
                    dp[i+1][j][k + grid[i+1][j]] = 1
                else:
                    dp[i][j+1][k - grid[i][j+1]] = 1
                    dp[i][j+1][k + grid[i][j+1]] = 1
ans = bias_inf
for i in range(-bias_inf,bias_inf):
    if dp[-1][-1][i]:
        ans = min(ans, abs(i))
print(ans)

Submission status: Accepted

Solution code:
# ref: https://atcoder.jp/contests/abc147/submissions/8873191

import sys
input = sys.stdin.buffer.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)]

# 負の値を表現するために、下駄を履かせる
# 偏りdが0のとき、X桁目が1になる

X = (H + W) * 80

# dp[i][j]: (i,j)で偏りの状態
dp = [[0] * W for _ in range(H)]

# 初期条件
d = abs(A[0][0] - B[0][0])
dp[0][0] = (1 << (X - d))

# 貰うDP
for i in range(H):
    for j,(a,b) in enumerate(zip(A[i],B[i])):
        if i == j == 0:
            continue

        # (i,j)の偏り
        d = abs(a - b)

        tmp = 0
        # (i-1,j)からの遷移
        if i:
            tmp |= (dp[i-1][j] << d) 
            tmp |= (dp[i-1][j] >> d)
        # (i,j-1)からの遷移
        if j:
            tmp |= (dp[i][j-1] << d) 
            tmp |= (dp[i][j-1] >> d)

        dp[i][j] = tmp

dp = dp[-1][-1]

# 二進数に変換して、先頭の0bを削除する
B = bin(dp)[2:]
can_make = [i - X for i,b in enumerate(B[::-1]) if b == '1']

answer = min(tmp if tmp >= 0 else -tmp for tmp in can_make)
print(answer)

Submission status: Accepted

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

C = abs(A - B)
# print (C)

D = 80 * (H + W + 1) + 10
D2 = D * 2

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

dp[0][0] = 1 << (D - int(C[0][0]))
# print (dp[0][0])
dp[0][0] |= 1 << (D + int(C[0][0]))
# print ((dp[0][0]))



# print (dp[0][0])
dp = np.array(dp)

for h in range(H):
    for w in range(W):
        tmp = int(C[h][w])
        if h:
            dp[h][w] |= dp[h - 1][w] << tmp
            dp[h][w] |= dp[h - 1][w] >> tmp
        if w:
            dp[h][w] |= dp[h][w - 1] << tmp
            dp[h][w] |= dp[h][w - 1] >> tmp

        
ans = D2
# print (ans)
tmp = dp[H - 1][W - 1]
for i in range(D2):
    if (tmp >> i) & 1:
        ans = min(ans, abs(i - D))

print (ans)

Submission status: Accepted

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)]
center = 80 * (H+W)
C = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]

dp = [[0] * W for i in range(H)]
dp[0][0] = (1 << (center + C[0][0])) | (1 >> (center - C[0][0]))

for i in range(H):
    for j in range(W):
        if i:
            dp[i][j] |= dp[i-1][j] << C[i][j]
            dp[i][j] |= dp[i-1][j] >> C[i][j]
        if j:
            dp[i][j] |= dp[i][j-1] << C[i][j]
            dp[i][j] |= dp[i][j-1] >> C[i][j]

ans = center * 2
for i in range(center*2):
    if (dp[H-1][W-1] >> i) & 1:
        ans = min(ans, abs(i-center))

print(ans)

Submission status: Accepted

Solution code:
def solve(h, w, A, B):
    center = 80 * (h + w - 1)
    C = [[abs(A[r][c] - B[r][c]) for c in range(w)] for r in range(h)]
    dp = [[0] * w for r in range(h)]
    dp[0][0] = (1 << (center + C[0][0])) | (1 << (center - C[0][0]))
    for r in range(h):
        for c in range(w):
            if r > 0:
                dp[r][c] |= (dp[r-1][c] << C[r][c]) | (dp[r-1][c] >> C[r][c])
            if c > 0:
                dp[r][c] |= (dp[r][c-1] << C[r][c]) | (dp[r][c-1] >> C[r][c])
    mask = bin(dp[-1][-1] >> center)
    return mask[::-1].find("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)]
print(solve(h, w, A, B))

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline

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)]
dp = [[0] * W for _ in range(H)]
X = (H + W) * 80
d = abs(A[0][0] - B[0][0])
dp[0][0] = 1 << (X - d)
for h in range(H):
    for w, (a, b) in enumerate(zip(A[h], B[h])):
        if h == w == 0:
            continue

        d = abs(a - b)
        x = 0
        if h != 0:
            x |= (dp[h - 1][w] << d)
            x |= (dp[h - 1][w] >> d)
        if w != 0:
            x |= (dp[h][w - 1] << d)
            x |= (dp[h][w - 1] >> d)
        dp[h][w] = x

bit = bin(dp[-1][-1])[2:]
can = [i - X for i, b in enumerate(bit[::-1]) if b == '1']
answer = min(x if x >= 0 else -x for x in can)
print(answer)

Submission status: Accepted

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

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

base = max_ab*(H + W)
dp = [[1 << base for w in range(W)] for h in range(H)]
dp[0][0] = (1 << base + AB[0][0]) | (1 << base - AB[0][0])
for h in range(1, H):
  dp[h][0] = (dp[h-1][0] << AB[h][0]) | (dp[h-1][0] >> AB[h][0])

for w in range(1, W):
  dp[0][w] = (dp[0][w-1] << AB[0][w]) | (dp[0][w-1] >> AB[0][w])

for h in range(1, H):
  for w in range(1, W):
    dp[h][w] = (
      (dp[h-1][w] << AB[h][w]) | (dp[h-1][w] >> AB[h][w]) 
      | (dp[h][w-1] << AB[h][w]) | (dp[h][w-1] >> AB[h][w]))

positive = bin(dp[H-1][W-1] >> base)
print(positive[::-1].find('1'))

Submission status: Accepted

Solution code:
def solve(h, w, A, B):
    center = (h+w)*80
    C = [[abs(A[r][c]-B[r][c]) for c in range(w)] for r in range(h)]
    dp = [[0]*w for r in range(h)]
    dp[0][0] = (1 << (center + C[0][0])) | (1 << (center - C[0][0]))
    for r in range(h):
        for c in range(w):
            if r > 0:
                dp[r][c] |= (dp[r-1][c] << C[r][c]) | (dp[r-1][c] >> C[r][c])
            if c > 0:
                dp[r][c] |= (dp[r][c-1] << C[r][c]) | (dp[r][c-1] >> C[r][c])
    mask = bin(dp[-1][-1] >> center)
    return mask[::-1].find("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)]
print(solve(h, w, A, B))

Submission status: Accepted

Solution code:
def run():
  ofs = 1000
  H, W = map(int, input().split())
  dp = [[0 for w in range(W)] for h in range(H)]
  a = [list(map(int, input().split())) for i in range(H)]
  b = [list(map(int, input().split())) for i in range(H)]
  c = [[abs(a[i][j] - b[i][j]) for j in range(W)] for i in range(H)]
  dp[0][0] = (1 << ofs + c[0][0]) | (1 << ofs - c[0][0])
  for h in range(1, H):
    dp[h][0] = (dp[h-1][0] << c[h][0]) | (dp[h-1][0] >> c[h][0])
  for w in range(1, W):
    dp[0][w] = (dp[0][w-1] << c[0][w]) | (dp[0][w-1] >> c[0][w])
  for h in range(1, H):
    for w in range(1, W):
      v = c[h][w]
      dp[h][w] = (dp[h-1][w] << v) | (dp[h-1][w] >> v)
      dp[h][w] |= (dp[h][w-1] << v) | (dp[h][w-1] >> v)
  s = list(bin(dp[-1][-1] >> ofs))
  for i in range(len(s)):
    if s[-1-i] == '1':
      print(i)
      break

if __name__ == '__main__':
  run()

Submission status: Accepted

Solution code:
from sys import stdin
input = stdin.buffer.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)]


dp = [0] * -~w
dp[0] = 1 << 6400
for i in range(h):
    for j in range(w):
        t = dp[j]
        if j:
            t |= dp[j-1]
        x = abs(a[i][j] - b[i][j])
        dp[j] = (t << x) | (t >> x)

for i, x in enumerate(bin(dp[w-1])[-6401:]):
    if int(x):
        print(i)
        exit(0)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    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)]

    d=abs(A[0][0]-B[0][0])
    ofs=12800
    dp=[[0]*w for _ in range(h)]
    dp[0][0]=(1<<(ofs+d))|(1<<(ofs-d))

    from itertools import product
    for i,j in product(range(h),range(w)):
        if(i==0 and j==0): continue
        d=abs(A[i][j]-B[i][j])
        if(i>0):
            dp[i][j]|=(dp[i-1][j]<<d)|(dp[i-1][j]>>d)
        if(j>0):
            dp[i][j]|=(dp[i][j-1]<<d)|(dp[i][j-1]>>d)

    for i in range(ofs):
        if((dp[-1][-1]>>(i+ofs))&1):
            print(i)
            return
resolve()

Submission status: Accepted

Solution code:
import sys
import numpy as np
input = sys.stdin.readline
h,w=[int(j) for j in input().split()]
a=[]
b=[]
a=[[int(j) for j in input().split()] for i in range(h)]
b=[[int(j) for j in input().split()] for i in range(h)]
x=(h+w)*80
l=2*x+1
dp=[[0]*w for i in range(h)]
dp[0][0]=np.zeros(l,np.bool)
d=abs(a[0][0]-b[0][0])
dp[0][0][x+d]=1
for i in range(h):
    for j,(s,t) in enumerate(zip(a[i],b[i])):
        if i==j==0:
            continue
        d=abs(s-t)
        p=np.zeros(l,np.bool)
        if i!=0:
            p[d:]|=dp[i-1][j][:l-d]
            p[:l-d]|=dp[i-1][j][d:]
        if j!=0:
            p[d:]|=dp[i][j-1][:l-d]
            p[:l-d]|=dp[i][j-1][d:]
        dp[i][j]=p

ans=np.abs(np.where(dp[-1][-1])[0]-x).min()
print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
write = sys.stdout.write
# template


def solve():
    ofs = 700
    h, w = map(int, readline().split())
    A = [list(map(int, readline().split())) for i in range(h)]
    B = [list(map(int, readline().split())) for i in range(h)]
    dp = [0]*(w+1)
    x = abs(A[0][0]-B[0][0])
    dp[0] = 1 << ofs

    for i in range(h):
        for j in range(w):
            x = abs(A[i][j]-B[i][j])
            t = dp[j] | dp[j-1]
            dp[j] = (t << x) | (t >> x)

    for i, s in enumerate(bin(dp[w-1])[-ofs-1:]):
        if int(s):
            write("%d\n" % i)
            return


if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
write = sys.stdout.write
# template


def solve():
    ofs = 700
    h, w = map(int, readline().split())
    A = [list(map(int, readline().split())) for i in range(h)]
    B = [list(map(int, readline().split())) for i in range(h)]
    dp = [0 for _ in range(w+1)]
    x = abs(A[0][0]-B[0][0])
    dp[0] = 1 << ofs

    for i in range(h):
        for j in range(w):
            x = abs(A[i][j]-B[i][j])
            t = dp[j] | dp[j-1]
            dp[j] = (t << x) | (t >> x)

    for i, s in enumerate(bin(dp[w-1])[-ofs-1:]):
        if(int(s)):
            print(i)
            return


if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
write = sys.stdout.write
# template


def solve():
    ofs = 1600
    h, w = map(int, readline().split())
    A = [list(map(int, readline().split())) for i in range(h)]
    B = [list(map(int, readline().split())) for i in range(h)]
    dp = [0 for _ in range(w+1)]
    x = abs(A[0][0]-B[0][0])
    dp[0] = 1 << ofs

    for i in range(h):
        for j in range(w):
            x = abs(A[i][j]-B[i][j])
            t = dp[j] | dp[j-1]
            dp[j] = (t << x) | (t >> x)

    b = list(map(lambda s: s == '1', list(bin(dp[w-1])[-ofs-1:-ofs+100])))
    for i, s in enumerate(b):
        if(s):
            print(i)
            return


if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
write = sys.stdout.write
# template


def solve():
    ofs = 700
    h, w = map(int, readline().split())
    A = [list(map(int, readline().split())) for i in range(h)]
    B = [list(map(int, readline().split())) for i in range(h)]
    dp = [0 for _ in range(w+1)]
    x = abs(A[0][0]-B[0][0])
    dp[0] = 1 << ofs

    for i in range(h):
        for j in range(w):
            x = abs(A[i][j]-B[i][j])
            t = dp[j] | dp[j-1]
            dp[j] = (t << x) | (t >> x)

    b = list(map(lambda s: s == '1', list(bin(dp[w-1])[-ofs-1:-ofs+100])))
    for i, s in enumerate(b):
        if(s):
            print(i)
            return


if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
write = sys.stdout.write
# template


def solve():
    ofs = 1000
    h, w = map(int, readline().split())
    A = [list(map(int, readline().split())) for i in range(h)]
    B = [list(map(int, readline().split())) for i in range(h)]
    dp = [0 for _ in range(w+1)]
    x = abs(A[0][0]-B[0][0])
    dp[0] = 1 << ofs

    for i in range(h):
        for j in range(w):
            x = abs(A[i][j]-B[i][j])
            t = (dp[j] | dp[j-1])
            dp[j] = ((t << x) | (t >> x))

    b = list(map(lambda s: s == '1', list(bin(dp[w-1])[-ofs-1:-ofs+100])))
    for i, s in enumerate(b):
        if(s):
            print(i)
            return


if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
write = sys.stdout.write
# template


def solve():
    ofs = 6400
    h, w = map(int, readline().split())
    A = [list(map(int, readline().split())) for i in range(h)]
    B = [list(map(int, readline().split())) for i in range(h)]
    dp = [0 for _ in range(w+1)]
    x = abs(A[0][0]-B[0][0])
    dp[0] = 1 << ofs

    for i in range(h):
        for j in range(w):
            x = abs(A[i][j]-B[i][j])
            t = (dp[j] | dp[j-1])
            dp[j] = ((t << x) | (t >> x))

    b = list(map(lambda s: s == '1', list(bin(dp[w-1])[-ofs-1:-ofs+100])))
    for i, s in enumerate(b):
        if(s):
            print(i)
            return


if __name__ == "__main__":
    solve()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
import itertools
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
write = sys.stdout.write
# template


def solve():
    ofs = 1000
    h, w = map(int, readline().split())
    A = [list(map(int, readline().split())) for i in range(h)]
    B = [list(map(int, readline().split())) for i in range(h)]
    dp = [0 for _ in range(w+1)]
    x = abs(A[0][0]-B[0][0])
    dp[0] = 1 << ofs

    for i in range(h):
        for j in range(w):
            x = abs(A[i][j]-B[i][j])
            t = (dp[j] | dp[j-1])
            dp[j] = ((t << x) | (t >> x))

    b = list(map(lambda s: s == '1', list(bin(dp[w-1])[-ofs-1:-ofs+100])))
    for i, s in enumerate(b):
        if(s):
            print(i)
            return


if __name__ == "__main__":
    solve()

Submission status: Accepted

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

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

dp[0][0] = 1 << ls[0][0]+bias

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

        if j != 0:  
            dp[i][j] |= dp[i][j-1] << ls[i][j]
            dp[i][j] |= dp[i][j-1] >> ls[i][j]

        if i != 0:
            dp[i][j] |= dp[i-1][j] << ls[i][j]
            dp[i][j] |= dp[i-1][j] >> ls[i][j]

ans = dp[h-1][w-1]
idx = 0
while(1):
    if 1 << (bias+idx) & ans or 1 << (bias-idx) & ans:
        break
    idx += 1
print(idx)

Submission status: Accepted

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

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

dp[0][0] = 1 << ls[0][0]+bias

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

        if j != 0:  
            dp[i][j] |= dp[i][j-1] << ls[i][j]
            dp[i][j] |= dp[i][j-1] >> ls[i][j]

        if i != 0:
            dp[i][j] |= dp[i-1][j] << ls[i][j]
            dp[i][j] |= dp[i-1][j] >> ls[i][j]

ans = dp[h-1][w-1] >> bias
idx = 0
while(1):
    if 1 << idx & ans:
        break
    idx += 1
print(idx)

Submission status: Accepted

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

x = (h + w) * 80

dp = [[0] * w for _ in range(h)]
d = abs(a[0][0] - b[0][0])
dp[0][0] = 1 << (x - d)

for i in range(h):
    for j, (ae, be) in enumerate(zip(a[i], b[i])):
        if i == j == 0:
            continue

        d = abs(ae - be)
        bt = 0
        if i != 0:
            bt |= dp[i-1][j] << d
            bt |= dp[i-1][j] >> d

        if j != 0:
            bt |= dp[i][j-1] << d
            bt |= dp[i][j-1] >> d

        dp[i][j] = bt

dp = dp[-1][-1]

b = bin(dp)[2:]
can_make = [i - x for i, be in enumerate(b[::-1]) if be == "1"]

ans = min(x if x >= 0 else -x for x in can_make)
print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

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

X = (H + W) * 80
# X点持った状態から始める
dp = [[0] * W for _ in range(H)]
d = abs(A[0][0] - B[0][0])
dp[0][0] = (1 << (X - d))
for h in range(H):
    for w,(a,b) in enumerate(zip(A[h],B[h])):
        if h == w == 0:
            continue
        d = abs(a - b)
        x = 0
        if h != 0:
            x |= (dp[h-1][w] << d) 
            x |= (dp[h-1][w] >> d)
        if w != 0:
            x |= (dp[h][w-1] << d) 
            x |= (dp[h][w-1] >> d)
        dp[h][w] = x

dp = dp[-1][-1]

B = bin(dp)[2:]
can_make = [i - X for i,b in enumerate(B[::-1]) if b == '1']

answer = min(x if x >= 0 else -x for x in can_make)
print(answer)

Submission status: Accepted

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

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

CONST = 80 * 80 * 2 + 100000
dp = [[0] * w for _ in range(h)]
dp[0][0] = 1 << (c[0][0] + CONST)

for i in range(h):
    for j in range(w):
        if i < h - 1:
            dp[i + 1][j] |= (dp[i][j] << c[i + 1][j])
            dp[i + 1][j] |= (dp[i][j] >> c[i + 1][j])
        if j < w - 1:
            dp[i][j + 1] |= (dp[i][j] << c[i][j + 1])
            dp[i][j + 1] |= (dp[i][j] >> c[i][j + 1])

for k in range(CONST):
    if dp[h - 1][w - 1] & (1 << (CONST + k)) != 0 or dp[h - 1][w - 1] & (
            1 << (CONST - k)) != 0:
        ans = k
        break

print(ans)

Submission status: Accepted

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

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

CONST = 80 * 80 * 2
dp = [[0] * w for _ in range(h)]
dp[0][0] = 1 << (c[0][0] + CONST)

for i in range(h):
    for j in range(w):
        if i < h - 1:
            dp[i + 1][j] |= (dp[i][j] << c[i + 1][j])
            dp[i + 1][j] |= (dp[i][j] >> c[i + 1][j])
        if j < w - 1:
            dp[i][j + 1] |= (dp[i][j] << c[i][j + 1])
            dp[i][j + 1] |= (dp[i][j] >> c[i][j + 1])

for k in range(CONST):
    if dp[h - 1][w - 1] & (1 << (CONST + k)) != 0 or dp[h - 1][w - 1] & (
            1 << (CONST - k)) != 0:
        ans = k
        break

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
write = sys.stdout.write
# template


def resolve():
    ofs = 1000
    h, w = map(int, readline().split())
    A = [list(map(int, readline().split())) for i in range(h)]
    B = [list(map(int, readline().split())) for i in range(h)]
    dp = [[0]*w for _ in range(h)]
    x = abs(A[0][0]-B[0][0])
    dp[0][0] |= ((1 << (ofs+x)) | (1 << (ofs-x)))

    for i in range(h):
        for j in range(w):
            x = abs(A[i][j]-B[i][j])
            t = 0
            if(i-1 >= 0):
                t |= dp[i-1][j]
            if(j-1 >= 0):
                t |= dp[i][j-1]
            dp[i][j] |= ((t << x) | (t >> x))

    for i, s in enumerate(bin(dp[-1][-1])[-ofs-1:]):
        if(s == '1'):
            print(i)
            return


if __name__ == "__main__":
    INF = float("inf")
    MOD = 10**9+7
    resolve()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
write = sys.stdout.write
# template


def resolve():
    ofs = 3200
    h, w = map(int, readline().split())
    A = [list(map(int, readline().split())) for i in range(h)]
    B = [list(map(int, readline().split())) for i in range(h)]
    dp = [[0]*w for _ in range(h)]
    x = abs(A[0][0]-B[0][0])
    dp[0][0] |= ((1 << (ofs+x)) | (1 << (ofs-x)))

    for i in range(h):
        for j in range(w):
            x = abs(A[i][j]-B[i][j])
            t = 0
            if(i-1 >= 0):
                t |= dp[i-1][j]
            if(j-1 >= 0):
                t |= dp[i][j-1]
            dp[i][j] |= ((t << x) | (t >> x))

    for i, s in enumerate(bin(dp[-1][-1])[-ofs-1:]):
        if(s == '1'):
            print(i)
            return


if __name__ == "__main__":
    INF = float("inf")
    MOD = 10**9+7
    resolve()

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
write = sys.stdout.write
# template


def resolve():
    ofs = 6400
    h, w = map(int, readline().split())
    A = [list(map(int, readline().split())) for i in range(h)]
    B = [list(map(int, readline().split())) for i in range(h)]
    dp = [[0]*w for _ in range(h)]
    x = abs(A[0][0]-B[0][0])
    dp[0][0] |= ((1 << (ofs+x)) | (1 << (ofs-x)))

    from itertools import product
    for i, j in product(range(h), range(w)):
        x = abs(A[i][j]-B[i][j])
        t = 0
        if(i-1 >= 0):
            t |= dp[i-1][j]
        if(j-1 >= 0):
            t |= dp[i][j-1]
        dp[i][j] |= ((t << x) | (t >> x))

    for i, s in enumerate(bin(dp[-1][-1])[-ofs-1:]):
        if(s == '1'):
            write("%d\n" % i)
            return


if __name__ == "__main__":
    INF = float("inf")
    MOD = 10**9+7
    resolve()