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: Time Limit Exceeded

Solution code:
import sys
input=sys.stdin.readline
h,w=map(int,input().split())
red=[list(map(int,input().split()))+[0] for i in range(h)]+[[0 for i in range(w+1)]]
blue=[list(map(int,input().split()))+[0] for i in range(h)]+[[0 for i in range(w+1)]]
field=[[[0 for h in range(6500)] for i in range(w+1)] for j in range(h+1)]
field[0][0][abs(red[0][0]-blue[0][0])]=1
for i in range(h):
    for j in range(w):
        for f in range(6401):
            if field[i][j][f]==1:
                field[i][j+1][f+abs(red[i][j+1]-blue[i][j+1])]=1
                field[i][j+1][abs(f-abs(red[i][j+1]-blue[i][j+1]))]=1
                field[i+1][j][f+abs(red[i+1][j]-blue[i+1][j])]=1
                field[i+1][j][abs(f-abs(red[i+1][j]-blue[i+1][j]))]=1
for i in range(6500):
    if field[h-1][w-1][i]==1:print(i);exit()

Submission status: Time Limit Exceeded

Solution code:
h,w=map(int,input().split())
red=[list(map(int,input().split()))+[0] for i in range(h)]+[[0 for i in range(w+1)]]
blue=[list(map(int,input().split()))+[0] for i in range(h)]+[[0 for i in range(w+1)]]
field=[[[0 for h in range(6500)] for i in range(w+1)] for j in range(h+1)]
field[0][0][abs(red[0][0]-blue[0][0])]=1
for i in range(h):
    for j in range(w):
        for f in range(6401):
            if field[i][j][f]==1:
                field[i][j+1][f+abs(red[i][j+1]-blue[i][j+1])]=1
                field[i][j+1][abs(f-abs(red[i][j+1]-blue[i][j+1]))]=1
                field[i+1][j][f+abs(red[i+1][j]-blue[i+1][j])]=1
                field[i+1][j][abs(f-abs(red[i+1][j]-blue[i+1][j]))]=1
for i in range(6500):
    if field[h-1][w-1][i]==1:print(i);exit()

Submission status: Time Limit Exceeded

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

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)]
M = 100*(h*w)
dp = [[[False]*2*M for j in range(w)] for i in range(h)]
d = abs(A[0][0] - B[0][0])
dp[0][0][M-d] = True
dp[0][0][M+d] = True
for i in range(h):
  for j in range(w):
    d = abs(A[i][j] - B[i][j])
    if i:
      for s in range(2*M):
        if s >= d:
          dp[i][j][s] |= dp[i-1][j][s-d]
        if s < 2*M-d:
          dp[i][j][s] |= dp[i-1][j][s+d]
    if j:
      for s in range(2*M):
        if s >= d:
          dp[i][j][s] |= dp[i][j-1][s-d]
        if s < 2*M-d:
          dp[i][j][s] |= dp[i][j-1][s+d]
for i, v in enumerate(dp[-1][-1]):
  if i >= M and v:
    print(i-M)
    break

Submission status: Time Limit Exceeded

Solution code:
import sys
# input = sys.stdin.buffer.readline
def input(): return sys.stdin.readline().rstrip()
sys.setrecursionlimit(10 ** 7)

MAX = 30000   # (80+80)*80=12800
MID = 15000   # 15000を0とする
H, W = map(int, input().split())
A = [[int(x) for x in input().split()] for _ in range(H)]
B = [[int(x) for x in input().split()] for _ in range(H)]
C = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        C[i][j] = abs(A[i][j] - B[i][j])

dp = [[[0]*MAX for _ in range(W)] for _ in range(H)]
dp[0][0][MID+C[0][0]] = 1
dp[0][0][MID-C[0][0]] = 1
for i in range(H):
    for j in range(W):
        if i:
            for k in range(MAX):
                if dp[i-1][j][k] == 1:
                    dp[i][j][k+C[i][j]] = 1
                    dp[i][j][k-C[i][j]] = 1
        if j:
            for k in range(MAX):
                if dp[i][j-1][k] == 1:
                    dp[i][j][k+C[i][j]] = 1
                    dp[i][j][k-C[i][j]] = 1
