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
MI=lambda:map(int,input().split())
LI=lambda:list(map(int,input().split()))

H,W=MI()
A=[LI() for _ in [0]*H]
B=[LI() for _ in [0]*H]
    
G=[[set() for i in range(W)] for j in range(H)]
dif=A[0][0]-B[0][0]
G[0][0].add(0)

M=80*(H+W)

for h in range(H):
    for w in range(W):
        d=A[h][w]-B[h][w]
        for a in G[h][w]:
            if h!=H-1:
                G[h+1][w].add(abs(a+d))
                G[h+1][w].add(abs(a-d))
            if w!=W-1:
                G[h][w+1].add(abs(a+d))
                G[h][w+1].add(abs(a-d))
    
res=set()
for a in G[H-1][W-1]:
    res.add(a)
d=A[H-1][W-1]-B[H-1][W-1]
for a in G[H-1][W-1]:
    res.add(abs(a+d))
    res.add(abs(a-d))

res=sorted(list(res))
print(min(res))

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


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

    RANGE = 80 * 80 + 5
    ORIGIN = RANGE // 2

    dp = [[[0] * RANGE for _ in range(W)] for _ in range(H)]
    dp[0][0][ORIGIN + (A[0][0] - B[0][0])] = 1
    dp[0][0][ORIGIN + (B[0][0] - A[0][0])] = 1

    for h in range(H):
        for w in range(W):
            a, b = A[h][w], B[h][w]
            if w - 1 >= 0:
                for i in range(RANGE):
                    if dp[h][w - 1][i]:
                        if 0 <= i + (a - b) < RANGE:
                            dp[h][w][i + (a - b)] = dp[h][w - 1][i]
                        if 0 <= i + (b - a) < RANGE:
                            dp[h][w][i + (b - a)] = dp[h][w - 1][i]
            if h - 1 >= 0:
                for i in range(RANGE):
                    if dp[h - 1][w][i]:
                        if 0 <= i + (a - b) < RANGE:
                            dp[h][w][i + (a - b)] = dp[h - 1][w][i]
                        if 0 <= i + (b - a) < RANGE:
                            dp[h][w][i + (b - a)] = dp[h - 1][w][i]

    res = f_inf
    for i in range(RANGE):
        if dp[-1][-1][i]:
            res = min(res, abs(ORIGIN - i))
    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


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

    RANGE = 200
    ORIGIN = RANGE // 2

    dp = [[[0] * RANGE for _ in range(W)] for _ in range(H)]
    dp[0][0][ORIGIN + (A[0][0] - B[0][0])] = 1
    dp[0][0][ORIGIN + (B[0][0] - A[0][0])] = 1

    for h in range(H):
        for w in range(W):
            a, b = A[h][w], B[h][w]
            if w - 1 >= 0:
                for i in range(RANGE):
                    if dp[h][w - 1][i]:
                        if 0 <= i + (a - b) < RANGE:
                            dp[h][w][i + (a - b)] = dp[h][w - 1][i]
                        if 0 <= i + (b - a) < RANGE:
                            dp[h][w][i + (b - a)] = dp[h][w - 1][i]
            if h - 1 >= 0:
                for i in range(RANGE):
                    if dp[h - 1][w][i]:
                        if 0 <= i + (a - b) < RANGE:
                            dp[h][w][i + (a - b)] = dp[h - 1][w][i]
                        if 0 <= i + (b - a) < RANGE:
                            dp[h][w][i + (b - a)] = dp[h - 1][w][i]

    res = f_inf
    for i in range(RANGE):
        if dp[-1][-1][i]:
            res = min(res, abs(ORIGIN - i))
    print(res)


if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

    M = 0
    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])
            if M < G[i][j]:
                M = G[i][j]

    M += 10
    dp = [[0] * W for _ in range(H)]
    dp[0][0] = 1 << (M + G[0][0]) | 1 << (M - G[0][0])

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

    ans = INF
    for i in range(2 * M + 2):
        if dp[H - 1][W - 1] & (1 << i) and ans > abs(i - M):
            ans = abs(i - M)

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

    M = 0
    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])
            if M < G[i][j]:
                M = G[i][j]

    M += 10
    dp = [[0] * W for _ in range(H)]
    dp[0][0] = 1 << (M + G[0][0]) | 1 << (M - G[0][0])

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

    ans = INF
    for i in range(2 * M + 2):
        if dp[H - 1][W - 1] & (1 << i) and ans > abs(i - M):
            ans = abs(i - M)

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

    M = 0
    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])
            if M < G[i][j]:
                M = G[i][j]

    dp = [[0] * W for _ in range(H)]
    dp[0][0] = 1 << (M + G[0][0]) | 1 << (M - G[0][0])

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

    ans = INF
    for i in range(2 * M + 2):
        if dp[H - 1][W - 1] & (1 << i) and ans > abs(i - M):
            ans = abs(i - M)

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))


