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: Time Limit Exceeded

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)]
grid = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
  for j in range(w):
    grid[i][j] = abs(a[i][j]-b[i][j])
dp = [[[False for i in range(12881)] for j in range(w)] for k in range(h)]
for i in range(h):
  for j in range(w):
    for k in range(12801):
      t = grid[i][j]
      if i == j == 0 and t == k:
        dp[i][j][k] = True
      if j > 0:
        dp[i][j][k] = dp[i][j][k] or dp[i][j-1][k+t] or dp[i][j-1][abs(k-t)]
      if i > 0:
        dp[i][j][k] = dp[i][j][k] or dp[i-1][j][k+t] or dp[i-1][j][abs(k-t)]
for i in range(12801):
  if dp[h-1][w-1][i]:
    print(i)
    break

Submission status: Time Limit Exceeded

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)]
grid = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
  for j in range(w):
    grid[i][j] = abs(a[i][j]-b[i][j])
dp = [[[0 for i in range(12881)] for j in range(w)] for k in range(h)]
for i in range(h):
  for j in range(w):
    for k in range(12801):
      t = grid[i][j]
      if i == j == 0 and t == k:
        dp[i][j][k] = 1
      if j > 0:
        dp[i][j][k] = dp[i][j][k] or dp[i][j-1][abs(k+t)] or dp[i][j-1][abs(k-t)]
      if i > 0:
        dp[i][j][k] = dp[i][j][k] or dp[i-1][j][abs(k+t)] or dp[i-1][j][abs(k-t)]
for i in range(12801):
  if dp[h-1][w-1][i] == 1:
    print(i)
    break

Submission status: Time Limit Exceeded

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)]
grid = [[0 for i in range(w)] for j in range(h)]
for i in range(h):
  for j in range(w):
    grid[i][j] = abs(a[i][j]-b[i][j])
dp = [[[0 for i in range(12881)] for j in range(w)] for k in range(h)]
for i in range(h):
  for j in range(w):
    for k in range(12801):
      t = grid[i][j]
      if i == j == 0 and t == k:
        dp[i][j][k] = 1
      if j > 0:
        dp[i][j][k] = dp[i][j][k] or dp[i][j-1][abs(k+t)] or dp[i][j-1][abs(k-t)]
      if i > 0:
        dp[i][j][k] = dp[i][j][k] or dp[i-1][j][abs(k+t)] or dp[i-1][j][abs(k-t)]
for i in range(12801):
  if dp[h-1][w-1][i] == 1:
    print(i)
    break

Submission status: Time Limit Exceeded

Solution code:
import itertools

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

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

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

S_S = sorted(list(itertools.chain.from_iterable(S)),reverse=True)
MAX_S = sum(S_S[0:H+W-1])
MAX_SS = [[0] * W for _ in range(H)]
DP = [[[False for _ in range(MAX_S+1)] for _ in range(W)] for _ in range(H)]

for i in range(H):
    for j in range(W):
        if i == 0 and j == 0:
            DP[0][0][S[0][0]] = True
            MAX_SS[0][0] = S[0][0]
        else:
            if i - 1 < 0:
                MAX_SS[i][j] = MAX_SS[i][j - 1] + S[i][j]
            elif j - 1 < 0:
                MAX_SS[i][j] = MAX_SS[i - 1][j] + S[i][j]
            else:
                MAX_SS[i][j] = max(MAX_SS[i - 1][j],MAX_SS[i][j - 1]) + S[i][j]

            for d in ((1, 0), (0, 1)):

                if i - d[0] >= 0 and j - d[1] >= 0:
                    for k in range(MAX_SS[i][j] + 1):
                        if k + S[i][j] <= MAX_S:
                            if DP[i-d[0]][j-d[1]][k + S[i][j]]:
                                DP[i][j][k] = True
                        if DP[i-d[0]][j-d[1]][abs(k - S[i][j])]:
                            DP[i][j][k] = True

for i in range(MAX_S+1):
    if DP[H-1][W-1][i]:
        print (i)
        exit(0)

Submission status: Time Limit Exceeded

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

C = [[] for i in range(H)]
for i,(arow,brow) in enumerate(zip(A,B)):
    for a,b in zip(arow,brow):
        d = abs(a-b)
        C[i].append(d)