ans = MAX
for i in range(MAX):
    if dp[H-1][W-1][i] == 1:
        ans = min(ans, abs(i-MID))
print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys
# input = sys.stdin.buffer.readline
def input(): return sys.stdin.readline().rstrip()
sys.setrecursionlimit(10 ** 7)

# MAX = 1200000    # 80*80*80=512000

H, W = map(int, input().split())
A = [[int(x) for x in input().split()] for _ in range(H)]
B = [[int(x) for x in input().split()] for _ in range(H)]
C = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        C[i][j] = abs(A[i][j] - B[i][j])

dp = [[set() for _ in range(W)] for _ in range(H)]
dp[0][0].add(C[0][0])
for i in range(H):
    for j in range(W):
        if i:
            for k in dp[i-1][j]:
                dp[i][j].add(abs(k+C[i][j]))
                dp[i][j].add(abs(k-C[i][j]))
        if j:
            for k in dp[i][j-1]:
                dp[i][j].add(abs(k+C[i][j]))
                dp[i][j].add(abs(k-C[i][j]))
print(min(dp[H-1][W-1]))

Submission status: Time Limit Exceeded

Solution code:
import sys
# input = sys.stdin.buffer.readline
def input(): return sys.stdin.readline().rstrip()
sys.setrecursionlimit(10 ** 7)

MAX = 1200000    # 80*80*80=512000

H, W = map(int, input().split())
A = [[int(x) for x in input().split()] for _ in range(H)]
B = [[int(x) for x in input().split()] for _ in range(H)]
C = [[0] * W for _ in range(H)]
for i in range(H):
    for j in range(W):
        C[i][j] = abs(A[i][j] - B[i][j])

dp = [[set() for _ in range(W)] for _ in range(H)]
dp[0][0].add(C[0][0])
for i in range(H):
    for j in range(W):
        if i:
            for k in dp[i-1][j]:
                dp[i][j].add(abs(k+C[i][j]))
                dp[i][j].add(abs(k-C[i][j]))
        if j:
            for k in dp[i][j-1]:
                dp[i][j].add(abs(k+C[i][j]))
                dp[i][j].add(abs(k-C[i][j]))
print(min(dp[H-1][W-1]))

Submission status: Time Limit Exceeded

Solution code:
import sys,collections as cl,bisect as bs
sys.setrecursionlimit(100000)
input = sys.stdin.readline
mod = 10**9+7
Max = sys.maxsize
def l(): #intのlist
    return list(map(int,input().split()))
def m(): #複数文字
    return map(int,input().split())
def onem(): #Nとかの取得
    return int(input())
def s(x): #圧縮
    a = []
    if len(x) == 0:
        return []
    aa = x[0]
    su = 1
    for i in range(len(x)-1):
        if aa != x[i+1]:
            a.append([aa,su])
            aa = x[i+1]
            su = 1
        else:
            su += 1
    a.append([aa,su])
    return a
def jo(x): #listをスペースごとに分ける
    return " ".join(map(str,x))
def max2(x): #他のときもどうように作成可能
    return max(map(max,x))
def In(x,a): #aがリスト(sorted)
    k = bs.bisect_left(a,x)
    if k != len(a) and a[k] ==  x:
        return True
    else:
        return False

mod = 10**9+7 #出力の制限

# a = cmb(n,r)
"""
def nibu(x,n,r):
    ll = 0
    rr = r
    while True:
        mid = (ll+rr)//2

    if rr == mid:
        return ll
    if (ここに評価入れる):
        rr = mid
    else:
        ll = mid+1
"""