"""
面倒なので差分を考えておく，
この差分を+ or - のどちらで使うかの2択，
合計値を0に近づけたい，
各マスごとに取れる範囲をsetで持ちながら，とやりたいが取れる範囲が多そうな気もする．

絶対値が6400を超える分は持つ必要ないな，ギリ行けるかも?
やっぱり遅いか.
dpだと遷移が遅そうだからsetにしようとしたけど，これも遅い．

dpで取れる値をいっぺんにシフトできれば高速化可能，めっちゃでかい二進数を用意する？

"""
def main():
    mod=10**9+7
    H,W=MI()
    A=[]
    B=[]
    for i in range(H):
        a=LI()
        A.append(a)
    for i in range(H):
        b=LI()
        B.append(b)
        
    AB=[[0]*W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            ab=abs(A[i][j] - B[i][j])
            AB[i][j]=ab
            
    offset = 80*(H+W) #負数があるのでbitをずらす
    
    dp=[[0]*(W) for _ in range(H)]
    
    def calc(a,d):
        # aをdだけ前後にずらす
        return (a<<d) | (a>>d) 
    
    
    dp[0][0]=1<<(offset+AB[0][0]) | 1<<(offset-AB[0][0])
    
    for i in range(H-1):
        for j in range(W-1):
            dp[i][j+1] = dp[i][j+1] | calc(dp[i][j],AB[i][j+1])
            dp[i+1][j] = dp[i+1][j] | calc(dp[i][j],AB[i+1][j])
            
    dp[-1][-1]=dp[-1][-1] | calc(dp[-1][-2],AB[-1][-1])
    dp[-1][-1]=dp[-1][-1] | calc(dp[-2][-1],AB[-1][-1])
    
    ans=10**10
    
    a=dp[-1][-1]
    
    # 正負で対称なので片側だけで良い
    a=a>>offset # ずらしたぶん戻す
    for i in range(offset):
        if a>>i & 1:
            print(i)
            break
        
    # for i in range(H):
    #     for j in range(W):
    #         # print(bin(dp[i][j]>>offset))
    #         print(bin(dp[i][j]))
    #         print()
    
    
        
                    
                
    

main()

Submission status: Wrong Answer

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))


"""
面倒なので差分を考えておく，
この差分を+ or - のどちらで使うかの2択，
合計値を0に近づけたい，
各マスごとに取れる範囲をsetで持ちながら，とやりたいが取れる範囲が多そうな気もする．

絶対値が6400を超える分は持つ必要ないな，ギリ行けるかも?
やっぱり遅いか.
dpだと遷移が遅そうだからsetにしようとしたけど，これも遅い．

dpで取れる値をいっぺんにシフトできれば高速化可能，めっちゃでかい二進数を用意する？

"""
def main():
    mod=10**9+7
    H,W=MI()
    A=[]
    B=[]
    for i in range(H):
        a=LI()
        A.append(a)
    for i in range(H):
        b=LI()
        B.append(b)
        
    AB=[[0]*W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            ab=abs(A[i][j] - B[i][j])
            AB[i][j]=ab
            
    offset = 80*(H+W) #負数があるのでbitをずらす
    
    dp=[[0]*(W) for _ in range(H)]
    
    def calc(a,d):
        # aをdだけ前後にずらす
        return (a<<d) | (a>>d) 
    
    
    dp[0][0]=1<<(offset+AB[0][0]) | 1<<(offset-AB[0][0])
    
    for i in range(H-1):
        for j in range(W-1):
            dp[i][j+1] = dp[i][j+1] | calc(dp[i][j],AB[i][j+1])
            dp[i+1][j] = dp[i][j] | calc(dp[i][j],AB[i+1][j])
            
    dp[-1][-1]=dp[-1][-1] | calc(dp[-1][-2],AB[-1][-1])
    dp[-1][-1]=dp[-1][-1] | calc(dp[-2][-1],AB[-1][-1])
    
    ans=10**10
    
    a=dp[-1][-1]
    
    # 正負で対称なので片側だけで良い
    a=a>>offset # ずらしたぶん戻す
    for i in range(offset):
        if a>>i & 1:
            print(i)
            exit()
    
    
        
                    
                
    

main()

Submission status: Wrong Answer

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

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))


