Problem:


Score : 500 points

Problem Statement

We have a grid with H horizontal rows and W vertical columns. Let (i,j) denote the square at the i-th row from the top and the j-th column from the left.

The square (i, j) has two numbers A_{ij} and B_{ij} written on it.

First, for each square, Takahashi paints one of the written numbers red and the other blue.

Then, he travels from the square (1, 1) to the square (H, W). In one move, he can move from a square (i, j) to the square (i+1, j) or the square (i, j+1). He must not leave the grid.

Let the unbalancedness be the absolute difference of the sum of red numbers and the sum of blue numbers written on the squares along Takahashi's path, including the squares (1, 1) and (H, W).

Takahashi wants to make the unbalancedness as small as possible by appropriately painting the grid and traveling on it.

Find the minimum unbalancedness possible.

Constraints

2 \leq H \leq 80

2 \leq W \leq 80

0 \leq A_{ij} \leq 80

0 \leq B_{ij} \leq 80

All values in input are integers.

Input

Input is given from Standard Input in the following format:

H W
A_{11} A_{12} \ldots A_{1W}
:
A_{H1} A_{H2} \ldots A_{HW}
B_{11} B_{12} \ldots B_{1W}
:
B_{H1} B_{H2} \ldots B_{HW}

Output

Print the minimum unbalancedness possible.

Sample Input 1

2 2
1 2
3 4
3 4
2 1

Sample Output 1

0

By painting the grid and traveling on it as shown in the figure below, the sum of red numbers and the sum of blue numbers are 3+3+1=7 and 1+2+4=7, respectively, for the unbalancedness of 0.

Sample Input 2

2 3
1 10 80
80 10 1
1 2 3
4 5 6

Sample Output 2

2



Sample input:
2 2
1 2
3 4
3 4
2 1


Sample output:
0

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
a = [tuple(map(int, input().split())) for _ in range(h)]
b = [tuple(map(int, input().split())) for _ in range(h)]
c = [[a[i][j] - b[i][j] for j in range(w)] for i in range(h)]

INF = 10**10
dp = [[INF] * (w + 1) for _ in range(h + 1)]
dp[0][0] = c[0][0]
for i in range(h):
    for j in range(w):
        if i < h - 1:
            dp[i + 1][j] = min(
                abs(dp[i + 1][j]),
                abs(c[i + 1][j] + dp[i][j]),
                abs(c[i + 1][j] - dp[i][j]),
            )
        if j < w - 1:
            dp[i][j + 1] = min(
                abs(dp[i][j + 1]),
                abs(c[i][j + 1] + dp[i][j]),
                abs(c[i][j + 1] - dp[i][j]),
            )
        # dp[i][j] = min(abs(c[i][j] + dp[i - 1][j]),
        #                abs(c[i][j] - dp[i - 1][j]),
        #                abs(c[i][j] + dp[i][j - 1]),
        #                abs(c[i][j] - dp[i][j - 1]))

# print(*dp, sep='\n')
print(dp[h - 1][w - 1])

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)
INF = 10**18


def input():
    return sys.stdin.readline().rstrip()


def main():
    H, W = map(int, input().split())
    A = [[] for _ in range(H)]
    B = [[] for _ in range(H)]
    C = [[0] * W for _ in range(H)]
    minusdigit = 80
    for i in range(H):
        A[i] = list(map(int, input().split()))
    for i in range(H):
        B[i] = list(map(int, input().split()))
    for i in range(H):
        for j in range(W):
            C[i][j] = abs(A[i][j] - B[i][j])
    dp = [[0] * W for _ in range(H)]
    dp[0][0] = ((1 << minusdigit) << C[0][0]) | ((1 << minusdigit) >> C[0][0])
    for i in range(1, H):
        dp[i][0] = (dp[i - 1][0] << C[i][0]) | (dp[i - 1][0] >> C[i][0])
    for j in range(1, W):
        dp[0][j] = (dp[0][j - 1] << C[0][j]) | (dp[0][j - 1] >> C[0][j])

    for i in range(1, H):
        for j in range(1, W):
            dp[i][j] = (dp[i - 1][j] << C[i][j]) | (dp[i - 1][j] >> C[i][j]) | (dp[i][j - 1] << C[i][j]) | (dp[i][j - 1] >> C[i][j])
    ans = dp[H - 1][W - 1]
    for i in range(len(bin(ans)) - (2 + minusdigit)):
        if (ans >> (i + minusdigit)) & 0b1 == 1 or (-i + minusdigit >= 0 and (ans >> (-i + minusdigit)) & 0b1 == 1):
            print(i)
            exit()


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)
INF = 10**18