dp = [set() for i in range(W)]
dp[0].add(0)
for i,crow in enumerate(C):
    dp2 = [set() for j in range(W)]
    for j,(c,dpr) in enumerate(zip(C[i],dp)):
        for d in dpr:
            dp2[j].add(d+c)
            dp2[j].add(d-c)
    for j,c in enumerate(C[i]):
        if j==0: continue
        for d in dp2[j-1]:
            dp2[j].add(d+c)
            dp2[j].add(d-c)
    dp = dp2

print(min(abs(a) for a in dp[-1]))

Submission status: Time Limit Exceeded

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)]
AB=[[abs(A[i][j]-B[i][j]) for j in range(W)] for i in range(H)]

G=[[[0]*(80*162) for j in range(W)] for i in range(H)]
G[0][0][AB[0][0]]=1
sumi=[[0]*W for i in range(H)]
  
from collections import deque

def f(data):
  i=data[0]
  j=data[1]
  owari=(i+j+1)*80+1
  if i<H-1 and j<W-1:
    for k in range(owari):
      if G[i][j][k]>0:
        G[i+1][j][k+AB[i+1][j]]=1
        G[i+1][j][abs(k-AB[i+1][j])]=1
        G[i][j+1][k+AB[i][j+1]]=1
        G[i][j+1][abs(k-AB[i][j+1])]=1
    if sumi[i+1][j]==0:
      q.append([i+1, j])
      sumi[i+1][j]=1
    if sumi[i][j+1]==0:
      q.append([i, j+1])
      sumi[i][j+1]=1
  elif i>=H-1 and j<W-1:
    for k in range(owari):
      if G[i][j][k]>0:
        G[i][j+1][k+AB[i][j+1]]=1
        G[i][j+1][abs(k-AB[i][j+1])]=1
    if sumi[i][j+1]==0:
      q.append([i, j+1])
      sumi[i][j+1]=1
  elif i<H-1 and j>=W-1:
    for k in range(owari):
      if G[i][j][k]>0:
        G[i+1][j][k+AB[i+1][j]]=1
        G[i+1][j][abs(k-AB[i+1][j])]=1
    if sumi[i+1][j]==0:
      q.append([i+1, j])
      sumi[i+1][j]=1
    
q=deque()
q.append([0, 0])
while len(q)>0:
  data=q.popleft()
  f(data)
  
out=0
while G[-1][-1][out]==0:
  out+=1
print(out)

Submission status: Time Limit Exceeded

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)]
AB=[[abs(A[i][j]-B[i][j]) for j in range(W)] for i in range(H)]

G=[[[0]*(80*162) for j in range(W)] for i in range(H)]
G[0][0][AB[0][0]]=1
  
from collections import deque

def f(data):
  i=data[0]
  j=data[1]
  owari=(i+j+1)*80+1
  if i<H-1 and j<W-1:
    for k in range(owari):
      if G[i][j][k]>0:
        G[i+1][j][k+AB[i+1][j]]=1
        G[i+1][j][abs(k-AB[i+1][j])]=1
        G[i][j+1][k+AB[i][j+1]]=1
        G[i][j+1][abs(k-AB[i][j+1])]=1
    q.append([i+1, j])
    q.append([i, j+1])
  elif i>=H-1 and j<W-1:
    for k in range(owari):
      if G[i][j][k]>0:
        G[i][j+1][k+AB[i][j+1]]=1
        G[i][j+1][abs(k-AB[i][j+1])]=1
    q.append([i, j+1])
  elif i<H-1 and j>=W-1:
    for k in range(owari):
      if G[i][j][k]>0:
        G[i+1][j][k+AB[i+1][j]]=1
        G[i+1][j][abs(k-AB[i+1][j])]=1
    q.append([i+1, j])
    
q=deque()
q.append([0, 0])
while len(q)>0:
  data=q.popleft()
  f(data)
  
out=0
while G[-1][-1][out]==0:
  out+=1
print(out)

Submission status: Time Limit Exceeded

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

