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

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    h, w = list(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)]
    dp[0][0] = 1 << abs(a[0][0] - b[0][0])

    from itertools import product

    for row, col in product(range(h), range(w)):
        diff_cur = abs(a[row][col] - b[row][col])
        prevset = 0

        if row - 1 >= 0:
            prevset |= dp[row - 1][col]
        if col - 1 >= 0:
            prevset |= dp[row][col - 1]

        dp[row][col] |= (prevset << diff_cur)
        dp[row][col] |= (prevset >> diff_cur)

        if diff_cur > 0:
            masked = prevset & ((1 << (diff_cur + 1)) - 1)
            maskedbit = format(masked, "b").zfill(diff_cur)
            rev = maskedbit[::-1]
            dp[row][col] |= (int(rev, base=2)<<1)

    for i in range(80 * 80 + 1):
        if dp[h - 1][w - 1] >> i & 1:
            print(i)
            sys.exit()


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    h, w = list(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)]
    dp[0][0] = 1 << abs(a[0][0] - b[0][0])

    from itertools import product

    for row, col in product(range(h), range(w)):
        diff_cur = abs(a[row][col] - b[row][col])
        prevset = 0

        if row - 1 >= 0:
            prevset |= dp[row - 1][col]
        if col - 1 >= 0:
            prevset |= dp[row][col - 1]

        dp[row][col] |= (prevset << diff_cur)
        dp[row][col] |= (prevset >> diff_cur)

        masked = prevset & ((1 << (diff_cur + 1)) - 1)
        maskedbit = format(masked, "b").zfill(diff_cur)
        rev = maskedbit[::-1]
        dp[row][col] |= (int(rev, base=2) << 1)

    for i in range(80 * 80 + 1):
        if dp[h - 1][w - 1] >> i & 1:
            print(i)
            sys.exit()


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(300000)

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


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

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

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(300000)

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


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

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

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

Submission status: Wrong Answer

Solution code:
def main():
    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)]
    n = 1200
    m = n//2
    dp = [[[False]*n for j in range(w)] for i in range(h)]
    dp[0][0][m+abs(a[0][0] - b[0][0])] = True
    dp[0][0][m-abs(a[0][0] - b[0][0])] = True
    for i in range(h):
        for j in range(w):
            x = abs(a[i][j] - b[i][j])
            if 0 < i and 0 < h:
                for k in range(n):
                    if dp[i-1][j][k] or dp[i][j-1][k]:
                        if x <= k:
                            dp[i][j][k-x] = True
                        if k < n-x:
                            dp[i][j][k+x] = True
            elif 0 < i:
                for k in range(n):
                    if dp[i-1][j][k]:
                        if x <= k:
                            dp[i][j][k-x] = True
                        if k < n-x:
                            dp[i][j][k+x] = True
            elif 0 < j:
                for k in range(n):
                    if dp[i][j-1][k]:
                        if x <= k:
                            dp[i][j][k-x] = True
                        if k < n-x:
                            dp[i][j][k+x] = True
    ans = float("inf")
    for i in range(m-80-1, m+80+2):
        if dp[h-1][w-1][i]:
            ans = min(ans, abs(m-i))
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
def main():
    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)]
    n = 1500
    m = n//2
    dp = [[[False]*n for j in range(w)] for i in range(h)]
    dp[0][0][m+abs(a[0][0] - b[0][0])] = True
    dp[0][0][m-abs(a[0][0] - b[0][0])] = True
    for i in range(h):
        for j in range(w):
            x = abs(a[i][j] - b[i][j])
            if 0 < i and 0 < h:
                for k in range(x, n-x):
                    if dp[i-1][j][k] or dp[i][j-1][k]:
                        dp[i][j][k-x] = True
                        dp[i][j][k+x] = True
            elif 0 < i:
                for k in range(x, n-x):
                    if dp[i-1][j][k]:
                        dp[i][j][k-x] = True
                        dp[i][j][k+x] = True
            elif 0 < j:
                for k in range(x, n-x):
                    if dp[i][j-1][k]:
                        dp[i][j][k-x] = True
                        dp[i][j][k+x] = True
    ans = float("inf")
    for i in range(n):
        if dp[h-1][w-1][i]:
            ans = min(ans, abs(m-i))
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import copy
def main():
    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 = [[0]*w for _ in range(h)]
    for i in reversed(range(h)):
        for j in reversed(range(w)):
            m = 0
            if i < h-1:
                m = max(m, c[i+1][j])
            if j < w-1:
                m = max(m, c[i][j+1])
            c[i][j] = abs(a[i][j] - b[i][j]) + m
    dp = [[set() for j in range(w)] for i in range(h)]
    dp[0][0].add(abs(a[0][0] - b[0][0]))
    for i in range(h):
        for j in range(w):
            x = abs(a[i][j] - b[i][j])
            for v in dp[i][j]:
                if i < h-1:
                    if v + x <= c[i+1][j] + 80:
                        dp[i+1][j].add(v + x)
                    dp[i+1][j].add(abs(v - x))
                if j < w-1:
                    if v + x <= c[i][j+1] + 80:
                        dp[i][j+1].add(v + x)
                    dp[i][j+1].add(abs(v - x))
    ans = float("inf")
    x = abs(a[h-1][w-1] - b[h-1][w-1])
    for v in dp[h-1][w-1]:
        ans = min(ans, abs(v-x))
    print(ans)