def ans():
    H,W = m()

    a = []

    for i in range(H):
        a.append(l())

    ma = 0
    b = []
    for i in range(H):
        b.append(l())
        for j in range(W):
            ma = max(ma,abs(a[i][j]-b[i][j]))
    ma += 1
    dp = [[[0 for i in range(ma*(H+W))]for j in range(W)] for k in range(H)]

    for i in range(H):
        for j in range(W):
            if i == 0 and j == 0:
                dp[0][0][abs(a[0][0] - b[0][0])] = 1
            elif i == 0:
                for k in range(ma*(H+W)):
                    if abs(k + abs(a[i][j] - b[i][j])) < ma*(H+W):
                        dp[i][j][abs(k + abs(a[i][j] - b[i][j]))] = max(dp[i][j-1][k],dp[i][j][abs(k + abs(a[i][j] - b[i][j]))])
                    if abs(k - abs(a[i][j] - b[i][j])) < ma*(H+W):
                        dp[i][j][abs(k - abs(a[i][j] - b[i][j]))] = max(dp[i][j-1][k],dp[i][j][abs(k - abs(a[i][j] - b[i][j]))])
            elif j == 0:
                for k in range(ma*(H+W)):
                    if abs(k + abs(a[i][j] - b[i][j])) < ma*(H+W):
                        dp[i][j][abs(k + abs(a[i][j] - b[i][j]))] = max(dp[i-1][j][k],dp[i][j][abs(k + abs(a[i][j] - b[i][j]))])
                    if abs(k - abs(a[i][j] - b[i][j])) < ma*(H+W):
                        dp[i][j][abs(k - abs(a[i][j] - b[i][j]))] = max(dp[i-1][j][k],dp[i][j][abs(k - abs(a[i][j] - b[i][j]))])
            else:
                for k in range(ma*(H+W)):
                    if abs(k + abs(a[i][j] - b[i][j])) < ma*(H+W):
                        dp[i][j][abs(k + abs(a[i][j] - b[i][j]))] = max(dp[i][j-1][k],dp[i-1][j][k],dp[i][j][abs(k + abs(a[i][j] - b[i][j]))])
                    if abs(k - abs(a[i][j] - b[i][j])) < ma*(H+W):
                        dp[i][j][abs(k - abs(a[i][j] - b[i][j]))] = max(dp[i][j-1][k],dp[i-1][j][k],dp[i][j][abs(k - abs(a[i][j] - b[i][j]))])

    for i in range(ma*(H+W)):
        if dp[-1][-1][i] == 1:
            print(i)
            exit()




if __name__ == "__main__":
    ans()

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
from collections import deque
from fractions import gcd
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9 +7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def inputI(): return int(input().strip())
def inputS(): return input().strip()
def inputIL(): return list(map(int,input().split()))
def inputSL(): return list(map(str,input().split()))
def inputILs(n): return list(int(input()) for _ in range(n))
def inputSLs(n): return list(input().strip() for _ in range(n))
def inputILL(n): return [list(map(int, input().split())) for _ in range(n)]
def inputSLL(n): return [list(map(str, input().split())) for _ in range(n)]

#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])
    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])
    return arr

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#############
# Main Code #
#############

H,W = inputIL()
a = inputILL(H)
b = inputILL(H)

for i in range(H):
  for j in range(W):
    a[i][j] = abs(a[i][j]-b[i][j])

m = 80*160
    
dp = [[[False for _ in range(m)] for __ in range(W)] for ___ in range(H)]

for i in range(H):
  for j in range(W):
    cur = a[i][j]
    if i == 0 and j == 0:     
      dp[i][j][cur] = True
    else:
      if i != 0:
        for k in range(m):
          if dp[i-1][j][k] == True:
            dp[i][j][abs(k+cur)] = True
            dp[i][j][abs(k-cur)] = True
      if j != 0:
        for k in range(m):
          if dp[i][j-1][k] == True:
            dp[i][j][abs(k+cur)] = True
            dp[i][j][abs(k-cur)] = True
      
