Problem p02839 (python)

Fixed submission:
#!/usr/bin/env python3
import sys, math, itertools, heapq, collections, bisect,copy
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
sys.setrecursionlimit(10**7)
inf = float('inf')
mod = 10**9+7
ans = inf ;count = 0 ;pro = 1

h,w=map(int,input().split())
A=[tuple(map(int,input().split())) for i in range(h)]
B=[tuple(map(int,input().split())) for i in range(h)]
# a=[set() for j in range(w)]
K=160*80
dp = [[0]*w for i in range(h)]
dp[0][0]=1<<K
for i in range(h):
    for j in range(w):
        d=abs(A[i][j]-B[i][j])
        if i-1>=0:
            dp[i][j] |= dp[i-1][j]
        if j-1>=0:
            dp[i][j] |= dp[i][j-1]
        dp[i][j] = (dp[i][j]<<d) | (dp[i][j]>>d)


ans = K
for i in range(2*K):
    if dp[h-1][w-1]  & (1 << i)>0:
        ans = min(ans,abs(K-i))
print(ans)
# for di in dp:
#     print(di)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys, math, itertools, heapq, collections, bisect,copy
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
sys.setrecursionlimit(10**7)
inf = float('inf')
mod = 10**9+7
ans = inf ;count = 0 ;pro = 1

h,w=map(int,input().split())
A=[tuple(map(int,input().split())) for i in range(h)]
B=[tuple(map(int,input().split())) for i in range(h)]
K = 55000
dp = [[[]*K for j in range(w)] for i in range(h)]
dp[0][0][0]=1
for i in range(h):
    for j in range(w):
        d=abs(A[i][j]-B[i][j])
        if i-1>=0:
            for k in range(K):
                if dp[i-1][j][k]:
                    dp[i][j][k+d]=1
                    dp[i][j][abs(k-d)]=1
        if j-1>=0:
            for k in range(K):
                if dp[i][j-1][k]:
                    dp[i][j][k+d] = 1
                    dp[i][j][abs(k-d)]=1
for ki in dp[h-1][w-1]:
    ans=min(ans,ki)
print(ans)
# for di in dp:
#     print(di)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys, math, itertools, heapq, collections, bisect,copy
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
sys.setrecursionlimit(10**7)
inf = float('inf')
mod = 10**9+7
ans = inf ;count = 0 ;pro = 1

h,w=map(int,input().split())
A=[tuple(map(int,input().split())) for i in range(h)]
B=[tuple(map(int,input().split())) for i in range(h)]
K = 55000
dp = [[[]*K for j in range(w)] for i in range(h)]
dp[0][0][0]=1
for i in range(h):
    for j in range(w):
        d=abs(A[i][j]-B[i][j])
        if i-1>=0:
            for k in range(K):
                if dp[i-1][j][k]:
                    dp[i][j][k+d]=1
                    dp[i][j][abs(k-d)]
        if j-1>=0:
            for k in range(K):
                if dp[i][j-1][k]:
                    dp[i][j][k+d] = 1
                    dp[i][j][abs(k-d)]
for ki in dp[h-1][w-1]:
    ans=min(ans,ki)
print(ans)
# for di in dp:
#     print(di)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
import sys, math, itertools, heapq, collections, bisect,copy
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
sys.setrecursionlimit(10**7)
inf = float('inf')
mod = 10**9+7
ans = inf ;count = 0 ;pro = 1

h,w=map(int,input().split())
A=[tuple(map(int,input().split())) for i in range(h)]
B=[tuple(map(int,input().split())) for i in range(h)]
K = 51300
dp = [[[]*K for j in range(w)] for i in range(h)]
dp[0][0][0]=1
for i in range(h):
    for j in range(w):
        d=abs(A[i][j]-B[i][j])
        if i-1>=0:
            for k in range(K):
                if dp[i-1][j][k]:
                    dp[i][j][k+d]=1
                    dp[i][j][abs(k-d)]
        if j-1>=0:
            for k in range(K):
                if dp[i][j-1][k]:
                    dp[i][j][k+d] = 1
                    dp[i][j][abs(k-d)]
for ki in dp[h-1][w-1]:
    ans=min(ans,ki)
print(ans)
# for di in dp:
#     print(di)