"""
面倒なので差分を考えておく，
この差分を+ or - のどちらで使うかの2択，
合計値を0に近づけたい，
各マスごとに取れる範囲をsetで持ちながら，とやりたいが取れる範囲が多そうな気もする．

絶対値が6400を超える分は持つ必要ないな，ギリ行けるかも?

"""
def main():
    mod=10**9+7
    H,W=MI()
    A=[]
    B=[]
    for i in range(H):
        a=LI()
        A.append(a)
    for i in range(H):
        b=LI()
        B.append(b)
        
    AB=[[0]*W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            ab=abs(A[i][j] - B[i][j])
            AB[i][j]=ab
            
    L=[]
    for i in range(H):
        temp=[]
        for j in range(W):
            temp.append(set())
        L.append(temp)
        

    L[0][0].add(AB[0][0])
    L[0][0].add(-1*AB[0][0])
    
    M=6400
    #配る
    for i in range(H-1):
        for j in range(W-1):
            for k in L[i][j]:
                aa=AB[i+1][j] # 下
                bb=AB[i][j+1] # 右
                if abs(k+aa)<=M:
                    L[i+1][j].add(k+aa)
                if abs(k-aa)<=M:
                    L[i+1][j].add(k-aa)
                if abs(k+bb)<=M:
                    L[i][j+1].add(k+bb)
                if abs(k-bb)<=M:
                    L[i][j+1].add(k-bb)
                    
    a=AB[-1][-1]
    for k in L[-1][-2]:
        # print(k)
        if abs(k+a)<=M:
            L[-1][-1].add(k+a)
        if abs(k-a)<=M:
            L[-1][-1].add(k-a)
    
    # print("--")  
          
    for k in L[-2][-1]:
        # print(k)
        if abs(k+a)<=M:
            L[i+1][j].add(k+a)
        if abs(k-a)<=M:
            L[i+1][j].add(k-a)
            
    ans=M
    for k in L[-1][-1]:
        k2=abs(k)
        ans=min(k2,ans)
        
    print(ans)
        
                    
                
    

main()

Submission status: Wrong Answer

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
from heapq import heappop, heappush
from collections import defaultdict
from collections import Counter
from collections import deque
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())
H, W = map(int, input().split())
A = [0] * H
for i in range(H):
    A[i] = list(map(int, input().split()))
B = [0] * H
for i in range(H):
    B[i] = list(map(int, input().split()))

C = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        C[i][j] = abs(A[i][j] - B[i][j])

MAX = H * W + 1
dp = [[[1000] * W for _ in range(H)] for _ in range(MAX)]
for k in range(MAX):
    for i in range(H):
        for j in range(W):
            if k == 0:
                if i == 0 and j == 0:
                    dp[k][i][j] = C[i][j]
                elif i == 0:
                    dp[k][i][j] = dp[k][i][j - 1] + C[i][j]
                elif j == 0:
                    dp[k][i][j] = dp[k][i - 1][j] + C[i][j]
                else:
                    dp[k][i][j] = min(dp[k][i - 1][j], dp[k][i][j - 1]) + C[i][j]
            else:
                if i == 0 and j == 0:
                    continue
                elif i == 0:
                    dp[k][i][j] = min(abs(dp[k - 1][i][j - 1] - C[i][j]), dp[k - 1][i][j])
                elif j == 0:
                    dp[k][i][j] = min(abs(dp[k - 1][i - 1][j] - C[i][j]), dp[k - 1][i][j])
                else:
                    dp[k][i][j] = min(abs(dp[k - 1][i - 1][j] - C[i][j]), \
                        abs(dp[k - 1][i][j - 1] - C[i][j]), dp[k - 1][i][j])

ans = INF
for k in range(MAX):
    val = dp[k][H - 1][W - 1]
    ans = min(ans, val)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
from heapq import heappop, heappush
from collections import defaultdict
from collections import Counter
from collections import deque
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())
H, W = map(int, input().split())
A = [0] * H
for i in range(H):
    A[i] = list(map(int, input().split()))
B = [0] * H
for i in range(H):
    B[i] = list(map(int, input().split()))

C = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        C[i][j] = abs(A[i][j] - B[i][j])