for k in range(m):
  if dp[H-1][W-1][k] == True:
    print(k)
    exit()

Submission status: Time Limit Exceeded

Solution code:
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

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)]
ma = 80*2*80
DP = [[[0]*ma for i in range(w)] for i in range(h)]

for i in range(h):
    for j in range(w):
        k = abs(a[i][j]-b[i][j])
        up = (i+j+1)*80+1
        if i==0 and j==0:
            DP[i][j][k] = 1
            continue
        elif i == 0:
            for l in range(up):
                DP[i][j][l] = DP[i][j-1][abs(l-k)] | DP[i][j-1][l+k]
        elif j == 0:
            for l in range(up):
                DP[i][j][l] = DP[i-1][j][abs(l-k)] | DP[i-1][j][l+k]
        else:
            for l in range(up):
                DP[i][j][l] = DP[i][j-1][abs(l-k)] | DP[i][j-1][l+k] | DP[i-1][j][abs(l-k)] | DP[i-1][j][l+k]
for i in range(ma):
    if DP[-1][-1][i]:
        print(i)
        exit()

Submission status: Time Limit Exceeded

Solution code:
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

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)]
ma = 80*2*80
DP = [[[0]*ma for i in range(w)] for i in range(h)]

for i in range(h):
    for j in range(w):
        k = abs(a[i][j]-b[i][j])
        up = (i+j+1)*80+1
        if i==0 and j==0:
            DP[i][j][k] = 1
            continue
        elif i == 0:
            for l in range(up):
                DP[i][j][l] = DP[i][j-1][abs(l-k)] | DP[i][j-1][l+k]
        elif j == 0:
            for l in range(up):
                DP[i][j][l] = DP[i-1][j][abs(l-k)] | DP[i-1][j][l+k]
        else:
            for l in range(up):
                DP[i][j][l] = DP[i][j-1][abs(l-k)] | DP[i][j-1][l+k] | DP[i-1][j][abs(l-k)] | DP[i-1][j][l+k]
print(DP[-1][-1].index(1))

Submission status: Time Limit Exceeded

Solution code:
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

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)]
ma = 80*2*80
DP = [[[0]*ma for i in range(w)] for i in range(h)]

for i in range(h):
    for j in range(w):
        k = abs(a[i][j]-b[i][j])
        # 重要、最大値の上限はできる限り正確に見積もる
        up = min((i+j+1)*80+1, ma-k)
        if i==0 and j==0:
            DP[i][j][k] = 1
            continue
        # h,w == 1の場合も考慮する、一応...
        elif i == 0:
            for l in range(up):
                DP[i][j][l] = DP[i][j-1][abs(l-k)] | DP[i][j-1][l+k]
        elif j == 0:
            for l in range(up):
                DP[i][j][l] = DP[i-1][j][abs(l-k)] | DP[i-1][j][l+k]
        else:
            for l in range(up):
                DP[i][j][l] = DP[i][j-1][abs(l-k)] | DP[i][j-1][l+k] | DP[i-1][j][abs(l-k)] | DP[i-1][j][l+k]
print(DP[-1][-1].index(1))

Submission status: Time Limit Exceeded

Solution code:
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

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)]
ma = 80*2*80
DP = [[[0]*ma for i in range(w)] for i in range(h)]

for i in range(h):
    for j in range(w):
        k = abs(a[i][j]-b[i][j])
        if i==0 and j==0:
            DP[i][j][k] = 1
        else:
            for l in range(ma-k):
                DP[i][j][l] = DP[i][j-1][abs(l-k)] | DP[i][j-1][l+k] | DP[i-1][j][abs(l-k)] | DP[i-1][j][l+k]

for i in range(ma):
    if DP[-1][-1][i]:
        print(i)
        exit()

Submission status: Time Limit Exceeded

