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: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
H, W = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
B = [list(map(int, input().split())) for _ in range(H)]
X = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]
dp = [[set() for _ in range(W)] for _ in range(H)]
dp[0][0].add(X[0][0])
dplimit = [[0] * W for _ in range(H)]
for i in range(H - 1, -1, -1):
  for j in range(W - 1, -1, -1):
    x = X[i][j]
    if i > 0: dplimit[i - 1][j] = max(dplimit[i - 1][j], dplimit[i][j] + x)
    if j > 0: dplimit[i][j - 1] = max(dplimit[i][j - 1], dplimit[i][j] + x)
for i in range(H):
  for j in range(W):
    for k in dp[i][j]:
      if i + 1 < H:
        u = k + X[i + 1][j]
        v = abs(k - X[i + 1][j])
        if u <= dplimit[i][j]: dp[i + 1][j].add(u)
        if v <= dplimit[i][j]: dp[i + 1][j].add(v)
      if j + 1 < W:
        u = k + X[i][j + 1]
        v = abs(k - X[i][j + 1])
        if u <= dplimit[i][j]: dp[i][j + 1].add(u)
        if v <= dplimit[i][j]: dp[i][j + 1].add(v)
res = float("inf")
#print(dp[1][1])
for k in dp[-1][-1]: res = min(k, res)
print(res)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
#ABC147 E

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

h,w = LI()
a = [LI() for _ in range(h)]
b = [LI() for _ in range(h)]

lst = [[0]*w for _ in range(h)]
m = 0
for i in range(h):
    for j in range(w):
        lst[i][j] = abs(a[i][j] - b[i][j])
        m = max(m,abs(a[i][j] - b[i][j]))

dp = [[[0]*((h+w)*m+1) for _ in range(w)] for _ in range(h)]
dp[0][0][lst[0][0]] = 1
for i in range(h):
    for j in range(w):
        for k in range(m*(h+w)):
            if dp[i][j][k]:
                if i+1 < h:
                    if k+lst[i+1][j] < m*(h+w):
                        dp[i+1][j][k+lst[i+1][j]] = 1
                    dp[i+1][j][abs(k-lst[i+1][j])] = 1
                if j+1 < w:
                    if k+lst[i][j+1] < m*(h+w):
                        dp[i][j+1][k+lst[i][j+1]] = 1
                    dp[i][j+1][abs(k-lst[i][j+1])] = 1

ans = 10**10
for i in range(m*(h+w)):
    if dp[-1][-1][i]:
        ans = min(ans,i)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
H, W = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
B = [list(map(int, input().split())) for _ in range(H)]
X = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]
dp = [[[0] * (641) for _ in range(W)] for _ in range(H)]
dp[0][0][X[0][0]] = 1
for i in range(H):
  for j in range(W):
    for k in range(641):
      if dp[i][j][k] == 0: continue
      if i + 1 < H:
        u = k + X[i + 1][j]
        v = abs(k - X[i + 1][j])
        if u <= 640: dp[i + 1][j][u] += dp[i][j][k]
        if v <= 640: dp[i + 1][j][v] += dp[i][j][k]
      if j + 1 < W:
        u = k + X[i][j + 1]
        v = abs(k - X[i][j + 1])
        if u <= 640: dp[i][j + 1][u] += dp[i][j][k]
        if v <= 640: dp[i][j + 1][v] += dp[i][j][k]
res = float("inf")
#print(dp[1][1])
for k in range(81):
  if dp[-1][-1][k]: res = min(k, res)
print(res)

Submission status: Wrong Answer

Solution code:
#https://atcoder.jp/contests/abc147/submissions/8874645
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    ofs=6400
    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 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'):
            print(i)
            return
                    
resolve()

Submission status: Wrong Answer

Solution code:
#https://atcoder.jp/contests/abc147/submissions/8874645
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    ofs=6400
    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 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'):
            print(i)
            return
                    
resolve()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys


def solve(H: int, W: int, A: "List[List[int]]", B: "List[List[int]]"):
    ab = [[0]*W for _ in range(H)]

    for h in range(H):
        for w in range(W):
            ab[h][w] = abs(A[h][w]-B[h][w])
    
    # i,jにいるときに作れる値
    setlist = [[set() for _ in range(W)] for _ in range(H)]
    for i in range(H):
        for j in range(W):
            if i == 0 and j == 0:
                setlist[i][j].add(ab[i][j])
                continue

            if  i == 0:
                for num in setlist[i][j-1]:
                    if abs(num+ab[i][j]) <160:
                        setlist[i][j].add(abs(num+ab[i][j]))
                    setlist[i][j].add(abs(num-ab[i][j]))
                continue

            if j == 0:
                for num in setlist[i-1][j]:
                    if abs(num+ab[i][j]) <160:
                        setlist[i][j].add(abs(num+ab[i][j]))
                    setlist[i][j].add(abs(num-ab[i][j]))
                continue                
            
            for num in setlist[i-1][j]:
                if abs(num+ab[i][j])<160:
                    setlist[i][j].add(abs(num+ab[i][j]))
                setlist[i][j].add(abs(num-ab[i][j]))
            for num in setlist[i][j-1]:
                if abs(num+ab[i][j])<160:
                    setlist[i][j].add(abs(num+ab[i][j]))
                setlist[i][j].add(abs(num-ab[i][j]))

    print(min(setlist[H-1][W-1]))
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    A = [[int(next(tokens)) for _ in range(W)] for _ in range(H)]  # type: "List[List[int]]"
    B = [[int(next(tokens)) for _ in range(W)] for _ in range(H)]  # type: "List[List[int]]"
    solve(H, W, A, B)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#https://atcoder.jp/contests/abc147/submissions/8874645
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda:sys.stdin.readline().rstrip()
def resolve():
    ofs=6400
    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 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:]):
        if(s=='1'):
            print(i)
            return
                    
resolve()

Submission status: Wrong Answer

Solution code:
#!/bin/bash
''':'
docker run --rm -it -v $(cd $(dirname $0) && pwd):/vo:ro pypy:3-2.4.0 /bin/bash -c "pypy3 /vo/$(basename $0) < /vo/g.inp"; exit $?
exit 0
'''
import sys

def _i(): return int(sys.stdin.readline().strip())
def _ia(): return [ int(x) for x in sys.stdin.readline().strip().split() ]

# read input
h,w = _ia()
ab = []
m = None
for _ in range(h): ab.append(_ia())
for i in range(h):
    r = ab[i]
    for j,bj in enumerate(_ia()):
        r[j] = abs(r[j] - bj)
    t = max(r) + 1
    if m is None or m<t: m=t
#
# check
def check_update(r,k,x,m):
    k_ = abs(k-x)
    if r[k_]:
        return True
    k_ = k + x
    if k_<m and r[k_]:
        return True
    return False
    
# initialize
dp = [[[False] * m for _ in range(w)] for _ in range(h)]
dp[0][0][ab[0][0]] = True
for ij in range(1,h+w-1):
    for i in range(min(ij+1,h)):
        j = ij - i
        if j>=w: continue
        #
        x = ab[i][j]
        nxt = dp[i][j]
        for k in range(m):
            k1 = abs(k-x)
            k2 = k+x
            if i>0:
                flag = check_update(dp[i-1][j],k,x,m)
                if flag:
                    nxt[k] = True
                    continue
            if j>0:
                flag = check_update(dp[i][j-1],k,x,m)
                if flag:
                    nxt[k] = True
                    continue
# stdout
ref=dp[h-1][w-1]
for k in range(m):
    if ref[k]:
        print(k)
        break

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
h,w=map(int,input().split())
a=[]
for i in range(h):
    a.append(list(map(int,input().split())))
b=[]
for i in range(h):
    b.append(list(map(int,input().split())))
dp=[[set([]) for i in range(w)] for j in range(h)]
dp[0][0].add(a[0][0]-b[0][0])
dp[0][0].add(b[0][0]-a[0][0])
for i in range(h):
    for j in range(w):
        if i==j==0:
            continue
        if i==0:
            for k in dp[i][j-1]:
                if k<=3550:
                    dp[i][j].add(abs(k+a[i][j]-b[i][j]))
                    dp[i][j].add(abs(k+b[i][j]-a[i][j]))
        elif j==0:
            for k in dp[i-1][j]:
                if k<=3550:
                    dp[i][j].add(abs(k+a[i][j]-b[i][j]))
                    dp[i][j].add(abs(k+b[i][j]-a[i][j]))
        else:
            for k in (dp[i][j-1]|dp[i-1][j]):
                if k<=5500:
                    dp[i][j].add(abs(k+a[i][j]-b[i][j]))
                    dp[i][j].add(abs(k+b[i][j]-a[i][j]))             