def main():
    h,w=map(int,input().split())
    a=[list(map(int,input().split())) for _ in range(h)]
    b=[list(map(int,input().split())) for _ in range(h)]
    d=[[] for _ in range(h)]
    for i in range(h):
        for j in range(w):
            d[i].append(abs(a[i][j]-b[i][j]))

    n = 80*(h+w)
    dp = numpy.array([[[1] * (n+1) for j in range(w)] for i in range(h)])
    dp[0][0][d[0][0]]=0

    for i in range(h-1):
        for k in range(n+1):
            if k+d[i+1][0] <= n:
                dp[i+1][0][k] = dp[i][0][k+d[i+1][0]] * dp[i][0][abs(k-d[i+1][0])]
            else:
                dp[i+1][0][k] = dp[i][0][abs(k-d[i+1][0])]

    for j in range(w-1):
        for k in range(n+1):
            if k+d[0][j+1] <= n:
                dp[0][j+1][k] = dp[0][j][k+d[0][j+1]] * dp[0][j][abs(k-d[0][j+1])]
            else:
                dp[0][j+1][k] = dp[0][j][abs(k-d[0][j+1])]

    for i in range(h-1):
        for j in range(w-1):
            for k in range(n+1):
                if k+d[i+1][j+1] <= n:
                    dp[i+1][j+1][k] = dp[i][j+1][k+d[i+1][j+1]] * dp[i][j+1][abs(k-d[i+1][j+1])] * dp[i+1][j][k+d[i+1][j+1]] * dp[i+1][j][abs(k-d[i+1][j+1])]
                else:
                    dp[i+1][j+1][k] = dp[i][j+1][abs(k-d[i+1][j+1])] * dp[i+1][j][abs(k-d[i+1][j+1])]

    res = -1
    t = 0
    while res == -1:
        if dp[h-1][w-1][t]==0:
            res = t
        else:
            t += 1
    print(res)

main()

Submission status: Time Limit Exceeded

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

def main():
    h,w=map(int,input().split())
    a=[list(map(int,input().split())) for _ in range(h)]
    b=[list(map(int,input().split())) for _ in range(h)]
    d=[[] for _ in range(h)]
    for i in range(h):
        for j in range(w):
            d[i].append(abs(a[i][j]-b[i][j]))

    n = 80*(h+w)
    dp = numpy.array([[[False] * (n+1) for j in range(w)] for i in range(h)])
    dp[0][0][d[0][0]]=True

    for i in range(h-1):
        for k in range(n+1):
            if k+d[i+1][0] <= n:
                dp[i+1][0][k] = dp[i][0][k+d[i+1][0]] or dp[i][0][abs(k-d[i+1][0])]
            else:
                dp[i+1][0][k] = dp[i][0][abs(k-d[i+1][0])]

    for j in range(w-1):
        for k in range(n+1):
            if k+d[0][j+1] <= n:
                dp[0][j+1][k] = dp[0][j][k+d[0][j+1]] or dp[0][j][abs(k-d[0][j+1])]
            else:
                dp[0][j+1][k] = dp[0][j][abs(k-d[0][j+1])]

    for i in range(h-1):
        for j in range(w-1):
            for k in range(n+1):
                if k+d[i+1][j+1] <= n:
                    dp[i+1][j+1][k] = dp[i][j+1][k+d[i+1][j+1]] or dp[i][j+1][abs(k-d[i+1][j+1])] or dp[i+1][j][k+d[i+1][j+1]] or dp[i+1][j][abs(k-d[i+1][j+1])]
                else:
                    dp[i+1][j+1][k] = dp[i][j+1][abs(k-d[i+1][j+1])] or dp[i+1][j][abs(k-d[i+1][j+1])]

    res = -1
    t = 0
    while res == -1:
        if dp[h-1][w-1][t]:
            res = t
        else:
            t += 1
    print(res)

main()

Submission status: Time Limit Exceeded

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

