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

Solution (Python):
def getval():
    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)]
    return h,w,a,b

def main(h,w,a,b):
    dp = []
    for i in range(w):
        dp.append([False for j in range(1+160*(i+1))])

    dp[0][abs(a[0][0]-b[0][0])] = True
    for i in range(w-1):
        cura = a[0][i+1]
        curb = b[0][i+1]
        prev = dp[i]
        for j in range(len(prev)):
            if prev[j]:
                u1 = abs(j-(cura-curb))
                u2 = abs(j-(curb-cura))
                for k in [u1,u2]:
                    if k<=160*80:
                        dp[i+1][k] = True
    
      
    for i in range(h-1):
        temp = []
        rowa = a[i+1]
        rowb = b[i+1]
        for j in range(w):
            cur = [False for k in range(min(1+160*(i+j+2),1+160*80))]
            prev1 = dp[j]
            prev2 = dp[j]
            if temp:
                prev2 = temp[-1]
            cura = rowa[j]
            curb = rowb[j]
            for k in range(len(prev1)):
                if prev1[k] or prev2[k]:
                    u1 = abs(k-(cura-curb))
                    u2 = abs(k-(curb-cura))
                    if u1<len(cur):
                        cur[u1] = True
                    if u2<len(cur):
                        cur[u2] = True
            temp.append(cur)
        dp = temp
        
          

    ans = 0
    for i in range(160*80+1):
        if dp[-1][i]:
            ans = i 
            break
    print(ans)        

if __name__=="__main__":
    h,w,a,b = getval()
    main(h,w,a,b)

Solution (Python):
f=lambda:map(int,input().split())
H,W=f()
A=[[*f()] for _ in [0]*H]
B=[[*f()] for _ in [0]*H]
G=[[set() for i in [0]*W] for j in [0]*H]
G[0][0].add(0)
for h in range(H):
    for w in range(W):
        d=A[h][w]-B[h][w]
        for a in G[h][w]:
            if h!=H-1:
                G[h+1][w].add(abs(a+d))
                G[h+1][w].add(abs(a-d))
            if w!=W-1:
                G[h][w+1].add(abs(a+d))
                G[h][w+1].add(abs(a-d))
res=set()
d=A[H-1][W-1]-B[H-1][W-1]
for a in G[H-1][W-1]:
    res.add(abs(a+d))
    res.add(abs(a-d))
print(min(res))

Solution (Python):
import sys
input = sys.stdin.readline
MI=lambda:map(int,input().split())
LI=lambda:list(map(int,input().split()))

H,W=MI()
A=[LI() for _ in [0]*H]
B=[LI() for _ in [0]*H]

G=[[set() for i in range(W)] for j in range(H)]
G[0][0].add(0)

for h in range(H):
    for w in range(W):
        d=A[h][w]-B[h][w]
        for a in G[h][w]:
            if h!=H-1:
                G[h+1][w].add(abs(a+d))
                G[h+1][w].add(abs(a-d))
            if w!=W-1:
                G[h][w+1].add(abs(a+d))
                G[h][w+1].add(abs(a-d))

res=set()
d=A[H-1][W-1]-B[H-1][W-1]
for a in G[H-1][W-1]:
    res.add(abs(a+d))
    res.add(abs(a-d))

res=sorted(list(res))
print(min(res))

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline
f_inf = float('inf')
mod = 10 ** 9 + 7


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

    dp = [[set() for _ in range(W)] for _ in range(H)]
    dp[0][0] = {abs(A[0][0] - B[0][0])}
    for h in range(H):
        for w in range(W):
            a, b = A[h][w], B[h][w]
            if w - 1 >= 0:
                for num in dp[h][w - 1]:
                    dp[h][w].add(abs(num + a - b))
                    dp[h][w].add(abs(-num + a - b))
            if h - 1 >= 0:
                for num in dp[h - 1][w]:
                    dp[h][w].add(abs(num + a - b))
                    dp[h][w].add(abs(-num + a - b))

    res = f_inf
    for num in dp[-1][-1]:
        res = min(res, abs(num))
    print(res)


if __name__ == '__main__':
    resolve()

Solution (Python):
H,W=map(int,input().split())
red=[]
blue=[]
for i in range(H):
  A=list(map(int,input().split()))
  red.append(A)
