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

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()


def main():
    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)]
    abss = [[abs(A[i][j]-B[i][j])for j in range(w)] for i in range(h)]
    kmax = 3500
    dp = [[[0]*kmax for j in range(w)]for i in range(h)]
    dp[0][0][abss[0][0]] = 1
    for i in range(h):
        for j in range(w):
            for k in range(kmax):
                if dp[i][j][k] == 1:
                    if i != h-1:
                        dp[i+1][j][abs(k-abss[i+1][j])] = 1
                        if k+abss[i+1][j] < kmax:
                            dp[i+1][j][k+abss[i+1][j]] = 1
                    if j != w-1:
                        dp[i][j+1][abs(k-abss[i][j+1])] = 1
                        if k+abss[i][j+1] < kmax:
                            dp[i][j+1][k+abss[i][j+1]] = 1
    for k in range(kmax):
        if dp[-1][-1][k] == 1:
            print(k)
            break


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()


def main():
    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)]
    abss = [[abs(A[i][j]-B[i][j])for j in range(w)] for i in range(h)]
    kmax = 3200
    dp = [[[0]*kmax for j in range(w)]for i in range(h)]
    dp[0][0][abss[0][0]] = 1
    for i in range(h):
        for j in range(w):
            for k in range(kmax):
                if dp[i][j][k] == 1:
                    if i != h-1:
                        dp[i+1][j][abs(k-abss[i+1][j])] = 1
                        if k+abss[i+1][j] < kmax:
                            dp[i+1][j][k+abss[i+1][j]] = 1
                    if j != w-1:
                        dp[i][j+1][abs(k-abss[i][j+1])] = 1
                        if k+abss[i][j+1] < kmax:
                            dp[i][j+1][k+abss[i][j+1]] = 1
    for k in range(kmax):
        if dp[-1][-1][k] == 1:
            print(k)
            break


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()


def main():
    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)]
    abss = [[abs(A[i][j]-B[i][j])for j in range(w)] for i in range(h)]
    kmax = 1600
    dp = [[[0]*kmax for j in range(w)]for i in range(h)]
    dp[0][0][abss[0][0]] = 1
    for i in range(h):
        for j in range(w):
            for k in range(kmax):
                if dp[i][j][k] == 1:
                    if i != h-1:
                        dp[i+1][j][abs(k-abss[i+1][j])] = 1
                        if k+abss[i+1][j] < kmax:
                            dp[i+1][j][k+abss[i+1][j]] = 1
                    if j != w-1:
                        dp[i][j+1][abs(k-abss[i][j+1])] = 1
                        if k+abss[i][j+1] < kmax:
                            dp[i][j+1][k+abss[i][j+1]] = 1
    for k in range(kmax):
        if dp[-1][-1][k] == 1:
            print(k)
            break


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
def input(): return sys.stdin.readline().rstrip()


def main():
    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)]
    abss = [[abs(A[i][j]-B[i][j])for j in range(w)] for i in range(h)]
    kmax = 400
    dp = [[[0]*kmax for j in range(w)]for i in range(h)]
    dp[0][0][abss[0][0]] = 1
    for i in range(h):
        for j in range(w):
            for k in range(kmax):
                if dp[i][j][k] == 1:
                    if i != h-1:
                        dp[i+1][j][abs(k-abss[i+1][j])] = 1
                        if k+abss[i+1][j] < kmax:
                            dp[i+1][j][k+abss[i+1][j]] = 1
                    if j != w-1:
                        dp[i][j+1][abs(k-abss[i][j+1])] = 1
                        if k+abss[i][j+1] < kmax:
                            dp[i][j+1][k+abss[i][j+1]] = 1
    for k in range(kmax):
        if dp[-1][-1][k] == 1:
            print(k)
            break


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
import numpy as np
def input(): return sys.stdin.readline().rstrip()