def input():
    return sys.stdin.readline().rstrip()


def main():
    H, W = map(int, input().split())
    A = [[] for _ in range(H)]
    B = [[] for _ in range(H)]
    C = [[0] * W for _ in range(H)]
    minusdigit = 160
    for i in range(H):
        A[i] = list(map(int, input().split()))
    for i in range(H):
        B[i] = list(map(int, input().split()))
    for i in range(H):
        for j in range(W):
            C[i][j] = abs(A[i][j] - B[i][j])
    dp = [[0] * W for _ in range(H)]
    dp[0][0] = ((1 << minusdigit) << C[0][0]) | ((1 << minusdigit) >> C[0][0])
    for i in range(1, H):
        dp[i][0] = (dp[i - 1][0] << C[i][0]) | (dp[i - 1][0] >> C[i][0])
    for j in range(1, W):
        dp[0][j] = (dp[0][j - 1] << C[0][j]) | (dp[0][j - 1] >> C[0][j])

    for i in range(1, H):
        for j in range(1, W):
            dp[i][j] = (dp[i - 1][j] << C[i][j]) | (dp[i - 1][j] >> C[i][j]) | (dp[i][j - 1] << C[i][j]) | (dp[i][j - 1] >> C[i][j])
    ans = dp[H - 1][W - 1]
    for i in range(len(bin(ans)) - (2 + minusdigit)):
        if (ans >> (i + minusdigit)) & 0b1 == 1 or (ans >> (-i + minusdigit)) & 0b1 == 1:
            print(i)
            exit()


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)
INF = 10**18


def input():
    return sys.stdin.readline().rstrip()


def main():
    H, W = map(int, input().split())
    A = [[] for _ in range(H)]
    B = [[] for _ in range(H)]
    C = [[0] * W for _ in range(H)]
    minusdigit = 80
    for i in range(H):
        A[i] = list(map(int, input().split()))
    for i in range(H):
        B[i] = list(map(int, input().split()))
    for i in range(H):
        for j in range(W):
            C[i][j] = abs(A[i][j] - B[i][j])
    dp = [[0] * W for _ in range(H)]
    dp[0][0] = ((1 << minusdigit) << C[0][0]) | ((1 << minusdigit) >> C[0][0])
    for i in range(1, H):
        dp[i][0] = (dp[i - 1][0] << C[i][0]) | (dp[i - 1][0] >> C[i][0])
    for j in range(1, W):
        dp[0][j] = (dp[0][j - 1] << C[0][j]) | (dp[0][j - 1] >> C[0][j])

    for i in range(1, H):
        for j in range(1, W):
            dp[i][j] = (dp[i - 1][j] << C[i][j]) | (dp[i - 1][j] >> C[i][j]) | (dp[i][j - 1] << C[i][j]) | (dp[i][j - 1] >> C[i][j])
    ans = dp[H - 1][W - 1]
    for i in range(len(bin(ans)) - (2 + minusdigit)):
        if (ans >> (i + minusdigit)) & 0b1 == 1:
            print(i)
            exit()
        if (ans >> (-i + minusdigit)) & 0b1 == 1:
            print(i)
            exit()


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[3]:


H, W = map(int, input().split())


# In[4]:


A = [[int(i) for i in input().split()] for i in range(H)]


# In[5]:


B = [[int(i) for i in input().split()] for i in range(H)]


# In[10]:


C = [[0 for _ in range(W)] for _ in range(H)]
for tate in range(H):
    for yoko in range(W):
        C[tate][yoko] = abs(A[tate][yoko] - B[tate][yoko])


# In[11]:


A


# In[12]:


B


# In[13]:


C


# In[15]:


M = 2500
d = [[[0 for _ in range(M+1)] for _ in range(W)] for _ in range(H)]


# 左上
d[0][0][C[0][0]] = 1
# 上1行
for yoko in range(1, W):
    for i in range(M+1):
        if 0 <= i + C[0][yoko] <= M and d[0][yoko-1][i + C[0][yoko]] == 1:
            d[0][yoko][i] = 1
        if 0 <= i - C[0][yoko] <= M and d[0][yoko-1][i - C[0][yoko]] == 1:
            d[0][yoko][i] = 1
        if 0 <= C[0][yoko] - i <= M and d[0][yoko-1][C[0][yoko] - i] == 1:
            d[0][yoko][i] = 1