#dp[h-1][w-1]
ans=10**10
for i in dp[h-1][w-1]:
    ans=min(ans,abs(i))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
h,w=map(int,input().split())
a=[]
for i in range(h):
    a.append(list(map(int,input().split())))
b=[]
for i in range(h):
    b.append(list(map(int,input().split())))
dp=[[set([]) for i in range(w)] for j in range(h)]
dp[0][0].add(a[0][0]-b[0][0])
dp[0][0].add(b[0][0]-a[0][0])
for i in range(h):
    for j in range(w):
        if i==j==0:
            continue
        if i==0:
            for k in dp[i][j-1]:
                if k<=3550:
                    dp[i][j].add(abs(k+a[i][j]-b[i][j]))
                    dp[i][j].add(abs(k+b[i][j]-a[i][j]))
        elif j==0:
            for k in dp[i-1][j]:
                if k<=3550:
                    dp[i][j].add(abs(k+a[i][j]-b[i][j]))
                    dp[i][j].add(abs(k+b[i][j]-a[i][j]))
        else:
            for k in (dp[i][j-1]|dp[i-1][j]):
                if k<=6000:
                    dp[i][j].add(abs(k+a[i][j]-b[i][j]))
                    dp[i][j].add(abs(k+b[i][j]-a[i][j]))             
#dp[h-1][w-1]
ans=10**10
for i in dp[h-1][w-1]:
    ans=min(ans,abs(i))
print(ans)

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().split())
A = [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):
        A[i][j] = abs(A[i][j]-b[j])
NUM = 2000
DP = [[[0]*NUM for i in range(W)]for j in range(H)]
DP[0][0][A[0][0]] = 1
i = 0
for j in range(W-1):
    a = A[i][j+1]
    for k in range(NUM):
        if DP[i][j][k] == 1:
            if 0 <= a-k < NUM:
                DP[i][j+1][a-k] = 1
            if 0 <= k-a < NUM:
                DP[i][j+1][k-a] = 1
            if 0 <= k+a < NUM:
                DP[i][j+1][k+a] = 1
j = 0
for i in range(H-1):
    a = A[i+1][j]
    for k in range(NUM):
        if DP[i][j][k] == 1:
            if 0 <= a-k < NUM:
                DP[i+1][j][a-k] = 1
            if 0 <= k-a < NUM:
                DP[i+1][j][k-a] = 1
            if 0 <= k+a < NUM:
                DP[i+1][j][k+a] = 1
for i in range(H-1):
    for j in range(W-1):
        a = A[i+1][j+1]
        for k in range(NUM):
            if DP[i+1][j][k] == 1:
                if 0 <= a-k < NUM:
                    DP[i+1][j+1][a-k] = 1
                if 0 <= k-a < NUM:
                    DP[i+1][j+1][k-a] = 1
                if 0 <= k+a < NUM:
                    DP[i+1][j+1][k+a] = 1
            if DP[i][j+1][k] == 1:
                if 0 <= a-k < NUM:
                    DP[i+1][j+1][a-k] = 1
                if 0 <= k-a < NUM:
                    DP[i+1][j+1][k-a] = 1
                if 0 <= k+a < NUM:
                    DP[i+1][j+1][k+a] = 1

print(DP[-1][-1].index(1))

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().split())
A = [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):
        A[i][j] = abs(A[i][j]-b[j])
NUM = 200
DP = [[[0]*NUM for i in range(W)]for j in range(H)]
DP[0][0][A[0][0]] = 1
i = 0
for j in range(W-1):
    a = A[i][j+1]
    for k in range(NUM):
        if DP[i][j][k] == 1:
            if 0 <= a-k < NUM:
                DP[i][j+1][a-k] = 1
            if 0 <= k-a < NUM:
                DP[i][j+1][k-a] = 1
            if 0 <= k+a < NUM:
                DP[i][j+1][k+a] = 1