def main():
    h, w = map(int, input().split())
    A = np.array([list(map(int, input().split()))
                  for i in range(h)], dtype=int)
    B = np.array([list(map(int, input().split()))
                  for i in range(h)], dtype=int)
    abss = np.abs(A-B)
    kmax = 160
    dp = np.zeros((h, w, kmax), dtype=int)
    dp[0][0][abss[0][0]] = 1
    for i in range(h):
        for j in range(w):
            for k in range(kmax):
                if dp[i][j][k] == 1:
                    if i != h-1:
                        dp[i+1][j][abs(k-abss[i+1][j])] = 1
                        if k+abss[i+1][j] < kmax:
                            dp[i+1][j][k+abss[i+1][j]] = 1
                    if j != w-1:
                        dp[i][j+1][abs(k-abss[i][j+1])] = 1
                        if k+abss[i][j+1] < kmax:
                            dp[i][j+1][k+abss[i][j+1]] = 1
    for k in range(kmax):
        if dp[-1][-1][k] == 1:
            print(k)
            break


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

B = []
for i in range(H):
  B.append(list(map(int, input().split())))
  
for i in range(H):
  for j in range(W):
    A[i][j] = abs(A[i][j] - B[i][j])
    
#dp[i][j][k] (i,j)に来たときに偏りがkになっているかFLG。
# kのオーダー(80+80)*80 = 12800くらいらしい。。。
MaxK = (H+W)*80+10
dp = [[[ False for _ in range(MaxK)] for _ in range(W)] for _ in range(H)]
# dkey[i][j] TrueになっているKeys。
dkey = [[set() for _ in range(W)] for _ in range(H)]

def updc(i,j,k,now):
  dp[i][j][abs(now-k)] = True
  dkey[i][j].add(abs(now-k))  
  dp[i][j][abs(now+k)] = True
  dkey[i][j].add(abs(now+k)) 
  return

def upd(i,j,now):
  if i+i < H:
    updc(i+1, j, A[i+1][j], now)
  if j+1 < W:
    updc(i, j+1, A[i][j+1], now)

updc(0,0,A[0][0],0)

now = A[0][0]
for i in range(H):
  for j in range(W):
    for k in dkey[i][j]:
      #print(k)
      upd(i,j,k)
      
for i in range(MaxK):
  if dp[H-1][W-1][i]:
    print(i)
    break
exit(0)
#debug
print(dp[H-1][W-1][0:10])
for k in dkey:
  print(k)

Submission status: Wrong Answer

Solution code:
def max2(x,y):
    return x if x > y else y

def lsb(n):
    temp = (0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
      31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9)
    return temp[(((n&-n)*0x077CB531)>>27)&0x1F]

H, W = map(int, input().split())
A = []
B = []
ofs = 80*H*W
for _ in range(H):
    A.append(list(map(int, input().split())))
for _ in range(H):
    B.append(list(map(int, input().split())))
d = [[0]*W for _ in range(H)]
for h in range(H):
    for w in range(W):
        d[h][w] = abs(A[h][w]-B[h][w])

dp = [[0 for _ in range(W)] for _ in range(H)]
dp[0][0] = 1<<ofs
dp[0][0] = (dp[0][0]<<d[0][0])|(dp[0][0]>>d[0][0])
for h in range(H):
    for w in range(W):
        if h < H-1:
            dp[h+1][w] |= (dp[h][w]<<d[h+1][w])|(dp[h][w]>>d[h+1][w])
        if w < W-1:
            dp[h][w+1] |= (dp[h][w]<<d[h][w+1])|(dp[h][w]>>d[h][w+1])
print(lsb(dp[-1][-1]>>ofs))

Submission status: Wrong Answer

Solution code:
def max2(x,y):
    return x if x > y else y

def lsb(n):
    temp = (0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
      31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9)
    return temp[(((n&-n)*0x077CB531)>>27)&0x1F]

H, W = map(int, input().split())
A = []
B = []
ofs = 80*H*W
for _ in range(H):
    A.append(list(map(int, input().split())))
for _ in range(H):
    B.append(list(map(int, input().split())))
d = [[0]*W for _ in range(H)]
for h in range(H):
    for w in range(W):
        d[h][w] = abs(A[h][w]-B[h][w])

dp = [[0 for _ in range(W)] for _ in range(H)]
dp[0][0] = 1<<ofs
dp[0][0] = (dp[0][0]<<d[0][0])|(dp[0][0]>>d[0][0])
for h in range(H):
    for w in range(W):
        if h < H-1:
            dp[h+1][w] |= (dp[h][w]<<d[h+1][w])|(dp[h][w]>>d[h+1][w])
        if w < W-1:
            dp[h][w+1] |= (dp[h][w]<<d[h][w+1])|(dp[h][w]>>d[h][w+1])