#    for i in range(h):
#        print(dp[i])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import copy
def main():
    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 = [[0]*w for _ in range(h)]
    for i in reversed(range(h)):
        for j in reversed(range(w)):
            c[i][j] = abs(a[i][j] - b[i][j])
            if i < h-1:
                c[i][j] += c[i+1][j]
            if j < w-1:
                c[i][j] += c[i][j+1]
    dp = [[set() for j in range(w)] for i in range(h)]
    dp[0][0].add(abs(a[0][0] - b[0][0]))
    for i in range(h):
        for j in range(w):
            x = abs(a[i][j] - b[i][j])
            for v in dp[i][j]:
                if i < h-1:
                    if v + x <= c[i+1][j] + 80:
                        dp[i+1][j].add(v + x)
                    dp[i+1][j].add(abs(v - x))
                if j < w-1:
                    if v + x <= c[i][j+1] + 80:
                        dp[i][j+1].add(v + x)
                    dp[i][j+1].add(abs(v - x))
    ans = float("inf")
    for v in dp[h-1][w-1]:
        ans = min(ans, v+abs(a[h-1][w-1] - b[h-1][w-1]), abs(v-abs(a[h-1][w-1] - b[h-1][w-1])))
    print(ans)
#    for i in range(h):
#        print(dp[i])

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import copy
def main():
    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)]
    m = 300
    dp = [[False]*(m+1) for _ in range(w)]
    dp[0][abs(a[0][0] - b[0][0])] = True
    for i in range(1, w):
        x = abs(a[0][i] - b[0][i])
        for j in range(m+1):
            if dp[i-1][j]:
                if j+x <= m:
                    dp[i][j+x] = True
                dp[i][abs(j-x)] = True
    for i in range(1, h):
        for j in range(w):
            buf = [False]*(m+1)
            x = abs(a[i][j] - b[i][j])
            for k in range(m+1):
                f = False
                if j == 0:
                    if dp[0][k]:
                        f = True
                else:
                    if dp[j-1][k] or dp[j][k]:
                        f = True
                if f:
                    if k+x <= m:
                        buf[k+x] = True
                    buf[abs(k-x)] = True
            dp[j] = copy.deepcopy(buf)
    for i in range(m+1):
        if dp[w-1][i]:
            print(i)
            break

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import copy
def main():
    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)]
    m = 200
    dp = [[False]*(m+1) for _ in range(w)]
    dp[0][abs(a[0][0] - b[0][0])] = True
    for i in range(1, w):
        x = abs(a[0][i] - b[0][i])
        for j in range(m+1):
            if dp[i-1][j]:
                if j+x <= m:
                    dp[i][j+x] = True
                dp[i][abs(j-x)] = True
    for i in range(1, h):
        for j in range(w):
            buf = [False]*(m+1)
            x = abs(a[i][j] - b[i][j])
            for k in range(m+1):
                f = False
                if j == 0:
                    if dp[0][k]:
                        f = True
                else:
                    if dp[j-1][k] or dp[j][k]:
                        f = True
                if f:
                    if k+x <= m:
                        buf[k+x] = True
                    buf[abs(k-x)] = True
            dp[j] = copy.deepcopy(buf)
    for i in range(m+1):
        if dp[w-1][i]:
            print(i)
            break

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
grid = []
for i in range(h):
    grid.append(list(map(int,input().split())))