for i in range(H):
  B=list(map(int,input().split()))
  blue.append(B)
list=[[[0 for i in range(80*(j+k+1)+1)] for j in range(W)] for k in range(H)]
list[0][0][abs(red[0][0]-blue[0][0])]=1
for i in range(H):
  for j in range(W):
    for k in range(len(list[i][j])):
      if list[i][j][k]==1:
        if i+1<H:
          if abs(k-abs(red[i+1][j]-blue[i+1][j]))<=80*(i+j+2): 
            list[i+1][j][abs(k-abs(red[i+1][j]-blue[i+1][j]))]=1
          if abs(k+abs(red[i+1][j]-blue[i+1][j]))<=80*(i+j+2): 
            list[i+1][j][abs(k+abs(red[i+1][j]-blue[i+1][j]))]=1
        if j+1<W:
          if abs(k-abs(red[i][j+1]-blue[i][j+1]))<=80*(i+j+2): 
            list[i][j+1][abs(k-abs(red[i][j+1]-blue[i][j+1]))]=1
          if abs(k+abs(red[i][j+1]-blue[i][j+1]))<=80*(i+j+2): 
            list[i][j+1][abs(k+abs(red[i][j+1]-blue[i][j+1]))]=1           
for i in range(12801):
  if list[H-1][W-1][i]==1:
    print(i)
    break

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    H, W = map(int, readline().split())
    A = [list(map(int, readline().split())) for _ in range(H)]
    B = [list(map(int, readline().split())) for _ in range(H)]

    M = 0
    G = [[0] * W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            G[i][j] = abs(A[i][j] - B[i][j])
            if M < G[i][j]:
                M = G[i][j]

    M *= max(H, W)
    dp = [0] * W
    dp[0] = 1 << (M + G[0][0]) | 1 << (M - G[0][0])

    for i in range(H):
        for j in range(W):
            if j + 1 < W:
                dp[j + 1] |= (dp[j] >> G[i][j + 1]) | (dp[j] << G[i][j + 1])
            if i + 1 < H:
                dp[j] = (dp[j] >> G[i + 1][j]) | (dp[j] << G[i + 1][j])

    ans = INF
    for i in range(2 * M + 2):
        if dp[W - 1] & (1 << i) and ans > abs(i - M):
            ans = abs(i - M)

    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    H, W = map(int, readline().split())
    A = [list(map(int, readline().split())) for _ in range(H)]
    B = [list(map(int, readline().split())) for _ in range(H)]

    M = 0
    G = [[0] * W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            G[i][j] = abs(A[i][j] - B[i][j])
            if M < G[i][j]:
                M = G[i][j]

    M *= max(H, W)
    dp = [[0] * W for _ in range(H)]
    dp[0][0] = 1 << (M + G[0][0]) | 1 << (M - G[0][0])

    for i in range(H):
        for j in range(W):
            if i + 1 < H:
                dp[i + 1][j] |= (dp[i][j] >> G[i + 1][j]) | (dp[i][j] << G[i + 1][j])
            if j + 1 < W:
                dp[i][j + 1] |= (dp[i][j] >> G[i][j + 1]) | (dp[i][j] << G[i][j + 1])

    ans = INF
    for i in range(2 * M + 2):
        if dp[H - 1][W - 1] & (1 << i) and ans > abs(i - M):
            ans = abs(i - M)

    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    H, W = map(int, readline().split())
    A = [list(map(int, readline().split())) for _ in range(H)]
    B = [list(map(int, readline().split())) for _ in range(H)]

    M = 0
    G = [[0] * W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            G[i][j] = abs(A[i][j] - B[i][j])
            if M < G[i][j]:
                M = G[i][j]

    M = 20000
    dp = [[0] * W for _ in range(H)]
    dp[0][0] = 1 << (M + G[0][0]) | 1 << (M - G[0][0])

    for i in range(H):
        for j in range(W):
            if i + 1 < H:
                dp[i + 1][j] |= (dp[i][j] >> G[i + 1][j]) | (dp[i][j] << G[i + 1][j])
            if j + 1 < W:
                dp[i][j + 1] |= (dp[i][j] >> G[i][j + 1]) | (dp[i][j] << G[i][j + 1])

    ans = INF
    for i in range(2 * M + 2):
        if dp[H - 1][W - 1] & (1 << i) and ans > abs(i - M):
            ans = abs(i - M)

    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    H, W = map(int, readline().split())
    A = [list(map(int, readline().split())) for _ in range(H)]
    B = [list(map(int, readline().split())) for _ in range(H)]

    M = 0
    G = [[0] * W for _ in range(H)]
    for i in range(H):
        for j in range(W):
            G[i][j] = abs(A[i][j] - B[i][j])
            if M < G[i][j]:
                M = G[i][j]

    M = 20000
    dp = [[0] * W for _ in range(H)]
    dp[0][0] = 1 << (M + G[0][0]) | 1 << (M - G[0][0])

    for i in range(H):
        for j in range(W):
            if i + 1 < H:
                dp[i + 1][j] |= (dp[i][j] >> G[i + 1][j]) | (dp[i][j] << G[i + 1][j])
            if j + 1 < W:
                dp[i][j + 1] |= (dp[i][j] >> G[i][j + 1]) | (dp[i][j] << G[i][j + 1])

    ans = INF
    for i in range(2 * M + 2):
        if dp[H - 1][W - 1] >> i & 1 and ans > abs(i - M):
            ans = abs(i - M)

    print(ans)
    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    H, W = map(int, readline().split())
    A = [list(map(int, readline().split())) for _ in range(H)]
    B = [list(map(int, readline().split())) for _ in range(H)]

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

    dp = [set() for j in range(W)]
    dp[0].add(G[0][0])

    for i in range(H):
        for j in range(W):
            if i == 0 and j == 0:
                continue
            s = set()
            b = G[i][j]
            if i > 0:
                for k in dp[j]:
                    s.add(k + b)
                    s.add(abs(k - b))
            if j > 0:
                for k in dp[j - 1]:
                    s.add(k + b)
                    s.add(abs(k - b))
            dp[j] = s

    print(min(dp[W - 1]))
    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    H, W = map(int, readline().split())
    A = [list(map(int, readline().split())) for _ in range(H)]
    B = [list(map(int, readline().split())) for _ in range(H)]

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

    dp = [[set() for j in range(W)] for i in range(H)]
    dp[0][0].add(G[0][0])

    for i in range(H):
        for j in range(W):
            if i == 0 and j == 0:
                continue
            s = dp[i][j]
            b = G[i][j]
            if i > 0:
                for k in dp[i - 1][j]:
                    s.add(k + b)
                    s.add(abs(k - b))
            if j > 0:
                for k in dp[i][j - 1]:
                    s.add(k + b)
                    s.add(abs(k - b))

    print(min(dp[H - 1][W - 1]))
    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    H, W = map(int, readline().split())
    A = [list(map(int, readline().split())) for _ in range(H)]
    B = [list(map(int, readline().split())) for _ in range(H)]

    G = [[0] * W for _ in range(H)]
    M = 0
    for i in range(H):
        for j in range(W):
            G[i][j] = abs(A[i][j] - B[i][j])
            if M < G[i][j]:
                M = G[i][j]

    dp = [[set() for j in range(W + 1)] for i in range(H + 1)]
    dp[0][1].add(0)
    dp[1][0].add(0)

    for i in range(H):
        for j in range(W):
            s = dp[i + 1][j + 1]
            b = G[i][j]
            for k in dp[i + 1][j]:
                s.add(k + b)
                s.add(abs(k - b))
            for k in dp[i][j + 1]:
                s.add(k + b)
                s.add(abs(k - b))

    print(min(dp[H][W]))
    return


if __name__ == '__main__':
    main()

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    H, W = map(int, readline().split())
    A = [list(map(int, readline().split())) for _ in range(H)]
    B = [list(map(int, readline().split())) for _ in range(H)]

    G = [[0] * W for _ in range(H)]
    M = 0
    for i in range(H):
        for j in range(W):
            G[i][j] = abs(A[i][j] - B[i][j])
            if M < G[i][j]:
                M = G[i][j]

    dp = [[set() for j in range(W + 1)] for i in range(H + 1)]
    dp[0][1].add(0)
    dp[1][0].add(0)

    for i in range(H):
        for j in range(W):
            b = G[i][j]
            for k in dp[i + 1][j]:
                dp[i + 1][j + 1].add(k + b)
                dp[i + 1][j + 1].add(abs(k - b))
            for k in dp[i][j + 1]:
                dp[i + 1][j + 1].add(k + b)
                dp[i + 1][j + 1].add(abs(k - b))

    print(min(dp[H][W]))
    return


if __name__ == '__main__':
    main()

Solution (Python):
H,W=map(int,input().split())
A=[[int(i) for i in input().split()] for j in range(H)]
B=[[int(i) for i in input().split()] for j in range(H)]
dp=[[0 for j in range(W)] for i in range(H)]
M=161*161
dp[0][0]|=1<<(A[0][0]-B[0][0]+M)
dp[0][0]|=1<<(-A[0][0]+B[0][0]+M)

for i in range(H):
    for j in range(W):
        dij=abs(A[i][j]-B[i][j])
        if i>0:
            dp[i][j]|=dp[i-1][j]<<dij
            dp[i][j]|=dp[i-1][j]>>dij
        if j>0:
            dp[i][j]|=dp[i][j-1]>>dij
            dp[i][j]|=dp[i][j-1]<<dij
ans=[]
for k in range(2*M+1):
    if (1<<k)&dp[-1][-1]!=0:
        ans.append(abs(k-M))
print(min(ans))

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]
def input():
    return sys.stdin.readline().rstrip()
def rand_N(ran1, ran2):
    return random.randint(ran1, ran2)
def rand_List(ran1, ran2, rantime):
    return [random.randint(ran1, ran2) for i in range(rantime)]
def rand_ints_nodup(ran1, ran2, rantime):
  ns = []
  while len(ns) < rantime:
    n = random.randint(ran1, ran2)
    if not n in ns:
      ns.append(n)
  return sorted(ns)

def rand_query(ran1, ran2, rantime):
  r_query = []
  while len(r_query) < rantime:
    n_q = rand_ints_nodup(ran1, ran2, 2)
    if not n_q in r_query:
      r_query.append(n_q)
  return sorted(r_query)

from collections import defaultdict, deque, Counter
from sys import exit
from decimal import *
from heapq import heapify, heappop, heappush
import math
import random
import string
from copy import deepcopy
from itertools import combinations, permutations, product
from operator import mul, itemgetter
from functools import reduce
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)
mod = 10 ** 9 + 7





MAX_DIFF = 80
h, w = getNM()

A = [getList() for i in range(h)]
B = [getList() for i in range(h)]
c = [[abs(A[i][j] - B[i][j]) for j in range(w)] for i in range(h)]



sets = [[0 for j in range(w)] for i in range(h)]

sets[0][0] = (1 << MAX_DIFF + c[0][0]) | (1 << MAX_DIFF - c[0][0])


for i in range(1, h):
    
    sets[i][0] |= (sets[i - 1][0] << MAX_DIFF + c[i][0]) | (sets[i - 1][0] << MAX_DIFF - c[i][0])

for i in range(1, w):
    sets[0][i] |= (sets[0][i - 1] << MAX_DIFF + c[0][i]) | (sets[0][i - 1] << MAX_DIFF - c[0][i])
for i in range(1, h):
    for j in range(1, w):
        sets[i][j] |= (sets[i - 1][j] << MAX_DIFF + c[i][j]) | (sets[i - 1][j] << MAX_DIFF - c[i][j])
        sets[i][j] |= (sets[i][j - 1] << MAX_DIFF + c[i][j]) | (sets[i][j - 1] << MAX_DIFF - c[i][j])


s = bin(sets[h - 1][w - 1] + (1 << (h + w) * MAX_DIFF))
min_diff = 1 << MAX_DIFF
for i in range(len(s)):
    if s[- 1 - i] == '1': 
        min_diff = min(min_diff, abs(i - (h + w - 1) * MAX_DIFF))
print(min_diff)

Solution (Python):
H,W=map(int,input().split())
A=[[int(i) for i in input().split()] for j in range(H)]
B=[[int(i) for i in input().split()] for j in range(H)]
dp=[[0 for j in range(W)] for i in range(H)]
M=161*161
dp[0][0]|=1<<(A[0][0]-B[0][0]+M)
dp[0][0]|=1<<(-A[0][0]+B[0][0]+M)

for i in range(H):
    for j in range(W):
        dij=abs(A[i][j]-B[i][j])
        if i>0:
            dp[i][j]|=dp[i-1][j]<<dij
            dp[i][j]|=dp[i-1][j]>>dij
        if j>0:
            dp[i][j]|=dp[i][j-1]>>dij
            dp[i][j]|=dp[i][j-1]<<dij
ans=[]
for k in range(2*M+1):
    if (1<<k)&dp[-1][-1]!=0:
        ans.append(abs(k-M))
print(min(ans))

Solution (Python):
ma = lambda :map(int,input().split())
lma = lambda :list(map(int,input().split()))
tma = lambda :tuple(map(int,input().split()))
ni = lambda:int(input())
yn = lambda fl:print("Yes") if fl else print("No")
import collections
import math
import itertools
import heapq as hq

h,w = ma()
A = []
for i in range(h):
    A.append(lma())
B= []
for i in range(h):
    B.append(lma())
D = [[abs(A[i][j]-B[i][j]) for j in range(w)] for i in range(h)]
mx = 0
for i in range(h):
    for j in range(w):
        mx = max(mx,D[i][j])

le = (h+w)*mx
prev = [0]*w
nex = [0]*w
prev[0]=prev[0] | 2**le
for i in range(h):
    for j in range(w):
        d = D[i][j]
        if j==0:
            nex[j] = nex[j] | prev[j]>>d |prev[j]<<d
        else:
            nex[j] = nex[j] | prev[j]>>d |prev[j]<<d
            nex[j] = nex[j] | nex[j-1]>>d |nex[j-1]<<d
        
    prev=nex
    nex = [0]*w
for i in range(le+1):
    if (prev[w-1]>>le+i &1) or (prev[w-1]>>le-i &1):
        print(i)
        exit()

Solution (Python):
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))