res1 = lsb(dp[-1][-1]>>ofs)
res2 = ofs - dp[-1][-1]&(1<<(dp[-1][-1].bit_length()-ofs+1)-1).bit_length()
print(min(res1,res2))

Submission status: Wrong Answer

Solution code:
import numpy as np

H, W = map(int, input().split())
As = [list(map(int, input().split())) for _ in range(H)]
Bs = [list(map(int, input().split())) for _ in range(H)]

ds = [[abs(A-B) for A, B in zip(row_A, row_B)] for row_A, row_B in zip(As, Bs)]

size = 3005

dp = np.full((H, W, size), False)
dp[0][0][ds[0][0]] = True
for i in range(H):
    for j in range(W):
        if i+1 < H:
            d = ds[i+1][j]
            dp[i+1][j][d:] = np.logical_or(dp[i][j][:size-d], dp[i+1][j][d:])
            dp[i+1][j][:size-d] = np.logical_or(dp[i][j][d:], dp[i+1][j][:size-d])
            dp[i+1][j][:d+1] = np.logical_or(dp[i][j][d::-1], dp[i+1][j][:d+1])
        if j+1 < W:
            d = ds[i][j+1]
            dp[i][j+1][d:] = np.logical_or(dp[i][j][:size-d], dp[i][j+1][d:])
            dp[i][j+1][:size-d] = np.logical_or(dp[i][j][d:], dp[i][j+1][:size-d])
            dp[i][j+1][:d+1] = np.logical_or(dp[i][j][d::-1], dp[i][j+1][:d+1])

print(np.min(np.where(dp[-1][-1])))

Submission status: Wrong Answer

Solution code:
import numpy as np

H, W = map(int, input().split())
As = [list(map(int, input().split())) for _ in range(H)]
Bs = [list(map(int, input().split())) for _ in range(H)]

ds = [[abs(A-B) for A, B in zip(row_A, row_B)] for row_A, row_B in zip(As, Bs)]

size = 805

dp = np.full((H, W, size), False)
dp[0][0][ds[0][0]] = True
for i in range(H):
    for j in range(W):
        if i+1 < H:
            d = ds[i+1][j]
            dp[i+1][j][d:] = np.logical_or(dp[i][j][:size-d], dp[i+1][j][d:])
            dp[i+1][j][:size-d] = np.logical_or(dp[i][j][d:], dp[i+1][j][:size-d])
            dp[i+1][j][:d+1] = np.logical_or(dp[i][j][d::-1], dp[i+1][j][:d+1])
        if j+1 < W:
            d = ds[i][j+1]
            dp[i][j+1][d:] = np.logical_or(dp[i][j][:size-d], dp[i][j+1][d:])
            dp[i][j+1][:size-d] = np.logical_or(dp[i][j][d:], dp[i][j+1][:size-d])
            dp[i][j+1][:d+1] = np.logical_or(dp[i][j][d::-1], dp[i][j+1][:d+1])

print(np.min(np.where(dp[-1][-1])))

Submission status: Wrong Answer

Solution code:
import numpy as np

H, W = map(int, input().split())
As = [list(map(int, input().split())) for _ in range(H)]
Bs = [list(map(int, input().split())) for _ in range(H)]

ds = [[abs(A-B) for A, B in zip(row_A, row_B)] for row_A, row_B in zip(As, Bs)]

size = 165

dp = np.full((H, W, size), False)
dp[0][0][ds[0][0]] = True
for i in range(H):
    for j in range(W):
        if i+1 < H:
            d = ds[i+1][j]
            dp[i+1][j][d:] = np.logical_or(dp[i][j][:size-d], dp[i+1][j][d:])
            dp[i+1][j][:size-d] = np.logical_or(dp[i][j][d:], dp[i+1][j][:size-d])
            dp[i+1][j][:d+1] = np.logical_or(dp[i][j][d::-1], dp[i+1][j][:d+1])
        if j+1 < W:
            d = ds[i][j+1]
            dp[i][j+1][d:] = np.logical_or(dp[i][j][:size-d], dp[i][j+1][d:])
            dp[i][j+1][:size-d] = np.logical_or(dp[i][j][d:], dp[i][j+1][:size-d])
            dp[i][j+1][:d+1] = np.logical_or(dp[i][j][d::-1], dp[i][j+1][:d+1])