# # 左端
# for i in range(M+1):
#     # 上から
#     if 0 <= i - C[1][0] <= M and d[0][0][i - C[1][0]] == 1:
#         d[1][0][i] = 1
#     if 0 <= i + C[1][0] <= M and d[0][0][i + C[1][0]] == 1:
#         d[1][0][i] = 1
# 中1行
for tate in range(1, H):
    for yoko in range(0, W):
        # 上から
        for i in range(M+1):
            if 0 <= i - C[tate][yoko] <= M and d[tate-1][yoko][i - C[tate][yoko]] == 1:
                d[tate][yoko][i] = 1
            if 0 <= C[tate][yoko] - i <= M and d[tate-1][yoko][C[tate][yoko] - i] == 1:
                d[tate][yoko][i] = 1
            if 0 <= i + C[tate][yoko] <= M and d[tate-1][yoko][i + C[tate][yoko]] == 1:
                d[tate][yoko][i] = 1
        
        if yoko >= 1:
            # 左から
            for i in range(M+1):
                if 0 <= i - C[tate][yoko] <= M and d[tate][yoko-1][i - C[tate][yoko]] == 1:
                    d[tate][yoko][i] = 1
                if 0 <= C[tate][yoko] - i <= M and d[tate][yoko-1][C[tate][yoko] - i] == 1:
                    d[tate][yoko][i] = 1
                if 0 <= i + C[tate][yoko] <= M and d[tate][yoko-1][i + C[tate][yoko]] == 1:
                    d[tate][yoko][i] = 1


# In[16]:


for i in range(M + 1):
    if d[H-1][W-1][i] == 1:
        print(i)
        break
    
    


# In[ ]:

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[3]:


H, W = map(int, input().split())


# In[4]:


A = [[int(i) for i in input().split()] for i in range(H)]


# In[5]:


B = [[int(i) for i in input().split()] for i in range(H)]


# In[10]:


C = [[0 for _ in range(W)] for _ in range(H)]
for tate in range(H):
    for yoko in range(W):
        C[tate][yoko] = abs(A[tate][yoko] - B[tate][yoko])


# In[11]:


A


# In[12]:


B


# In[13]:


C


# In[15]:


M = 1000
d = [[[0 for _ in range(M+1)] for _ in range(W)] for _ in range(H)]


# 左上
d[0][0][C[0][0]] = 1
# 上1行
for yoko in range(1, W):
    for i in range(M+1):
        if 0 <= i + C[0][yoko] <= M and d[0][yoko-1][i + C[0][yoko]] == 1:
            d[0][yoko][i] = 1
        if 0 <= i - C[0][yoko] <= M and d[0][yoko-1][i - C[0][yoko]] == 1:
            d[0][yoko][i] = 1
        if 0 <= C[0][yoko] - i <= M and d[0][yoko-1][C[0][yoko] - i] == 1:
            d[0][yoko][i] = 1

# # 左端
# for i in range(M+1):
#     # 上から
#     if 0 <= i - C[1][0] <= M and d[0][0][i - C[1][0]] == 1:
#         d[1][0][i] = 1
#     if 0 <= i + C[1][0] <= M and d[0][0][i + C[1][0]] == 1:
#         d[1][0][i] = 1
# 中1行
for tate in range(1, H):
    for yoko in range(0, W):
        # 上から
        for i in range(M+1):
            if 0 <= i - C[tate][yoko] <= M and d[tate-1][yoko][i - C[tate][yoko]] == 1:
                d[tate][yoko][i] = 1
            if 0 <= C[tate][yoko] - i <= M and d[tate-1][yoko][C[tate][yoko] - i] == 1:
                d[tate][yoko][i] = 1
            if 0 <= i + C[tate][yoko] <= M and d[tate-1][yoko][i + C[tate][yoko]] == 1:
                d[tate][yoko][i] = 1
        
        if yoko >= 1:
            # 左から
            for i in range(M+1):
                if 0 <= i - C[tate][yoko] <= M and d[tate][yoko-1][i - C[tate][yoko]] == 1:
                    d[tate][yoko][i] = 1
                if 0 <= C[tate][yoko] - i <= M and d[tate][yoko-1][C[tate][yoko] - i] == 1:
                    d[tate][yoko][i] = 1
                if 0 <= i + C[tate][yoko] <= M and d[tate][yoko-1][i + C[tate][yoko]] == 1:
                    d[tate][yoko][i] = 1