"""
面倒なので差分を考えておく，
この差分を+ or - のどちらで使うかの2択，
合計値を0に近づけたい，
各マスごとに取れる範囲をsetで持ちながら，とやりたいが取れる範囲が多そうな気もする．

絶対値が6400を超える分は持つ必要ないな，ギリ行けるかも?
やっぱり遅いか.
dpだと遷移が遅そうだからsetにしようとしたけど，これも遅い．

dpで取れる値をいっぺんにシフトできれば高速化可能，めっちゃでかい二進数を用意する？

"""
def main():
    mod=10**9+7
    H,W=MI()
    A=[]
    B=[]
    for i in range(H):
        a=LI()
        A.append(a)
    for i in range(H):
        b=LI()
        B.append(b)
        
    AB=[[0]*(W+1) for _ in range(H+1)]
    for i in range(H):
        for j in range(W):
            ab=abs(A[i][j] - B[i][j])
            AB[i][j]=ab
            
    offset = 80*(H+W) 
    
    dp=[[0]*(W+1) for _ in range(H+1)]
    
    def calc(a,d):
        
        return (a<<d) | (a>>d) 
    
    
    dp[0][0]=1<<(offset+AB[0][0]) | 1<<(offset-AB[0][0])
    
    for i in range(H):
        for j in range(W):
            dp[i][j+1] = dp[i][j+1] | calc(dp[i][j],AB[i][j+1])
            dp[i+1][j] = dp[i+1][j] | calc(dp[i][j],AB[i+1][j])

    
    a=dp[-2][-2]
    
    
    a=a>>offset 
    for i in range(offset):
        if a>>i & 1:
            print(i)
            break
        
    
    
    
    
    
    
    
        
                    
                
    

