Problem p02839 (python)

Fixed submission:
#!/usr/bin/env python3
#ABC147 E

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

h,w = LI()
a = [LI() for _ in range(h)]
b = [LI() for _ in range(h)]

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

dp[0][0] |= 1 << (6400+lst[0][0])
dp[0][0] |= 1 << (6400-lst[0][0])
for i in range(h):
    for j in range(w):
        t = dp[i][j]
        if i+1 < h:
            dp[i+1][j] |= t << lst[i+1][j]
            dp[i+1][j] |= t >> lst[i+1][j]
        if j+1 < w:
            dp[i][j+1] |= t << lst[i][j+1]
            dp[i][j+1] |= t >> lst[i][j+1]

x = dp[-1][-1]
for i in range(6401)[::-1]:
    if (x >> i) & 1:
        print(6400 - i)
        break

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
#ABC147 E

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

h,w = LI()
a = [LI() for _ in range(h)]
b = [LI() for _ in range(h)]

lst = [[0]*w for _ in range(h)]
m = 0
for i in range(h):
    for j in range(w):
        lst[i][j] = abs(a[i][j] - b[i][j])
        m = max(m,abs(a[i][j] - b[i][j]))

dp = [[[0]*((h+w-1)*m) for _ in range(w)] for _ in range(h)]
dp[0][0][lst[0][0]] = 1
for i in range(h):
    for j in range(w):
        for k in range(m*(h+w-1)):
            if dp[i][j][k]:
                if i+1 < h:
                    if k+lst[i+1][j] < m*(h+w-1):
                        dp[i+1][j][k+lst[i+1][j]] = 1
                    if abs(k-lst[i+1][j]) < m*(h+w-1):
                        dp[i+1][j][abs(k-lst[i+1][j])] = 1
                if j+1 < w:
                    if k+lst[i][j+1] < m*(h+w-1):
                        dp[i][j+1][k+lst[i][j+1]] = 1
                    if abs(k-lst[i][j+1]) < m*(h+w-1):
                        dp[i][j+1][abs(k-lst[i][j+1])] = 1

ans = 10**10
for i in range(m*(h+w-1)):
    if dp[-1][-1][i]:
        ans = min(ans,i)
print(ans)

Buggy submission (verdict: Runtime Error):
#!/usr/bin/env python3
#ABC147 E

import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(1000000)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

h,w = LI()
a = [LI() for _ in range(h)]
b = [LI() for _ in range(h)]

lst = [[0]*w for _ in range(h)]
m = 0
for i in range(h):
    for j in range(w):
        lst[i][j] = abs(a[i][j] - b[i][j])
        m = max(m,abs(a[i][j] - b[i][j]))

dp = [[[0]*(h+w)*m for _ in range(w)] for _ in range(h)]
dp[0][0][lst[0][0]] = 1
for i in range(h):
    for j in range(w):
        for k in range(m*(h+w)):
            if dp[i][j][k]:
                if i+1 < h:
                    if k+lst[i+1][j] < m*(h+w):
                        dp[i+1][j][k+lst[i+1][j]] = 1
                    dp[i+1][j][abs(k-lst[i+1][j])] = 1
                if j+1 < w:
                    if k+lst[i][j+1] < m*(h+w):
                        dp[i][j+1][k+lst[i][j+1]] = 1
                    dp[i][j+1][abs(k-lst[i][j+1])] = 1

ans = 10**10
for i in range(m*(h+w)):
    if dp[-1][-1][i]:
        ans = min(ans,i)
print(ans)