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:
def main():  
  import sys
  read = sys.stdin.buffer.read
  readline = sys.stdin.buffer.readline
  readlines = sys.stdin.buffer.readlines

  h,w = map(int,readline().split())
  a = []
  for i in range(h):
    s = list(map(int,readline().split()))
    a.append(s)
  b = []
  for i in range(h):
    t = list(map(int,readline().split()))
    b.append(t)

  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 k in dp[i][j]:
          if i < h-1:
            c = abs(a[i+1][j]-b[i+1][j])
            if k+c < 5000:
              dp[i+1][j].add(k+c)
            dp[i+1][j].add(abs(k-c))
          if j < w-1:
            d = abs(a[i][j+1]-b[i][j+1])
            if k+d < 5000:
              dp[i][j+1].add(k+d)
            dp[i][j+1].add(abs(k-d))

  print(min(dp[h-1][w-1]))
if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def MAP():  return map(int, input().split())
def LIST(): return list(map(int, input().split()))

H, W = MAP()
A = []
B = []
for i in range(H):
    A.append(LIST())
for i in range(H):
    B.append(LIST())
dp = [[set() for _ in range(W)] for _ in range(H)]
for i in range(H):
    for j in range(W):
        difference = abs(A[i][j] - B[i][j])
        if i == 0 and j == 0:
            dp[i][j].add(difference)
            continue
        if i == 0:
            for value in dp[i][j - 1]:
                val_1, val_2 = value + difference, value - difference
                dp[i][j].add(val_1)
                dp[i][j].add(val_2)
            continue
        if j == 0:
            for value in dp[i - 1][j]:
                val_1, val_2 = value + difference, value - difference
                dp[i][j].add(val_1)
                dp[i][j].add(val_2)
            continue
        for value in dp[i][j - 1]:
                val_1, val_2 = value + difference, value - difference
                dp[i][j].add(val_1)
                dp[i][j].add(val_2)
        for value in dp[i - 1][j]:
                val_1, val_2 = value + difference, value - difference
                dp[i][j].add(val_1)
                dp[i][j].add(val_2)
print(min(dp[H - 1][W - 1], key=lambda x: abs(x)))

Submission status: Wrong Answer

Solution code:
def MAP():  return map(int, input().split())
def LIST(): return list(map(int, input().split()))

H, W = MAP()
A = []
B = []
for i in range(H):
    A.append(LIST())
for i in range(H):
    B.append(LIST())
dp = [[set() for _ in range(W)] for _ in range(H)]
for i in range(H):
    for j in range(W):
        difference = abs(A[i][j] - B[i][j])
        if i == 0 and j == 0:
            dp[i][j].add(difference)
            continue
        if i == 0:
            for value in dp[i][j - 1]:
                val_1, val_2 = value + difference, value - difference
                dp[i][j].add(val_1)
                dp[i][j].add(val_2)
                continue
        if j == 0:
            for value in dp[i - 1][j]:
                val_1, val_2 = value + difference, value - difference
                dp[i][j].add(val_1)
                dp[i][j].add(val_2)
                continue
        for value in dp[i][j - 1]:
                val_1, val_2 = value + difference, value - difference
                dp[i][j].add(val_1)
                dp[i][j].add(val_2)
        for value in dp[i - 1][j]:
                val_1, val_2 = value + difference, value - difference
                dp[i][j].add(val_1)
                dp[i][j].add(val_2)
print(min(dp[H - 1][W - 1], key=lambda x: abs(x)))

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.buffer.readline

h, w = map(int, input().split())
AA = [list(map(int, input().split())) for _ in range(h)]
BB = [list(map(int, input().split())) for _ in range(h)]

kouho = [[set() for _ in range(w)] for _ in range(h)]

delta = AA[0][0] - BB[0][0]
kouho[0][0].add(delta)
kouho[0][0].add(-delta)
dx = [1, 0]
dy = [0, 1]
for y in range(h):
    for x in range(w):
        for d in range(2):
            nx = x + dx[d]
            ny = y + dy[d]
            if nx < 0 or nx >= w or ny < 0 or ny >= h:
                continue
            delta = AA[ny][nx] - BB[ny][nx]
            for num in kouho[y][x]:
                if 160 >= abs(num + delta):
                    kouho[ny][nx].add(num + delta)
                if 160 >= abs(num - delta):
                    kouho[ny][nx].add(num - delta)