MAX = H * W + 1
dp = [[[INF] * W for _ in range(H)] for _ in range(MAX)]
for k in range(MAX):
    for i in range(H):
        for j in range(W):
            if k == 0:
                if i == 0 and j == 0:
                    dp[k][i][j] = C[i][j]
                elif i == 0:
                    dp[k][i][j] = dp[k][i][j - 1] + C[i][j]
                elif j == 0:
                    dp[k][i][j] = dp[k][i - 1][j] + C[i][j]
                else:
                    dp[k][i][j] = min(dp[k][i - 1][j], dp[k][i][j - 1]) + C[i][j]
            else:
                if i == 0 and j == 0:
                    continue
                elif i == 0:
                    dp[k][i][j] = min(abs(dp[k - 1][i][j - 1] - C[i][j]), dp[k - 1][i][j])
                elif j == 0:
                    dp[k][i][j] = min(abs(dp[k - 1][i - 1][j] - C[i][j]), dp[k - 1][i][j])
                else:
                    dp[k][i][j] = min(abs(dp[k - 1][i - 1][j] - C[i][j]), \
                        abs(dp[k - 1][i][j - 1] - C[i][j]), dp[k - 1][i][j])

ans = INF
for k in range(MAX):
    val = dp[k][H - 1][W - 1]
    ans = min(ans, val)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
from heapq import heappop, heappush
from collections import defaultdict
from collections import Counter
from collections import deque
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())
H, W = map(int, input().split())
A = [0] * H
for i in range(H):
    A[i] = list(map(int, input().split()))
B = [0] * H
for i in range(H):
    B[i] = list(map(int, input().split()))

C = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        C[i][j] = abs(A[i][j] - B[i][j])

MAX = H * W + 1
dp = [[[INF] * W for _ in range(H)] for _ in range(MAX)]
for k in range(MAX):
    for i in range(H):
        for j in range(W):
            if k == 0:
                if i == 0 and j == 0:
                    dp[k][i][j] = C[i][j]
                elif i == 0:
                    dp[k][i][j] = dp[k][i][j - 1] + C[i][j]
                elif j == 0:
                    dp[k][i][j] = dp[k][i - 1][j] + C[i][j]
                else:
                    dp[k][i][j] = min(dp[k][i - 1][j], dp[k][i][j - 1]) + C[i][j]
            else:
                if i == 0 and j == 0:
                    continue
                elif i == 0:
                    dp[k][i][j] = min(abs(dp[k - 1][i][j - 1] - C[i][j]), dp[k][i][j])
                elif j == 0:
                    dp[k][i][j] = min(abs(dp[k - 1][i - 1][j] - C[i][j]), dp[k][i][j])
                else:
                    dp[k][i][j] = min(abs(dp[k - 1][i - 1][j] - C[i][j]), \
                        abs(dp[k - 1][i][j - 1] - C[i][j]), dp[k][i][j])

ans = INF
for k in range(MAX):
    val = dp[k][H - 1][W - 1]
    ans = min(ans, val)
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
from heapq import heappop, heappush
from collections import defaultdict
from collections import Counter
from collections import deque
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())
H, W = map(int, input().split())
A = [0] * H
for i in range(H):
    A[i] = list(map(int, input().split()))
B = [0] * H
for i in range(H):
    B[i] = list(map(int, input().split()))

C = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        C[i][j] = abs(A[i][j] - B[i][j])

MAX = H * W
dp = [[[INF] * W for _ in range(H)] for _ in range(MAX)]
for k in range(MAX):
    for i in range(H):
        for j in range(W):
            if k == 0:
                if i == 0 and j == 0:
                    dp[k][i][j] = C[i][j]
                elif i == 0:
                    dp[k][i][j] = dp[k][i][j - 1] + C[i][j]
                elif j == 0:
                    dp[k][i][j] = dp[k][i - 1][j] + C[i][j]
                else:
                    dp[k][i][j] = min(dp[k][i - 1][j], dp[k][i][j - 1]) + C[i][j]
            else:
                if i == 0 and j == 0:
                    continue
                elif i == 0:
                    dp[k][i][j] = min(abs(dp[k - 1][i][j - 1] - C[i][j]), dp[k][i][j])
                elif j == 0:
                    dp[k][i][j] = min(abs(dp[k - 1][i - 1][j] - C[i][j]), dp[k][i][j])
                else:
                    dp[k][i][j] = min(abs(dp[k - 1][i - 1][j] - C[i][j]), \
                        abs(dp[k - 1][i][j - 1] - C[i][j]), dp[k][i][j])

ans = INF
for k in range(MAX):
    val = dp[k][H - 1][W - 1]
    ans = min(ans, val)
print(ans)