main()

Solution (Python):
import sys
import itertools

import time
import math
from heapq import heappop, heappush
from collections import defaultdict
from collections import Counter
from collections import deque
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


H,W = map(int,readline().split())
A = [list(map(int,readline().split())) for _ in range(H)]
B = [list(map(int,readline().split())) for _ in range(H)]
C = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]
 
X = (H + W) * 80
L = X + X + 1
dp = [[0] * W for _ in range(H)]


dp[0][0] |= 1 << (X - C[0][0])
dp[0][0] |= 1 << (X + C[0][0])
for i in range(H):
    for j in range(W):
        if i > 0:
            dp[i][j] |= dp[i - 1][j] << C[i][j]
            dp[i][j] |= dp[i - 1][j] >> C[i][j]
        if j > 0:
            dp[i][j] |= dp[i][j - 1] << C[i][j]
            dp[i][j] |= dp[i][j - 1] >> C[i][j]

ans = L
for i in range(L):
    if dp[-1][-1] & 1 << i > 0:
        ans = min(ans, abs(i - X))
print(ans)

Solution (Python):
import sys
import itertools

import time
import math
from heapq import heappop, heappush
from collections import defaultdict
from collections import Counter
from collections import deque
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines


H,W = map(int,readline().split())
A = [list(map(int,readline().split())) for _ in range(H)]
B = [list(map(int,readline().split())) for _ in range(H)]
C = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]
 