Solution code:
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

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)]
ma = 80*2*80
DP = [[[0]*ma for i in range(w)] for i in range(h)]

for i in range(h):
    for j in range(w):
        k = abs(a[i][j]-b[i][j])
        if i==0 and j==0:
            DP[i][j][k] = 1
            continue
        elif i == 0:
            for l in range(ma-k):
                DP[i][j][l] = DP[i][j-1][abs(l-k)] | DP[i][j-1][l+k]
        elif j == 0:
            for l in range(ma-k):
                DP[i][j][l] = DP[i-1][j][abs(l-k)] | DP[i-1][j][l+k]
        else:
            for l in range(ma-k):
                DP[i][j][l] = DP[i][j-1][abs(l-k)] | DP[i][j-1][l+k] | DP[i-1][j][abs(l-k)] | DP[i-1][j][l+k]

for i in range(ma):
    if DP[-1][-1][i]:
        print(i)
        exit()

Submission status: Time Limit Exceeded

Solution code:
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

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)]
ma = 80*2*80
DP = [[[0]*ma for i in range(w)] for i in range(h)]

for i in range(h):
    for j in range(w):
        k = abs(a[i][j]-b[i][j])
        if i==0 and j==0:
            DP[i][j][k] = 1
            continue
        if i != 0:
            for l in range(ma):
                if DP[i-1][j][l]:
                    DP[i][j][abs(l-k)] = 1
                    DP[i][j][l+k] = 1
        if j != 0:
            for l in range(ma):
                if DP[i][j-1][l]:
                    DP[i][j][abs(l-k)] = 1
                    DP[i][j][l+k] = 1

for i in range(ma):
    if DP[-1][-1][i]:
        print(i)
        exit()

Submission status: Time Limit Exceeded

Solution code:
import sys
reader = (s.rstrip() for s in sys.stdin)
input = reader.__next__

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)]
c = [[set() for i in range(w)] for i in range(h)]

for i in range(h):
    for j in range(w):
        k = abs(a[i][j]-b[i][j])
        if i==0 and j==0:
            prev = set([0])
        elif i==0:
            prev = c[i][j-1]
        elif j==0:
            prev = c[i-1][j]
        else:
            prev = c[i][j-1] | c[i-1][j]
        for l in prev:
            c[i][j].add(l-k)
            c[i][j].add(l+k)

ans = min(abs(i) for i in c[-1][-1])
print(ans)

Submission status: Time Limit Exceeded

Solution code:
h,w = map(int,input().split())
a = []
b = []
for i in range(h):
    a1 = list(map(int,input().split()))
    a.append(a1)
for i in range(h):
    b1 = list(map(int,input().split()))
    b.append(b1)
l = [[] for i in range(h)]
m = 0
for i in range(h):
    for j in range(w):
        m = max(m,a[i][j]-b[i][j])
        l[i].append(abs(a[i][j]-b[i][j]))