for i in range(h):
    lst = list(map(int,input().split()))
    for j in range(w):
        grid[i][j] = abs(grid[i][j]-lst[j])
lst = [[set() for _ in range(w)] for _ in range(h)]
lst[0][0].add(grid[0][0])
for j in range(1,w):
    for x in lst[0][j-1]:
        lst[0][j].add(abs(grid[0][j]-x))
        if (grid[0][j] + x) <= ((h-1)+(w-1-j))*80:
            lst[0][j].add(grid[0][j]+x)
for i in range(1,h):
    for x in lst[i-1][0]:
        lst[i][0].add(abs(grid[i][0]-x))
        if grid[i][0] + x <= ((h-1-i)+(w-1))*80:
            lst[i][0].add(grid[i][0]+x)
for i in range(1,h):
    for j in range(1,w):
        for x in lst[i][j-1]:
            lst[i][j].add(abs(grid[i][j]-x))
            if grid[i][j] + x <= ((h-1-i)+(w-1-j))*80:
                lst[i][j].add(grid[i][j]+x)
        for x in lst[i-1][j]:
            lst[i][j].add(abs(grid[i][j]-x))
            if grid[i][j] + x <= ((h-1-i)+(w-1-j))*80:
                lst[i][j].add(grid[i][j]+x)
print(min(lst[h-1][w-1]))

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
grid = []
for i in range(h):
    grid.append(list(map(int,input().split())))
for i in range(h):
    lst = list(map(int,input().split()))
    for j in range(w):
        grid[i][j] = abs(grid[i][j]-lst[j])
lst = [[set() for _ in range(w)] for _ in range(h)]
lst[0][0].add(grid[0][0])
for j in range(1,w):
    for x in lst[0][j-1]:
        lst[0][j].add(abs(grid[0][j]-x))
        if grid[0][j] + x <= ((h-1-i)+(w-1-j)*80):
            lst[0][j].add(grid[0][j]+x)
for i in range(1,h):
    for x in lst[i-1][0]:
        lst[i][0].add(abs(grid[i][0]-x))
        if grid[i][0] + x <= ((h-1-i)+(w-1-j)*80):
            lst[i][0].add(grid[i][0]+x)
for i in range(1,h):
    for j in range(1,w):
        for x in lst[i][j-1]:
            lst[i][j].add(abs(grid[i][j]-x))
            if grid[i][j] + x <= 6400:
                lst[i][j].add(grid[i][j]+x)
        for x in lst[i-1][j]:
            lst[i][j].add(abs(grid[i][j]-x))
            if grid[i][j] + x <= ((h-1-i)+(w-1-j)*80):
                lst[i][j].add(grid[i][j]+x)
print(min(lst[h-1][w-1]))

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np

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

H, W = lr()
A = np.array([lr() for _ in range(H)])
B = np.array([lr() for _ in range(H)])
diff = np.abs(A - B).tolist()