def main():
    h,w=map(int,input().split())
    a=[list(map(int,input().split())) for _ in range(h)]
    b=[list(map(int,input().split())) for _ in range(h)]
    d=[[] for _ in range(h)]
    for i in range(h):
        for j in range(w):
            d[i].append(abs(a[i][j]-b[i][j]))

    n = 80*(h+w)
    dp = [[[False] * (n+1) for j in range(w)] for i in range(h)]
    dp[0][0][d[0][0]]=True

    for i in range(h-1):
        for k in range(n+1):
            if k+d[i+1][0] <= n:
                dp[i+1][0][k] = dp[i][0][k+d[i+1][0]] or dp[i][0][abs(k-d[i+1][0])]
            else:
                dp[i+1][0][k] = dp[i][0][abs(k-d[i+1][0])]

    for j in range(w-1):
        for k in range(n+1):
            if k+d[0][j+1] <= n:
                dp[0][j+1][k] = dp[0][j][k+d[0][j+1]] or dp[0][j][abs(k-d[0][j+1])]
            else:
                dp[0][j+1][k] = dp[0][j][abs(k-d[0][j+1])]

    for i in range(h-1):
        for j in range(w-1):
            for k in range(n+1):
                if k+d[i+1][j+1] <= n:
                    dp[i+1][j+1][k] = dp[i][j+1][k+d[i+1][j+1]] or dp[i][j+1][abs(k-d[i+1][j+1])] or dp[i+1][j][k+d[i+1][j+1]] or dp[i+1][j][abs(k-d[i+1][j+1])]
                else:
                    dp[i+1][j+1][k] = dp[i][j+1][abs(k-d[i+1][j+1])] or dp[i+1][j][abs(k-d[i+1][j+1])]

    res = -1
    t = 0
    while res == -1:
        if dp[h-1][w-1][t]:
            res = t
        else:
            t += 1
    print(res)

main()

Submission status: Time Limit Exceeded

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

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)]
  
  dp=[[[] for i in range(w)] for j in range(h)]
  dp[0][0].append(abs(a[0][0]-b[0][0]))
  for i in range(h):
    for j in range(w):
      c=abs(a[i][j]-b[i][j])
      for k in dp[i-1][j]:
        dp[i][j].append(abs(k-c))
        dp[i][j].append(k+c)
      for k in dp[i][j-1]:
        dp[i][j].append(abs(k-c))
        dp[i][j].append(k+c)
      dp[i][j]=list(set(dp[i][j]))
  print(min(dp[h-1][w-1]))

if __name__=='__main__':
  main()

Submission status: Time Limit Exceeded

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

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)]
  
  c=[[0]*w for i in range(h)]
  for i in range(h):
    for j in range(w):
      c[i][j]=abs(a[i][j]-b[i][j])
  
  dp=[[[] for i in range(w)] for j in range(h)]
  dp[0][0].append(c[0][0])
  for i in range(h):
    for j in range(w):
      for k in dp[i-1][j]:
        dp[i][j].append(abs(k-c[i][j]))
        dp[i][j].append(k+c[i][j])
      for k in dp[i][j-1]:
        dp[i][j].append(abs(k-c[i][j]))
        dp[i][j].append(k+c[i][j])
      dp[i][j]=list(set(dp[i][j]))
  print(min(dp[h-1][w-1]))

if __name__=='__main__':
  main()

Submission status: Time Limit Exceeded

Solution code:
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)]
  
  c=[[0]*w for i in range(h)]
  for i in range(h):
    for j in range(w):
      c[i][j]=abs(a[i][j]-b[i][j])
  
  dp=[[[] for i in range(w)] for j in range(h)]
  dp[0][0].append(c[0][0])
  for i in range(h):
    for j in range(w):
      for k in range(len(dp[i-1][j])):
        dp[i][j].append(abs(dp[i-1][j][k]-c[i][j]))
        dp[i][j].append(dp[i-1][j][k]+c[i][j])
      for k in range(len(dp[i][j-1])):
        dp[i][j].append(abs(dp[i][j-1][k]-c[i][j]))
        dp[i][j].append(dp[i][j-1][k]+c[i][j])
      dp[i][j]=list(set(dp[i][j]))
  print(min(dp[h-1][w-1]))

if __name__=='__main__':
  main()

Submission status: Time Limit Exceeded

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

c=[[0]*w for i in range(h)]
for i in range(h):
  for j in range(w):
    c[i][j]=abs(a[i][j]-b[i][j])

dp=[[() for i in range(w)] for j in range(h)]
dp[0][0]+=(c[0][0],)
for i in range(h):
  for j in range(w):
    for k in range(len(dp[i-1][j])):
      dp[i][j]+=(abs(dp[i-1][j][k]-c[i][j]),)
      dp[i][j]+=(dp[i-1][j][k]+c[i][j],)
    for k in range(len(dp[i][j-1])):
      dp[i][j]+=(abs(dp[i][j-1][k]-c[i][j]),)
      dp[i][j]+=(dp[i][j-1][k]+c[i][j],)
    dp[i][j]=tuple(set(dp[i][j]))