ans = 10 ** 10
for a in kouho[h - 1][w - 1]:
    ans = min(ans, abs(a))
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

input = sys.stdin.buffer.readline

h, w = map(int, input().split())
AA = [list(map(int, input().split())) for _ in range(h)]
BB = [list(map(int, input().split())) for _ in range(h)]

kouho = [[set() for _ in range(w)] for _ in range(h)]

delta = AA[0][0] - BB[0][0]
kouho[0][0].add(delta)
kouho[0][0].add(-delta)
dx = [1, 0]
dy = [0, 1]
for y in range(h):
    for x in range(w):
        for d in range(2):
            nx = x + dx[d]
            ny = y + dy[d]
            if nx < 0 or nx >= w or ny < 0 or ny >= h:
                continue
            delta = AA[ny][nx] - BB[ny][nx]
            for num in kouho[y][x]:
                if 80 >= abs(num + delta):
                    kouho[ny][nx].add(num + delta)
                if 80 >= abs(num - delta):
                    kouho[ny][nx].add(num - delta)
ans = 10 ** 10
for a in kouho[h - 1][w - 1]:
    ans = min(ans, abs(a))
print(ans)

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)]
    
    ofset = 80*164
    #mask = (1 << (ofset*2)) - 1
    dp=[[0 for i in range(w)] for j in range(h)]
    dp[0][0] |= 1 << (a[0][0] - b[0][0] + ofset)
    dp[0][0] |= 1 << (b[0][0] - a[0][0] + ofset)
    """
    for i in range(h):
        for j in range(w):
            if i < h - 1:
                A = abs(a[i+1][j] - b[i+1][j])
                dp[i+1][j] |= (dp[i][j] << A)
                dp[i+1][j] |= (dp[i][j] >> A)
            if j < w - 1:
                A = abs(a[i][j+1] - b[i][j+1])
                dp[i][j+1] |= (dp[i][j] << A)
                dp[i][j+1] |= (dp[i][j] >> A)
    """
    ans = 10000000000
    
    for i in range(ofset, 80*330):
        if (dp[h-1][w-1]>>i)&1:
            ans = i - ofset
            break
    for i in reversed(range(ofset)):
        if (dp[h-1][w-1]>>i)&1:
            ans = min(ans, ofset - i)
            break
    print(ans)
    
if __name__=='__main__':
  main()

Submission status: Wrong Answer

Solution code:
# 経路の数は、(79 + 2) C 79 = 2^79

import sys
readline = sys.stdin.readline

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

# dp[(i,j)] = マスi,jで発生しうる偏り（set型）
from collections import defaultdict
dp = defaultdict(set)
dp[(0,0)].add(abs(A[0][0] - B[0][0]))
for i in range(H):
  for j in range(W):
    val = abs(A[i][j] - B[i][j])
    if i > 0:
      for s in dp[(i - 1,j)]:
        if s + val < 1001:
          dp[(i,j)].add(s + val)
        dp[(i,j)].add(abs(s - val))
      del dp[(i - 1,j)]
    if j > 0:
      for s in dp[(i,j - 1)]:
        if s + val < 1001:
          dp[(i,j)].add(s + val)
        dp[(i,j)].add(abs(s - val))
      
print(min(dp[(H - 1,W - 1)]))

Submission status: Wrong Answer

Solution code:
# 経路の数は、(79 + 2) C 79 = 2^79

import sys
readline = sys.stdin.readline

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

# dp[(i,j)] = マスi,jで発生しうる偏り（set型）
from collections import defaultdict
dp = defaultdict(set)
dp[(0,0)].add(abs(A[0][0] - B[0][0]))
for i in range(H):
  for j in range(W):
    val = abs(A[i][j] - B[i][j])
    if i > 0:
      for s in dp[(i - 1,j)]:
        if s + val < 241:
          dp[(i,j)].add(s + val)
        dp[(i,j)].add(abs(s - val))
      del dp[(i - 1,j)]
    if j > 0:
      for s in dp[(i,j - 1)]:
        if s + val < 241:
          dp[(i,j)].add(s + val)
        dp[(i,j)].add(abs(s - val))
      
print(min(dp[(H - 1,W - 1)]))