print(np.min(np.where(dp[-1][-1])))

Submission status: Wrong Answer

Solution code:
h,w = map(int, input().split())
a,b = [],[]
c = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
    a.append(list(map(int, input().split())))
for i in range(h):
    b.append(list(map(int, input().split())))
for i in range(h):
    for j in range(w):
        c[i][j] = abs(a[i][j] - b[i][j])

ans = [[[False for k in range(330)] for j in range(w+1)] for i in range(h+1)]
ans[1][0][0] = True

for i in range(1,h+1):
    for j in range(1,w+1):
        n = c[i-1][j-1]
        for k in range(-160,161):
            if (ans[i-1][j][k] == True):
                if (-160 <= k-n):
                    ans[i][j][k-n] = True
                if (160 >= k+n):
                    ans[i][j][k+n] = True
            if (ans[i][j-1][k] == True):
                if (-160 <= k-n):
                    ans[i][j][k-n] = True
                if (160 >= k+n):
                    ans[i][j][k+n] = True

for i in range(81):
    if (ans[h][w][i] or ans[h][w][i*(-1)]):
        print(i)
        break

Submission status: Wrong Answer

Solution code:
h,w = map(int, input().split())
a,b = [],[]
c = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
    a.append(list(map(int, input().split())))
for i in range(h):
    b.append(list(map(int, input().split())))
for i in range(h):
    for j in range(w):
        c[i][j] = abs(a[i][j] - b[i][j])

ans = [[[False for k in range(170)] for j in range(w+1)] for i in range(h+1)]
ans[1][0][0] = True

for i in range(1,h+1):
    for j in range(1,w+1):
        n = c[i-1][j-1]
        for k in range(-80,81):
            if (ans[i-1][j][k] == True):
                if (-80 <= k-n):
                    ans[i][j][k-n] = True
                if (80 >= k+n):
                    ans[i][j][k+n] = True
            if (ans[i][j-1][k] == True):
                if (-80 <= k-n):
                    ans[i][j][k-n] = True
                if (80 >= k+n):
                    ans[i][j][k+n] = True

for i in range(81):
    if (ans[h][w][i] or ans[h][w][i*(-1)]):
        print(i)
        break

Submission status: Wrong Answer

Solution code:
h,w = map(int, input().split())
a,b = [],[]
c = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
    a.append(list(map(int, input().split())))
for i in range(h):
    b.append(list(map(int, input().split())))
for i in range(h):
    for j in range(w):
        c[i][j] = abs(a[i][j] - b[i][j])

ans = [[[False for k in range(170)] for j in range(w+1)] for i in range(h+1)]
ans[1][0][0] = True

for i in range(h+1):
    for j in range(w+1):
        n = c[i-1][j-1]
        for k in range(-80,81):
            if (ans[i-1][j][k] == True):
                if (-80 <= k-n):
                    ans[i][j][k-n] = True
                if (80 >= k+n):
                    ans[i][j][k+n] = True
            if (ans[i][j-1][k] == True):
                if (-80 <= k-n):
                    ans[i][j][k-n] = True
                if (80 >= k+n):
                    ans[i][j][k+n] = True

for i in range(81):
    if (ans[h][w][i] or ans[h][w][i*(-1)]):
        print(i)
        break

Submission status: Wrong Answer

Solution code:
h,w = map(int, input().split())
a,b = [],[]
c = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
    a.append(list(map(int, input().split())))
for i in range(h):
    b.append(list(map(int, input().split())))
for i in range(h):
    for j in range(w):
        c[i][j] = abs(a[i][j] - b[i][j])

ans = [[[False for k in range(170)] for j in range(w+1)] for i in range(h+1)]
ans[1][0][0] = True

for i in range(h+1):
    for j in range(w+1):
        n = c[i-1][j-1]
        for k in range(-80,81):
            if (ans[i-1][j][k] == True):
                if (n-k >= -80):
                    ans[i][j][n-k] = True
                if (n+k <= 80):
                    ans[i][j][n+k] = True
            if (ans[i][j-1][k] == True):
                if (n-k >= -80):
                    ans[i][j][n-k] = True
                if (n+k <= 80):
                    ans[i][j][n+k] = True