X = (H+W) * 80
L = X + X + 1 #真ん中を0とする、最後に-X
dp = [[np.zeros(L, np.bool)] * W for _ in range(H)]
dp[0][0] = np.zeros(L, np.bool)
di = diff[0][0]
dp[0][0][X+di] = 1; dp[0][0][X-di] = 1
for i in range(H):
    for j in range(W):
        if i == 0 and j == 0:
            continue
        di = diff[i][j]
        if i > 0:
            prev = dp[i-1][j]
            dp[i][j][di:] |= prev[:L-di]
            dp[i][j][:L-di] |= prev[di:]
        if j > 0:
            prev = dp[i][j-1]
            dp[i][j][di:] |= prev[:L-di]
            dp[i][j][:L-di] |= prev[di:]

dp = dp[-1][-1][X:]
possible = np.nonzero(dp)[0]
answer = possible.min()
print(answer)

Submission status: Wrong Answer

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

def main():
    inp = lambda: list(map(int, input().split())) 
    h,w=inp()
    a=[inp() for _ in range(h)]
    b=[inp() for _ in range(h)]
    d=[[0]*w for _ in range(h)]
    for i in range(h):
        for j in range(w):
            d[i][j]=abs(a[i][j]-b[i][j])
    zero=80*(h+w)+2
    dp=[[0]*w for _ in range(h)]
    dp[0][0]=1<<(zero+d[0][0])|1<<(zero-d[0][0])
    for i in range(h):
        for j in range(w):
            if i==0:
                if j==0:
                    continue
                else:
                    dp[i][j]=dp[i][j-1]<<d[i][j]|dp[i][j-1]>>d[i][j]
            else:
                if j==0:
                    dp[i][j]=dp[i-1][j]<<d[i][j]|dp[i-1][j]>>d[i][j]
                else:
                    dp[i][j]=dp[i-1][j]<<d[i][j]|dp[i-1][j]>>d[i][j]
                    dp[i][j]=dp[i][j]|(dp[i][j-1]<<d[i][j]|dp[i][j-1]>>d[i][j])
    ans=dp[-1][-1]
    print(dp)
    for i in range(zero):
        if ans>>(zero+i)&1 or ans>>(zero-i)&1:
            print(i)
            exit()

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

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)]
ma = 3000
DP = [[[0]*ma for i in range(w)] for i in range(h)]

for i in range(h):
    for j in range(w):
        k = abs(a[i][j]-b[i][j])
        if i==0 and j==0:
            DP[i][j][k] = 1
            continue
        else:
            up = min((i+j+1)*80+1, ma-k)
            for l in range(up):
                DP[i][j][l] = DP[i][j-1][abs(l-k)] | DP[i][j-1][l+k] | DP[i-1][j][abs(l-k)] | DP[i-1][j][l+k]
for i in range(ma):
    if DP[-1][-1][i]:
        print(i)
        exit()

Submission status: Wrong Answer

Solution code:
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

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)]
ma = 20*80+2
DP = [[[0]*ma for i in range(w)] for i in range(h)]

for i in range(h):
    for j in range(w):
        k = abs(a[i][j]-b[i][j])
        if i==0 and j==0:
            DP[i][j][k] = 1
            continue
        else:
            up = min((i+j+1)*80+1, ma-k)
            for l in range(up):
                DP[i][j][l] = DP[i][j-1][abs(l-k)] | DP[i][j-1][l+k] | DP[i-1][j][abs(l-k)] | DP[i-1][j][l+k]
for i in range(ma):
    if DP[-1][-1][i]:
        print(i)
        exit()

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
a = []
b = []
for i in range(h):
    a1 = list(map(int,input().split()))
    a.append(a1)
for i in range(h):
    b1 = list(map(int,input().split()))
    b.append(b1)
l = [[] for i in range(h)]
for i in range(h):
    for j in range(w):
        l[i].append(abs(a[i][j]-b[i][j]))
