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


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


# dp[h][w][x] := (h, w)の時点で差をxにできるか？
M = 1280
dp = [[[False] * (M + 1) for j in range(W)] for i in range(H)]
dp[0][0][X[0][0]] = True


for h in range(H):
    for w in range(W):
        for m in range(M):
            x = X[h][w]
            if (0 <= h - 1) and abs(m + X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h - 1][w][abs(m + x)]

            if (0 <= h - 1) and abs(m - X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h - 1][w][abs(m - x)]

            if (0 <= w - 1) and abs(m + X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h][w - 1][abs(m + x)]

            if (0 <= w - 1) and abs(m + X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h][w - 1][abs(m - x)]


ans = float('inf')
for x in range(M + 1):
    if dp[H - 1][W - 1][x]:
        ans = min(ans, x)

print(ans)

Submission status: Wrong Answer

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


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


# dp[h][w][x] := (h, w)の時点で差をxにできるか？
M = 640
dp = [[[False] * (M + 1) for j in range(W)] for i in range(H)]
dp[0][0][X[0][0]] = True


for h in range(H):
    for w in range(W):
        for m in range(M):
            x = X[h][w]
            if (0 <= h - 1) and abs(m + X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h - 1][w][abs(m + x)]

            if (0 <= h - 1) and abs(m - X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h - 1][w][abs(m - x)]

            if (0 <= w - 1) and abs(m + X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h][w - 1][abs(m + x)]

            if (0 <= w - 1) and abs(m + X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h][w - 1][abs(m - x)]


ans = float('inf')
for x in range(M + 1):
    if dp[H - 1][W - 1][x]:
        ans = min(ans, x)

print(ans)

Submission status: Wrong Answer

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


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


# dp[h][w][x] := (h, w)の時点で差をxにできるか？
M = 160
dp = [[[False] * (M + 1) for j in range(W)] for i in range(H)]
dp[0][0][X[0][0]] = True


for h in range(H):
    for w in range(W):
        for m in range(M):
            x = X[h][w]
            if (0 <= h - 1) and abs(m + X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h - 1][w][abs(m + x)]

            if (0 <= h - 1) and abs(m - X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h - 1][w][abs(m - x)]

            if (0 <= w - 1) and abs(m + X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h][w - 1][abs(m + x)]

            if (0 <= w - 1) and abs(m + X[h][w] <= M):
                dp[h][w][m] = dp[h][w][m] or dp[h][w - 1][abs(m - x)]


ans = float('inf')
for x in range(M + 1):
    if dp[H - 1][W - 1][x]:
        ans = min(ans, x)

print(ans)

Submission status: Wrong Answer

Solution code:
H, W = map(int, input().split())
A = [[0] * (W+1) for _ in range(H+1)]
B = [[0] * (W+1) for _ in range(H+1)]
dp = [[set([]) for _ in range(W+1)] for _ in range(H+1)]
for h in range(1, H+1):
    A[h][1::] = list(map(int, input().split()))

for h in range(1, H+1):
    B[h][1::] = list(map(int, input().split()))

for h in range(H+1):
    dp[h][0].add(0)

for w in range(W+1):
    dp[0][w].add(0)

for h in range(1, H+1):
    for w in range(1, W+1):
        diff = A[h][w] - B[h][w]
        for k in dp[h-1][w]:
            dp[h][w] = dp[h][w].union({k+diff, k-diff})
        for k in dp[h][w-1]:
            dp[h][w] = dp[h][w].union({k+diff, k-diff})

print(min([abs(res) for res in dp[H][W]]))

Submission status: Wrong Answer

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


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

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

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


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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


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

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

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


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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


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

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

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


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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


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

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

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


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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


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

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

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


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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


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

    for i, (al, bl) in enumerate(zip(A, B)):
        for j, (a, b) in enumerate(zip(al, bl)):
            x = abs(a-b)
            dp[j] |= dp[j-1]
            dp[j] = (dp[j] << x) | (dp[j] >> x)

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


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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


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

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

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


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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


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

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

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


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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


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

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

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


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

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

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


if __name__ == "__main__":
    solve1()

Submission status: Wrong Answer

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


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

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

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


if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

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


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

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

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


if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

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


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

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

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


if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

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


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

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

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


if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

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


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

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

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


if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

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


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

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

    b = map(int, list(bin(dp[h-1][w-1])[-ofs-1:]))
    for i, s in enumerate(b):
        if(s):
            print(i)
            print(0)
            return


if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

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


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

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

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


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

Submission status: Wrong Answer

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


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

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

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


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

Submission status: Wrong Answer

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


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

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

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


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

Submission status: Wrong Answer

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


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

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

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


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

Submission status: Wrong Answer

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


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

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

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


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

Submission status: Wrong Answer

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


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

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

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


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

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)]
D = [[abs(A[h][w]-B[h][w]) for w in range(W)] for h in range(H)]
def cal():
    dp1 = [set() for _ in range(W)]
    dp2 = [set() for _ in range(W)]
    for w in range(W):
        dp1[w].add(0)
    for h in range(H):
        for w in range(W):
            for v in dp1[w]:
                v1 = v+D[h][w]
                v2 = v-D[h][w] if v>=D[h][w] else -v+D[h][w]
                dp2[w].add(v1)
                dp2[w].add(v2)
            if 0 < w:
                for v in dp2[w-1]:
                    v1 = v+D[h][w]
                    v2 = v-D[h][w] if v>=D[h][w] else -v+D[h][w]
                    dp2[w].add(v1)
                    dp2[w].add(v2)
        dp1 = dp2
        dp2 = [set() for _ in range(W)]
    return min(dp1[W-1])
