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: Runtime Error

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(H: int, W: int, A: "List[List[int]]", B: "List[List[int]]"):
    L = 5000
    base = [[None]*W for _ in range(H)]
    for h in range(H):
        for w in range(W):
            base[h][w] = abs(A[h][w]-B[h][w])

    table = [[[False]*L for _ in range(W)] for _ in range(H)]
    table[0][0][base[0][0]] = True
    for h in range(H):
        for w in range(W):
            if h > 0:
                for i in range(L):
                    if table[h-1][w][i] == True:
                        table[h][w][abs(i-base[h][w])] = True
                        table[h][w][abs(i+base[h][w])] = True
            if w > 0:
                for i in range(L):
                    if table[h][w-1][i] == True:
                        table[h][w][abs(i-base[h][w])] = True
                        table[h][w][abs(i+base[h][w])] = True
    for i in range(L):
        if table[H-1][W-1][i] == True:
            print(i)
            return

    return


def main():

    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    H = int(next(tokens))  # type: int
    W = int(next(tokens))  # type: int
    A = [[int(next(tokens)) for _ in range(W)]
         for _ in range(H)]  # type: "List[List[int]]"
    B = [[int(next(tokens)) for _ in range(W)]
         for _ in range(H)]  # type: "List[List[int]]"
    solve(H, W, A, B)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

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)]
Diff = [[0] * (W + 1) for _ in range(H + 1)]
M = 0
for i in range(H):
    for j in range(W):
        m = abs(A[i][j] - B[i][j])
        Diff[i][j] = m
        M = max(M, m)

M = M * H * W

dp = [[[0] * (M + 1) for _ in range(W + 1)] for _ in range(H + 1)]
dp[0][0][Diff[0][0]] = 1

for i in range(0, H):
    for j in range(0, W):
        for k in range(M + 1):
            if dp[i][j][k]:
                dp[i + 1][j][k + Diff[i + 1][j]] = 1
                dp[i + 1][j][abs(k - Diff[i + 1][j])] = 1
                dp[i][j + 1][k + Diff[i][j + 1]] = 1
                dp[i][j + 1][abs(k - Diff[i][j + 1])] = 1

for k in range(M + 1):
    if dp[H - 1][W - 1][k] == 1:
        ans = k
        break

print(ans)

Submission status: Runtime Error

Solution code:
def solve():
    import numpy as np
    from sys import stdin
    f_i = stdin
    
    H, W = map(int, f_i.readline().split())
    A = tuple(tuple(map(int, f_i.readline().split())) for i in range(H))
    B = tuple(tuple(map(int, f_i.readline().split())) for i in range(H))
    
    DP = np.full((H + 1, W + 1, (H * W) * 80), False)
    DP[0][1][0] = True
    for i in range(1, H + 1):
        for j in range(1, W + 1):
            diff = A[i-1][j-1] - B[i-1][j-1]
            for pre_bias in np.where(DP[i-1][j] == True)[0]:
                bias = abs(pre_bias + diff)
                DP[i][j][bias] = True
                bias = abs(pre_bias - diff)
                DP[i][j][bias] = True
            for pre_bias in np.where(DP[i][j-1] == True)[0]:
                bias = abs(pre_bias + diff)
                DP[i][j][bias] = True
                bias = abs(pre_bias - diff)
                DP[i][j][bias] = True
    
    ans = np.where(DP[-1][-1] == True)[0][0]
    print(ans)

solve()

Submission status: Runtime Error

Solution code:
def solve():
    import numpy as np
    from sys import stdin
    f_i = stdin
    
    H, W = map(int, f_i.readline().split())
    A = tuple(tuple(map(int, f_i.readline().split())) for i in range(H))
    B = tuple(tuple(map(int, f_i.readline().split())) for i in range(H))
    
    DP = np.full((H + 1, W + 1, (H * W) * 80), False)
    DP[0][1][0] = True
    for i in range(1, H + 1):
        for j in range(1, W + 1):
            diff = A[i-1][j-1] - B[i-1][j-1]
            for pre_bias in np.where(DP[i-1][j] == True):
                bias = abs(pre_bias + diff)
                DP[i][j][bias] = True
                bias = abs(pre_bias - diff)
                DP[i][j][bias] = True
            for pre_bias in np.where(DP[i][j-1] == True):
                bias = abs(pre_bias + diff)
                DP[i][j][bias] = True
                bias = abs(pre_bias - diff)
                DP[i][j][bias] = True
    
    b = np.where(DP[-1][-1] == True)[-1]
    print(min(b))