j = 0
for i in range(H-1):
    a = A[i+1][j]
    for k in range(NUM):
        if DP[i][j][k] == 1:
            if 0 <= a-k < NUM:
                DP[i+1][j][a-k] = 1
            if 0 <= k-a < NUM:
                DP[i+1][j][k-a] = 1
            if 0 <= k+a < NUM:
                DP[i+1][j][k+a] = 1
for i in range(H-1):
    for j in range(W-1):
        a = A[i+1][j+1]
        for k in range(NUM):
            if DP[i+1][j][k] == 1:
                if 0 <= a-k < NUM:
                    DP[i+1][j+1][a-k] = 1
                if 0 <= k-a < NUM:
                    DP[i+1][j+1][k-a] = 1
                if 0 <= k+a < NUM:
                    DP[i+1][j+1][k+a] = 1
            if DP[i][j+1][k] == 1:
                if 0 <= a-k < NUM:
                    DP[i+1][j+1][a-k] = 1
                if 0 <= k-a < NUM:
                    DP[i+1][j+1][k-a] = 1
                if 0 <= k+a < NUM:
                    DP[i+1][j+1][k+a] = 1

print(DP[-1][-1].index(1))

Submission status: Wrong Answer

Solution code:
H,W = map(int,input().split())
A = [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):
        A[i][j] = abs(A[i][j]-b[j])
NUM = 100
DP = [[[0]*NUM for i in range(W)]for j in range(H)]
DP[0][0][A[0][0]] = 1
i = 0
for j in range(W-1):
    a = A[i][j+1]
    for k in range(NUM):
        if DP[i][j][k] == 1:
            if 0 <= a-k < NUM:
                DP[i][j+1][a-k] = 1
            if 0 <= k-a < NUM:
                DP[i][j+1][k-a] = 1
            if 0 <= k+a < NUM:
                DP[i][j+1][k+a] = 1
j = 0
for i in range(H-1):
    a = A[i+1][j]
    for k in range(NUM):
        if DP[i][j][k] == 1:
            if 0 <= a-k < NUM:
                DP[i+1][j][a-k] = 1
            if 0 <= k-a < NUM:
                DP[i+1][j][k-a] = 1
            if 0 <= k+a < NUM:
                DP[i+1][j][k+a] = 1
for i in range(H-1):
    for j in range(W-1):
        a = A[i+1][j+1]
        for k in range(NUM):
            if DP[i+1][j][k] == 1:
                if 0 <= a-k < NUM:
                    DP[i+1][j+1][a-k] = 1
                if 0 <= k-a < NUM:
                    DP[i+1][j+1][k-a] = 1
                if 0 <= k+a < NUM:
                    DP[i+1][j+1][k+a] = 1
            if DP[i][j+1][k] == 1:
                if 0 <= a-k < NUM:
                    DP[i+1][j+1][a-k] = 1
                if 0 <= k-a < NUM:
                    DP[i+1][j+1][k-a] = 1
                if 0 <= k+a < NUM:
                    DP[i+1][j+1][k+a] = 1

print(DP[-1][-1].index(1))

Submission status: Wrong Answer

Solution code:
from itertools import product

def offset(a, b):
    result = 0
    for i in range(b+1):
        if (a >> i) & 1:
            result |= (1 << b) >> i
    return result

H, W = map(int, input().split())
diff = [list(map(int, input().split())) for _ in range(H)]
M = 80
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])
dp = [[0]*W for _ in range(H)]
dp[0][0] = 1 << diff[0][0]
for i, j in product(range(H), range(W)):
    if (i, j) == (H-1, W-1): break
    if i+1 < H:
        k = diff[i+1][j]
        dp[i+1][j] = (dp[i][j] >> k) | (dp[i][j] << k) | (offset(dp[i][j], k))
    if j+1 < W:
        k = diff[i][j+1]
        dp[i][j+1] = (dp[i][j] >> k) | (dp[i][j] << k) | (offset(dp[i][j], k))
ans = dp[H-1][W-1]
print((ans & -ans).bit_length() - 1)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
import sys