X = (H + W) * 80
L = X + X + 1
dp = [[0] * W for _ in range(H)]


dp[0][0] |= 1 << (X - C[0][0])
dp[0][0] |= 1 << (X + C[0][0])
for i in range(H):
    for j in range(W):
        if i > 0:
            dp[i][j] |= dp[i - 1][j] << C[i][j]
            dp[i][j] |= dp[i - 1][j] >> C[i][j]
        if j > 0:
            dp[i][j] |= dp[i][j - 1] << C[i][j]
            dp[i][j] |= dp[i][j - 1] >> C[i][j]

ans = L
for i in range(L):
    if dp[-1][-1] & 1 << i > 0:
        ans = min(ans, abs(i - X))
print(ans)

Solution (Python):
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)]
diffs = [[abs(A[i][j] - B[i][j]) for j in range(W)] for i in range(H)]
max_diff = 0
for diff in diffs:
    max_diff = max(max_diff, max(diff))
r_k = max_diff*(H+W)//2+1
dp = [[[False] * r_k for _ in range(W)] for _ in range(H)]
dp[0][0][0] = True
for i in range(H):
    for j in range(W):
        diff = diffs[i][j]
        dp_ = dp[i][j]
        if j < W-1:
            dpj = dp[i][j + 1]
            for k in range(r_k):
                if dp_[k] == True:
                    dpj[abs(k - diff)] = True
                    if k+diff<r_k:
                        dpj[k + diff] = True
        if i < H - 1:
            dpi = dp[i + 1][j]
            for k in range(r_k):
                if dp_[k] == True:
                    dpi[abs(k-diff)] = True
                    if k+diff < r_k:
                        dpi[k + diff] = True

