Problem p02839 (python)

Fixed submission:
import sys

input = sys.stdin.buffer.readline

h, w = map(int, input().split())
AA = [list(map(int, input().split())) for _ in range(h)]
BB = [list(map(int, input().split())) for _ in range(h)]

kouho = [[0] * (w) for _ in range(h)]
delta = abs(AA[0][0] - BB[0][0])
kouho[0][0] |= 1 << delta
for y in range(h):
    for x in range(w):
        nx = x + 1
        ny = y
        if nx < w:
            delta = abs(AA[ny][nx] - BB[ny][nx])
            temp = 0
            for i in range(1, delta + 1):
                if (kouho[y][x] >> i) % 2:
                    temp |= 1 << (delta - i)
            # print(x,y,kouho[y][x],temp,delta)
            kouho[ny][nx] |= kouho[y][x] << delta
            kouho[ny][nx] |= kouho[y][x] >> delta
            kouho[ny][nx] |= temp

        nx = x
        ny = y + 1
        if ny < h:
            delta = abs(AA[ny][nx] - BB[ny][nx])
            temp = 0
            for i in range(1, delta + 1):
                if (kouho[y][x] >> i) % 2:
                    temp |= 1 << (delta - i)
            # print(x,y,kouho[y][x],temp,delta)
            kouho[ny][nx] |= kouho[y][x] << delta
            kouho[ny][nx] |= kouho[y][x] >> delta
            kouho[ny][nx] |= temp
# print(kouho)
for i in range(81):
    if (kouho[h - 1][w - 1] >> i) % 2:
        print(i)
        exit()

Buggy submission (verdict: Runtime Error):
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]

def main():
    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)]

    ablst=[[0]*w for i in range(h)]
    ma=0
    for i in range(h):
        for j in range(w):
            ablst[i][j]=abs(a[i][j]-b[i][j])
            ma=max(ma,ablst[i][j])
    lst1=[0]*ma*(h+w+10)

    lst=[[lst1[:] for i in range(w)] for i in range(h)]
    lst[0][0][abs(a[0][0]-b[0][0])]=1


    # print(lst)
    for i in range(h):
        for j in range(w):
            if i+1<h:
                for k in range(len(lst[i][j])):
                    if lst[i][j][k]==1:
                        lst[i+1][j][k+ablst[i+1][j]]=1
                        lst[i+1][j][abs(k-ablst[i+1][j])]=1
            if j+1<w:
                for k in range(len(lst[i][j])):
                    if lst[i][j][k]==1:
                        lst[i][j+1][k+ablst[i][j+1]]=1
                        lst[i][j+1][abs(k-ablst[i][j+1])]=1
        # print(d)
            # print(lst[i][j])
    # anslst=list(lst[-1][-1].keys())
    # anslst.sort(key=lambda x: abs(x))
    # print(lst)
    for i in range(len(lst[-1][-1])):
        if lst[-1][-1][i]==1:
            print(i)
            break

main()

Buggy submission (verdict: Runtime Error):
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]


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)]

ablst=[[0]*w for i in range(h)]
ma=0
for i in range(h):
    for j in range(w):
        ablst[i][j]=abs(a[i][j]-b[i][j])
        ma=max(ma,ablst[i][j])
lst1=[0]*ma*(h+w+10)

lst=[[lst1[:] for i in range(w)] for i in range(h)]
lst[0][0][abs(a[0][0]-b[0][0])]=1


# print(lst)
for i in range(h):
    for j in range(w):
        if i+1<h:
            for k in range(len(lst[i][j])):
                if lst[i][j][k]==1:
                    lst[i+1][j][k+ablst[i+1][j]]=1
                    lst[i+1][j][abs(k-ablst[i+1][j])]=1
        if j+1<w:
             for k in range(len(lst[i][j])):
                if lst[i][j][k]==1:
                    lst[i][j+1][k+ablst[i][j+1]]=1
                    lst[i][j+1][abs(k-ablst[i][j+1])]=1
    # print(d)
        # print(lst[i][j])
# anslst=list(lst[-1][-1].keys())
# anslst.sort(key=lambda x: abs(x))
# print(lst)
for i in range(len(lst[-1][-1])):
    if lst[-1][-1][i]==1:
        print(i)
        break

Buggy submission (verdict: Runtime Error):
import bisect
import copy
import heapq
import math
import sys
from collections import *
from functools import lru_cache
from itertools import accumulate, combinations, permutations, product
def input():
    return sys.stdin.readline()[:-1]