solve()

Submission status: Runtime Error

Solution code:
/********   All Required Header Files ********/
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
#include <queue>
#include <deque>
#include <bitset>
#include <iterator>
#include <list>
#include <stack>
#include <map>
#include <set>
#include <functional>
#include <numeric>
#include <utility>
#include <limits>
#include <time.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>

using namespace std;

int main()
{
    int H, W;
    cin >> H >> W;
    vector<vector<int>> A, B;
    for (int i = 0; i < H; i++) {
        vector<int> l;
        for (int j = 0; j < W; j++) {
            int x;
            cin >> x;
            l.push_back(x);
        }
        A.push_back(l);
    }
    for (int i = 0; i < H; i++) {
        vector<int> l;
        for (int j = 0; j < W; j++) {
            int x;
            cin >> x;
            l.push_back(x);
        }
        B.push_back(l);
    }

    vector<vector<set<int>>> dp(H, vector<set<int>>(W));

    dp[0][0].insert(abs(A[0][0] - B[0][0]));

    for (int i = 0; i < H; i++) {
        for (int j = 0; j < W; j++) {
            if (i == 0 && j == 0)
                continue;
            int d = abs(A[i][j] - B[i][j]);
            set<int> possible;
            if (i-1 >= 0) {
                for (int diff : dp[i-1][j]) {
                    possible.insert(abs(diff+d));
                    possible.insert(abs(diff-d));
                }
            }
            if (j-1 >= 0) {
                for (int diff : dp[i][j-1]) {
                    possible.insert(abs(diff+d));
                    possible.insert(abs(diff-d));
                }
            }
            for (int diff : possible) {
                if (diff > 6400) {
                    possible.erase(diff);
                }
            }
            dp[i][j] = possible;
        }
    }

    set<int> possible = dp[H-1][W-1];
    int best = 6400;
    for (int diff : possible) {
        best = min(best, diff);
    }
    cout << best << endl;
    return 0;
}

Submission status: Runtime Error

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)]
 
#差分だけで良い，これを*1 or -1していく
diff = [[0]*W for _ in range(H)]
for i in range(H):
    for j in range(W):
        diff[i][j] = abs(A[i][j] - B[i][j])
        
 # i,jマスにおいて，偏りがkになれるかどうかでDP
K = 6401 #適当
kk = int((K-1)/2)
dp = [[[False]*K for _ in range(W)] for _ in range(H)]
 
dp[0][0][kk+diff[0][0]] = True
dp[0][0][kk-diff[0][0]] = True
 

for i in range(H):
    for j in range(W):
        a= min((i+j)*80,K)
        for k in range(kk-a,kk+a):
            if i-1>=0:
                if k+diff[i][j]<K:
                    if dp[i-1][j][k+diff[i][j]]:
                        dp[i][j][k] =True
                if k-diff[i][j]>=0:
                    if dp[i-1][j][k-diff[i][j]]:
                        dp[i][j][k] =True
            if j-1>=0:
                if k+diff[i][j]<K:
                    if dp[i][j-1][k+diff[i][j]]:
                        dp[i][j][k] =True
                if k-diff[i][j]>=0:
                    if dp[i][j-1][k-diff[i][j]]:
                        dp[i][j][k] =True
ans = K


for k in range(K):
    if dp[H-1][W-1][k]:
        ans = min(ans,abs(k-kk))
print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np
HW = list(map(int, input().split()))
H = HW[0]
W = HW[1]
A = np.empty((H, W), dtype=np.int64)
B = np.empty((H, W), dtype=np.int64)
for i in range(H):
  A[i] = np.fromstring(input(), dtype = np.int64, sep = ' ')
for i in range(H):
  B[i] = np.fromstring(input(), dtype = np.int64, sep = ' ')

C = np.abs(A - B)
K = (H + W) * (C.max())