for i in range(81):
    if (ans[h][w][i] or ans[h][w][i*(-1)]):
        print(i)
        break

Submission status: Wrong Answer

Solution code:
I = [int(_) for _ in open(0).read().split()]
H, W = I[:2]
A = I[2:2 + H * W]
B = I[2 + H * W:]
D = [abs(a - b) for a, b in zip(A, B)]
dp = [0] + [1] + [0] * (W - 1)
for h in range(H):
    dpo = dp
    dp = [0] * (W + 1)
    for w, d in enumerate(D[W * h:W * (h + 1)]):
        s = dp[w] | dpo[w + 1]
        dp[w + 1] = (s << d) | (s >> d)
        t = bin(s)[2:][-1 - d:][::-1]
        t += '0' * (d - len(t))
        dp[w + 1] |= int(t, 2)
for i in range(160 * 80):
    if dp[-1] & 2**i:
        print(i)
        exit()

Submission status: Wrong Answer

Solution code:
I = [int(_) for _ in open(0).read().split()]
H, W = I[:2]
A = I[2:2 + H * W]
B = I[2 + H * W:]
D = [abs(a - b) for a, b in zip(A, B)]
dp = [1] + [0] * W
for h in range(H):
    dpo = dp
    dp = [1] + [0] * W
    for w, d in enumerate(D[W * h:W * (h + 1)]):
        s = dp[w] | dpo[w + 1]
        dp[w + 1] = (s << d) | (s >> d)
        t = bin(s)[2:][-1 - d:][::-1]
        t += '0' * (d - len(t))
        dp[w + 1] |= int(t, 2)
for i in range(160 * 80):
    if dp[-1] & 2**i:
        print(i)
        exit()

Submission status: Wrong Answer

Solution code:
I = [int(_) for _ in open(0).read().split()]
H, W = I[:2]
A = I[2:2 + H * W]
B = I[2 + H * W:]
D = [abs(a - b) for a, b in zip(A, B)]
dp = [set([0]) for _ in range(W + 1)]
for h in range(H):
    dpo = dp
    dp = [set() for _ in range(W + 1)]
    for w, d in enumerate(D[W * h:W * (h + 1)]):
        s = dp[w] | dpo[w + 1]
        dp[w + 1] = {d + v for v in s} | {abs(d - v) for v in s}
print(min(dp[-1]))

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
A,B=[],[]
for i in range(H):
  X=list(map(int, input().split()))
  A.append(X)
for i in range(H):
  X=list(map(int, input().split()))
  B.append(X)
print(A,B)
C=[]
m=0
for i in range(H):
  D=[]
  for j in range(W):
    D.append(abs(A[i][j]-B[i][j]))
    m+=abs(A[i][j]-B[i][j])
  C.append(D)
print(C)
dp=[[set() for i in range(W)] for i in range(H)]
dp[0][0].add(C[0][0])
dp[0][0].add(-C[0][0])
print(dp)
for i in range(H):
  for j in range(W):
    if i!=0:
      for k in dp[i-1][j]:
        dp[i][j].add(abs(k-C[i][j]))
        dp[i][j].add(abs(k+C[i][j]))
    if j!=0:
      for k in dp[i][j-1]:
        dp[i][j].add(abs(k-C[i][j]))
        dp[i][j].add(abs(k+C[i][j]))
print(min(dp[-1][-1]))

Submission status: Wrong Answer

Solution code:
import sys
import math
import bisect
import heapq


def input():
    return sys.stdin.readline().rstrip()


def main():
    H, W = map(int, input().split())
    M = 80
    A = [[0] * W for _ in range(H)]
    K = M * (H + W) + 1
    for i in range(H):
        a = list(map(int, input().split()))
        A[i] = a

    for i in range(H):
        b = list(map(int, input().split()))
        for j in range(W):
            A[i][j] -= b[j]

    dp = [[set() for i in range(W)] for i in range(H)]
    # dp[i][j][k]
    dp[0][0].add(0)

    for i in range(H):
        for j in range(W):
            if i != 0:
                for k in dp[i-1][j]:
                    dp[i][j].add(abs(k - A[i][j]))
                    dp[i][j].add(abs(k + A[i][j]))
            if j != 0:
                for k in dp[i][j-1]:
                    dp[i][j].add(abs(k - A[i][j]))
                    dp[i][j].add(abs(k + A[i][j]))

    print(min(dp[H-1][W-1]))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]