Submission status: Wrong Answer

Solution code:
# 経路の数は、(79 + 2) C 79 = 2^79

import sys
readline = sys.stdin.readline

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

# dp[(i,j)] = マスi,jで発生しうる偏り（set型）
from collections import defaultdict
dp = defaultdict(set)
dp[(0,0)].add(abs(A[0][0] - B[0][0]))
for i in range(H):
  for j in range(W):
    val = abs(A[i][j] - B[i][j])
    if i > 0:
      for s in dp[(i - 1,j)]:
        if s + val < 161:
          dp[(i,j)].add(s + val)
        dp[(i,j)].add(abs(s - val))
      del dp[(i - 1,j)]
    if j > 0:
      for s in dp[(i,j - 1)]:
        if s + val < 161:
          dp[(i,j)].add(s + val)
        dp[(i,j)].add(abs(s - val))
      
print(min(dp[(H - 1,W - 1)]))

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)]
grid = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        grid[i][j] = abs(A[i][j] - B[i][j])

dp = [[set() for _ in range(W + 1)] for _ in range(H + 1)]
for i in range(H + 1):
    dp[i][0].add(0)
for j in range(W + 1):
    dp[0][j].add(0)

for i in range(H):
    for j in range(W):
        here = grid[i][j]
        # i -> i + 1
        for v in dp[i][j + 1]:
            dp[i + 1][j + 1].add(abs(v + here))
            dp[i + 1][j + 1].add(abs(-v + here))
            dp[i + 1][j + 1].add(abs(v - here))
            dp[i + 1][j + 1].add(abs(-v - here))
        # j -> j + 1
        for v in dp[i + 1][j]:
            dp[i + 1][j + 1].add(abs(v + here))
            dp[i + 1][j + 1].add(abs(-v + here))
            dp[i + 1][j + 1].add(abs(v - here))
            dp[i + 1][j + 1].add(abs(-v - here))

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

Submission status: Wrong Answer

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

MX = 160
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)]
D = [[0]*W for i in range(H)]
for i in range(H):
    for j in range(W):
        D[i][j] = abs(A[i][j]-B[i][j])
dp = [[[0]*(MX+1) for _ in range(W+1)] for _ in range(H+1)]
dp[1][1][D[0][0]] = 1
for i in range(1,H+1):
    for j in range(1,W+1):
        for k in range(MX+1):
            if dp[i][j][k]:
                if j+1 <= W:
                    d = D[i-1][j]
                    if k+d <= MX:
                        dp[i][j+1][k+d] = 1
                    dp[i][j+1][abs(k-d)] = 1
                if i+1 <= H:
                    d = D[i][j-1]
                    if k+d <= MX:
                        dp[i+1][j][k+d] = 1
                    dp[i+1][j][abs(k-d)] = 1
#for i in range(H+1):
#    for j in range(W+1):
#        print(i, j)
#        print(dp[i][j])
for i in range(MX+1):
    if dp[H][W][i]:
        print(i)
        exit()

Submission status: Wrong Answer

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

MX = 160
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)]
D = [[0]*W for i in range(H)]
for i in range(H):
    for j in range(W):
        D[i][j] = abs(A[i][j]-B[i][j])
dp = [[[0]*(MX+1) for i in range(W+1)] for i in range(H+1)]
for i in range(1,H+1):
    for j in range(1,W+1):
        for k in range(MX+1):
            if i == 1 and j == 1:
                if k == D[i-1][j-1]:
                    dp[i][j][k] = 1
            else:
                if j-1 >= 1 and dp[i][j-1][k]:
                    if k+D[i-1][j-1] <= MX:
                        dp[i][j][k+D[i-1][j-1]] = 1
                    dp[i][j][abs(k-D[i-1][j-1])] = 1
                if i-1 >= 1 and dp[i-1][j][k]:
                    if k+D[i-1][j-1] <= MX:
                        dp[i][j][k+D[i-1][j-1]] = 1
                    dp[i][j][abs(k-D[i-1][j-1])] = 1
#for i in range(H+1):
#    for j in range(W+1):
#        print(i, j)
#        print(dp[i][j])
for i in range(MX+1):
    if dp[H][W][i]:
        print(i)
        exit()

Submission status: Wrong Answer

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