dp = [[[0 for i in range(81)] for i in range(w)] for i in range(h)]
for i in range(h):
    for j in range(w):
        #print(dp)
        for k in range(81):
            if i == 0:
                if j == 0:
                    dp[i][j][l[i][j]] = 1
                else:
                    if dp[i][j-1][k] == 1:
                        if k+l[i][j] <= 80:
                            dp[i][j][k+l[i][j]] = 1
                        if abs(k-l[i][j]) <= 80:
                            dp[i][j][abs(k-l[i][j])] = 1
            else:
                if j == 0:
                    if dp[i-1][j][k] == 1:
                        if k+l[i][j] <= 80:
                            dp[i][j][k+l[i][j]] = 1
                        if abs(k-l[i][j]) <= 80:
                            dp[i][j][abs(k-l[i][j])] = 1
                else:
                    if dp[i-1][j][k] == 1:
                        if k+l[i][j] <= 80:
                            dp[i][j][k+l[i][j]] = 1
                        if abs(k-l[i][j]) <= 80:
                            dp[i][j][abs(k-l[i][j])] = 1
                    if dp[i][j-1][k] == 1:
                        if k+l[i][j] <= 80:
                            dp[i][j][k+l[i][j]] = 1
                        if abs(k-l[i][j]) <= 80:
                            dp[i][j][abs(k-l[i][j])] = 1
ans = 80
for i in range(81):
    if dp[h-1][w-1][i] == 1:
        ans = i
        break
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    h,w=map(int,input().split())
    L=80*(h+w)+1
    a=[list(map(int,input().split())) for i in range(h)]
    b=[list(map(int,input().split())) for i in range(h)]


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

    L=12800
    
    state=[[0 for j in range(w)] for i in range(h)]
    delta=x[0][0]
    state[0][0]=(2**L>>delta)|(2**L<<delta)

    for j in range(1,w):
        delta=x[0][j-1]
        state[0][j]=(state[0][j-1]>>delta)|(state[0][j-1]<<delta)

    for i in range(1,h):
        delta=x[i-1][0]
        state[i][0]=(state[i-1][0]>>delta)|(state[i-1][0]<<delta)

    for i in range(1,h):
        for j in range(1,w):
            delta=x[i][j]
            state[i][j]=(state[i][j-1]>>delta)|(state[i][j-1]<<delta)
            state[i][j]=(state[i-1][j]>>delta)|(state[i-1][j]<<delta)

    
    ans = 10**9
    for i in range(L*2):
        if state[h-1][w-1] & 1:
            ans = min(ans,abs(i-L))
        state[h-1][w-1]>>=1
    print(ans-1)



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    h,w=map(int,input().split())
    L=80*(h+w)+1
    a=[list(map(int,input().split())) for i in range(h)]
    b=[list(map(int,input().split())) for i in range(h)]


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

    L=12800
    
    state=[[2**L for j in range(w)] for i in range(h)]
    delta=x[0][0]
    state[0][0]=(2**L>>delta)|(2**L<<delta)

    for j in range(1,w):
        delta=x[0][j-1]
        state[0][j]=(state[0][j-1]>>delta)|(state[0][j-1]<<delta)

    for i in range(1,h):
        delta=x[i-1][0]
        state[i][0]=(state[i-1][0]>>delta)|(state[i-1][0]<<delta)

    for i in range(1,h):
        for j in range(1,w):
            delta=x[i][j]
            state[i][j]=(state[i][j-1]>>delta)|(state[i][j-1]<<delta)
            state[i][j]=(state[i-1][j]>>delta)|(state[i-1][j]<<delta)

    
    ans = 10**9
    for i in range(L*2):
        if state[h-1][w-1] & 1:
            ans = min(ans,abs(i-L))
        state[h-1][w-1]>>=1
    print(ans-1)



if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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