dp = [[[0 for i in range(m*(h+w)//2+1)] for i in range(w)] for i in range(h)]
for i in range(h):
    for j in range(w):
        #print(dp)
        for k in range(m*(h+w)//2+1):
            if i == 0:
                if j == 0:
                    dp[i][j][l[i][j]] = 1
                else:
                    if dp[i][j-1][k] == 1:
                        if k+l[i][j] <= m*(h+w)//2:
                            dp[i][j][k+l[i][j]] = 1
                        dp[i][j][abs(k-l[i][j])] = 1
            else:
                if j == 0:
                    if dp[i-1][j][k] == 1:
                        if k+l[i][j] <= m*(h+w)//2:
                            dp[i][j][k+l[i][j]] = 1
                        dp[i][j][abs(k-l[i][j])] = 1
                else:
                    if dp[i-1][j][k] == 1:
                        if k+l[i][j] <= m*(h+w)//2:
                            dp[i][j][k+l[i][j]] = 1
                        dp[i][j][abs(k-l[i][j])] = 1
                    if dp[i][j-1][k] == 1:
                        if k+l[i][j] <= m*(h+w)//2:
                            dp[i][j][k+l[i][j]] = 1
                        dp[i][j][abs(k-l[i][j])] = 1
ans = 80
for i in range(81):
    if dp[h-1][w-1][i] == 1:
        ans = i
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
h,w = map(int,input().split())
a = []
b = []
for i in range(h):
    a1 = list(map(int,input().split()))
    a.append(a1)
for i in range(h):
    b1 = list(map(int,input().split()))
    b.append(b1)
l = [[] for i in range(h)]
for i in range(h):
    for j in range(w):
        l[i].append(abs(a[i][j]-b[i][j]))
dp = [[[0 for i in range(81*(h+w)//2+1)] for i in range(w)] for i in range(h)]
for i in range(h):
    for j in range(w):
        #print(dp)
        for k in range(81*(h+w)//2+1):
            if i == 0:
                if j == 0:
                    dp[i][j][l[i][j]] = 1
                else:
                    if dp[i][j-1][k] == 1:
                        if k+l[i][j] <= 81*(h+w)//2:
                            dp[i][j][k+l[i][j]] = 1
                        dp[i][j][abs(k-l[i][j])] = 1
            else:
                if j == 0:
                    if dp[i-1][j][k] == 1:
                        if k+l[i][j] <= 81*(h+w)//2:
                            dp[i][j][k+l[i][j]] = 1
                        dp[i][j][abs(k-l[i][j])] = 1
                else:
                    if dp[i-1][j][k] == 1:
                        if k+l[i][j] <= 81*(h+w)//2:
                            dp[i][j][k+l[i][j]] = 1
                        dp[i][j][abs(k-l[i][j])] = 1
                    if dp[i][j-1][k] == 1:
                        if k+l[i][j] <= 81*(h+w)//2:
                            dp[i][j][k+l[i][j]] = 1
                        dp[i][j][abs(k-l[i][j])] = 1
ans = 80
for i in range(81):
    if dp[h-1][w-1][i] == 1:
        ans = i
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
h,w = map(int,input().split())
a = []
b = []
for i in range(h):
    a1 = list(map(int,input().split()))
    a.append(a1)
for i in range(h):
    b1 = list(map(int,input().split()))
    b.append(b1)
l = [[] for i in range(h)]
for i in range(h):
    for j in range(w):
        l[i].append(abs(a[i][j]-b[i][j]))
dp = [[[0 for i in range(81*(h+w))] for i in range(w)] for i in range(h)]
for i in range(h):
    for j in range(w):
        #print(dp)
        for k in range(81*(h+w)):
            if i == 0:
                if j == 0:
                    dp[i][j][l[i][j]] = 1
                else:
                    if dp[i][j-1][k] == 1:
                        dp[i][j][k+l[i][j]] = 1
                        dp[i][j][abs(k-l[i][j])] = 1
            else:
                if j == 0:
                    if dp[i-1][j][k] == 1:
                        dp[i][j][k+l[i][j]] = 1
                        dp[i][j][abs(k-l[i][j])] = 1
                else:
                    if dp[i-1][j][k] == 1:
                        dp[i][j][k+l[i][j]] = 1
                        dp[i][j][abs(k-l[i][j])] = 1
                    if dp[i][j-1][k] == 1:
                        dp[i][j][k+l[i][j]] = 1
                        dp[i][j][abs(k-l[i][j])] = 1
ans = 80
for i in range(81):
    if dp[h-1][w-1][i] == 1:
        ans = i
        break
print(ans)

Submission status: Time Limit Exceeded

Solution code:
def main():
    h,w=map(int,input().split())
    L=80*(h+w)+1
    a=[list(map(int,input().split())) for i in range(h)]
    b=[list(map(int,input().split())) for i in range(h)]


    x=[[abs(a[i][j]-b[i][j]) for j in range(w)] for i in range(h)]
    
    p=x[0][0]
    L=8000
    
    state=[[[False for k in range(L)] for j in range(w)] for i in range(h)]


    state[0][0][p]=True

    for j in range(1,w):
        p=x[0][j]
        for k in range(L):
            if state[0][j-1][k]==True:
                state[0][j][k+p]=True
                state[0][j][abs(k-p)]=True

    for i in range(1,h):
        p=x[i][0]
        for k in range(L):
            if state[i-1][0][k]==True:
                state[i][0][k+p]=True
                state[i][0][abs(k-p)]=True

    for i in range(1,h):
        for j in range(1,w):
            p=x[i][j]
            for k in range(L):
                if state[i-1][j][k] | state[i][j-1][k]==True:
                    if k+p<L: state[i][j][k+p]=True
                    state[i][j][abs(k-p)]=True



    for k in range(L):
        if state[h-1][w-1][k]==True:
            print(k)
            break
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
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)]


    x=[[abs(a[i][j]-b[i][j]) for j in range(w)] for i in range(h)]
    
    p=x[0][0]

    """
    tmp=[ x[0][j] for j in range(1,w)]
    for i in range(1,h):
        tmp+=x[i]
    tmp.sort(reverse=True)
    
    L=p+sum(tmp[:h+w-2])+1
    """
    state=[[set() for j in range(w)] for i in range(h)]


    state[0][0].add(x[0][0])

    for j in range(1,w):
        p=x[0][j]
        for k in state[0][j-1]:
            state[0][j].add(k+p)
            state[0][j].add(abs(k-p))

    for i in range(1,h):
        p=x[i][0]
        for k in state[i-1][0]:
            state[i][0].add(k+p)
            state[i][0].add(abs(k-p))

    for i in range(1,h):
        for j in range(1,w):
            p=x[i][j]
            for k in state[i][j-1]:
                state[i][j].add(k+p)
                state[i][j].add(abs(k-p))
            for k in state[i-1][j]:
                state[i][j].add(k+p)
                state[i][j].add(abs(k-p))

    print(min(state[h-1][w-1]))

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    h,w=map(int,input().split())
    L=80*(h+w)+1
    a=[list(map(int,input().split())) for i in range(h)]
    b=[list(map(int,input().split())) for i in range(h)]


    x=[[abs(a[i][j]-b[i][j]) for j in range(w)] for i in range(h)]
    
    p=x[0][0]
    tmp=[ x[0][j] for j in range(1,w)]
    for i in range(1,h):
        tmp+=x[i]
    tmp.sort(reverse=True)
    
    L=p+sum(tmp[:h+w-2])+1
    
    state=[[[0 for k in range(L)] for j in range(w)] for i in range(h)]


    state[0][0][p]=1

    for j in range(1,w):
        p=x[0][j]
        for k in range(L):
            if state[0][j-1][k]==1:
                state[0][j][k+p]=1
                state[0][j][abs(k-p)]=1

    for i in range(1,h):
        p=x[i][0]
        for k in range(L):
            if state[i-1][0][k]==1:
                state[i][0][k+p]=1
                state[i][0][abs(k-p)]=1

    for i in range(1,h):
        for j in range(1,w):
            p=x[i][j]
            for k in range(L):
                if state[i-1][j][k]==1 or state[i][j-1][k]==1:
                    state[i][j][k+p],state[i][j][abs(k-p)]=1,1



    for k in range(L):
        if state[h-1][w-1][k]==1:
            print(k)
            break
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    h,w=map(int,input().split())
    L=80*(h+w)+1
    a=[list(map(int,input().split())) for i in range(h)]
    b=[list(map(int,input().split())) for i in range(h)]


    x=[[abs(a[i][j]-b[i][j]) for j in range(w)] for i in range(h)]
    
    p=x[0][0]
    L=p+80*(h+w-2)+1
    
    state=[[[0 for k in range(L)] for j in range(w)] for i in range(h)]


    state[0][0][p]=1

    for j in range(1,w):
        p=x[0][j]
        for k in range(L):
            if state[0][j-1][k]==1:
                state[0][j][k+p]=1
                state[0][j][abs(k-p)]=1

    for i in range(1,h):
        p=x[i][0]
        for k in range(L):
            if state[i-1][0][k]==1:
                state[i][0][k+p]=1
                state[i][0][abs(k-p)]=1

    for i in range(1,h):
        for j in range(1,w):
            p=x[i][j]
            for k in range(L):
                if state[i-1][j][k]==1 or state[i][j-1][k]==1:
                    state[i][j][k+p],state[i][j][abs(k-p)]=1,1



    for k in range(L):
        if state[h-1][w-1][k]==1:
            print(k)
            break
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    h,w=map(int,input().split())
    L=80*h*w+1
    a=[list(map(int,input().split())) for i in range(h)]
    b=[list(map(int,input().split())) for i in range(h)]


    x=[[abs(a[i][j]-b[i][j]) for j in range(w)] for i in range(h)]
    state=[[[0 for k in range(L)] for j in range(w)] for i in range(h)]

    p=x[0][0]
    state[0][0][p]=1

    for j in range(1,w):
        p=x[0][j]
        for k in range(L):
            if state[0][j-1][k]==1:
                state[0][j][k+p]=1
                state[0][j][abs(k-p)]=1

    for i in range(1,h):
        p=x[i][0]
        for k in range(L):
            if state[i-1][0][k]==1:
                state[i][0][k+p]=1
                state[i][0][abs(k-p)]=1

    for i in range(1,h):
        for j in range(1,w):
            p=x[i][j]
            for k in range(L):
                if state[i-1][j][k]==1 or state[i][j-1][k]==1:
                    state[i][j][k+p],state[i][j][abs(k-p)]=1,1



    for k in range(L):
        if state[h-1][w-1][k]==1:
            print(k)
            break
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline
h,w = map(int,input().split())
la = [list(map(int,input().split())) for i in range(h)]
lb = [list(map(int,input().split())) for i in range(h)]
k = (h+w)*80+2

dp = [[[-1 for l in range(k)] for i in range(w)] for j in range(h)]

dp[0][0][abs(la[0][0]-lb[0][0])] = 1


for i in range(h):
    for j in range(w):
        for l in range(k):
            if dp[i][j][l] == 1:
                if j + 1 < w:
                    a = abs(la[i][j+1]-lb[i][j+1])
                
                    dp[i][j+1][l+a] = 1
                    dp[i][j+1][abs(l-a)] = 1
                if i + 1 < h:
                    a = abs(la[i+1][j]-lb[i+1][j])
                
                    dp[i+1][j][l+a] = 1
                    dp[i+1][j][abs(l-a)] = 1
print(dp[-1][-1].index(1))

Submission status: Time Limit Exceeded

Solution code:
h,w = map(int,input().split())
la = [list(map(int,input().split())) for i in range(h)]
lb = [list(map(int,input().split())) for i in range(h)]
k = (h+w)*80+2

dp = [[[-1 for l in range(k)] for i in range(w)] for j in range(h)]

dp[0][0][abs(la[0][0]-lb[0][0])] = 1


for i in range(h):
    for j in range(w):
        for l in range(k):
            if dp[i][j][l] == 1:
                if j + 1 < w:
                    a = abs(la[i][j+1]-lb[i][j+1])
                
                    dp[i][j+1][l+a] = 1
                    dp[i][j+1][abs(l-a)] = 1
                if i + 1 < h:
                    a = abs(la[i+1][j]-lb[i+1][j])
                
                    dp[i+1][j][l+a] = 1
                    dp[i+1][j][abs(l-a)] = 1
for i in range(k):
    if dp[-1][-1][i] == 1:
        print(i)
        exit()