H, W = map(int, input().split())
A = [list(map(int, input().split())) for i in range(H)]
B = [list(map(int, input().split())) for i in range(H)]
D = [[0]*W for i in range(H)]
for i in range(H):
    for j in range(W):
        D[i][j] = abs(A[i][j]-B[i][j])
dp = [[[0]*81 for i in range(W+1)] for i in range(H+1)]
for i in range(1,H+1):
    for j in range(1,W+1):
        for k in range(81):
            if i == 1 and j == 1:
                if k == D[i-1][j-1]:
                    dp[i][j][k] = 1
            else:
                if j-1 >= 1 and dp[i][j-1][k]:
                    if k+D[i-1][j-1] <= 80:
                        dp[i][j][k+D[i-1][j-1]] = 1
                    dp[i][j][abs(k-D[i-1][j-1])] = 1
                if i-1 >= 1 and dp[i-1][j][k]:
                    if k+D[i-1][j-1] <= 80:
                        dp[i][j][k+D[i-1][j-1]] = 1
                    dp[i][j][abs(k-D[i-1][j-1])] = 1
#for i in range(H+1):
#    for j in range(W+1):
#        print(i, j)
#        print(dp[i][j])
for i in range(81):
    if dp[H][W][i]:
        print(i)
        exit()

Submission status: Wrong Answer

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

H, W = map(int, input().split())
A = [list(map(int, input().split())) for i in range(H)]
B = [list(map(int, input().split())) for i in range(H)]
D = [[0]*W for i in range(H)]
for i in range(H):
    for j in range(W):
        D[i][j] = abs(A[i][j]-B[i][j])
dp = [[[0]*81 for i in range(W+1)] for i in range(H+1)]
for i in range(H+1):
    for j in range(W+1):
        for k in range(81):
            if i == 0 or j == 0:
                if (i == 0 and j == 1) or (i == 1 and j == 0):
                    if k == 0:
                        dp[i][j][k] = 1
            else:
                if dp[i][j-1][k]:
                    if k+D[i-1][j-1] <= 80:
                        dp[i][j][k+D[i-1][j-1]] = 1
                    dp[i][j][abs(k-D[i-1][j-1])] = 1
                if dp[i-1][j][k]:
                    if k+D[i-1][j-1] <= 80:
                        dp[i][j][k+D[i-1][j-1]] = 1
                    dp[i][j][abs(k-D[i-1][j-1])] = 1
#for i in range(H+1):
#    for j in range(W+1):
#        print(i, j)
#        print(dp[i][j])
for i in range(81):
    if dp[H][W][i]:
        print(i)
        exit()

Submission status: Wrong Answer

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

H, W = map(int, input().split())
A = [list(map(int, input().split())) for i in range(H)]
B = [list(map(int, input().split())) for i in range(H)]
D = [[0]*W for i in range(H)]
for i in range(H):
    for j in range(W):
        D[i][j] = abs(A[i][j]-B[i][j])
dp = [[[0]*81 for i in range(W+1)] for i in range(H+1)]
dp[0][0][0] = 1
for i in range(H+1):
    for j in range(W+1):
        for k in range(81):
            if i == 0 or j == 0:
                if k == 0:
                    dp[i][j][k] = 1
            else:
                if dp[i][j-1][k]:
                    if k+D[i-1][j-1] <= 80:
                        dp[i][j][k+D[i-1][j-1]] = 1
                    dp[i][j][abs(k-D[i-1][j-1])] = 1
                if dp[i-1][j][k]:
                    if k+D[i-1][j-1] <= 80:
                        dp[i][j][k+D[i-1][j-1]] = 1
                    dp[i][j][abs(k-D[i-1][j-1])] = 1
for i in range(81):
    if dp[H][W][i]:
        print(i)
        exit()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
h, w = map(int, input().split())
a = [list(map(int, input().split())) for _ in range(h)]
b = [list(map(int, input().split())) for _ in range(h)]

inf = 10 ** 4
dp = [[set() for _ in range(w+1)] for _ in range(h+1)]
for i in range(h+1):
    dp[i][0].add(0)
for i in range(w+1):
    dp[0][i].add(0)