def ruiseki(lst):
    return [0]+list(accumulate(lst))
sys.setrecursionlimit(5000000)
mod=pow(10,9)+7
al=[chr(ord('a') + i) for i in range(26)]
direction=[[1,0],[0,1],[-1,0],[0,-1]]


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)]

ablst=[[0]*w for i in range(h)]
ma=0
for i in range(h):
    for j in range(w):
        ablst[i][j]=abs(a[i][j]-b[i][j])
        ma=max(ma,ablst[i][j])
lst1=[0]*ma*(h*w+10)

lst=[[lst1[:] for i in range(w)] for i in range(h)]
lst[0][0][abs(a[0][0]-b[0][0])]=1


# print(lst)
for i in range(h):
    for j in range(w):
        if i+1<h:
            for k in range(len(lst[i][j])):
                if lst[i][j][k]==1:
                    lst[i+1][j][k+ablst[i+1][j]]=1
                    lst[i+1][j][abs(k-ablst[i+1][j])]=1
        if j+1<w:
             for k in range(len(lst[i][j])):
                if lst[i][j][k]==1:
                    lst[i][j+1][k+ablst[i][j+1]]=1
                    lst[i][j+1][abs(k-ablst[i][j+1])]=1
    # print(d)
        # print(lst[i][j])
# anslst=list(lst[-1][-1].keys())
# anslst.sort(key=lambda x: abs(x))
# print(lst)
for i in range(len(lst[-1][-1])):
    if lst[-1][-1][i]==1:
        print(i)
        break

Buggy submission (verdict: Runtime Error):
import sys
import time
def input():
    return sys.stdin.readline()[:-1]
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)]
kata=[]
for i in range(h):
    tmp=[]
    for j in range(w):
        tmp.append(abs(a[i][j]-b[i][j]))
    kata.append(tmp)
# print(kata)
tmp=0
for i in range(h):
    tmp=max(tmp,max(kata[i]))
# print(tmp)

dp=[[[0 for i in range(tmp*(h+w)+1)] for j in range(w+1)] for k in range(h+1)]
dp[0][0][kata[0][0]]+=1
i=0
j=0
# print(dp)
while i+j!=h+w-2:
    # print(i,j)
    for k in range(tmp*(h+w)+1):
        if dp[i][j][k]!=0:
            if i+1!=h:
                dp[i+1][j][k+kata[i+1][j]]+=1
                dp[i+1][j][abs(k-kata[i+1][j])]+=1
            if j+1!=w:
                dp[i][j+1][k+kata[i][j+1]]+=1
                dp[i][j+1][abs(k-kata[i][j+1])]+=1
    i-=1
    j+=1
    if i<0:
        i=abs(i+j)+1
        j=0
    while i>=h:
        i-=1
        j+=1
    # time.sleep(1)
# print(dp[1][0])
# print(dp[0][1])
# print(dp[h-1][w-1])
for i in range(tmp*(h+w)+1):
    if dp[h-1][w-1][i]!=0:
        print(i)
        quit()

Buggy submission (verdict: Runtime Error):
import sys
import time
def input():
    return sys.stdin.readline()[:-1]
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)]
kata=[]
for i in range(h):
    tmp=[]
    for j in range(w):
        tmp.append(abs(a[i][j]-b[i][j]))
    kata.append(tmp)
# print(kata)
tmp=0
for i in range(h):
    tmp=max(tmp,max(kata[i]))
# print(tmp)

dp=[[[0 for i in range(tmp*(h+w)+1)] for j in range(w+1)] for k in range(h+1)]
dp[0][0][kata[0][0]]+=1
i=0
j=0
# print(dp)
while i+j!=h+w-2:
    # print(i,j)
    for k in range(tmp*(h+2)+1):
        if dp[i][j][k]!=0:
            if i+1!=h:
                dp[i+1][j][k+kata[i+1][j]]+=1
                dp[i+1][j][abs(k-kata[i+1][j])]+=1
            if j+1!=w:
                dp[i][j+1][k+kata[i][j+1]]+=1
                dp[i][j+1][abs(k-kata[i][j+1])]+=1
    i-=1
    j+=1
    if i<0:
        i=abs(i+j)+1
        j=0
    while i>=h:
        i-=1
        j+=1
    # time.sleep(1)
# print(dp[1][0])
# print(dp[0][1])
# print(dp[h-1][w-1])
for i in range(tmp*(h+w)+1):
    if dp[h-1][w-1][i]!=0:
        print(i)
        quit()