h,w=map(int,input().split())
a=[list(map(int,input().split())) for i in range(h)]
b=[list(map(int,input().split())) for i in range(h)]

lst=[[0]*w for i in range(h)]

lst[0][0]={abs(a[0][0]-b[0][0]):1}
# print(lst)
for i in range(h):
    for j in range(w):
            tmp=lst[i][j].keys()
            if i+1<h:
                if lst[i+1][j]==0:
                    lst[i+1][j]={}
                ab=abs(a[i+1][j]-b[i+1][j])
                for k in tmp:
                    lst[i+1][j][k+ab]=1
                    lst[i+1][j][abs(k-ab)]=1

            # print(lst)
            if j+1<w:
                if lst[i][j+1]==0:
                    lst[i][j+1]={}
                ab=abs(a[i][j+1]-b[i][j+1])
                for k in tmp:
                    lst[i][j+1][k+ab]=1
                    lst[i][j+1][abs(k-ab)]=1
    # print(d)
    # print(lst)
anslst=list(lst[-1][-1].keys())
# anslst.sort(key=lambda x: abs(x))
print(abs(anslst[0]))

Submission status: Wrong Answer

Solution code:
import sys

stdin = sys.stdin


def ni(): return int(ns())


def na(): return list(map(int, stdin.readline().split()))


def naa(N): return [na() for _ in range(N)]


def ns(): return stdin.readline().rstrip()  # ignore trailing spaces


H, W = na()
A_array = naa(H)
B_array = naa(H)
C_array = [[abs(aa-bb) for aa, bb in zip(a, b)]
           for a, b in zip(A_array, B_array)]
# print(C_array)

ans_array = [[set() for _ in range(W+1)] for _ in range(H+1)]

for i in range(H+1):
    ans_array[i][0].add(0)

for i in range(W+1):
    ans_array[0][i].add(0)

for i in range(H):
    for j in range(W):
        for s in ans_array[i][j+1]:
            ans_array[i+1][j+1].add(s + C_array[i][j])
            ans_array[i+1][j+1].add(s - C_array[i][j])

        for s in ans_array[i+1][j]:
            ans_array[i+1][j+1].add(s + C_array[i][j])
            ans_array[i+1][j+1].add(s - C_array[i][j])

# print(ans_array)

ans = 10 ** 9
for s in ans_array[H][W]:
    if abs(s) < ans:
        ans = abs(s)
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    import os
    import sys
    from io import BytesIO, IOBase
    
    BUFSIZE = 8192
    
    
    class FastIO(IOBase):
        newlines = 0
    
        def __init__(self, file):
            self._fd = file.fileno()
            self.buffer = BytesIO()
            self.writable = "x" in file.mode or "r" not in file.mode
            self.write = self.buffer.write if self.writable else None
    
        def read(self):
            while True:
                b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
                if not b:
                    break
                ptr = self.buffer.tell()
                self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
            self.newlines = 0
            return self.buffer.read()
    
        def readline(self):
            while self.newlines == 0:
                b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
                self.newlines = b.count(b"\n") + (not b)
                ptr = self.buffer.tell()
                self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
            self.newlines -= 1
            return self.buffer.readline()
    
        def flush(self):
            if self.writable:
                os.write(self._fd, self.buffer.getvalue())
                self.buffer.truncate(0), self.buffer.seek(0)
    
    
    class IOWrapper(IOBase):
        def __init__(self, file):
            self.buffer = FastIO(file)
            self.flush = self.buffer.flush
            self.writable = self.buffer.writable
            self.write = lambda s: self.buffer.write(s.encode("ascii"))
            self.read = lambda: self.buffer.read().decode("ascii")
            self.readline = lambda: self.buffer.readline().decode("ascii")
    
    
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
    input = lambda: sys.stdin.readline().rstrip("\r\n")
    mod = 10**9 + 7


            

    h, w = map(int, input().split())
    a = [[int(x) for x in input().split()] for _ in range(h)]
    b = [[int(x) for x in input().split()] for _ in range(h)]
    c = [[0]*w for _ in range(h)]
    max_value = 80 * (h + w)
    for i in range(h):
        for j in range(w):
            c[i][j] = abs(a[i][j] - b[i][j])
    dp = [[[False]*(max_value + 1) for _ in range(w)] for _ in range(h)]
    dp[0][0][c[0][0]] = True
    for i in range(h):
        for j in range(w):
            for k in range(max_value + 1):
                # print(i, j, k, k + c[i][j + 1])
                if 0 <= i + 1 < h:
                    if 0 <= k + c[i + 1][j] <= max_value:
                        dp[i + 1][j][k + c[i + 1][j]] = True
                    if 0 <= abs(k - c[i + 1][j]) <= max_value:
                        dp[i + 1][j][abs(k - c[i + 1][j])] = True
                if 0 <= j + 1 < w:
                    if 0 <= k + c[i][j + 1] <= max_value:
                        dp[i][j + 1][k + c[i][j + 1]] = True
                    if 0 <= abs(k - c[i][j + 1]) <= max_value:
                        dp[i][j + 1][abs(k - c[i][j + 1])] = True
    for k in range(max_value + 1):
        if dp[h - 1][w - 1][k]:
            print(k)
            sys.exit()