# In[16]:


for i in range(M + 1):
    if d[H-1][W-1][i] == 1:
        print(i)
        break
    
    


# In[ ]:

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline

def main():
    H, W = map(int, input().split())
    P = W+1
    A = [list(map(int, input().split())) for _ in range(H)]
    B = [list(map(int, input().split())) for _ in range(H)]
    L = 80*80
    dp = [[False]*(L+1) for _ in range((W+1)*(H+1))]
    for h in range(H+1):
        dp[h*P][0] = True
    for w in range(H+1):
        dp[w][0] = True
    
    for h in range(H):
        for w in range(W):
            c = abs(A[h][w] - B[h][w])
            for l in range(L-c+1):
                b1 = abs(l-c)
                b2 = abs(l+c)
                dp[(h+1)*P+w+1][l] = dp[h*P+w+1][b1] or dp[(h+1)*P+w][b1] or dp[h*P+w+1][b2] or dp[(h+1)*P+w][b2]

    for l in range(L+1):
        if dp[H*P+W][l]:
            print(l)
            break


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)
INF=10**18
def input():
    return sys.stdin.readline().rstrip()

def main():
    H,W=map(int,input().split())
    A=[[] for _ in range(H)]
    B=[[] for _ in range(H)]
    C=[[0]*W for _ in range(H)]
    minusdigit=80
    for i in range(H):
        A[i]=list(map(int,input().split()))
    for i in range(H):
        B[i]=list(map(int,input().split()))
    for i in range(H):
        for j in range(W):
            C[i][j]=abs(A[i][j]-B[i][j])
    dp=[[0]*W for _ in range(H)]
    dp[0][0]=((1<<minusdigit)<<C[0][0])|((1<<minusdigit)>>C[0][0])
    for i in range(1,H):
        dp[i][0]=(dp[i-1][0]<<C[i][0])|(dp[i-1][0]>>C[i][0])
    for j in range(1,W):
        dp[0][j]=(dp[0][j-1]<<C[0][j])|(dp[0][j-1]>>C[0][j])
    
    for i in range(1,H):
        for j in range(1,W):
            dp[i][j]=(dp[i-1][j]<<C[i][j])|(dp[i-1][j]>>C[i][j])|(dp[i][j-1]<<C[i][j])|(dp[i][j-1]>>C[i][j])
    ans=dp[H-1][W-1]>>minusdigit
    for i in range(len(bin(ans))-2):
        if (ans>>i)&0b1==1:
            print(i)
            exit()
    
    

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10**9)
INF=10**18
def input():
    return sys.stdin.readline().rstrip()

def main():
    H,W=map(int,input().split())
    A=[[] for _ in range(H)]
    B=[[] for _ in range(H)]
    C=[[0]*W for _ in range(H)]
    minusdigit=80
    for i in range(H):
        A[i]=list(map(int,input().split()))
    for i in range(H):
        B[i]=list(map(int,input().split()))
    for i in range(H):
        for j in range(W):
            C[i][j]=abs(A[i][j]-B[i][j])
    dp=[[0]*W for _ in range(H)]
    dp[0][0]=((1<<minusdigit)<<C[0][0])|((1<<minusdigit)>>C[0][0])
    for i in range(1,H):
        dp[i][0]=(dp[i-1][0]<<C[i][0])|(dp[i-1][0]>>C[i][0])
    for j in range(1,W):
        dp[0][j]=(dp[0][j-1]<<C[0][j])|(dp[0][j-1]>>C[0][j])
    
    for i in range(1,H):
        for j in range(1,W):
            dp[i][j]=(dp[i-1][j]<<C[i][j])|(dp[i-1][j]>>C[i][j])|(dp[i][j-1]<<C[i][j])|(dp[i-1][j]>>C[i][j])
    ans=dp[H-1][W-1]>>minusdigit
    for i in range(len(bin(ans))-2):
        if (ans>>i)&0b1==1:
            print(i)
            exit()
    
    

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
H, W=map(int, input().split())
maxK=161
A=[list(map(int, input().split())) for _ in range(H)]
B=[list(map(int, input().split())) for _ in range(H)]
C=[[abs(A[i][j]-B[i][j]) for j in range(W)] for i in range(H)]
dp=[[[0 for _ in range(maxK+1)] for _ in range(W+1)] for _ in range(H+1)]
dp[1][1][C[0][0]]=1
for i in range(1, H+1):
    for j in range(1, W+1):
        for k in range(maxK):
            if dp[i-1][j][k]==1 or dp[i][j-1][k]==1:
                k_candidate1, k_candidate2=abs(k-C[i-1][j-1]), abs(k+C[i-1][j-1])
                if k_candidate1<maxK:
                    dp[i][j][k_candidate1]=1
                if k_candidate2<maxK:
                    dp[i][j][k_candidate2]=1
