Problem p02839 (python)

Fixed submission:
import sys
def main():
  input = sys.stdin.readline
  h, w = map(int, input().split())
  A = []
  AB = []
  A_append = A.append
  AB_append = AB.append
  for i in range(h):
    row = list(map(int, input().split()))
    A_append(row)
  for i in range(h):
    row = [abs(A[i][j] - int(b)) for j, b in enumerate(input().split())]
    AB_append(row)
 
  dp_cur = [set([]) for _ in range(w)]
  dp_cur[0].add(AB[0][0])
  for i in range(h):
    dp_next = [set([]) for _ in range(w)]
    for j in range(w):
      if j < w - 1:
        add1 = dp_cur[j+1].add
        dif1 = AB[i][j+1]
      if i < h - 1:
        add2 = dp_next[j].add
        dif2 = AB[i+1][j]
      for item in dp_cur[j]:
        if j < w - 1:
          add1(item+dif1)
          add1(abs(item-dif1))
        if i < h - 1:
          add2(item+dif2)
          add2(abs(item-dif2))
    if i == h - 1:
      break
    dp_cur = dp_next
  print(min(dp_cur[w-1]))
if __name__ == '__main__':
  main()

Buggy submission (verdict: Runtime Error):
import sys
def main():
  input = sys.stdin.readline
  h, w = map(int, input().split())
  A = []
  AB = []
  A_append = A.append
  AB_append = AB.append
  for i in range(h):
    row = list(map(int, input().split()))
    A_append(row)
  for i in range(h):
    row = [abs(A[i][j] - int(b)) for j, b in enumerate(input().split())]
    AB_append(row)
 
  dp_cur = [set([]) for _ in range(w)]
  dp_cur[0].add(AB[0][0])
  for i in range(h):
    dp_next = [set([]) for _ in range(w)]
    for j in range(w):
      if j < w - 1:
        add1 = dp_cur[j+1].add
        dif1 = AB[i][j+1]
      if i < h - 1:
        add2 = dp_next[j].add
        dif2 = AB[i+1][j]
      for item in dp_cur[j]:
        if j < w - 1:
          add1(item+dif1)
          add1(abs(item-dif1))
        if i < h - 1:
          add2(item+dif2)
          add2(abs(item-dif2))
    dp_cur = dp_next
  print(min(dp_cur[w-1]))
if __name__ == '__main__':
  main()

Buggy submission (verdict: Runtime Error):
import sys
def main():
  input = sys.stdin.readline
  h, w = map(int, input().split())
  A = []
  AB = []
  A_append = A.append
  AB_append = AB.append
  for i in range(h):
    row = list(map(int, input().split()))
    A_append(row)
  for i in range(h):
    row = [abs(A[i][j] - int(b)) for j, b in enumerate(input().split())]
    AB_append(row)
 
  dp = [[set([]) for j in range(w+1)] for i in range(h+1)]
  dp[0][0].add(AB[0][0])
  for i in range(h):
    for j in range(w):
      dif1 = 0
      dif2 = 0
      if j < w - 1:
        add1 = dp[i][j+1].add
        dif1 = AB[i][j+1]
      if i < h - 1:
        add2 = dp[j+1][i].add
        dif2 = AB[i+1][j]
      for item in dp[i][j]:
        if j < w - 1:
          add1(item+dif1)
          add1(abs(item-dif1))
        if i < h - 1:
          add2(item+dif2)
          add2(abs(item-dif2))
  print(min(dp[h-1][w-1]))
if __name__ == '__main__':
  main()

Buggy submission (verdict: Runtime Error):
import sys
def main():
  input = sys.stdin.readline
  h, w = map(int, input().split())
  A = []
  AB = []
  A_append = A.append
  AB_append = AB_append
  for i in range(h):
    row = list(map(int, input().split()))
    A_append(row)
  for i in range(h):
    row = [abs(A[i][j] - int(b)) for j, b in enumerate(input().split())]
    AB_append(row)
 
  dp = [[set([]) for j in range(w+1)] for i in range(h+1)]
  dp[0][0].add(AB[0][0])
  for i in range(h):
    for j in range(w):
      dif1 = 0
      dif2 = 0
      if j < w - 1:
        add1 = dp[i][j+1].add
        dif1 = AB[i][j+1]
      if i < h - 1:
        add2 = dp[j+1][i].add
        dif2 = AB[i+1][j]
      for item in dp[i][j]:
        if j < w - 1:
          add1(item+dif1)
          add1(abs(item-dif1))
        if i < h - 1:
          add2(item+dif2)
          add2(abs(item-dif2))
  print(min(dp[h-1][w-1]))
