Problem p02839 (python)

Fixed submission:
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)

Buggy submission (verdict: Runtime Error):
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)]
AB=[[abs(A[i][j]-B[i][j]) for j in range(W)] for i in range(H)]
 
G=[[[0]*(80*160+1) for j in range(W)] for i in range(H)]
G[0][0][AB[0][0]]=1
 
for i in range(1, W):
  owari=80*i+1
  G[0][i][:owari]=[1 if G[0][i-1][abs(k-AB[0][i])]+G[0][i-1][abs(k+AB[0][i])]>0 else 0 for k in range(owari+1)]
  
for i in range(1, H):
  owari=80*i+1
  G[i][0][:owari]=[1 if G[i-1][0][abs(k-AB[i][0])]+G[0][i-1][abs(k+AB[i][0])]>0 else 0 for k in range(owari+1)]

from collections import deque

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

Buggy submission (verdict: Runtime Error):
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)]
AB=[[abs(A[i][j]-B[i][j]) for j in range(W)] for i in range(H)]
 
G=[[[0]*(80*160+1) for j in range(W)] for i in range(H)]
G[0][0][AB[0][0]]=1
 
for i in range(1, W):
  owari=80*i+1
  G[0][i][:owari]=[1 if G[0][i-1][abs(k-AB[0][i])]+G[0][i-1][abs(k+AB[0][i])]>0 else 0 for k in range(owari+1)]
  
for i in range(1, H):
  owari=80*i+1
  G[i][0][:owari]=[1 if G[i-1][0][abs(k-AB[i][0])]+G[0][i-1][abs(k+AB[i][0])]>0 else 0 for k in range(owari+1)]

from collections import deque

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