for k in range(maxK):
    if dp[H][W][k]==1:
        print(k)
        break

Submission status: Wrong Answer

Solution code:

H,W = map(int,input().split())
A = [list(map(int,input().split())) for _ in range(H)]
B = [list(map(int,input().split())) for _ in range(H)]



start = abs(A[H-1][W-1] - B[H-1][W-1])
C = [[0]*W for _ in range(H)]
for i in range(W):
    for j in range(H):
        C[j][i] = abs(A[j][i]-B[j][i])
        

inf = 1<<30
DP = [[inf]*W for _ in range(H)]
#DP[0][0] = min(abs(start + C[0][0]), abs(start-C[0][0]))
DP[0][0] = C[0][0]

for i in range(H):
    for j in range(W):
        if i==H-1 and j==W-1:
            break
        if i==H-1:
            DP[i][j+1] = min(abs(DP[i][j]+C[i][j+1]), abs(DP[i][j]-C[i][j+1]),DP[i][j+1])
            continue
        if j==W-1:
            DP[i+1][j] = min(abs(DP[i][j]+C[i+1][j]), abs(DP[i][j]-C[i+1][j]),DP[i+1][j])
            continue
        DP[i+1][j] = min(abs(DP[i][j]+C[i+1][j]), abs(DP[i][j]-C[i+1][j]),DP[i+1][j])
        DP[i][j+1] = min(abs(DP[i][j]+C[i][j+1]), abs(DP[i][j]-C[i][j+1]),DP[i][j+1])
        
print(DP[H-1][W-1])

Submission status: Wrong Answer

Solution code:
n,m = map(int,input().split())
a =  []
for i in range(n):
	a.append([])
	for j in range(m):
		a[i].append([])
# for r in a:
# 	print(r)
for i in range(n):
	r = list( map(int,input().split()) )

	for j in range(m):
		a[i][j] = [r[j],0]

for i in range(n):
	b = list( map(int,input().split()) )
	for j in range(m):
		a[i][j][1] = b[j]
# print(a)
dp1 = []
for i in range(n):
	dp1.append([])
	for j in range(m):
		dp1[i].append([0,0])

# for r in a:
# 	print(r)

dp1[0][0][0],dp1[0][0][1] =  a[0][0][0],a[0][0][1]
for i in range(1,m):
	s1 = abs(dp1[0][i-1][0] + a[0][i][0] - (dp1[0][i-1][1] + a[0][i][1]) )
	s2 = abs(dp1[0][i-1][1] + a[0][i][0] - (dp1[0][i-1][0] + a[0][i][1]) )
	if s1 < s2:
		dp1[0][i][0],dp1[0][i][1] = dp1[0][i-1][0] + a[0][i][0],dp1[0][i-1][1] + a[0][i][1]
	else:
		dp1[0][i][0],dp1[0][i][1] = dp1[0][i-1][0] + a[0][i][1],dp1[0][i-1][1] + a[0][i][0]
	
for i in range(1,n):
	s1 = abs(dp1[i-1][0][0] + a[i][0][0] - (dp1[i-1][0][1] + a[i][0][1]) )
	s2 = abs(dp1[i-1][0][1] + a[i][0][0] - (dp1[i-1][0][0] + a[i][0][1]) )
	if s1 < s2:
		dp1[i][0][0],dp1[i][0][1] = dp1[i-1][0][0] + a[i][0][0],dp1[i-1][0][1] + a[i][0][1]
	else:
		dp1[i][0][0],dp1[i][0][1] = dp1[i-1][0][0] + a[i][0][1],dp1[i-1][0][1] + a[i][0][0]