for i in range(1, h+1):
    for j in range(1, w+1):
        t = abs(b[i-1][j-1] - a[i-1][j-1])
        for x in dp[i][j-1]:
            dp[i][j].add(abs(x + t))
            dp[i][j].add(abs(x - t))
        for x in dp[i-1][j]:
            dp[i][j].add(abs(x + t))
            dp[i][j].add(abs(x - t))

#print(dp)
ans = sorted(list(dp[h][w]))
print(ans[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)]

inf = 10 ** 4
dp = [[set() for _ in range(w+1)] for _ in range(h+1)]
for i in range(h+1):
    dp[i][0].add(0)
for i in range(w+1):
    dp[0][i].add(0)
for i in range(h):
    for j in range(w):
            for x in dp[i][j+1]:
                dp[i+1][j+1].add(x + (b[i][j] - a[i][j]))
                dp[i+1][j+1].add(x - (b[i][j] - a[i][j]))
            for x in dp[i+1][j]:
                dp[i+1][j+1].add(x + (b[i][j] - a[i][j]))
                dp[i+1][j+1].add(x - (b[i][j] - a[i][j]))

ans = [abs(x) for x in dp[h][w]]
ans.sort()
print(ans[0])

Submission status: Wrong Answer

Solution code:
# Atcoder Problem Solving
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)]
for i in range(H):
    for j in range(W):
        a[i][j]=abs(a[i][j]-b[i][j])
dp=[[0 for   i in range(W)] for j in range(H)]

# Bitset演算
# 答えは絶対値が絶対-80から80以内に収まる

X=80

dp[0][0]|= (1<<(X+a[0][0]))  
dp[0][0]|= (1<<(X-a[0][0]))
# 初期化の条件

for i in range(H):
    for j in range(W):
        if 0<=i<H and 0<=j+1<W:
            dp[i][j+1]|=(dp[i][j]<<a[i][j+1])
            dp[i][j+1]|=(dp[i][j]>>a[i][j+1]) 
            
        if 0<=i+1<H and 0<=j<W:
            d=a[i+1][j]
            dp[i+1][j]|=(dp[i][j]<<a[i+1][j])
            dp[i+1][j]|=(dp[i][j]>>a[i+1][j])
            
A=str(bin(dp[H-1][W-1]))[::-1]
L=len(A)
ans=80
for i in range(L):
    if A[i]=="1":
        ans=min(ans,abs(i-X))
print(ans)

Submission status: Wrong Answer

Solution code:
# Atcoder Problem Solving
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)]
for i in range(H):
    for j in range(W):
        a[i][j]=abs(a[i][j]-b[i][j])
dp=[[0 for   i in range(W)] for j in range(H)]

# Bitset演算
# 答えは絶対値が絶対-80から80以内に収まる

X=80

dp[0][0]|= (1<<(X+a[0][0]))  
dp[0][0]|= (1<<(X-a[0][0]))
# 初期化の条件

for i in range(H):
    for j in range(W):
        if 0<=i<H and 0<=j+1<W:
            dp[i][j+1]|=(dp[i][j]<<a[i][j+1])
            dp[i][j+1]|=(dp[i][j]>>a[i][j+1]) 
            
        if 0<=i+1<H and 0<=j-1<W:
            d=a[i+1][j]
            dp[i+1][j]|=(dp[i][j]<<a[i+1][j])
            dp[i+1][j]|=(dp[i][j]>>a[i+1][j])
            
A=str(bin(dp[H-1][W-1]))[::-1]
L=len(A)

ans=80
for i in range(L-2):
    if A[i]=="1":
        ans=min(ans,abs(i-X))
print(ans)

Submission status: Wrong Answer

Solution code:
# Atcoder Problem Solving
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)]
for i in range(H):
    for j in range(W):
        a[i][j]=abs(a[i][j]-b[i][j])
dp=[[0 for   i in range(W)] for j in range(H)]

# Bitset演算
# 答えは絶対値が絶対-80から80以内に収まる

X=80

dp[0][0]|= (1<<(X+a[0][0]))  
dp[0][0]|= (1<<(X-a[0][0]))
# 初期化の条件

for i in range(H):
    for j in range(W):
        if 0<=i-1<H and 0<=j<W:
            dd=a[i-1][j]
            dp[i][j]|=(dp[i-1][j]<<dd)
            dp[i][j]|=(dp[i-1][j]>>dd) 
            
        if 0<=i<H and 0<=j-1<W:
            d=a[i][j-1]
            dp[i][j]|=(dp[i][j-1]<<d)
            dp[i][j]|=(dp[i][j-1]>>d)
            