Submission status: Wrong Answer

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*10
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(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: Wrong Answer

Solution code:
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
sys.setrecursionlimit(10**6)
# INF =  float("inf")
INF = 10**18
import bisect
import statistics
mod = 10**9+7
# mod = 998244353

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

A = [[0]*(W+1)]
for i in range(H):
    A.append([0] + list(map(int, input().split())))

B = [[0]*(W+1)]
for i in range(H):
    B.append([0] + list(map(int, input().split())))

C = [[0 for j in range(W+1)] for i in range(H+1)]
for i in range(H+1):
    for j in range(W+1):
        C[i][j] = A[i][j] - B[i][j]

dp = [[set() for j in range(W+1)] for i in range(H+1)]
dp[0][1] = set([0])
dp[1][0] = set([0])
for i in range(1,H+1):
    for j in range(1,W+1):
        for k in list(dp[i-1][j]):
            if k + C[i][j] <= 1000 and k + C[i][j] >= -1000:
                dp[i][j].add(k + C[i][j])
            if k - C[i][j] <= 1000 and k - C[i][j] >= -1000:
                dp[i][j].add(k - C[i][j])
        for k in list(dp[i][j-1]):
            if k + C[i][j] <= 1000 and k + C[i][j] >= -1000:
                dp[i][j].add(k + C[i][j])
            if k - C[i][j] <= 1000 and k - C[i][j] >= -1000:
                dp[i][j].add(k - C[i][j])

ans = list(dp[H][W])
ans = [abs(i) for i in ans]
print(sorted(ans)[0])

Submission status: Wrong Answer

Solution code:
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
sys.setrecursionlimit(10**6)
# INF =  float("inf")
INF = 10**18
import bisect
import statistics
mod = 10**9+7
# mod = 998244353

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

A = [[0]*(W+1)]
for i in range(H):
    A.append([0] + list(map(int, input().split())))

B = [[0]*(W+1)]
for i in range(H):
    B.append([0] + list(map(int, input().split())))

C = [[0 for j in range(W+1)] for i in range(H+1)]
for i in range(H+1):
    for j in range(W+1):
        C[i][j] = A[i][j] - B[i][j]

dp = [[set() for j in range(W+1)] for i in range(H+1)]
dp[0][1] = set([0])
dp[1][0] = set([0])
for i in range(1,H+1):
    for j in range(1,W+1):
        for k in list(dp[i-1][j]):
            if k + C[i][j] <= 160 and k + C[i][j] >= -160:
                dp[i][j].add(k + C[i][j])
            if k - C[i][j] <= 160 and k - C[i][j] >= -160:
                dp[i][j].add(k - C[i][j])
        for k in list(dp[i][j-1]):
            if k + C[i][j] <= 160 and k + C[i][j] >= -160:
                dp[i][j].add(k + C[i][j])
            if k - C[i][j] <= 160 and k - C[i][j] >= -160:
                dp[i][j].add(k - C[i][j])

ans = list(dp[H][W])
ans = [abs(i) for i in ans]
print(sorted(ans)[0])

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
A=[]
B=[]
for i in range(h):
    A.append([int(i) for i in input().split()])
for i in range(h):
    B.append([int(i) for i in input().split()])

for i in range(h):
    A[i]=[abs(A[i][q]-B[i][q]) for q in range(w)]
    

DP=[[[0 for i in range(3201)] for q in range(w)] for r in range(h)]
DP[0][0][A[0][0]]=1

for i in range(h):
    for q in range(w):
        now=DP[i][q]
        if i<h-1:
            nex=DP[i+1][q]
            for r in range(3201):
                if now[r]==1:
                    nxsc=A[i+1][q]
                    nex[abs(r-nxsc)]=1
                    if r+nxsc<3201:
                        nex[r+nxsc]=1
                        
        if q<w-1:
            nex=DP[i][q+1]
            for r in range(3201):
                if now[r]==1:
                    nxsc=A[i][q+1]
                    nex[abs(r-nxsc)]=1
                    if r+nxsc<3201:
                        nex[r+nxsc]=1

las=DP[-1][-1]
ans=3201
for i in range(3200,-1,-1):
    if las[i]==1:
        ans=i
print(ans)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
A=[]
B=[]
for i in range(h):
    A.append([int(i) for i in input().split()])
for i in range(h):
    B.append([int(i) for i in input().split()])

for i in range(h):
    A[i]=[abs(A[i][q]-B[i][q]) for q in range(w)]
    

DP=[[[0 for i in range(3201)] for q in range(w)] for r in range(h)]
DP[0][0][A[0][0]]=1

for i in range(h):
    for q in range(w):
        now=DP[i][q]
        if i<h-1:
            nex=DP[i+1][q]
            for r in range(3201):
                if now[r]==1:
                    nxsc=A[i+1][q]
                    nex[abs(r-nxsc)]=1
                    if r+nxsc<81:
                        nex[r+nxsc]=1
                        
        if q<w-1:
            nex=DP[i][q+1]
            for r in range(3201):
                if now[r]==1:
                    nxsc=A[i][q+1]
                    nex[abs(r-nxsc)]=1
                    if r+nxsc<81:
                        nex[r+nxsc]=1

las=DP[-1][-1]
ans=3201
for i in range(3200,-1,-1):
    if las[i]==1:
        ans=i
print(ans)

Submission status: Wrong Answer

Solution code:
h,w=map(int,input().split())
A=[]
B=[]
for i in range(h):
    A.append([int(i) for i in input().split()])
for i in range(h):
    B.append([int(i) for i in input().split()])

for i in range(h):
    A[i]=[abs(A[i][q]-B[i][q]) for q in range(w)]
    

DP=[[[0 for i in range(81)] for q in range(w)] for r in range(h)]
DP[0][0][A[0][0]]=1

for i in range(h):
    for q in range(w):
        now=DP[i][q]
        if i<h-1:
            nex=DP[i+1][q]
            for r in range(81):
                if now[r]==1:
                    nxsc=A[i+1][q]
                    nex[abs(r-nxsc)]=1
                    if r+nxsc<81:
                        nex[r+nxsc]=1
                        
        if q<w-1:
            nex=DP[i][q+1]
            for r in range(81):
                if now[r]==1:
                    nxsc=A[i][q+1]
                    nex[abs(r-nxsc)]=1
                    if r+nxsc<81:
                        nex[r+nxsc]=1

las=DP[-1][-1]
ans=81
for i in range(80,-1,-1):
    if las[i]==1:
        ans=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)]