print(min(dp[h-1][w-1]))

Submission status: Time Limit Exceeded

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

c=[[0]*w for i in range(h)]
for i in range(h):
  for j in range(w):
    c[i][j]=abs(a[i][j]-b[i][j])

dp=[[[] for i in range(w)] for j in range(h)]
dp[0][0].append(c[0][0])
for i in range(h):
  for j in range(w):
    for k in range(len(dp[i-1][j])):
      dp[i][j].append(abs(dp[i-1][j][k]-c[i][j]))
      dp[i][j].append(dp[i-1][j][k]+c[i][j])
    for k in range(len(dp[i][j-1])):
      dp[i][j].append(abs(dp[i][j-1][k]-c[i][j]))
      dp[i][j].append(dp[i][j-1][k]+c[i][j])
    dp[i][j]=list(set(dp[i][j]))
print(min(dp[h-1][w-1]))

Submission status: Time Limit Exceeded

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

c=[[0]*w for i in range(h)]
for i in range(h):
  for j in range(w):
    c[i][j]=abs(a[i][j]-b[i][j])

dp=[[[0]*(80*(h+w)+1) for i in range(w)] for j in range(h)]
dp[0][0][c[0][0]]=1
for i in range(h):
  for j in range(w):
    for k in range(80*(i+j)+1):
      if dp[i-1][j][abs(k-c[i][j])]==1 or dp[i][j-1][abs(k-c[i][j])]==1 or dp[i-1][j][k+c[i][j]]==1 or dp[i][j-1][k+c[i][j]]:
        dp[i][j][k]=1

for i in range(80*(h+w)+1):
  if dp[h-1][w-1][i]==1:
    print(i)
    break

Submission status: Time Limit Exceeded

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

c=[[0]*w for i in range(h)]
for i in range(h):
  for j in range(w):
    c[i][j]=abs(a[i][j]-b[i][j])

dp=[[[0 for k in range(80*(h+w)+1)] for i in range(w)] for j in range(h)]
dp[0][0][c[0][0]]=1
for i in range(h):
  for j in range(w):
    for k in range(80*(i+j)+1):
      if dp[i-1][j][abs(k-c[i][j])]==1 or dp[i][j-1][abs(k-c[i][j])]==1 or dp[i-1][j][k+c[i][j]]==1 or dp[i][j-1][k+c[i][j]]:
        dp[i][j][k]=1

for i in range(80*(h+w)+1):
  if dp[h-1][w-1][i]==1:
    print(i)
    break

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
import heapq
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, m=DVSR): return pow(x, m - 2, m)
def DIV(x, y, m=DVSR): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def FLIST(n):
    res = [1]
    for i in range(1, n+1): res.append(res[i-1]*i%DVSR)
    return res

H,W=LI()

RS=[LI() for _ in range(H)]
BS=[LI() for _ in range(H)]
DS=[[0]*W for _ in range(H)]

siz = 0
for i in range(H):
    for j in range(W):
        DS[i][j] = abs(RS[i][j] - BS[i][j])
        siz = max(siz, DS[i][j])

siz = 81*(H+W+1)
MP = [[[0]*siz for _ in range(W)] for _ in range(H)]
MP[0][0][DS[0][0]] = 1

for i in range(H):
    for j in range(W):
        d = DS[i][j]
        for k in range(0, siz-d):
            if i > 0:
                MP[i][j][abs(k-d)] = MP[i][j][abs(k-d)] or MP[i-1][j][k]
                MP[i][j][abs(k+d)] = MP[i][j][abs(k+d)] or MP[i-1][j][k]
            if j > 0:
                MP[i][j][abs(k-d)] = MP[i][j][abs(k-d)] or MP[i][j-1][k]
                MP[i][j][abs(k+d)] = MP[i][j][abs(k+d)] or MP[i][j-1][k]

# print(MP[i][j][:100])
for i in range(siz):
    if MP[H-1][W-1][i]:
        print(i)
        exit()

Submission status: Time Limit Exceeded