def solve(H: int, W: int, A: "List[List[int]]", B: "List[List[int]]"):

    # dp = [[0]*160 for _ in range(H+W-1)]
    ab = [[0]*W for _ in range(H)]

    for h in range(H):
        for w in range(W):
            ab[h][w] = abs(A[h][w]-B[h][w])
    
    setlist = [set() for _ in range(H+W-1)]
    setlist[0].add(ab[0][0])
    for i in range(1,H+W-1):
        h = i
        w = 0
        while h >= 0:
            if h >= H or w>=W:
                h -= 1
                w += 1
                continue
            for num in setlist[i-1]:
                if abs(num+ab[h][w]) < 160:
                    setlist[i].add(abs(num+ab[h][w]))
                setlist[i].add(abs(num-ab[h][w]))
            h -= 1
            w += 1
    
    print(min(setlist[H+W-2]))
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    A = [[int(next(tokens)) for _ in range(W)] for _ in range(H)]  # type: "List[List[int]]"
    B = [[int(next(tokens)) for _ in range(W)] for _ in range(H)]  # type: "List[List[int]]"
    solve(H, W, A, B)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
H, W = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
B = [list(map(int, input().split())) for _ in range(H)]
X = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]
dp = [[[0] * (161) for _ in range(W)] for _ in range(H)]
dp[0][0][X[0][0]] = 1
for i in range(H):
  for j in range(W):
    for k in range(161):
      if dp[i][j][k] == 0: continue
      if i + 1 < H:
        u = k + X[i + 1][j]
        v = abs(k - X[i + 1][j])
        if u <= 160: dp[i + 1][j][u] += dp[i][j][k]
        if v <= 160: dp[i + 1][j][v] += dp[i][j][k]
      if j + 1 < W:
        u = k + X[i][j + 1]
        v = abs(k - X[i][j + 1])
        if u <= 160: dp[i][j + 1][u] += dp[i][j][k]
        if v <= 160: dp[i][j + 1][v] += dp[i][j][k]
res = float("inf")
#print(dp[1][1])
for k in range(81):
  if dp[-1][-1][k]: res = min(k, res)
print(res)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
H, W = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
B = [list(map(int, input().split())) for _ in range(H)]
X = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]
dp = [[[0] * (161) for _ in range(W)] for _ in range(H)]
dp[0][0][X[0][0]] = 1
for i in range(H):
  for j in range(W):
    for k in range(161):
      if dp[i][j][k] == 0: continue
      if i + 1 < H:
        u = k + X[i + 1][j]
        v = abs(k - X[i + 1][j])
        if u <= 160: dp[i + 1][j][u] += dp[i][j][k]
        if v <= 160: dp[i + 1][j][v] += dp[i][j][k]
      if j + 1 < W:
        u = k + X[i][j + 1]
        v = abs(k - X[i][j + 1])
        if u <= 160: dp[i][j + 1][u] += dp[i][j][k]
        if v <= 160: dp[i][j + 1][v] += dp[i][j][k]
res = 80
#print(dp[1][1])
for k in range(81):
  if dp[-1][-1][k]: res = min(k, res)
print(res)

Submission status: Wrong Answer

Solution code:
H,W = map(int, input().split())
As = []
for i in range(H):
    As.append(list(map(int, input().split())))
diffs = []
for i in range(H):
    bs = list(map(int, input().split()))
    diffs.append([a-b for a,b in zip(As[i],bs)])

# 左右の対称性から、がんばったら80を超えることはなくないか？
M = 161

# (i,j)において、偏りがkになることがあるか
diff_s = abs(diffs[0][0])
# 初期値
dp = [[[False]*M for i in range(W)] for j in range(H)]
dp[0][0][diff_s] = True
# もらうDP
for i in range(H):
    for j in range(W):
        # 行方向
        if i+1 < H:
            d = abs(diffs[i+1][j])
            for k in range(M):
                x = dp[i][j][k]
                if x:
                    if d + k < M:
                        dp[i+1][j][d+k] = x
                    dp[i+1][j][abs(d-k)] = x
        # 列方向
        if j+1 < W:
            d = abs(diffs[i][j+1])
            for k in range(M):
                x = dp[i][j][k]
                if x:                
                    if d + k < M:                  
                        dp[i][j+1][d+k] = x
                    dp[i][j+1][abs(d-k)] = x
        # print(i,j,diffs[i][j],dp)