D = np.full((H, W, K), False)
D[0][0][C[0][0]] = True
D[0][1][C[0][0] + C[0][1]] = True
D[0][1][abs(C[0][0] - C[0][1])] = True
D[1][0][C[0][0] + C[1][0]] = True
D[1][0][abs(C[0][0] - C[1][0])] = True

for i in range(1, H):
  for j in range(1, W):
    for k in range(K):
      if k + C[i][j] < K:
        if D[i-1][j][k + C[i][j]] or D[i][j-1][k + C[i][j]]:
          D[i][j][k] = True
          continue
      if D[i-1][j][abs(k - C[i][j])] or D[i][j-1][abs(k - C[i][j])]:
        D[i][j][k] = True

for k in range(K):
  if D[H-1][W-1][k]:
    print(k)
    break

Submission status: Runtime Error

Solution code:
import numpy as np
HW = list(map(int, input().split()))
H = HW[0]
W = HW[1]
A = np.empty((H, W), dtype=np.int64)
B = np.empty((H, W), dtype=np.int64)
for i in range(H):
  A[i] = np.fromstring(input(), dtype = np.int64, sep = ' ')
for i in range(H):
  B[i] = np.fromstring(input(), dtype = np.int64, sep = ' ')

C = np.abs(A - B)
K = H * W * (C.max())

D = np.full((H, W, K), False)
D[0][0][C[0][0]] = True
D[0][1][C[0][0] + C[0][1]] = True
D[0][1][abs(C[0][0] - C[0][1])] = True
D[1][0][C[0][0] + C[1][0]] = True
D[1][0][abs(C[0][0] - C[1][0])] = True

for i in range(1, H):
  for j in range(1, W):
    for k in range(K):
      if k + C[i][j] < K:
        if D[i-1][j][k + C[i][j]] or D[i][j-1][k + C[i][j]] or \
        D[i-1][j][abs(k - C[i][j])] or D[i][j-1][abs(k - C[i][j])]:
          D[i][j][k] = True
          #print('i, j, k: %d, %d, %d' % (i,j,k))
          continue
      if D[i-1][j][abs(k - C[i][j])] or D[i][j-1][abs(k - C[i][j])]:
        #print('i, j, k: %d, %d, %d' % (i,j,k))
        D[i][j][k] = True
        continue

for k in range(K):
  if D[H-1][W-1][k]:
    print(k)
    break

Submission status: Runtime Error

Solution code:
import numpy as np
HW = list(map(int, input().split()))
H = HW[0]
W = HW[1]
A = np.empty((H, W), dtype=np.int64)
B = np.empty((H, W), dtype=np.int64)
for i in range(H):
  A[i] = np.fromstring(input(), dtype = np.int64, sep = ' ')
for i in range(H):
  B[i] = np.fromstring(input(), dtype = np.int64, sep = ' ')

C = np.abs(A - B)
K = H * W * (C.max())

D = np.full((H, W, K), False)
D[0][0][C[0][0]] = True

for i in range(0, H):
  for j in range(0, W):
    for k in range(K):
      if i == 0 and j == 0:
        D[0][0][C[0][0]] = True
        continue
      if k + C[i][j] < K:
        if D[i-1][j][k + C[i][j]] or D[i][j-1][k + C[i][j]] or \
        D[i-1][j][abs(k - C[i][j])] or D[i][j-1][abs(k - C[i][j])]:
          D[i][j][k] = True
          #print('i, j, k: %d, %d, %d' % (i,j,k))
          continue
      if D[i-1][j][abs(k - C[i][j])] or D[i][j-1][abs(k - C[i][j])]:
        #print('i, j, k: %d, %d, %d' % (i,j,k))
        D[i][j][k] = True
        continue

for k in range(K):
  if D[H-1][W-1][k]:
    print(k)
    break

Submission status: Runtime Error

Solution code:
H, W = list(map(int,input().split()))
A = []
B = []
for i in range(H):
    A_tmp = list(map(int,input().split()))
    A_tmp = A_tmp + [0]
    A.append(A_tmp)
A.append([0]*(W+1))
for i in range(H):
    B_tmp = list(map(int,input().split()))
    B_tmp = B_tmp + [0]
    B.append(B_tmp)
B.append([0]*(W+1))

M = 0
for i in range(H):
    for j in range(W):
        M = max(M, abs(A[i][j] - B[i][j]))