A=str(bin(dp[H-1][W-1]))[::-1]
L=len(A)

ans=80
for i in range(L-2):
    if A[i]=="1":
        ans=min(ans,abs(i-X))
print(ans)

Submission status: Wrong Answer

Solution code:
# Atcoder Problem Solving
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)]
for i in range(H):
    for j in range(W):
        a[i][j]=abs(a[i][j]-b[i][j])
dp=[[0 for   i in range(W)] for j in range(H)]

# Bitset演算
# 答えは絶対値が絶対-80から80以内に収まる

X=80

dp[0][0]|= (1<<(X+a[0][0]))  
dp[0][0]|= (1<<(X-a[0][0]))
# 初期化の条件

for i in range(H):
    for j in range(W):
        if 0<=i-1<H and 0<=j<W:
            dd=a[i-1][j]
            dp[i][j]|=(dp[i-1][j]<<dd)
            dp[i][j]|=(dp[i-1][j]>>dd) 
            
        if 0<=i<H and 0<=j-1<W:
            d=a[i][j-1]
            dp[i][j]|=(dp[i][j-1]<<d)
            dp[i][j]|=(dp[i][j-1]>>d)
            
A=str(bin(dp[H-1][W-1]))[::-1]
L=len(A)

ans=80
for i in range(L-2):
    if A[i]=="1":
        ans=min(ans,abs(i-X))
print(ans-1)

Submission status: Wrong Answer

Solution code:
import numpy as np
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=[]
for i in range(h):
  d.append([abs(a[i][j]-b[i][j]) for j in range(w)])
dp=np.zeros((h,w,6400))
# dp[r][c][j]:(r,c)まで考えたときjにできるか。0：できない。1以上：できる。
dp[0,0,d[0][0]+3200]=1
for i in range(w-1):
  if d[0][i+1]==0:
    dp[0,i+1]+=dp[0,i]
    continue
  dp[0,i+1,d[0][i+1]:]+=dp[0,i,:-d[0][i+1]]
  dp[0,i+1,:-d[0][i+1]]+=dp[0,i,d[0][i+1]:]
for i in range(h-1):
  if d[i+1][0]==0:
    dp[i+1,0]+=dp[i,0]
    continue
  dp[i+1,0,d[i+1][0]:]+=dp[i,0,:-d[i+1][0]]
  dp[i+1,0,:-d[i+1][0]]+=dp[i,0,d[i+1][0]:]
for r in range(1,h):
  for c in range(1,w):
    if d[r][c]==0:
      dp[r,c]+=dp[r-1,c]
      dp[r,c]+=dp[r,c-1]
      continue
    dp[r,c,d[r][c]:]+=dp[r-1,c,:-d[r][c]]
    dp[r,c,:-d[r][c]]+=dp[r-1,c,d[r][c]:]
    dp[r,c,d[r][c]:]+=dp[r,c-1,:-d[r][c]]
    dp[r,c,:-d[r][c]]+=dp[r,c-1,d[r][c]:]
print(min([abs(e-3200) for e in np.where(dp[h-1,w-1]>0)[0]]))

Submission status: Wrong Answer

Solution code:
import numpy as np
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=[]
for i in range(h):
  d.append([abs(a[i][j]-b[i][j]) for j in range(w)])
dp=np.zeros((h,w,200))
# dp[r][c][j]:(r,c)まで考えたときjにできるかbool値
dp[0,0,d[0][0]+100]=1
for i in range(w-1):
  if d[0][i+1]==0:
    dp[0,i+1]+=dp[0,i]
    continue
  dp[0,i+1,d[0][i+1]:]+=dp[0,i,:-d[0][i+1]]
  dp[0,i+1,:-d[0][i+1]]+=dp[0,i,d[0][i+1]:]
for i in range(h-1):
  if d[i+1][0]==0:
    dp[i+1,0]+=dp[i,0]
    continue
  dp[i+1,0,d[i+1][0]:]+=dp[i,0,:-d[i+1][0]]
  dp[i+1,0,:-d[i+1][0]]+=dp[i,0,d[i+1][0]:]