if __name__ == '__main__':
  main()

Buggy submission (verdict: Runtime Error):
def main():
  import sys
  input = sys.stdin.readline
  h, w = map(int, input().split())
  A = []
  AB = []
  for i in range(h):
    row = list(map(int, input().split()))
    A.append(row)
  for i in range(h):
    row = [abs(A[i][j] - int(b)) for j, b in enumerate(input().split())]
    AB.append(row)
 
  dp = [[{} for j in range(w+1)] for i in range(h+1)]
  dp[0][0][abs(A[0][0] - B[0][0])] = 1
  for i in range(h):
    for j in range(w):
      dif1 = 0
      dif2 = 0
      if j < w - 1:
        dif1 = AB[i][j+1]
      if i < h - 1:
        dif2 = AB[i+1][j]
      for item in dp[i][j]:
        if j < w - 1:
          dp[i][j+1][item+dif1] = 1
          dp[i][j+1][abs(item-dif1)] = 1
        if i < h - 1:
          dp[i+1][j][item+dif2] = 1
          dp[i+1][j][abs(item-dif2)] = 1
  print(min(dp[h-1][w-1].keys()))
if __name__ == '__main__':
  main()

Buggy submission (verdict: Runtime Error):
def main():
  import sys
  input = sys.stdin.readline
  h, w = map(int, input().split())
  A = []
  B = []
  for i in range(h):
    row = list(map(int, input().split()))
    A.append(row)
  for i in range(h):
    row = list(map(int, input().split()))
    B.append(row)
 
  dp = [[{} for j in range(w+1)] for i in range(h+1)]
  dp[0][0][abs(A[0][0] - B[0][0])] = 1
  for i in range(h):
    for j in range(w):
      for key in dp[i][j]:
        if j < w - 1:
          dif = abs(A[i][j+1] - B[i][j+1])
          dp[i][j+1][item+dif] = 1
          dp[i][j+1][abs(item-dif)] = 1
        if i < h - 1:
          dif = abs(A[i+1][j] - B[i+1][j])
          dp[i+1][j][item+dif] = 1
          dp[i+1][j][abs(item-dif)] = 1
  print(min(dp[h-1][w-1].keys()))
if __name__ == '__main__':
  main()

Buggy submission (verdict: Runtime Error):
def main():
  import sys
  input = sys.stdin.readline
  h, w = map(int, input().split())
  A = []
  B = []
  for i in range(h):
    row = list(map(int, input().split()))
    A.append(row)
  for i in range(h):
    row = list(map(int, input().split()))
    B.append(row)
 
  dp = [[[0 for k in range(12801)] for j in range(w+1)] for i in range(h+1)]
  dp[0][0].add(abs(A[0][0] - B[0][0]))
  for i in range(h):
    for j in range(w):
      for item, f in enumerate(dp[i][j]):
        if not f:
          continue
        if j < w - 1:
          dif = abs(A[i][j+1] - B[i][j+1])
          dp[i][j+1].add(item+dif)
          dp[i][j+1].add(abs(item-dif))
        if i < h - 1:
          dif = abs(A[i+1][j] - B[i+1][j])
          dp[i+1][j].add(item+dif)
          dp[i+1][j].add(abs(item-dif))
  print(min(dp[h-1][w-1]))
if __name__ == '__main__':
  main()

Buggy submission (verdict: Runtime Error):
h, w = map(int, input().split())
A = []
B = []
for i in range(h):
  row = list(map(int, input().split()))
  row.append(0)
  A.append(row)
A.append([0 for i in range(w+1)])

for i in range(h):
  row = list(map(int, input().split()))
  row.append(0)
  B.append(row)
B.append([0 for i in range(w+1)])

dp = [[set([]) for j in range(w+1)] for i in range(h+1)]
dp[0][0].add(abs(A[0][0] - B[0][0]))
for i in range(h):
  for j in range(w):
    for item in dp[i][j]:
      dif = abs(A[i][j+1] - B[i][j+1])
      dp[i][j+1].add(item+dif)
      dp[i][j+1].add(abs(item-dif))
      dif = abs(A[i+1][j] - B[i+1][j])
      dp[i+1][j].add(item+dif)
      dp[i+1][j].add(abs(item-dif))
print(min(dp[h][w]))