DP = [[[False for i in range((H+W)*M)] for j in range(W+1)] for k in range(H+1)]
DP[0][0][abs(A[0][0] - B[0][0])] = True
for i in range(H):
    for j in range(W):
        for k in range((H+W)*M):
            if DP[i][j][k] == True:
                DP[i+1][j][k + abs(A[i+1][j] - B[i+1][j])] = True
                DP[i+1][j][abs(k - abs(A[i+1][j] - B[i+1][j]))] = True
                DP[i][j+1][k + abs(A[i][j+1] - B[i][j+1])] = True
                DP[i][j+1][abs(k - abs(A[i][j+1] - B[i][j+1]))] = True               
ans = 0
for i in range((H+W)*M):
    if DP[H-1][W-1][i] == True:
        ans = i
        break
print(ans)

Submission status: Runtime Error

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

C = [[] for _ in range(H)]
for i in range(H):
  for j in range(W):
    C[i].append(abs(A[i][j] - B[i][j]))
#print(C)

ww = max([sum(C[i]) for i in range(H)])
hh = max([sum([C[j][i] for j in range(H)]) for i in range(W)])

dp = [[[0 for _ in range(hh + ww + 1)] for _ in range(W)] for _ in range(H)]
#print(dp)

dp[0][0][C[0][0]] = 1
for i in range(H):
  for j in range(W):
    for k in range(hh + ww + 1):
      if dp[i-1][j][k]:
        dp[i][j][k + C[i][j]] = 1
        dp[i][j][abs(k - C[i][j])] = 1
      if dp[i][j-1][k]:
        dp[i][j][k + C[i][j]] = 1
        dp[i][j][abs(k - C[i][j])] = 1

for i in range(hh + ww + 1):
  if dp[H-1][W-1][i]:
    print(i)
    exit()

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import math
import datetime
import itertools
 
# 入力
#N = int(input())
#S = str(input())
#N, A, B = map(int, input().split())
 
H, W = map(int, input().split())
A = []
B = []
for i in range(H) :
  al = list(map(int, input().split()))
  A.append(al)
 
for i in range(H) :
  bl = list(map(int, input().split()))
  B.append(bl)
 
#print (A)
#print (B)
 
kmax = 80*80
#kmax = 10

dp = [[[False for _ in range(kmax)] for _ in range(W)] for _ in range(H)]
 
#print (dp)
 
for i in range(H) :
  for j in range(W) :
    #初期値
    if i == 0 and j == 0 :
      dp[i][j][abs(A[i][j] - B[i][j])] = True
    else :
    #初期値以降
      val = abs(A[i][j] - B[i][j])  #値の差
      
      #i=0のときはj-1のkの状態を参照する
      if i == 0 :
        for k in range(kmax) :
          if dp[i][j-1][k] == True :
              dp[i][j][k+val] = True
              dp[i][j][abs(k-val)] = True
        
      #j=0のときはi-1のkの状態を参照する
      elif j == 0 :
        for k in range(kmax) :
          if dp[i-1][j][k] == True :
              dp[i][j][k+val] = True
              dp[i][j][abs(k-val)] = True
      
      #i!=0, j!=0のときはi-1、j-1の両方のkの状態を参照する
      else :
        for k in range(kmax) :
          if dp[i-1][j][k] == True :
              dp[i][j][k+val] = True
              dp[i][j][abs(k-val)] = True
          if dp[i][j-1][k] == True :
              dp[i][j][k+val] = True
              dp[i][j][abs(k-val)] = True
 
#答え
for k in range(kmax) :
  if dp[H-1][W-1][k] == True :
    print (k)
    exit(0)
 
#print (dp[H-1][W-1])

Submission status: Runtime Error

Solution code:
import numpy as np

h, w = list(map(int, input().split()))

aa = [list(map(int, input().split())) for _ in range(h)]
bb = [list(map(int, input().split())) for _ in range(h)]

dif = []
for i in range(h):
    dif.append([int(abs(a - b)) for a, b in zip(aa[i], bb[i])])

dp = [[set() for _ in range(w + 2)] for _ in range(h + 2)]

dp[1][0] = [0]