for i in range(1,n):
	for j in range(1,m):
		# for cell (i-1,j)
		s1 = abs(dp1[i-1][j][0] + a[i][j][0] - (dp1[i-1][j][1] + a[i][j][1]) ) 
		s2 = abs(dp1[i-1][j][1] + a[i][j][0] - (dp1[i-1][j][0] + a[i][j][1]) )

		# for cell (i,j-1)
		s3 = abs(dp1[i][j-1][0] + a[i][j][0] - (dp1[i][j-1][1] + a[i][j][1]) ) 
		s4 = abs(dp1[i][j-1][1] + a[i][j][0] - (dp1[i][j-1][0] + a[i][j][1]) )

		if s1 < s2:
			if s3 < s4:
				if s1 < s3:
					dp1[i][j][0],dp1[i][j][1] = dp1[i-1][j][0] + a[i][j][0], dp1[i-1][j][1] + a[i][j][1]
				else:
					dp1[i][j][0],dp1[i][j][1] = dp1[i][j-1][0] + a[i][j][0], dp1[i][j-1][1] + a[i][j][1]
			else:
				if s1 < s4:
					dp1[i][j][0],dp1[i][j][1] = dp1[i-1][j][0] + a[i][j][0], dp1[i-1][j][1] + a[i][j][1]
				else:
					dp1[i][j][0],dp1[i][j][1] = dp1[i][j-1][1] + a[i][j][0], dp1[i][j-1][0] + a[i][j][1]
		else:
			if s3<s4:
				if s2<s3:
					dp1[i][j][0],dp1[i][j][1] = dp1[i-1][j][1] + a[i][j][0], dp1[i-1][j][0] + a[i][j][1]
				else:
					dp1[i][j][0],dp1[i][j][1] = dp1[i][j-1][0] + a[i][j][0], dp1[i][j-1][1] + a[i][j][1]
			else:
				if s2<s4:
					dp1[i][j][0],dp1[i][j][1] = dp1[i-1][j][1] + a[i][j][0], dp1[i-1][j][0] + a[i][j][1]
				else:
					dp1[i][j][0],dp1[i][j][1] = dp1[i][j-1][1] + a[i][j][0], dp1[i][j-1][0] + a[i][j][1]




ans = abs(dp1[n-1][m-1][0] - dp1[n-1][m-1][1])
print(ans)

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
inf = float('INF')
A = []
B = []

for i in range(h):
    line = list(map(int, input().split()))
    A.append(line)

for i in range(h):
    line = list(map(int, input().split()))
    B.append(line)

D = [[0]*w for i in range(h)]
for i in range(h):
    for j in range(w):
        D[i][j] = abs(A[i][j] - B[i][j])


def slide_down(y, x, possible: set):
    new = set()
    delta = D[y+1][x]
    for num in possible:
        if num + delta <=3200:
            new.add(num+delta)
        if abs(num-delta) <= 3200:
            new.add(abs(num-delta))
    return new


def slide_right(y, x, possible: set):
    new = set()
    delta = D[y][x+1]
    for num in possible:
        if num + delta <=3200:
            new.add(num+delta)
        if abs(num-delta) <=3200:
            new.add(abs(num-delta))
    return new


DP = [[set() for j in range(w)] for i in range(h)]
DP[0][0] = {D[0][0]}
for row in range(h):
    for col in range(w):
        possible = DP[row][col]
        if col < w-1:
            new = slide_right(row, col, possible)
            DP[row][col+1] |= new
        if row < h-1:
            new = slide_down(row, col, possible)
            DP[row+1][col] |= new

ans = min(DP[h-1][w-1])
print(ans)

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
inf = float('INF')
A = []
B = []

for i in range(h):
    line = list(map(int, input().split()))
    A.append(line)

for i in range(h):
    line = list(map(int, input().split()))
    B.append(line)

D = [[0]*w for i in range(h)]
for i in range(h):
    for j in range(w):
        D[i][j] = abs(A[i][j] - B[i][j])


def slide_down(y, x, possible: set):
    new = set()
    delta = D[y+1][x]
    for num in possible:
        if num + delta <=160:
            new.add(num+delta)
        if abs(num-delta) <= 160:
            new.add(abs(num-delta))
    return new