Solution code:
import sys
import math
import heapq
sys.setrecursionlimit(10**7)
INTMAX = 9223372036854775807
INTMIN = -9223372036854775808
DVSR = 1000000007
def POW(x, y): return pow(x, y, DVSR)
def INV(x, m=DVSR): return pow(x, m - 2, m)
def DIV(x, y, m=DVSR): return (x * INV(y, m)) % m
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def II(): return int(sys.stdin.readline())
def FLIST(n):
    res = [1]
    for i in range(1, n+1): res.append(res[i-1]*i%DVSR)
    return res

H,W=LI()
siz = 81*(H+W+1)
MP = [[[0]*siz for _ in range(W)] for _ in range(H)]

RS=[LI() for _ in range(H)]
BS=[LI() for _ in range(H)]
MP[0][0][abs(RS[0][0]-BS[0][0])] = 1

for i in range(H):
    for j in range(W):
        d = abs(RS[i][j] - BS[i][j])
        for k in range(0, siz-d):
            if i > 0:
                MP[i][j][abs(k-d)] = MP[i][j][abs(k-d)] or MP[i-1][j][k]
                MP[i][j][abs(k+d)] = MP[i][j][abs(k+d)] or MP[i-1][j][k]
            if j > 0:
                MP[i][j][abs(k-d)] = MP[i][j][abs(k-d)] or MP[i][j-1][k]
                MP[i][j][abs(k+d)] = MP[i][j][abs(k+d)] or MP[i][j-1][k]

# print(MP[i][j][:100])
for i in range(siz):
    if MP[H-1][W-1][i]:
        print(i)
        exit()

Submission status: Time Limit Exceeded

Solution code:
def main():
    H, W = map(int, input().split())
    N = 80 * (H+W) // 2
    A = [list(map(int, input().split())) for _ in range(H)]
    B = [list(map(int, input().split())) for _ in range(H)]
    dp = [[set() for _ in range(W+1)] for _ in range(2)]
    dp[0][1].add(0)
    def ltN(x): return x <= N
    for i in range(H):
        dp[(i+1) & 1] = [set() for _ in range(W+1)]
        for j in range(W):
            diff = abs(A[i][j] - B[i][j])
            dp[(i+1) & 1][j+1] |= set(filter(ltN, [abs(x + diff)
                                                   for x in dp[i & 1][j+1]]))
            dp[(i+1) & 1][j+1] |= set(filter(ltN, [abs(x - diff)
                                                   for x in dp[i & 1][j+1]]))
            dp[(i+1) & 1][j+1] |= set(filter(ltN, [abs(x + diff)
                                                   for x in dp[(i+1) & 1][j]]))
            dp[(i+1) & 1][j+1] |= set(filter(ltN, [abs(x - diff)
                                                   for x in dp[(i+1) & 1][j]]))
    print(min(dp[H & 1][W]))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    H, W = map(int, input().split())
    N = 80 * (H+W) // 2 + 1
    A = [list(map(int, input().split())) for _ in range(H)]
    B = [list(map(int, input().split())) for _ in range(H)]
    dp = [[[False] * N for _ in range(W+1)] for _ in range(2)]
    dp[0][1][0] = True
    for i in range(H):
        dp[(i+1) & 1] = [[False] * N for _ in range(W+1)]
        for j in range(W):
            diff = abs(A[i][j] - B[i][j])
            for k in range(N):
                dp[(i+1) & 1][j+1][abs(k - diff)] |= dp[i & 1][j+1][k]
                dp[(i+1) & 1][j+1][abs(k - diff)] |= dp[(i+1) & 1][j][k]
                if k + diff < N:
                    dp[(i+1) & 1][j+1][k + diff] |= dp[i & 1][j+1][k]
                    dp[(i+1) & 1][j+1][k + diff] |= dp[(i+1) & 1][j][k]
    print(dp[H & 1][W].index(True))


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
H,W = map(int,input().split())
A = []
B = []
for i in range(H):
    a = list(map(int,input().split()))
    A.append(a)
for i in range(H):
    b = list(map(int,input().split()))
    B.append(b)
s = 0
for i in range(W):
    for j in range(H):
        s += abs(A[j][i]-B[j][i])