def main():
	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=[[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])
	D=[[0]*w for _ in range(h)]
	D[0][0]=(1<<C[0][0]+500)|(1>>C[0][0]+500)
	for i in range(h):
		for j in range(w):
			if i>0:
				D[i][j]=D[i][j]|(D[i-1][j]<<C[i][j])|(D[i-1][j]>>C[i][j])
			if j>0:
				D[i][j] = D[i][j]|(D[i][j-1] << C[i][j]) | (D[i][j-1] >> C[i][j])

	val=D[-1][-1]
	ans=500

	for i in range(1000):
		if val>>i&1:
			ans=min(ans,abs(i-500))
	print(ans)

if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
import sys
def main():
    input = sys.stdin.readline
    H,W = map(int, input().split())
    A = [list(map(int, input().split())) for _ in range(H)]
    B = [list(map(int, input().split())) for _ in range(H)]
    C = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]

    M = 160
    dp = [[0 for _ in range(W)] for __ in range(H)]
    dp[0][0] = 1 << (C[0][0] + M)
    for i in range(H):
        for j in range(W):
            for ni,nj in [(i,j+1),(i+1,j)]:
                if ni >= H or nj >= W: continue
                t = C[ni][nj]
                dp[ni][nj] |= dp[i][j] << t
                dp[ni][nj] |= dp[i][j] >> t
    
    a = dp[H-1][W-1]
    ans = 0
    while True:
        if a & (1 << (M+ans)) or a & (1 >> (M-ans)): break
        ans += 1
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
def main():
    input = sys.stdin.readline
    H,W = map(int, input().split())
    A = [list(map(int, input().split())) for _ in range(H)]
    B = [list(map(int, input().split())) for _ in range(H)]
    C = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]

    dp = [[0 for _ in range(W)] for __ in range(H)]
    dp[0][0] = 1 << (C[0][0] + 80)
    for i in range(H):
        for j in range(W):
            for ni,nj in [(i,j+1),(i+1,j)]:
                if ni >= H or nj >= W: continue
                t = C[ni][nj]
                dp[ni][nj] |= dp[i][j] << t
                dp[ni][nj] |= dp[i][j] >> t
    a = dp[H-1][W-1]
    ans = 0
    while True:
        if a & (1 << (80+ans)) or a & (1 >> (80-ans)): break
        ans += 1
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
def main():
    input = sys.stdin.readline
    H,W = map(int, input().split())
    A = [list(map(int, input().split())) for _ in range(H)]
    B = [list(map(int, input().split())) for _ in range(H)]

    dp = [[set() for _ in range(W)] for __ in range(H)]
    dp[0][0].add(abs(A[0][0] - B[0][0]))
    for i in range(H):
        for j in range(W):
            for s in dp[i][j]:
                for ni,nj in [(i,j+1),(i+1,j)]:
                    if ni >= H or nj >= W: continue
                    t = abs(A[ni][nj] - B[ni][nj])
                    if s+t <= 160:
                        dp[ni][nj].add(s + t)
                    dp[ni][nj].add(abs(s - t))
    print(min(dp[H-1][W-1]))

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
def main():
    input = sys.stdin.readline
    H,W = map(int, input().split())
    A = [list(map(int, input().split())) for _ in range(H)]
    B = [list(map(int, input().split())) for _ in range(H)]

    dp = [[set() for _ in range(W)] for __ in range(H)]
    dp[0][0].add(abs(A[0][0] - B[0][0]))
    for i in range(H):
        for j in range(W):
            for s in dp[i][j]:
                for ni,nj in [(i,j+1),(i+1,j)]:
                    if ni >= H or nj >= W: continue
                    t = abs(A[ni][nj] - B[ni][nj])
                    if s+t <= 80:
                        dp[ni][nj].add(s + t)
                    dp[ni][nj].add(abs(s - t))
    print(min(dp[H-1][W-1]))

if __name__ == '__main__':
    main()