ans = 0
for i,b in enumerate(dp[i][j]):
    if b:
        print(i)
        exit()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
H, W = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
B = [list(map(int, input().split())) for _ in range(H)]
X = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]
dp = [[[0] * 81 for _ in range(W)] for _ in range(H)]
dp[0][0][X[0][0]] = 1
for i in range(H):
  for j in range(W):
    for k in range(81):
      if i + 1 < H:
        u = k + X[i + 1][j]
        v = abs(k - X[i + 1][j])
        if u <= 80: dp[i + 1][j][u] += dp[i][j][k]
        if v <= 80: dp[i + 1][j][v] += dp[i][j][k]
      if j + 1 < W:
        u = k + X[i][j + 1]
        v = abs(k - X[i][j + 1])
        if u <= 80: dp[i][j + 1][u] += dp[i][j][k]
        if v <= 80: dp[i][j + 1][v] += dp[i][j][k]
res = 80
#print(dp[1][1])
for k in range(81):
  if dp[-1][-1][k]: res = min(k, res)
print(res)

Submission status: Wrong Answer

Solution code:
H,W = map(int, input().split())
As = []
for i in range(H):
    As.append(list(map(int, input().split())))
diffs = []
for i in range(H):
    bs = list(map(int, input().split()))
    diffs.append([a-b for a,b in zip(As[i],bs)])

# 左右の対称性から、がんばったら80を超えることはなくないか？
M = 81

# (i,j)において、偏りがkになることがあるか
diff_s = abs(diffs[0][0])
# 初期値
dp = [[[False]*M for i in range(W)] for j in range(H)]
dp[0][0][diff_s] = True
# もらうDP
for i in range(H):
    for j in range(W):
        # 行方向
        if i+1 < H:
            d = abs(diffs[i+1][j])
            for k in range(M):
                x = dp[i][j][k]
                if x:
                    if d + k < M:
                        dp[i+1][j][d+k] = x
                    dp[i+1][j][abs(d-k)] = x
        # 列方向
        if j+1 < W:
            d = abs(diffs[i][j+1])
            for k in range(M):
                x = dp[i][j][k]
                if x:                
                    if d + k < M:                  
                        dp[i][j+1][d+k] = x
                    dp[i][j+1][abs(d-k)] = x
        # print(i,j,diffs[i][j],dp)

ans = 0
for i,b in enumerate(dp[i][j]):
    if b:
        print(i)
        exit()

Submission status: Wrong Answer

Solution code:
def solve():
    import numpy as np
    import sys

    input = sys.stdin.readline

    MaxBias = 80

    h, w = map(int, input().split())
    d = [list(map(int, input().split())) for _ in range(h * 2)]
    for r in range(h):
        for c in range(w):
            d[r][c] = abs(d[r][c] - d[r + h][c])

    dp = np.zeros(shape=(h, w, MaxBias + 1))
    dp[0, 0, d[0][0]] = 1

    for r in range(1, h):
        for bias in range(MaxBias + 1):
            if dp[r - 1, 0, bias]:
                for new_bias in abs(bias + d[r][0]), abs(bias - d[r][0]):
                    if 0 <= new_bias <= MaxBias:
                        dp[r, 0, new_bias] = 1

    for c in range(1, w):
        for bias in range(MaxBias + 1):
            if dp[0, c - 1, bias]:
                for new_bias in abs(bias + d[0][c]), abs(bias - d[0][c]):
                    if 0 <= new_bias <= MaxBias:
                        dp[0, c, new_bias] = 1

    for r in range(1, h):
        for c in range(1, w):
            for bias in range(MaxBias + 1):
                if dp[r - 1, c, bias] or dp[r, c - 1, bias]:
                    for new_bias in abs(bias + d[r][c]), abs(bias - d[r][c]):
                        if 0 <= new_bias <= MaxBias:
                            dp[r, c, new_bias] = 1

    for bias in range(MaxBias + 1):
        if dp[h - 1, w - 1, bias]:
            print(bias)
            return


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
H,W = map(int, input().split())
As = []
for i in range(H):
    As.append(list(map(int, input().split())))
diffs = []
for i in range(H):
    bs = list(map(int, input().split()))
    diffs.append([a-b for a,b in zip(As[i],bs)])