dp = [[[0 for i in range(s+1)] for i in range(H+1)] for i in range(W+1)]
dp[0][1][0] = 1
dp[1][0][0] = 1
for i in range(1,W+1):
    for j in range(1,H+1):
        x = i-1
        y = j-1
        z = A[y][x] - B[y][x]
        if z < 0:
            z = -z
        for k in range(s+1):
            p = k - z
            if p < 0:
                p = -p
            if dp[i-1][j][p] == 1:
                dp[i][j][k] = 1
            if dp[i][j-1][p] == 1:
                dp[i][j][k] = 1
            if k + z <= s:
                q = k + z
                if dp[i-1][j][q] == 1:
                    dp[i][j][k] = 1
                if dp[i][j-1][q] == 1:
                    dp[i][j][k] = 1
for i in range(s+1):
    if dp[W][H][i] == 1:
        print(i)
        quit()

Submission status: Time Limit Exceeded

Solution code:
H,W = map(int,input().split())
A = []
B = []
for i in range(H):
    a = list(map(int,input().split()))
    A.append(a)
for i in range(H):
    b = list(map(int,input().split()))
    B.append(b)
s = 0
for i in range(W):
    for j in range(H):
        s += abs(A[j][i]-B[j][i])
dp = [[[0 for i in range(s+1)] for i in range(H+1)] for i in range(W+1)]
dp[0][1][0] = 1
dp[1][0][0] = 1
for i in range(1,W+1):
    for j in range(1,H+1):
        x = i-1
        y = j-1
        z = A[y][x] - B[y][x]
        if z < 0:
            z = -z
        for k in range(s+1):
            p = k - z
            if p < 0:
                p = -p
            if dp[i-1][j][p] == 1:
                dp[i][j][k] = 1
            if dp[i][j-1][p] == 1:
                dp[i][j][k] = 1
            if k + z <= s:
                q = k + z
                if dp[i-1][j][q] == 1:
                    dp[i][j][k] = 1
                if dp[i][j-1][q] == 1:
                    dp[i][j][k] = 1
for i in range(s+1):
    if dp[W][H][i] == 1:
        print(i)
        quit()

Submission status: Time Limit Exceeded

Solution code:
H,W = map(int,input().split())
dp = [[[0 for i in range(80*(W+H)+1)] for i in range(H+1)] for i in range(W+1)]
dp[0][1][0] = 1
dp[1][0][0] = 1
A = []
B = []
for i in range(H):
    a = list(map(int,input().split()))
    A.append(a)
for i in range(H):
    b = list(map(int,input().split()))
    B.append(b)
for i in range(1,W+1):
    for j in range(1,H+1):
        x = i-1
        y = j-1
        z = A[y][x] - B[y][x]
        if z < 0:
            z = -z
        for k in range(80*(H+W)+1):
            p = k - z
            if p < 0:
                p = -p
            if dp[i-1][j][p] == 1:
                dp[i][j][k] = 1
            if dp[i][j-1][p] == 1:
                dp[i][j][k] = 1
            if k + z <= 80*(H+W):
                q = k + z
                if dp[i-1][j][q] == 1:
                    dp[i][j][k] = 1
                if dp[i][j-1][q] == 1:
                    dp[i][j][k] = 1
for i in range(80*(W+H)+1):
    if dp[W][H][i] == 1:
        print(i)
        quit()

Submission status: Time Limit Exceeded

Solution code:

import sys
from collections import defaultdict
from collections import deque
import numpy as np
import heapq
from heapq import heappush, heappop
import itertools
import math
import copy

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

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

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

C = []
for i in range(H):
    cs = []
    for j in range(W):
        c = abs(A[i][j] - B[i][j])
        cs.append(c)

    C.append(cs)

D = [[[False for _ in range(H*W*80)] for _ in range(W)] for _ in range(H)]

D[0][0][C[0][0]] = True

# print(D[0][0])

for i in range(1, H+W-1):
    #print("i", i)
    for j in range(i+1):
        #print("j", j)
        x = i - j
        y = j
        #print("x", x, "y", y)

        if 0 <= x <= H-1 and 0 <= y <= W - 1:
            if 0 <= x-1 <= H-1:
                for n, bl in enumerate(D[x-1][y]):
                    if not bl:
                        continue
                    # print(n)

                    n2 = n + C[x][y]
                    D[x][y][n2] = True

                    n2 = abs(n - C[x][y])
                    D[x][y][n2] = True

            if 0 <= y-1 <= W-1:
                for n, bl in enumerate(D[x][y-1]):
                    if not bl:
                        continue

                    # print(n)

                    n2 = n + C[x][y]
                    D[x][y][n2] = True

                    n2 = abs(n - C[x][y])
                    D[x][y][n2] = True

            #print("x", x, "y", y, "dxy",  D[x][y])