main()

Submission status: Wrong Answer

Solution code:
def main():
    import os
    import sys
    from io import BytesIO, IOBase
    
    BUFSIZE = 8192
    
    
    class FastIO(IOBase):
        newlines = 0
    
        def __init__(self, file):
            self._fd = file.fileno()
            self.buffer = BytesIO()
            self.writable = "x" in file.mode or "r" not in file.mode
            self.write = self.buffer.write if self.writable else None
    
        def read(self):
            while True:
                b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
                if not b:
                    break
                ptr = self.buffer.tell()
                self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
            self.newlines = 0
            return self.buffer.read()
    
        def readline(self):
            while self.newlines == 0:
                b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
                self.newlines = b.count(b"\n") + (not b)
                ptr = self.buffer.tell()
                self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
            self.newlines -= 1
            return self.buffer.readline()
    
        def flush(self):
            if self.writable:
                os.write(self._fd, self.buffer.getvalue())
                self.buffer.truncate(0), self.buffer.seek(0)
    
    
    class IOWrapper(IOBase):
        def __init__(self, file):
            self.buffer = FastIO(file)
            self.flush = self.buffer.flush
            self.writable = self.buffer.writable
            self.write = lambda s: self.buffer.write(s.encode("ascii"))
            self.read = lambda: self.buffer.read().decode("ascii")
            self.readline = lambda: self.buffer.readline().decode("ascii")
    
    
    sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
    input = lambda: sys.stdin.readline().rstrip("\r\n")
    mod = 10**9 + 7


            

    h, w = map(int, input().split())
    a = [[int(x) for x in input().split()] for _ in range(h)]
    b = [[int(x) for x in 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])
    dp = [[[False]*(80 * (h + w) + 1) for _ in range(w)] for _ in range(h)]
    dp[0][0][c[0][0]] = True
    for i in range(h):
        for j in range(w):
            for k in range(80 * (h + w) + 1):
                # print(i, j, k, k + c[i][j + 1])
                if 0 <= i + 1 < h:
                    if 0 <= k + c[i + 1][j] <= 80 * (h + w):
                        dp[i + 1][j][k + c[i + 1][j]] = True
                    if 0 <= abs(k - c[i + 1][j]) <= 80 * (h + w):
                        dp[i + 1][j][abs(k - c[i + 1][j])] = True
                if 0 <= j + 1 < w:
                    if 0 <= k + c[i][j + 1] <= 80 * (h + w):
                        dp[i][j + 1][k + c[i][j + 1]] = True
                    if 0 <= abs(k - c[i][j + 1]) <= 80 * (h + w):
                        dp[i][j + 1][abs(k - c[i][j + 1])] = True
    for k in range(80 * (h + w) + 1):
        if dp[h - 1][w - 1][k]:
            print(k)
            sys.exit()
main()