# max_d = 0
# for i in range(H):
#     for j in range(W):
#         max_d = max(max_d,abs(diffs[i][j]))

# M = max_d*(H+W)

# 左右の対称性から、がんばったら80を超えることはなくないか？
M = 160

# (i,j)において、偏りがkになることがあるか
diff_s = abs(diffs[0][0])
# 初期値
dp = [[[False]*M for i in range(W)] for j in range(H)]
dp[0][0][diff_s] = True
# もらうDP
for i in range(H):
    for j in range(W):
        # 行方向
        if i+1 < H:
            d = abs(diffs[i+1][j])
            for k in range(M):
                x = dp[i][j][k]
                if x:
                    if d + k < M:
                        dp[i+1][j][d+k] = x
                    dp[i+1][j][abs(d-k)] = x
        # 列方向
        if j+1 < W:
            d = abs(diffs[i][j+1])
            for k in range(M):
                x = dp[i][j][k]
                if x:                
                    if d + k < M:                  
                        dp[i][j+1][d+k] = x
                    dp[i][j+1][abs(d-k)] = x
        # print(i,j,diffs[i][j],dp)

ans = 0
for i,b in enumerate(dp[i][j]):
    if b:
        print(i)
        exit()

Submission status: Wrong Answer

Solution code:
def solve():
    import numpy as np
    from operator import add
    import sys

    input = sys.stdin.readline

    MaxBias = 80

    h, w = map(int, input().split())
    d = [list(map(int, input().split())) for _ in range(h * 2)]
    for r in range(h):
        for c in range(w):
            d[r][c] = abs(d[r][c] - d[r + h][c])

    dp = np.zeros(shape=(h, w, MaxBias + 1))
    for bias in (-d[0][0], d[0][0]):
        dp[0, 0, bias] = 1

    for r in range(1, h):
        for bias in range(MaxBias + 1):
            if dp[r - 1, 0, bias]:
                for new_bias in abs(bias + d[r][0]), abs(bias - d[r][0]):
                    if 0 <= new_bias <= MaxBias:
                        dp[r, 0, new_bias] = 1

    for c in range(1, w):
        for bias in range(MaxBias + 1):
            if dp[0, c - 1, bias]:
                for new_bias in abs(bias + d[0][c]), abs(bias - d[0][c]):
                    if 0 <= new_bias <= MaxBias:
                        dp[0, c, new_bias] = 1

    for r in range(1, h):
        for c in range(1, w):
            for bias in range(MaxBias + 1):
                if dp[r - 1, c, bias] or dp[r, c - 1, bias]:
                    for new_bias in abs(bias + d[r][c]), abs(bias - d[r][c]):
                        if 0 <= new_bias <= MaxBias:
                            dp[r, c, new_bias] = 1

    for bias in range(MaxBias + 1):
        if dp[h - 1, w - 1, bias]:
            print(bias)
            return


if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs = sys.stdin.buffer.readlines


# mod=10**9+7
# rstrip().decode('utf-8')
# map(int,input().split())
# import numpy as np

def main():
	h, w = map(int, input().split())
	
	G = [[0] * w for i in range(h)]
	A = [0]*h
	B = [0]*h
	
	for i in range(h):
		A[i] = list(map(int, input().split()))
	for i in range(h):
		B[i] = list(map(int, input().split()))
	
	for i in range(h):
		for j in range(w):
			G[i][j]=abs(A[i][j]-B[i][j])

	M = (h + w) * 80
	
	li = [[[0] * M for i in range(w)] for j in range(h)]
	
	li[0][0][G[0][0]] = 1
	for i in range(h):
		for j in range(w):
			for k in range(M):
				
				if li[i][j][k] == 1:
					if i < h - 1:
						li[i + 1][j][abs(k - G[i][j])] = 1
						li[i + 1][j][abs(k + G[i][j])] = 1
					if j < w - 1:
						li[i][j + 1][abs(k - G[i][j])] = 1
						li[i][j + 1][abs(k + G[i][j])] = 1
	
	for i in range(M):
		if li[h - 1][w - 1][i] == 1:
			print(i - 1)
			exit(0)


if __name__ == "__main__":
	main()