def slide_right(y, x, possible: set):
    new = set()
    delta = D[y][x+1]
    for num in possible:
        if num + delta <=160:
            new.add(num+delta)
        if abs(num-delta) <=160:
            new.add(abs(num-delta))
    return new


DP = [[set() for j in range(w)] for i in range(h)]
DP[0][0] = {D[0][0]}
for row in range(h):
    for col in range(w):
        possible = DP[row][col]
        if col < w-1:
            new = slide_right(row, col, possible)
            DP[row][col+1] |= new
        if row < h-1:
            new = slide_down(row, col, possible)
            DP[row+1][col] |= new

ans = min(DP[h-1][w-1])
print(ans)

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
inf = float('INF')
A = []
B = []

for i in range(h):
    line = list(map(int, input().split()))
    A.append(line)

for i in range(h):
    line = list(map(int, input().split()))
    B.append(line)

D = [[0]*w for i in range(h)]
for i in range(h):
    for j in range(w):
        D[i][j] = abs(A[i][j] - B[i][j])


def slide_down(y, x, possible: set):
    new = set()
    delta = D[y+1][x]
    for num in possible:
        if num + delta <=80:
            new.add(num+delta)
        if abs(num-delta) <= 80:
            new.add(abs(num-delta))
    return new


def slide_right(y, x, possible: set):
    new = set()
    delta = D[y][x+1]
    for num in possible:
        if num + delta <=80:
            new.add(num+delta)
        if abs(num-delta) <=80:
            new.add(abs(num-delta))
    return new


DP = [[set() for j in range(w)] for i in range(h)]
DP[0][0] = {D[0][0]}
for row in range(h):
    for col in range(w):
        possible = DP[row][col]
        if col < w-1:
            new = slide_right(row, col, possible)
            DP[row][col+1] |= new
        if row < h-1:
            new = slide_down(row, col, possible)
            DP[row+1][col] |= new

ans = min(DP[h-1][w-1])
print(ans)

Submission status: Wrong Answer

Solution code:
h, w = map(int, input().split())
inf = float('INF')
A = []
B = []

for i in range(h):
    line = list(map(int, input().split()))
    A.append(line)

for i in range(h):
    line = list(map(int, input().split()))
    B.append(line)

D = [[0]*w for i in range(h)]
for i in range(h):
    for j in range(w):
        D[i][j] = abs(A[i][j] - B[i][j])


def slide_down(y, x, possible: set):
    new = set()
    delta = D[y+1][x]
    for num in possible:
        if num + delta <=80:
            new.add(num+delta)
        if abs(num-delta) <= 80:
            new.add(abs(num-delta))
    return new


def slide_right(y, x, possible: set):
    new = set()
    delta = D[y][x+1]
    for num in possible:
        if num + delta <=80:
            new.add(num+delta)
        if abs(num-delta) <= 80:
            new.add(abs(num-delta))
    return new


DP = [[set()] * w for i in range(h)]
DP[0][0] = {D[0][0]}
for row in range(h):
    for col in range(w):
        possible = DP[row][col]
        if col < w-1:
            new = slide_right(row, col, possible)
            DP[row][col+1] |= new
        if row < h-1:
            new = slide_down(row, col, possible)
            DP[row+1][col] |= new

ans = min(DP[h-1][w-1])
print(ans)

Submission status: Wrong Answer

Solution code:
from heapq import heappush,heappop,heapify
from collections import deque,defaultdict,Counter
import itertools
from itertools import permutations,combinations
import sys
import bisect
import string
import math
import time
import random
def I():
    return int(input())
def MI():
    return map(int,input().split())
def LI():
    return [int(i) for i in input().split()]
def LI_():
    return [int(i)-1 for i in input().split()]
def StoI():
    return [ord(i)-97 for i in input()]
def ItoS(nn):
    return chr(nn+97)
def GI(V,E,Directed=False,index=0):
    org_inp=[]
    g=[[] for i in range(n)]
    for i in range(E):
        inp=LI()
        org_inp.append(inp)
        if index==0:
            inp[0]-=1
            inp[1]-=1
        if len(inp)==2:
            a,b=inp
            g[a].append(b)
            if not Directed:
                g[b].append(a)
        elif len(inp)==3:
            a,b,c=inp
            aa=(inp[0],inp[2])
            bb=(inp[1],inp[2])
            g[a].append(bb)
            if not Directed:
                g[b].append(aa)
    return g,org_inp
def show(*inp,end='\n'):
    if show_flg:
        print(*inp,end=end)