for r in range(1,h):
  for c in range(1,w):
    if d[r][c]==0:
      dp[r,c]+=dp[r-1,c]
      dp[r,c]+=dp[r,c-1]
      continue
    dp[r,c,d[r][c]:]+=dp[r-1,c,:-d[r][c]]
    dp[r,c,:-d[r][c]]+=dp[r-1,c,d[r][c]:]
    dp[r,c,d[r][c]:]+=dp[r,c-1,:-d[r][c]]
    dp[r,c,:-d[r][c]]+=dp[r,c-1,d[r][c]:]
print(min([abs(e-100) for e in np.where(dp[h-1,w-1]>0)[0]]))

Submission status: Wrong Answer

Solution code:
import numpy as np
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=[]
for i in range(h):
  d.append([abs(a[i][j]-b[i][j]) for j in range(w)])
dp=np.full((h,w,200),False)
# dp[r][c][j]:(r,c)まで考えたときjにできるかbool値
dp[0,0,d[0][0]+100]=True
for i in range(w-1):
  if d[0][i+1]==0:
    dp[0,i+1]+=dp[0,i]
    continue
  dp[0,i+1,d[0][i+1]:]+=dp[0,i,:-d[0][i+1]]
  dp[0,i+1,:-d[0][i+1]]+=dp[0,i,d[0][i+1]:]
for i in range(h-1):
  if d[i+1][0]==0:
    dp[i+1,0]+=dp[i,0]
    continue
  dp[i+1,0,d[i+1][0]:]+=dp[i,0,:-d[i+1][0]]
  dp[i+1,0,:-d[i+1][0]]+=dp[i,0,d[i+1][0]:]
for r in range(1,h):
  for c in range(1,w):
    if d[r][c]==0:
      dp[r,c]+=dp[r-1,c]
      dp[r,c]+=dp[r,c-1]
      continue
    dp[r,c,d[r][c]:]+=dp[r-1,c,:-d[r][c]]
    dp[r,c,:-d[r][c]]+=dp[r-1,c,d[r][c]:]
    dp[r,c,d[r][c]:]+=dp[r,c-1,:-d[r][c]]
    dp[r,c,:-d[r][c]]+=dp[r,c-1,d[r][c]:]
print(min([abs(e-100) for e in np.where(dp[h-1,w-1]>0)[0]]))

Submission status: Wrong Answer

Solution code:
H,W=map(int,input().split())
A=[list(map(int,input().split())) for i in range(H)]
for i in range(H):
    B=list(map(int,input().split()))
    for j in range(W):
        A[i][j]=abs(A[i][j]-B[j])
D=80*(H+W)+1
D2=D*2
dp=[[0]*W for i in range(H)]
dp[0][0]|=1<<(D+A[0][0])
dp[0][0]|=1<<(D-A[0][0])
for i in range(H):
    for j in range(W):
        for nx,ny in [(i+1,j),(i,j+1)]:
            if nx>=H or ny>=W:
                continue
            dp[nx][ny]|=dp[i][j]<<A[i][j]
            dp[nx][ny]|=dp[i][j]>>A[i][j]
res=dp[-1][-1]
ans=10**18
for i in range(D2):
    if res>>i&1:
        ans=min(ans,abs(D-i))
print(ans-1)

Submission status: Wrong Answer

Solution code:
INF=10**18
H,W=map(int,input().split())
A=[list(map(int,input().split())) for i in range(H)]
for i in range(H):
    B=list(map(int,input().split()))
    for j in range(W):
        A[i][j]=abs(A[i][j]-B[j])

dp=[[[False]*(80*(H+W-1)) for i in range(W)] for i in range(H)]
dp[0][0][A[0][0]]=True
for i in range(H):
    for j in range(W):
        for x,y in [(0,1),(1,0)]:
            nx,ny=i+x,j+y
            if nx>=H or ny>=W:
                continue
            for x in range(81):
                if dp[i][j][x]:
                    if x+A[nx][ny]<=80:
                        dp[nx][ny][x+A[nx][ny]]=True
                        dp[nx][ny][abs(x-A[nx][ny])]=True
for x in range(81):
    if dp[i][j][x]:
        print(x)
        exit()

Submission status: Wrong Answer