lim = [[0 for _ in range(w + 2)] for _ in range(h + 2)]
for i in reversed(range(1, h + 1)):
    for j in reversed(range(1, w + 1)):
        lim[i][j] = max(lim[i + 1][j], lim[i][j + 1]) + dif[i - 1][j - 1]



for i in range(1, h + 1):
    for j in range(1, w + 1):
        cand = list(set([x + dif[i - 1][j - 1] for x in dp[i - 1][j]]
                          + [x - dif[i - 1][j - 1] for x in dp[i - 1][j]]
                          + [x + dif[i - 1][j - 1] for x in dp[i][j - 1]]
                          + [x - dif[i - 1][j - 1] for x in dp[i][j - 1]]))
        dp[i][j] = [x for x in cand if x <= lim[i][j]] + [min([x for x in cand if x > lim[i][j]] + [10 ** 10])]

print(min([abs(x) for x in dp[h][w]]))

Submission status: Runtime Error

Solution code:
import numpy as np

h, w = list(map(int, input().split()))

aa = [list(map(int, input().split())) for _ in range(h)]
bb = [list(map(int, input().split())) for _ in range(h)]

dif = []
for i in range(h):
	dif.append([int(abs(a - b)) for a, b in zip(aa[i], bb[i])])


# dp[i][j][k] = マス (i, j) までの経路の偏りが k になることがあるか？
dp = np.zeros((w + 1, h + 1, 82 ** 3))