ans = 0

for i, bl in enumerate(D[H-1][W-1]):
    if bl:
        ans = i
        break


print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
X, Y, U = [], [], []
if True:
    H, W, *nums = map(int, sys.stdin.read().split())
    i = 0
    for _ in range(H):
        X.append(nums[i:i + W])
        i += W
        U.append([set() for _ in range(W)])
    for _ in range(H):
        Y.append(nums[i:i + W])
        i += W
else:
    H, W = map(int, input().split())
    for _ in range(H):
        X.append(list(map(int, input().split())))
        U.append([set() for _ in range(W)])
    for _ in range(H):
        Y.append(list(map(int, input().split())))

U[0][0] = set([ abs(X[0][0] - Y[0][0]) ])

for i in range(W):
    for j in range(H):
        for k in U[j][i]:
            if j + 1 < H:
                x = abs(X[j + 1][i] - Y[j + 1][i])
                U[j + 1][i].update([abs(k + x), abs(k - x)])
            if i + 1 < W:
                x = abs(X[j][i + 1] - Y[j][i + 1])
                U[j][i + 1].update([abs(k + x), abs(k - x)])
print(min(U[-1][-1]))

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
X, Y, U = [], [], []
for _ in range(H):
    X.append(list(map(int, input().split())))
    U.append([set() for _ in range(W)])
for _ in range(H):
    Y.append(list(map(int, input().split())))

U[0][0] = set([ abs(X[0][0] - Y[0][0]) ])

for i in range(W):
    for j in range(H):
        for k in U[j][i]:
            if j + 1 < H:
                x = abs(X[j + 1][i] - Y[j + 1][i])
                U[j + 1][i].update([abs(k + x), abs(k - x)])
            if i + 1 < W:
                x = abs(X[j][i + 1] - Y[j][i + 1])
                U[j][i + 1].update([abs(k + x), abs(k - x)])
print(min(U[-1][-1]))

Submission status: Time Limit Exceeded

Solution code:
H, W = map(int, input().split())
X, Y, U = [], [], []
for _ in range(H):
    X.append(list(map(int, input().split())))
    U.append([set() for _ in range(W)])
for _ in range(H):
    Y.append(list(map(int, input().split())))

U[0][0] = set([ abs(X[0][0] - Y[0][0]) ])

for i in range(W):
    for j in range(H):
        for k in U[j][i]:
            if j + 1 < H:
                U[j + 1][i].add(abs(k + abs(X[j + 1][i] - Y[j + 1][i])))
                U[j + 1][i].add(abs(k - abs(X[j + 1][i] - Y[j + 1][i])))
            if i + 1 < W:
                U[j][i + 1].add(abs(k + abs(X[j][i + 1] - Y[j][i + 1])))
                U[j][i + 1].add(abs(k - abs(X[j][i + 1] - Y[j][i + 1])))
print(min(U[-1][-1]))

Submission status: Time Limit Exceeded

Solution code:
def main():
    H, W = map(int, input().split())
    A = [list(map(int, input().split())) for _ in range(H)]
    B = [list(map(int, input().split())) for _ in range(H)]
    dp = [[set() for _ in range(W+1)] for _ in range(H+1)]
    dp[1][1].add(abs(A[0][0] - B[0][0]))
    for i in range(H):
        for j in range(W):
            diff = abs(A[i][j] - B[i][j])
            dp[i+1][j+1] |= set(map(lambda x: abs(x + diff), dp[i][j+1]))
            dp[i+1][j+1] |= set(map(lambda x: abs(x - diff), dp[i][j+1]))
            dp[i+1][j+1] |= set(map(lambda x: abs(x + diff), dp[i+1][j]))
            dp[i+1][j+1] |= set(map(lambda x: abs(x - diff), dp[i+1][j]))
    print(min(dp[H][W]))


if __name__ == "__main__":
    main()