ans = pow(80, 3)
diff = diffs[H-1][W-1]
for k, flg in enumerate(dp[H - 1][W - 1]):
    if flg:
        ans = min(ans, abs(k-diff), k+diff)
print(ans)

Solution (Python):
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy


import heapq
sys.setrecursionlimit(10**6)

INF = 10**18
import bisect
import statistics
mod = 10**9+7


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

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

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

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

dp = [[0 for j in range(W+1)] for i in range(H+1)]
dp[0][1] = 1<<6400
dp[1][0] = 1<<6400
for i in range(1,H+1):
    for j in range(1,W+1):
        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]

for i in range(6400,INF):
    if dp[H][W]>>i & 1:
        print(i-6400)
        sys.exit()

Solution (Python):
h,w=map(int,input().split())
A=[]
B=[]
for i in range(h):
    A.append([int(i) for i in input().split()])
for i in range(h):
    B.append([int(i) for i in input().split()])

for i in range(h):
    A[i]=[abs(A[i][q]-B[i][q]) for q in range(w)]
    

DP=[[[0 for i in range(12801)] for q in range(w)] for r in range(h)]
DP[0][0][A[0][0]]=1

for i in range(h):
    for q in range(w):
        now=DP[i][q]
        if i<h-1:
            nex=DP[i+1][q]
            for r in range(12801):
                if now[r]==1:
                    nxsc=A[i+1][q]
                    nex[abs(r-nxsc)]=1
                    if r+nxsc<12801:
                        nex[r+nxsc]=1
                        
        if q<w-1:
            nex=DP[i][q+1]
            for r in range(12801):
                if now[r]==1:
                    nxsc=A[i][q+1]
                    nex[abs(r-nxsc)]=1
                    if r+nxsc<12801:
                        nex[r+nxsc]=1

las=DP[-1][-1]
ans=12801
for i in range(12800,-1,-1):
    if las[i]==1:
        ans=i
print(ans)