ans = cal()
print(ans)

Submission status: Wrong Answer

Solution code:
H,W = map(int, input().split())
A = [[int(a) for a in input().split()] for _ in range(H)]
B = [[int(b) for b in input().split()] for _ in range(H)]

dp = [[{} for _ in range(W)] for _ in range(H)]
t = A[0][0]-B[0][0]
if t < 0:
    dp[0][0][-t*2] = True
    dp[0][0][-t*2-1] = True
elif t > 0:
    dp[0][0][2*t-1] = True
    dp[0][0][2*t] = True
else:
    dp[0][0][0] = True

for i in range(H):
    for j in range(W):
        if i > 0:
            for k in dp[i-1][j]:
                if k % 2 == 0:
                    num = -k//2
                else:
                    num = (k+1)//2
                num += A[i][j]-B[i][j]
                if num <= 0:
                    idx = -num*2
                else:
                    idx = num*2-1
                dp[i][j][idx] = True
                num -= (-A[i][j]+B[i][j])*2
                if num <= 0:
                    idx = -num*2
                else:
                    idx = num*2-1
                dp[i][j][idx] = True
        if j > 0:
            for k in dp[i][j-1]:
                if k % 2 == 0:
                    num = -k//2
                else:
                    num = (k+1)//2
                num += A[i][j]-B[i][j]
                if num <= 0:
                    idx = -num*2
                else:
                    idx = num*2-1
                dp[i][j][idx] = True
                num += (-A[i][j]+B[i][j])*2
                if num <= 0:
                    idx = -num*2
                else:
                    idx = num*2-1
                dp[i][j][idx] = True
                    
ans = 10**9
for k in dp[H-1][W-1]:
    ans = min(ans, k//2+k%2)
        
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)]
dp1 = [{} for _ in range(W)]
dp2 = [{} for _ in range(W)]
for w in range(W):
    dp1[w][0] = True

for h in range(H):
    for w in range(W):
        va = A[h][w]
        vb = B[h][w]
        for v in dp1[w]:
            dp2[w][abs(v+va-vb)] = True
            dp2[w][abs(v-va+vb)] = True
        if w!=0:
            for v in dp2[w-1]:
                dp2[w][abs(v+va-vb)] = True
                dp2[w][abs(v-va+vb)] = True
    dp1 = dp2
    dp2 = [{} for _ in range(W)]

ans = min(dp1[W-1])
print(ans)

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
a=[None]*H
b=[None]*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()))

dp=[[[False for i in range(240)] for j in range(W)] for p in range(H)]

dp[0][0][abs(a[0][0]-b[0][0])+80]=True
for i in range(H):
  for j in range(W):
    for k in range(240):
      if dp[i][j][k]:
        if i+1<H:
          p=abs(a[i+1][j]-b[i+1][j])
          if k-p>=0:
            dp[i+1][j][k-p]=True
          if k+p<=239:
            dp[i+1][j][k+p]=True
        if j+1<W:
          p=abs(a[i][j+1]-b[i][j+1])
          if k-p>=0:
            dp[i][j+1][k-p]=True
          if k+p<=239:
            dp[i][j+1][k+p]=True
for k in range(240):
  if dp[-1][-1][k]:
    if k-80>=0:
      print(k-80)
      break

Submission status: Wrong Answer

Solution code:
import numpy as np

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

A = []
B = []

bias = 80

for i in range(H):
    A.append(list(map(int, input().split())))

for i in range(H):
    B.append(list(map(int, input().split())))

A = np.array(A, dtype=np.int)
B = np.array(B, dtype=np.int)
D = np.abs(A - B)

dp = np.zeros([H, W, bias * 2 + 1], dtype=bool)
dp[0][0][bias - D[0][0]] = True
dp[0][0][bias + D[0][0]] = True

for i in range(H):
    for j in range(W):
        diff = D[i][j]
        
        if i > 0:
            dp[i, j, diff:] |= dp[i - 1, j, :bias * 2 + 1 - diff]
            dp[i, j, : bias * 2 + 1 - diff] |= dp[i - 1, j, diff:]

        if j > 0:
            dp[i, j, diff:] |= dp[i, j - 1, :bias * 2 + 1 - diff]
            dp[i, j, : bias * 2 + 1 - diff] |= dp[i, j - 1, diff:]

idx = dp[H - 1][W - 1][:].nonzero()[0]
print(np.abs(idx - bias).min())

Submission status: Wrong Answer

Solution code:
def main():
    H, W = [int(i) for i in input().split()]
    A = [[int(i) for i in input().split()] for _ in range(H)]
    B = [[int(i) for i in input().split()] for _ in range(H)]

    u = [[False] * 81 for _ in range(W)]
    v = [[False] * 81 for _ in range(W)]
    v[0][0] = True
    for h in range(H):
        v, u = u, v
        for w in range(W):
            a, b = A[h][w], B[h][w]
            d = a - b if a > b else b - a
            for i in range(81):
                t = False
                if 0 <= i + d <= 80:
                    t = t or u[w][i + d] or (w > 0 and v[w - 1][i + d])
                if 0 <= i - d <= 80:
                    t = t or u[w][i - d] or (w > 0 and v[w - 1][i - d])
                if 0 <= d - i <= 80:
                    t = t or u[w][d - i] or (w > 0 and v[w - 1][d - i])
                v[w][i] = t
    vv = v[-1]
    for i in range(81):
        if vv[i]:
            print(i)
            return
    raise

main()