Solution code:
INF=10**18
H,W=map(int,input().split())
A=[list(map(int,input().split())) for i in range(H)]
for i in range(H):
    B=list(map(int,input().split()))
    for j in range(W):
        A[i][j]=abs(A[i][j]-B[j])

dp=[[[False]*81 for i in range(W)] for i in range(H)]
dp[0][0][A[0][0]]=True
for i in range(H):
    for j in range(W):
        for x,y in [(0,1),(1,0)]:
            nx,ny=i+x,j+y
            if nx>=H or ny>=W:
                continue
            for x in range(81):
                if dp[i][j][x]:
                    if x+A[nx][ny]<=80:
                        dp[nx][ny][x+A[nx][ny]]=True
                        dp[nx][ny][abs(x-A[nx][ny])]=True
for x in range(81):
    if dp[i][j][x]:
        print(x)
        exit()

Submission status: Wrong Answer

Solution code:
INF=10**18
H,W=map(int,input().split())
A=[list(map(int,input().split())) for i in range(H)]
for i in range(H):
    B=list(map(int,input().split()))
    for j in range(W):
        A[i][j]=abs(A[i][j]-B[j])

dp=[[[INF]*2 for i in range(W)] for i in range(H)]
dp[0][0][1]=A[0][0]
dp[0][0][0]=-A[0][0]
for i in range(H):
    for j in range(W):
        for x,y in [(0,1),(1,0)]:
            nx,ny=i+x,j+y
            if nx>=H or ny>=W:
                continue
            dp[nx][ny][1]=min(dp[nx][ny][1],dp[i][j][1]+A[nx][ny],dp[i][j][0]+A[nx][ny])
            dp[nx][ny][0]=min(dp[nx][ny][0],abs(dp[i][j][0]-A[nx][ny]),abs(dp[i][j][0]-A[nx][ny]))
print(min(dp[H-1][W-1]))

Submission status: Wrong Answer

Solution code:
h,w = map(int,input().split())
a = []
for i in range(h):
  s = list(map(int,input().split()))
  a.append(s)
b = []
for i in range(h):
  t = list(map(int,input().split()))
  b.append(t)

dp = [[[False]*81 for i 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):
    for k in range(81):
      if dp[i][j][k]:
        if i < h-1:
          c = abs(a[i+1][j]-b[i+1][j])
          if k+c < 81:
            dp[i+1][j][k+c] = True
          dp[i+1][j][abs(k-c)] = True
        if j < w-1:
          d = abs(a[i][j+1]-b[i][j+1])
          if k+d < 81:
            dp[i][j+1][k+d] = True
          dp[i][j+1][abs(k-d)] = True
          
for i in range(81):
  if dp[h-1][w-1][i]:
    print(i)
    break

Submission status: Wrong Answer

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

H, W = map(int, input().split())
A = [list(map(int, input().split())) for _ in range(H)]
B = [list(map(int, input().split())) for _ in range(H)]
can_make = [[[False] * 161 for _ in range(W)] for _ in range(H)]
can_make[0][0][A[0][0]-B[0][0]+80] = True
for i in range(H):
    for j in range(W):
        x = abs(A[i][j] - B[i][j])
        if i > 0:
            for k in range(161):
                if can_make[i-1][j][k]:
                    if k + x <= 160:
                        can_make[i][j][k+x] = True
                    if k - x >= 0:
                        can_make[i][j][k-x] = True
        if j > 0:
            for k in range(161):
                if can_make[i][j-1][k]:
                    if k + x <= 160:
                        can_make[i][j][k+x] = True
                    if k - x >= 0:
                        can_make[i][j][k-x] = True
ans = float('inf')
for k in range(161):
    if can_make[-1][-1][k]:
        ans = min(ans, abs(k - 80))
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)]

Z = 100

dp = [[0] * (W + 1) for _ in range(H + 1)]
dp[0][0] = (1 << Z)

for h in range(H):
    for w in range(W):
        d = dp[h][w]
        k = abs(A[h][w] - B[h][w])

        dp[h + 1][w] |= (d << k) | (d >> k)
        dp[h][w + 1] |= (d << k) | (d >> k)

ans = 10**18
for a in (dp[H][W - 1], dp[H - 1][W]):
    for d in range(Z * 2 + 10):
        if a & (1 << d) != 0:
            ans = min(ans, abs(d - Z))
print(ans)