YN=['Yes','No']
mo=10**9+7
inf=float('inf')
l_alp=string.ascii_lowercase
u_alp=string.ascii_uppercase
ts=time.time()
#sys.setrecursionlimit(10**5)
input=lambda: sys.stdin.readline().rstrip()

def ran_input():
    import random
    n=random.randint(4,16)
    a=sorted([random.randint(0,5) for _ in range(n)])[::-1]
    return n,a

show_flg=False
show_flg=True

h,w=LI()
ans=0
a=[[0]*(1+w)]
b=[[0]*(1+w)]
for i in range(h):
    a.append([0]+LI())
for i in range(h):
    b.append([0]+LI())

dp=[[set() for i in range(w+1)]for j in range(h+1)]

dp[0][1].add(0)

for i in range(1,h+1):
    for j in range(1,w+1):
        d=a[i][j]-b[i][j]
        for x in dp[i-1][j]:
            if -7000<=x+d<=7000:dp[i][j].add(x+d)
            if -7000<=x-d<=7000:dp[i][j].add(x-d)

        for x in dp[i][j-1]:
            if -7000<=x+d<=7000:dp[i][j].add(x+d)
            if -7000<=x-d<=7000:dp[i][j].add(x-d)

ans=min([abs(i) for i in dp[h][w]])

print(ans)


show(a,b)
show(dp[h][w])
show(dp)

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#solve
def solve():
    h, w = LI()
    a = LIR(h)
    b = LIR(h)
    mab = [[abs(a[y][x]-b[y][x]) for x in range(w)] for y in range(h)]
    ab = abs(a[0][0] - b[0][0])
    hw = h + w
    dp = [[0] * w for i in range(h)]
    dp[0][0] |= (1 << (6400 + ab) | 1 >> (6400 - ab))
    for i in range(1, h + w - 1):
        for x in range(w):
            y = i - x - 1
            if 0 <= x < w and 0 <= y < h:
                value = dp[y][x]
            else:
                continue
            for mx, my in [[0, 1], [1, 0]]:
                mx += x
                my += y
                if 0 <= mx < w and 0 <= my < h:
                    mxy = mab[my][mx]
                    dp[my][mx] |= ((value << mxy) | (value >> mxy))
    ans = dp[-1][-1]
    for i in range(ans.bit_length()):
        if ans & (1 << (6400-i)):
            print(i)
            return
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#solve
def solve():
    h, w = LI()
    a = LIR(h)
    b = LIR(h)
    mab = [[abs(a[y][x]-b[y][x]) for x in range(w)] for y in range(h)]
    ab = abs(a[0][0] - b[0][0])
    hw = h + w
    dp = [[0] * w for i in range(h)]
    dp[0][0] = 1 << (6400 + ab)
    for i in range(1, h + w - 1):
        for x in range(w):
            y = i - x - 1
            if 0 <= x < w and 0 <= y < h:
                value = dp[y][x]
            else:
                continue
            for mx, my in [[0, 1], [1, 0]]:
                mx += x
                my += y
                if 0 <= mx < w and 0 <= my < h:
                    mxy = mab[my][mx]
                    dp[my][mx] |= ((value << mxy) | (value >> mxy))
    ans = dp[-1][-1]
    for i in range(ans.bit_length()):
        if ans & (1 << (6400-i)):
            print(i)
            return
    return


#main
if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

Solution code:
import numpy as np

H, W = map(int, input().split())
lst1 = []
lst2 = []
for _ in range(H):
    lst1.append([int(i) for i in input().split()])
for _ in range(H):
    lst2.append([int(i) for i in input().split()])
arr1, arr2 = np.array(lst1), np.array(lst2)
arr = np.abs(arr1 - arr2)

d = [[0] * W for _ in range(H)]
m = 80 * (H + W)
d[0][0] = 1 << m
for h in range(H):
    for w in range(W):
        x = arr[h][w]
        if h > 0:
            print(h,w)
            d[h][w] |= d[h - 1][w]
        if w > 0:
            d[h][w] |= d[h][w - 1]
        d[h][w] = d[h][w] << x | d[h][w] >> x

x, y= 1 << m, 1 << m
for i in range(m + 1):
    if d[H - 1][W - 1] & x or d[H - 1][W - 1] & y:
        print(i)
        quit()
    x<<=1
    y>>=1