Solution (Python):
H, W = map(int, input().split())
A = [None]*H
for i in range(H):
    A[i] = list(map(int, input().split()))
B = [None]*H
for i in range(H):
    B[i] = list(map(int, input().split()))

C = [[None]*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])

K = 80*(H+W-1)//2 + 1
dp = [[[0]*(K+1) for _ in range(W)] for _ in range(H)]
dp[0][0][C[0][0]] = 1

for i in range(H):
    for j in range(W):
        for k in range(K+1):
            if i and dp[i-1][j][k]:
                if k+C[i][j] < K:
                    dp[i][j][k+C[i][j]] = 1
                dp[i][j][abs(k-C[i][j])] = 1
            if j and dp[i][j-1][k]:
                if k+C[i][j] < K:
                    dp[i][j][k+C[i][j]] = 1
                dp[i][j][abs(k-C[i][j])] = 1

for k in range(K+1):
    res = dp[H-1][W-1][k]
    if res:
        print(k)
        break

Solution (Python):
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 = [[abs(a[i][j] - b[i][j]) for j in range(w)] for i in range(h)]
dp = [[0] * (13000 * (w + 1)) for _ in range(h + 1)]
dp[1][0] = 1
dp[0][13000] = 1
for i in range(1, h + 1):
    for j in range(1, w + 1):
        x = c[i - 1][j - 1]
        for k in range(13000):
            y = 0
            if k + x <= 12999:
                dp[i][13000 * j + k] |= (dp[i - 1][13000 * j + k + x] | dp[i][13000 * (j - 1) + k + x])
            dp[i][13000 * j + k] |= (dp[i - 1][13000 * j + abs(k - x)] | dp[i][13000 * (j - 1) + abs(k - x)])
for i in range(13000):
    if dp[h][13000 * w + i]:
        ans = i
        print(ans)
        exit()

Solution (Python):
'''
研究室PCでの解答
'''
import math

import queue
import bisect
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit


ipt = stdin.readline
setrecursionlimit(10**7)
mod = 10**9+7 
dir = [(-1,0),(1,0),(0,-1),(0,1)]
alp = "abcdefghijklmnopqrstuvwxyz"


def main():
    h,w = map(int,ipt().split())
    a = [[int(i) for i in ipt().split()] for j in range(h)]
    b = [[int(i) for i in ipt().split()] for j in range(h)]
    dp = [[set() for j in range(w)] for i in range(h)]

    d = abs(a[0][0]-b[0][0])
    dp[0][0].add(d)
    for i in range(1,h):
        d = a[i][0]-b[i][0]
        for j in dp[i-1][0]:
            dp[i][0].add(abs(j-d))
            dp[i][0].add(abs(j+d))
    for i in range(1,w):
        d = a[0][i]-b[0][i]
        for j in dp[0][i-1]:
            dp[0][i].add(abs(j-d))
            dp[0][i].add(abs(j+d))

    for i in range(1,h):
        for j in range(1,w):
            d = a[i][j]-b[i][j]
            for k in dp[i][j-1]:
                dp[i][j].add(abs(k-d))
                dp[i][j].add(abs(k+d))
            for k in dp[i-1][j]:
                dp[i][j].add(abs(k-d))
                dp[i][j].add(abs(k+d))


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


    return None

if __name__ == '__main__':
    main()

Solution (Python):

def resolve():
    
    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)]
    Sub = [[0] * W for i in range(H)]
    for i in range(H):
        for j in range(W):
            Sub[i][j] = abs(A[i][j] - B[i][j])

    
    
    
    

    
    max_k = 80 * (H + W)
    dp = [[0] * W for _ in range(H)]
    dp[0][0] = 1 << (max_k + Sub[0][0]) | 1 << (max_k - Sub[0][0])

    for i in range(H):
        for j in range(W):
            d = Sub[i][j]
            if i:
                dp[i][j] |= dp[i - 1][j] << d | dp[i - 1][j] >> d
            if j:
                dp[i][j] |= dp[i][j - 1] << d | dp[i][j - 1] >> d

    ans = 10 ** 18
    for i in range(max_k * 2 + 1):
        if dp[H - 1][W - 1] & (1 << i):
            ans = min(ans, abs(max_k - i))
    print(ans)


if __name__ == "__main__":
    resolve()