Problem p02839 (python)

Fixed submission:
h, w = map(int, input().split())
mat = []
for i in range(h):
  a = list(map(int, input().split()))
  mat.append(a)
for i in range(h):
  b = list(map(int, input().split()))
  for j in range(w):
    mat[i][j] = abs(mat[i][j]-b[j])
m = max(map(max, mat))
dp = [[0b0 for i in range(w)] for k in range(h)]
dp[0][0] |= 0b1 << mat[0][0]
for y in range(h):
  for x in range(w):
        if x > 0:
          dp[y][x] |= dp[y][x-1] << mat[y][x]
          dp[y][x] |= dp[y][x-1] >> mat[y][x]
          anded = dp[y][x-1] & int('1'*(mat[y][x]+1), 2)
          reverse = format(anded, "0"+str(mat[y][x]+1)+"b")[::-1]

          dp[y][x] |= int(reverse, 2)
        if y > 0:
          dp[y][x] |= dp[y-1][x] << mat[y][x]
          dp[y][x] |= dp[y-1][x] >> mat[y][x]
          anded = dp[y-1][x] & int('1'*(mat[y][x]+1), 2)
          reverse = format(anded, "0"+str(mat[y][x]+1)+"b")[::-1]

          dp[y][x] |= int(reverse, 2)
        #print(bin(dp[y][x]))

keta = 0
while dp[h-1][w-1] % 2 == 0:
  keta += 1
  dp[h-1][w-1] //= 2
print(keta)

Buggy submission (verdict: Runtime Error):
h, w = map(int, input().split())
mat = []
for i in range(h):
  a = list(map(int, input().split()))
  mat.append(a)
for i in range(h):
  b = list(map(int, input().split()))
  for j in range(w):
    mat[i][j] = abs(mat[i][j]-b[j])
m = max(map(max, mat))
dp = [[[False]*(m*(h+w)) for i in range(w)] for k in range(h)]
dp[0][0][mat[0][0]] = True
for y in range(h):
  for x in range(w):
      for k in range(m*(y+x)):
        if x > 0 and dp[y][x-1][k] or y > 0 and dp[y-1][x][k]:
          d = dp[y][x]
          d[k+mat[y][x]] = True
          d[abs(k-mat[y][x])] = True

for k in range(m*(h+w)):
  if dp[h-1][w-1][k]:
    print(k)
    exit()

Buggy submission (verdict: Runtime Error):
h, w = map(int, input().split())
mat = []
for i in range(h):
  a = list(map(int, input().split()))
  mat.append(a)
for i in range(h):
  b = list(map(int, input().split()))
  for j in range(w):
    mat[i][j] = abs(mat[i][j]-b[j])

dp = [[[False]*(80*(h+w)) for i in range(w)] for k in range(h)]
dp[0][0][mat[0][0]] = True
for y in range(h):
  for x in range(w):
      for k in range(sum(map(sum, mat[y][x]))):
        if x > 0 and dp[y][x-1][k] or y > 0 and dp[y-1][x][k]:
          d = dp[y][x]
          d[k+mat[y][x]] = True
          d[abs(k-mat[y][x])] = True

for k in range(80*(h+w)):
  if dp[h-1][w-1][k]:
    print(k)
    exit()

Buggy submission (verdict: Runtime Error):
h, w = map(int, input().split())
mat = []
count = 0
for i in range(h):
  a = list(map(int, input().split()))
  mat.append(a)
for i in range(h):
  b = list(map(int, input().split()))
  for j in range(w):
    mat[i][j] = abs(mat[i][j]-b[j])
    count += mat[i][j]

dp = [[[False]*(count) for i in range(w)] for k in range(h)]
dp[0][0][mat[0][0]] = True
for y in range(h):
  for x in range(w):
      for k in range(count):
        if x > 0 and dp[y][x-1][k] or y > 0 and dp[y-1][x][k]:
          d = dp[y][x]
          d[k+mat[y][x]] = True
          d[abs(k-mat[y][x])] = True

for k in range(count):
  if dp[h-1][w-1][k]:
    print(k)
    exit()

Buggy submission (verdict: Runtime Error):
h, w = map(int, input().split())
mat = []
for i in range(h):
  a = list(map(int, input().split()))
  mat.append(a)
for i in range(h):
  b = list(map(int, input().split()))
  for j in range(w):
    mat[i][j] = abs(mat[i][j]-b[j])

dp = [[80*80*80]*w for i in range(h)]
for y in range(h):
  for x in range(w):
    if x > 0:
      dp[y][x] = min(dp[y][x], abs(dp[y][x-1]-mat[y][x]))
    if y > 0:
      dp[y][x] = min(dp[y][x], abs(dp[y-1][x]-mat[y][x]))
print(dp[h][w])