b = [list(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)]
dp = [[0] * (3000 * (w + 1)) for _ in range(h + 1)]
dp[1][0] = 1
dp[0][3000] = 1
for i in range(1, h + 1):
    for j in range(1, w + 1):
        x = c[i - 1][j - 1]
        for k in range(3000):
            y = 0
            if k + x <= 2999:
                dp[i][3000 * j + k] |= (dp[i - 1][3000 * j + k + x] | dp[i][3000 * (j - 1) + k + x])
            dp[i][3000 * j + k] |= (dp[i - 1][3000 * j + abs(k - x)] | dp[i][3000 * (j - 1) + abs(k - x)])
for i in range(3000):
    if dp[h][3000 * w + i]:
        ans = i
        print(ans)
        exit()

Submission status: Wrong Answer

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)]
c = [[abs(a[i][j] - b[i][j]) for j in range(w)] for i in range(h)]
dp = [[0] * (1000 * (w + 1)) for _ in range(h + 1)]
dp[1][0] = 1
dp[0][1000] = 1
for i in range(1, h + 1):
    for j in range(1, w + 1):
        x = c[i - 1][j - 1]
        for k in range(1000):
            y = 0
            if k + x <= 999:
                dp[i][1000 * j + k] |= (dp[i - 1][1000 * j + k + x] | dp[i][1000 * (j - 1) + k + x])
            dp[i][1000 * j + k] |= (dp[i - 1][1000 * j + abs(k - x)] | dp[i][1000 * (j - 1) + abs(k - x)])
for i in range(1000):
    if dp[h][1000 * w + i]:
        ans = i
        print(ans)
        exit()

Submission status: Wrong Answer

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)]
c = [[abs(a[i][j] - b[i][j]) for j in range(w)] for i in range(h)]
dp = [[0] * (161 * (w + 1)) for _ in range(h + 1)]
dp[1][0] = 1
dp[0][161] = 1
for i in range(1, h + 1):
    for j in range(1, w + 1):
        x = c[i - 1][j - 1]
        for k in range(161):
            y = 0
            if k + x <= 160:
                dp[i][161 * j + k] |= (dp[i - 1][161 * j + k + x] | dp[i][161 * j + k + x - 161])
            dp[i][161 * j + k] |= (dp[i - 1][161 * j + abs(k - x)] | dp[i][161 * j + abs(k - x) - 161])
for i in range(161):
    if dp[h][161 * w + i]:
        ans = i
        print(ans)
        exit()