dp[0, 1, (82 ** 3 // 2)] = 1

#print(dif)
for i in range(1, w + 1):
	for j in range(1, h + 1):
		dp[i, j, 82: (82 ** 3 - 82) ] = (dp[i, j - 1, (82 + dif[j - 1][i - 1]):(82 ** 3 - 82 + dif[j - 1][i - 1])]
										+ dp[i, j - 1, (82 - dif[j - 1][i - 1]):(82 ** 3 - 82 - dif[j - 1][i - 1])]
										+ dp[i - 1, j, (82 + dif[j - 1][i - 1]):(82 ** 3 - 82 + dif[j - 1][i - 1])]
										+ dp[i - 1, j, (82 - dif[j - 1][i - 1]):(82 ** 3 - 82 - dif[j - 1][i - 1])])


ans = 10 ** 10
for i in range(len(dp[w, h])):
	if dp[w, h, i] >= 1:
		ans = min(ans, abs(i - 82 ** 3 // 2))

print(ans)

Submission status: Runtime Error

Solution code:
import numpy as np

h, w = list(map(int, input().split()))

aa = [list(map(int, input().split())) for _ in range(h)]
bb = [list(map(int, input().split())) for _ in range(h)]

dif = []
for i in range(h):
	dif.append([abs(a - b) for a, b in zip(aa[i], bb[i])])


# dp[i][j][k] = マス (i, j) までの経路の偏りが k になることがあるか？
dp = np.zeros((w + 1, h + 1, 82 ** 3))

dp[1, 0, (82 ** 3 // 2)] = 1

#print(dif)
for i in range(1, w + 1):
	for j in range(1, h + 1):
		dp[i, j, 82: (82 ** 3 - 82) ] = (dp[i, j - 1, (82 + dif[j - 1][i - 1]):(82 ** 3 - 82 + dif[j - 1][i - 1])]
										+ dp[i, j - 1, (82 - dif[j - 1][i - 1]):(82 ** 3 - 82 - dif[j - 1][i - 1])]
										+ dp[i - 1, j, (82 + dif[j - 1][i - 1]):(82 ** 3 - 82 + dif[j - 1][i - 1])]
										+ dp[i - 1, j, (82 - dif[j - 1][i - 1]):(82 ** 3 - 82 - dif[j - 1][i - 1])])


ans = 10 ** 10
for i in range(len(dp[w, h])):
	if dp[w, h, i] >= 1:
		ans = min(ans, abs(i - 82 ** 3 // 2))

print(ans)

Submission status: Runtime Error

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

x = (h + w) * 80

dp = [[0] * w for _ in range(h)]
d = abs(a[0][0] - b[0][0])
dp[0][0] = 1 << (x - d)

for i in range(h):
    for j, (ae, be) in enumerate(zip(a[i], b[i])):
        if h == w == 0:
            continue

        d = abs(ae - be)
        bt = 0
        if i != 0:
            bt |= dp[i-1][j] << d
            bt |= dp[i-1][j] >> d

        if j != 0:
            bt |= dp[i][j-1] << d
            bt |= dp[i][j-1] >> d

        dp[i][j] = bt

dp = dp[-1][-1]

b = bin(dp)[2:]
can_make = [i - x for i, be in enumerate(b[::-1]) if be == "1"]

ans = min(x if x >= 0 else -x for x in can_make)
print(ans)

Submission status: Runtime Error

Solution code:
h, w = map(int, input().split())
lsa = [list(map(int, input().split())) for i in range(h)]
lsb = [list(map(int, input().split())) for i in range(h)]
ls = [[abs(lsa[i][j] - lsb[i][j]) for j in range(w)] for i in range(h)]

p=0
for i in range(h):
    p+= sum(ls[i])

dp = [[[False]*p for _ in range(w)] for _ in range(h)]

dp[0][0][ls[0][0]] = True

for i in range(h):
    for j in range(w):

        if j != 0:  
            for k in range(p):
                if dp[i][j-1][k]:
                    dp[i][j][k+ls[i][j]] = True
                    dp[i][j][abs(k-ls[i][j])] = True

        if i != 0:
            for k in range(p):
                if dp[i-1][j][k]:
                    dp[i][j][k+ls[i][j]] = True
                    dp[i][j][abs(k-ls[i][j])] = True

for i in range(p):
    if dp[h-1][w-1][i]:
        break

print(i)

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
dp = [[[0] * 6401 for i in range(w)] for j in range(h)]
a = [list(map(int,input().split())) for i in range(h)]
b = [list(map(int,input().split())) for i in range(h)]
dp[0][0][abs(a[0][0]-b[0][0])] = 1
for i in range(h):
    for j in range(w):
        if i != 0:
            for k in range(6401):
                if dp[i-1][j][k]:
                    dp[i][j][abs(k-abs(a[i][j]-b[i][j]))] = 1
                    dp[i][j][k+abs(a[i][j]-b[i][j])] = 1
        if j != 0:
            for k in range(6401):
                if dp[i][j-1][k]:
                    dp[i][j][abs(k-abs(a[i][j]-b[i][j]))] = 1
                    dp[i][j][k+abs(a[i][j]-b[i][j])] = 1
for i in range(6401):
    if dp[h-1][w-1][i]:
        print(i)
        exit()

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
dp = [[[0] * 6500 for i in range(w)] for j in range(h)]
a = [list(map(int,input().split())) for i in range(h)]
b = [list(map(int,input().split())) for i in range(h)]
dp[0][0][abs(a[0][0]-b[0][0])] = 1
for i in range(h):
    for j in range(w):
        if i != 0:
            for k in range(6500):
                if dp[i-1][j][k]:
                    dp[i][j][abs(k-abs(a[i][j]-b[i][j]))] = 1
                    dp[i][j][k+abs(a[i][j]-b[i][j])] = 1
        if j != 0:
            for k in range(6500):
                if dp[i][j-1][k]:
                    dp[i][j][abs(k-abs(a[i][j]-b[i][j]))] = 1
                    dp[i][j][k+abs(a[i][j]-b[i][j])] = 1
for i in range(6500):
    if dp[h-1][w-1][i]:
        print(i)
        exit()

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
dp = [[[0] * 6500 for i in range(w)] for j in range(h)]
a = [list(map(int,input().split())) for i in range(h)]
b = [list(map(int,input().split())) for i in range(h)]
dp[0][0][abs(a[0][0]-b[0][0])] = 1
for i in range(h):
    for j in range(w):
        for k in range(6500):
            if i != 0:
                if dp[i-1][j][k]:
                    dp[i][j][abs(k-abs(a[i][j]-b[i][j]))] = 1
                    dp[i][j][k+abs(a[i][j]-b[i][j])] = 1
            if j != 0:
                if dp[i][j-1][k]:
                    dp[i][j][abs(k-abs(a[i][j]-b[i][j]))] = 1
                    dp[i][j][k+abs(a[i][j]-b[i][j])] = 1
for i in range(6500):
    if dp[h-1][w-1][i]:
        print(i)
        exit()

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
dp = [[[0] * 6500 for i in range(w)] for j in range(h)]
a = [list(map(int,input().split())) for i in range(h)]
b = [list(map(int,input().split())) for i in range(h)]
dp[0][0][abs(a[0][0]-b[0][0])] = 1
for i in range(h):
    for j in range(w):
        for k in range(6500):
            if i != 0:
                if dp[i-1][j][k]:
                    dp[i][j][abs(k-abs(a[i][j]-b[i][j]))] = 1
                    dp[i][j][k+abs(a[i][j]-b[i][j])] = 1
            if j != 0:
                if dp[i][j-1][k]:
                    dp[i][j][abs(k-abs(a[i][j]-b[i][j]))] = 1
                    dp[i][j][k+abs(a[i][j]-b[i][j])] = 1
for i in range(6401):
    if dp[h-1][w-1][i]:
        print(i)
        exit()

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
dp = [[[0] * 6410 for i in range(w)] for j in range(h)]
a = [list(map(int,input().split())) for i in range(h)]
b = [list(map(int,input().split())) for i in range(h)]
dp[0][0][abs(a[0][0]-b[0][0])] = 1
for i in range(h):
    for j in range(w):
        for k in range(6401):
            if i != 0:
                if dp[i-1][j][k]:
                    dp[i][j][abs(k-abs(a[i][j]-b[i][j]))] = 1
                    dp[i][j][k+abs(a[i][j]-b[i][j])] = 1
            if j != 0:
                if dp[i][j-1][k]:
                    dp[i][j][abs(k-abs(a[i][j]-b[i][j]))] = 1
                    dp[i][j][k+abs(a[i][j]-b[i][j])] = 1
for i in range(6401):
    if dp[h-1][w-1][i]:
        print(i)
        exit()

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
dp = [[[0] * 6410 for i in range(w)] for j in range(h)]
a = [list(map(int,input().split())) for i in range(h)]
b = [list(map(int,input().split())) for i in range(h)]
dp[0][0][abs(a[0][0]-b[0][0])] = 1
for i in range(h):
    for j in range(w):
        for k in range(6401):
            if i != 0:
                if dp[i-1][j][k]:
                    dp[i][j][abs(k-(a[i][j]-b[i][j]))] = 1
                    dp[i][j][abs(k-(b[i][j]-a[i][j]))] = 1
            if j != 0:
                if dp[i][j-1][k]:
                    dp[i][j][abs(k-(a[i][j]-b[i][j]))] = 1
                    dp[i][j][abs(k-(b[i][j]-a[i][j]))] = 1
for i in range(6401):
    if dp[h-1][w-1][i]:
        print(i)
        exit()

Submission status: Runtime Error

Solution code:
h,w = map(int,input().split())
dp = [[[0] * 6401 for i in range(w)] for j in range(h)]
a = [list(map(int,input().split())) for i in range(h)]
b = [list(map(int,input().split())) for i in range(h)]
dp[0][0][abs(a[0][0]-b[0][0])] = 1
for i in range(h):
    for j in range(w):
        for k in range(6401):
            if i != 0:
                if dp[i-1][j][k]:
                    dp[i][j][abs(k-(a[i][j]-b[i][j]))] = 1
                    dp[i][j][abs(k-(b[i][j]-a[i][j]))] = 1
            if j != 0:
                if dp[i][j-1][k]:
                    dp[i][j][abs(k-(a[i][j]-b[i][j]))] = 1
                    dp[i][j][abs(k-(b[i][j]-a[i][j]))] = 1
for i in range(6401):
    if dp[h-1][w-1][i]:
        print(i)
        exit()

Submission status: Runtime Error

Solution code:
# -*- coding: utf-8 -*-
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
write = sys.stdout.write
# template


def resolve():
    ofs = 6400
    h, w = map(int, readline().split())
    A = [list(map(int, readline().split())) for i in range(h)]
    B = [list(map(int, readline().split())) for i in range(h)]
    dp = [[0]*w for _ in range(h)]
    x = abs(A[0][0]-B[0][0])
    dp[0][0] |= ((1 << (ofs+x)) | (1 << (ofs-x)))

    for i in range(h):
        for j in range(w):
            x = abs(A[i][j]-B[i][j])
            t = 0
            if(i-1 >= 0):
                t |= dp[i-1][j]
            if(j-1 >= 0):
                t |= dp[i][j-1]
            dp[i][j] |= ((t << x) | (t >> x))

    for i, s in enumerate(bin(dp[-1][-1])[-ofs-1:]):
        if(s == '1'):
            write("%d\n" % i+1)
            return


if __name__ == "__main__":
    INF = float("inf")
    MOD = 10**9+7
    resolve()

Submission status: Runtime Error

Solution code:
H,W=map(int,input().split())
a=[None]*H
b=[None]*H
for i in range(H):
  a[i]=list(map(int,input().split()))
for i in range(H):
  b[i]=list(map(int,input().split()))

dp=[[[False for i in range(160)] for j in range(W)] for p in range(H)]

dp[0][0][abs(a[0][0]-b[0][0])+80]=True
for i in range(H):
  for j in range(W):
    for k in range(160):
      if dp[i][j][k]:
        if i+1<H:
          p=abs(a[i+1][j]-b[i+1][j])
          if k-p>=0:
            dp[i+1][j][k-p]=True
          if k+p<=159:
            dp[i+1][j][k+p]=True
        if j+1<W:
          p=abs(a[i][j+1]-b[i][j+1])
          if k-p>=0:
            dp[i][j+1][k-p]=True
          if k+p<=159:
            dp[i][j+1][k+p]=True
for k in range(160):
  if dp[-1][-1][k]:
    if k-80>=0:
      print(k-80)
      break

Submission status: Runtime Error

Solution code:
import numpy as np

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

A = []
B = []

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

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

A = np.array(A, dtype=np.uint8)
B = np.array(B, dtype=np.uint8)

dp = np.zeros([H, W, 80**2+1], dtype=bool)
dp[0][0][abs(A[0][0] - B[0][0])] = True

for i in range(H):
    for j in range(W):
        diff = A[i][j] - B[i][j]
        if i > 0:
            idx = np.where(dp[i - 1][j][:])[0]
            dp[i][j][np.clip(np.abs(idx - diff), 0, 80**2)] = True
            dp[i][j][np.clip(np.abs(idx + diff), 0, 80**2)] = True

        if j > 0:
            idx = np.where(dp[i][j - 1][:])[0]
            dp[i][j][np.clip(np.abs(idx - diff), 0, 80**2)] = True
            dp[i][j][np.clip(np.abs(idx + diff), 0, 80**2)] = True

print(dp[H - 1][W - 1][:].nonzero()[0].min())

Submission status: Runtime Error

Solution code:
import numpy as np

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

A = []
B = []

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

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

dp = False * np.ones([H, W, 80**3], dtype=bool)
dp[0][0][abs(A[0][0] - B[0][0])] = True

for i in range(H):
    for j in range(W):
        diff = A[i][j] - B[i][j]
        if i > 0:
            idx = np.where(dp[i - 1][j][:])[0]
            dp[i][j][np.abs(idx - diff)] = True
            dp[i][j][np.abs(idx + diff)] = True

        if j > 0:
            idx = np.where(dp[i][j - 1][:])[0]
            dp[i][j][np.abs(idx - diff)] = True
            dp[i][j][np.abs(idx + diff)] = True

print(dp[H - 1][W - 1][:].nonzero()[0].min())

Submission status: Runtime Error

Solution code:
import numpy as np

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

A = []
B = []

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

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

dp = False * np.ones([H, W, 80**3], dtype=bool)
dp[0][0][abs(A[0][0] - B[0][0])] = True

for i in range(H):
    for j in range(W):
        diff = A[i][j] - B[i][j]
        if i > 0:
            idx = np.where(dp[i - 1][j][:])[0]
            idx1 = np.clip(np.abs(idx - diff), 0, 80**3-1)
            idx2 = np.clip(np.abs(idx + diff), 0, 80**3-1)
            dp[i][j][idx1] = True
            dp[i][j][idx2] = True

        if j > 0:
            idx = np.where(dp[i][j - 1][:])[0]
            idx1 = np.clip(np.abs(idx - diff), 0, 80**3-1)
            idx2 = np.clip(np.abs(idx + diff), 0, 80**3-1)
            dp[i][j][idx1] = True
            dp[i][j][idx2] = True

print(dp[H - 1][W - 1][:].nonzero()[0].min())