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:
def main():
    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)]
    n = 1300
    m = n//2
    dp = [[[False]*n for j in range(w)] for i in range(h)]
    dp[0][0][m+abs(a[0][0] - b[0][0])] = True
    dp[0][0][m-abs(a[0][0] - b[0][0])] = True
    for i in range(h):
        for j in range(w):
            x = abs(a[i][j] - b[i][j])
            q = min(min((i+j+2), (h-1-i)+(w-1-j)+2)*80, m)
            if 0 < i and 0 < h:
                for k in range(m-q, m+q):
                    if dp[i-1][j][k] or dp[i][j-1][k]:
                        if x <= k:
                            dp[i][j][k-x] = True
                        if k < n-x:
                            dp[i][j][k+x] = True
            elif 0 < i:
                for k in range(m-q, m+q):
                    if dp[i-1][j][k]:
                        if x <= k:
                            dp[i][j][k-x] = True
                        if k < n-x:
                            dp[i][j][k+x] = True
            elif 0 < j:
                for k in range(m-q, m+q):
                    if dp[i][j-1][k]:
                        if x <= k:
                            dp[i][j][k-x] = True
                        if k < n-x:
                            dp[i][j][k+x] = True
    ans = float("inf")
    for i in range(m-80-1, m+80+2):
        if dp[h-1][w-1][i]:
            ans = min(ans, abs(m-i))
    print(ans)

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 _ in range(h)]
    b = [list(map(int, input().split())) for _ in range(h)]
    n = 2000
    m = n//2
    dp = [[[False]*n for j in range(w)] for i in range(h)]
    dp[0][0][m+abs(a[0][0] - b[0][0])] = True
    dp[0][0][m-abs(a[0][0] - b[0][0])] = True
    for i in range(h):
        for j in range(w):
            x = abs(a[i][j] - b[i][j])
            q = min(min((i+j+2), (h-1-i)+(w-1-j)+2)*80, m)
            if 0 < i and 0 < h:
                for k in range(m-q, m+q):
                    if dp[i-1][j][k] or dp[i][j-1][k]:
                        if x <= k:
                            dp[i][j][k-x] = True
                        if k < n-x:
                            dp[i][j][k+x] = True
            elif 0 < i:
                for k in range(m-q, m+q):
                    if dp[i-1][j][k]:
                        if x <= k:
                            dp[i][j][k-x] = True
                        if k < n-x:
                            dp[i][j][k+x] = True
            elif 0 < j:
                for k in range(m-q, m+q):
                    if dp[i][j-1][k]:
                        if x <= k:
                            dp[i][j][k-x] = True
                        if k < n-x:
                            dp[i][j][k+x] = True
    ans = float("inf")
    for i in range(m-80-1, m+80+2):
        if dp[h-1][w-1][i]:
            ans = min(ans, abs(m-i))
    print(ans)

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 _ in range(h)]
    b = [list(map(int, input().split())) for _ in range(h)]
    n = 2000
    m = n//2
    dp = [[[False]*n for j in range(w)] for i in range(h)]
    dp[0][0][m+abs(a[0][0] - b[0][0])] = True
    dp[0][0][m-abs(a[0][0] - b[0][0])] = True
    for i in range(h):
        for j in range(w):
            x = abs(a[i][j] - b[i][j])
            if 0 < i and 0 < h:
                for k in range(x, n-x):
                    if dp[i-1][j][k] or dp[i][j-1][k]:
                        dp[i][j][k-x] = True
                        dp[i][j][k+x] = True
            elif 0 < i:
                for k in range(x, n-x):
                    if dp[i-1][j][k]:
                        dp[i][j][k-x] = True
                        dp[i][j][k+x] = True
            elif 0 < j:
                for k in range(x, n-x):
                    if dp[i][j-1][k]:
                        dp[i][j][k-x] = True
                        dp[i][j][k+x] = True
    ans = float("inf")
    for i in range(n):
        if dp[h-1][w-1][i]:
            ans = min(ans, abs(m-i))
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import copy
def main():
    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)]
    n = 2*80*160 + 10
    m = n//2
    dp = [[[False]*n for j in range(w)] for i in range(h)]
    dp[0][0][m+abs(a[0][0] - b[0][0])] = True
    dp[0][0][m-abs(a[0][0] - b[0][0])] = True
    for i in range(h):
        for j in range(w):
            x = abs(a[i][j] - b[i][j])
            if 0 < i and 0 < h:
                for k in range(x, n-x):
                    if dp[i-1][j][k] or dp[i][j-1][k]:
                        dp[i][j][k-x] = True
                        dp[i][j][k+x] = True
            elif 0 < i:
                for k in range(x, n-x):
                    if dp[i-1][j][k]:
                        dp[i][j][k-x] = True
                        dp[i][j][k+x] = True
            elif 0 < j:
                for k in range(x, n-x):
                    if dp[i][j-1][k]:
                        dp[i][j][k-x] = True
                        dp[i][j][k+x] = True
    ans = float("inf")
    for i in range(n):
        if dp[h-1][w-1][i]:
            ans = min(ans, abs(m-i))
    print(ans)

if __name__ == "__main__":
    main()

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 i in range(h)]
b=[list(map(int,input().split())) for i in range(h)]
d=[]
for i in range(h):
    lis=[]
    for j in range(w):
        lis.append(abs(a[i][j]-b[i][j]))
    d.append(lis)

NUM=80*160+1
dp = [[[0 for _ in range(80*160+1)] for _ in range(w)] for _ in range(h)]
dp[0][0][d[0][0]]=1

for i in range(h):
    for j in range(w):
        for k in range(80*160+1):
            if dp[i][j][k]==1:
                if j+1<w:
                    nxt=d[i][j+1]
                    dp[i][j+1][k+nxt] = 1
                    dp[i][j+1][abs(k-nxt)] = 1
                if i+1<h:
                    nxt=d[i+1][j]
                    dp[i+1][j][k+nxt] = 1
                    dp[i+1][j][abs(k-nxt)] = 1


for ind in range(len(dp[h-1][w-1])):
    if dp[h-1][w-1][ind]==1:
        print(ind)
        exit()

Submission status: Time Limit Exceeded

Solution code:
import copy
def main():
    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 = 400
    dp = [[False]*(m+1) for _ in range(w)]
    dp[0][abs(a[0][0] - b[0][0])] = True
    for i in range(1, w):
        x = abs(a[0][i] - b[0][i])
        for j in range(m+1):
            if dp[i-1][j]:
                if j+x <= m:
                    dp[i][j+x] = True
                dp[i][abs(j-x)] = True
    for i in range(1, h):
        for j in range(w):
            buf = [False]*(m+1)
            x = abs(a[i][j] - b[i][j])
            for k in range(m+1):
                f = False
                if j == 0:
                    if dp[0][k]:
                        f = True
                else:
                    if dp[j-1][k] or dp[j][k]:
                        f = True
                if f:
                    if k+x <= m:
                        buf[k+x] = True
                    buf[abs(k-x)] = True
            dp[j] = copy.deepcopy(buf)
    for i in range(m+1):
        if dp[w-1][i]:
            print(i)
            break

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import copy
def main():
    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 = 1000
    dp = [[False]*(m+1) for _ in range(w)]
    dp[0][abs(a[0][0] - b[0][0])] = True
    for i in range(1, w):
        x = abs(a[0][i] - b[0][i])
        for j in range(m+1):
            if dp[i-1][j]:
                if j+x <= m:
                    dp[i][j+x] = True
                dp[i][abs(j-x)] = True
    for i in range(1, h):
        for j in range(w):
            buf = [False]*(m+1)
            x = abs(a[i][j] - b[i][j])
            for k in range(m+1):
                f = False
                if j == 0:
                    if dp[0][k]:
                        f = True
                else:
                    if dp[j-1][k] or dp[j][k]:
                        f = True
                if f:
                    if k+x <= m:
                        buf[k+x] = True
                    buf[abs(k-x)] = True
            dp[j] = copy.deepcopy(buf)
    for i in range(m+1):
        if dp[w-1][i]:
            print(i)
            break

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import copy
def main():
    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 = 3000
    dp = [[False]*(m+1) for _ in range(w)]
    c = [0]*w
    dp[0][abs(a[0][0] - b[0][0])] = True
    c[0] = abs(a[0][0] - b[0][0])
    for i in range(1, w):
        x = abs(a[0][i] - b[0][i])
        for j in range(min(c[i-1], m)+1):
            if dp[i-1][j]:
                if j+x <= m:
                    dp[i][j+x] = True
                    c[i] = max(c[i], j+x)
                dp[i][abs(j-x)] = True
    for i in range(1, h):
        for j in range(w):
            buf = [False]*(m+1)
            x = abs(a[i][j] - b[i][j])
            z = 0
            if 0 < j:
                z = min(max(c[j], c[j-1]), m)
            else:
                z = min(c[j], m)
            for k in range(z+1):
                f = False
                if j == 0:
                    if dp[0][k]:
                        f = True
                else:
                    if dp[j-1][k] or dp[j][k]:
                        f = True
                if f:
                    if k+x <= m:
                        buf[k+x] = True
                        c[j] = max(c[j], k+x)
                    buf[abs(k-x)] = True
            dp[j] = copy.deepcopy(buf)
    for i in range(m+1):
        if dp[w-1][i]:
            print(i)
            break

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import copy
def main():
    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 = 3500
    dp = [[False]*(m+1) for _ in range(w)]
    dp[0][abs(a[0][0] - b[0][0])] = True
    for i in range(1, w):
        x = abs(a[0][i] - b[0][i])
        for j in range(m+1):
            if dp[i-1][j]:
                if j+x <= m:
                    dp[i][j+x] = True
                dp[i][abs(j-x)] = True
    for i in range(1, h):
        for j in range(w):
            buf = [False]*(m+1)
            x = abs(a[i][j] - b[i][j])
            for k in range(m+1):
                f = False
                if j == 0:
                    if dp[0][k]:
                        f = True
                else:
                    if dp[j-1][k] or dp[j][k]:
                        f = True
                if f:
                    if k+x <= m:
                        buf[k+x] = True
                    buf[abs(k-x)] = True
            dp[j] = copy.deepcopy(buf)
    for i in range(m+1):
        if dp[w-1][i]:
            print(i)
            break

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import copy
def main():
    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 = 80**2 + 10
    dp = [[False]*(m+1) for _ in range(w)]
    dp[0][abs(a[0][0] - b[0][0])] = True
    for i in range(1, w):
        x = abs(a[0][i] - b[0][i])
        for j in range(m+1):
            if dp[i-1][j]:
                if j+x <= m:
                    dp[i][j+x] = True
                dp[i][abs(j-x)] = True
    for i in range(1, h):
        for j in range(w):
            buf = [False]*(m+1)
            x = abs(a[i][j] - b[i][j])
            for k in range(m+1):
                f = False
                if j == 0:
                    if dp[0][k]:
                        f = True
                else:
                    if dp[j-1][k] or dp[j][k]:
                        f = True
                if f:
                    if k+x <= m:
                        buf[k+x] = True
                    buf[abs(k-x)] = True
            dp[j] = copy.deepcopy(buf)
    for i in range(m+1):
        if dp[w-1][i]:
            print(i)
            break

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import copy
def main():
    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 = (h+w+5)*80
    dp = [[False]*(m+1) for _ in range(w)]
    dp[0][abs(a[0][0] - b[0][0])] = True
    for i in range(1, w):
        x = abs(a[0][i] - b[0][i])
        for j in range(m+1):
            if dp[i-1][j]:
                if j+x <= m:
                    dp[i][j+x] = True
                dp[i][abs(j-x)] = True
    for i in range(1, h):
        for j in range(w):
            buf = [False]*(m+1)
            x = abs(a[i][j] - b[i][j])
            for k in range(m+1):
                f = False
                if j == 0:
                    if dp[0][k]:
                        f = True
                else:
                    if dp[j-1][k] or dp[j][k]:
                        f = True
                if f:
                    if k+x <= m:
                        buf[k+x] = True
                    buf[abs(k-x)] = True
            dp[j] = copy.deepcopy(buf)
    for i in range(m+1):
        if dp[w-1][i]:
            print(i)
            break

if __name__ == "__main__":
    main()

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 i in range(h)]
b=[list(map(int,input().split())) for i in range(h)]
d=[]
for i in range(h):
    lis=[]
    for j in range(w):
        lis.append(abs(a[i][j]-b[i][j]))
    d.append(lis)

dp = [[[0 for _ in range(80*(h+w))] for _ in range(w)] for _ in range(h)]
dp[0][0][d[0][0]]=1

for i in range(h):
    for j in range(w):
        for k in range((i+j+1)*80+1):
            if dp[i][j][k]==1:
                if j+1<w:
                    nxt=d[i][j+1]
                    dp[i][j+1][k+nxt] = 1
                    dp[i][j+1][abs(k-nxt)] = 1
                if i+1<h:
                    nxt=d[i+1][j]
                    dp[i+1][j][k+nxt] = 1
                    dp[i+1][j][abs(k-nxt)] = 1


for ind in range(len(dp[h-1][w-1])):
    if dp[h-1][w-1][ind]==1:
        print(ind)
        exit()

Submission status: Time Limit Exceeded

Solution code:
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)]
d=[]
for i in range(h):
    lis=[]
    for j in range(w):
        lis.append(abs(a[i][j]-b[i][j]))
    d.append(lis)

dp = [[[0 for _ in range(80*(h+w))] for _ in range(w)] for _ in range(h)]
dp[0][0][d[0][0]]=1

for i in range(h):
    for j in range(w):
        for k in range((i+j+1)*80+1):
            if dp[i][j][k]==1:
                if j+1<w:
                    nxt=d[i][j+1]
                    dp[i][j+1][k+nxt] = 1
                    dp[i][j+1][abs(k-nxt)] = 1
                if i+1<h:
                    nxt=d[i+1][j]
                    dp[i+1][j][k+nxt] = 1
                    dp[i+1][j][abs(k-nxt)] = 1


for ind in range(len(dp[h-1][w-1])):
    if dp[h-1][w-1][ind]==1:
        print(ind)
        exit()

Submission status: Time Limit Exceeded

Solution code:
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)]
d=[]
for i in range(h):
    lis=[]
    for j in range(w):
        lis.append(abs(a[i][j]-b[i][j]))
    d.append(lis)

dp = [[[0 for _ in range(80*(h+w))] for _ in range(w)] for _ in range(h)]
dp[0][0][d[0][0]]=1

for i in range(h):
    for j in range(w):
        for k in range(80*(h+w)):
            if dp[i][j][k]==1:
                if j+1<w:
                    nxt=d[i][j+1]
                    dp[i][j+1][k+nxt] = 1
                    dp[i][j+1][abs(k-nxt)] = 1
                if i+1<h:
                    nxt=d[i+1][j]
                    dp[i+1][j][k+nxt] = 1
                    dp[i+1][j][abs(k-nxt)] = 1


for ind in range(len(dp[h-1][w-1])):
    if dp[h-1][w-1][ind]==1:
        print(ind)
        exit()

Submission status: Time Limit Exceeded

Solution code:
import copy
def main():
    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 = [[None]*w for _ in range(h)]
    for i in reversed(range(h)):
        for j in reversed(range(w)):
            c[i][j] = abs(a[i][j] - b[i][j])
            if i < h-1:
                c[i][j] += c[i+1][j]
            if j < w-1:
                c[i][j] += c[i][j+1]
    dp = [set() for i in range(w)]
    dp[0].add(abs(a[0][0] - b[0][0]))
    for j in range(1, w):
        x = abs(a[0][j] - b[0][j])
        for v in dp[j-1]:
            dp[j].add(v + x)
            dp[j].add(abs(v - x))
    for i in range(1, h):
        for j in range(w):
            m = (h-1-i) + (w-1-j) + 1
            x = abs(a[i][j] - b[i][j])
            if j == 0:
                buf = copy.deepcopy(dp[j])
                dp[j].clear()
                for v in buf:
                    if 80*m < v:
                        continue
                    dp[j].add(v + x)
                    dp[j].add(abs(v - x))
            else:
                buf = dp[j-1] | dp[j]
                dp[j].clear()
                for v in buf:
                    if 80*m < v:
                        continue
                    dp[j].add(v + x)
                    dp[j].add(abs(v - x))
    ans = float("inf")
    for v in dp[w-1]:
        ans = min(ans, abs(v))
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import copy
def main():
    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 = [[None]*w for _ in range(h)]
    for i in reversed(range(h)):
        for j in reversed(range(w)):
            c[i][j] = abs(a[i][j] - b[i][j])
            if i < h-1:
                c[i][j] += c[i+1][j]
            if j < w-1:
                c[i][j] += c[i][j+1]
    dp = [set() for i in range(w)]
    dp[0].add(abs(a[0][0] - b[0][0]))
    for j in range(1, w):
        x = abs(a[0][j] - b[0][j])
        for v in dp[j-1]:
            dp[j].add(v + x)
            dp[j].add(abs(v - x))
    for i in range(1, h):
        for j in range(w):
            x = abs(a[i][j] - b[i][j])
            if j == 0:
                buf = copy.deepcopy(dp[j])
                dp[j].clear()
                for v in buf:
                    dp[j].add(v + x)
                    dp[j].add(abs(v - x))
            else:
                buf = dp[j-1] | dp[j]
                dp[j].clear()
                for v in buf:
                    dp[j].add(v + x)
                    dp[j].add(abs(v - x))
    ans = float("inf")
    for v in dp[w-1]:
        ans = min(ans, abs(v))
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import copy
def main():
    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 = [[None]*w for _ in range(h)]
    for i in reversed(range(h)):
        for j in reversed(range(w)):
            c[i][j] = abs(a[i][j] - b[i][j])
            if i < h-1:
                c[i][j] += c[i+1][j]
            if j < w-1:
                c[i][j] += c[i][j+1]
    dp = [set() for i in range(w)]
    dp[0].add(a[0][0] - b[0][0])
    for j in range(1, w):
        x = a[0][j] - b[0][j]
        for v in dp[j-1]:
            dp[j].add(v + x)
            dp[j].add(v - x)
    for i in range(1, h):
#        print(dp)
        for j in range(w):
            x = a[i][j] - b[i][j]
            if j == 0:
                buf = copy.deepcopy(dp[j])
                dp[j].clear()
                for v in buf:
                    dp[j].add(v + x)
                    dp[j].add(v - x)
            else:
                buf = dp[j-1] | dp[j]
                dp[j].clear()
                for v in buf:
                    dp[j].add(v + x)
                    dp[j].add(v - x)
    ans = float("inf")
#    print(dp)
    for v in dp[w-1]:
        ans = min(ans, abs(v))
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
h,w = map(int,input().split())
grid = []
for i in range(h):
    grid.append(list(map(int,input().split())))
for i in range(h):
    lst = list(map(int,input().split()))
    for j in range(w):
        grid[i][j] = abs(grid[i][j]-lst[j])
lst = [[set() for _ in range(w)] for _ in range(h)]
lst[0][0].add(grid[0][0])
for j in range(1,w):
    for x in lst[0][j-1]:
        lst[0][j].add(abs(grid[0][j]-x))
        if grid[0][j] + x <= 6400:
            lst[0][j].add(grid[0][j]+x)
for i in range(1,h):
    for x in lst[i-1][0]:
        lst[i][0].add(abs(grid[i][0]-x))
        if grid[i][0] + x <= 6400:
            lst[i][0].add(grid[i][0]+x)
for i in range(1,h):
    for j in range(1,w):
        for x in lst[i][j-1]:
            lst[i][j].add(abs(grid[i][j]-x))
            if grid[i][j] + x <= 6400:
                lst[i][j].add(grid[i][j]+x)
        for x in lst[i-1][j]:
            lst[i][j].add(abs(grid[i][j]-x))
            if grid[i][j] + x <= 6400:
                lst[i][j].add(grid[i][j]+x)
print(min(lst[h-1][w-1]))

Submission status: Time Limit Exceeded

Solution code:
def main():
    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 = [[None]*w for _ in range(h)]
    for i in reversed(range(h)):
        for j in reversed(range(w)):
            c[i][j] = abs(a[i][j] - b[i][j])
            if i < h-1:
                c[i][j] += c[i+1][j]
            if j < w-1:
                c[i][j] += c[i][j+1]
    t = [[set() for j in range(w)] for i in range(h)]
    t[0][0].add(abs(a[0][0] - b[0][0]))
    for i in range(h):
        for j in range(w):
            if 0 < i and 0 < j:
                buf = t[i-1][j] | t[i][j-1]
                for v in buf:
                    t[i][j].add(abs(v + (a[i][j] - b[i][j])))
                    t[i][j].add(abs(v - (a[i][j] - b[i][j])))
            elif 0 < i:
                buf = t[i-1][j]
                for v in buf:
                    t[i][j].add(abs(v + (a[i][j] - b[i][j])))
                    t[i][j].add(abs(v - (a[i][j] - b[i][j])))
            elif 0 < j:
                buf = t[i][j-1]
                for v in buf:
                    t[i][j].add(abs(v + (a[i][j] - b[i][j])))
                    t[i][j].add(abs(v - (a[i][j] - b[i][j])))
    ans = float("inf")
    for v in t[i][j]:
        ans = min(ans, abs(v))
    print(ans)

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 _ in range(h)]
    b = [list(map(int, input().split())) for _ in range(h)]
    t = [[set() for j in range(w)] for i in range(h)]
    t[0][0].add(a[0][0] - b[0][0])
    for i in range(h):
        for j in range(w):
            if 0 < i and 0 < j:
                buf = t[i-1][j] | t[i][j-1]
                for v in buf:
                    t[i][j].add(v + (a[i][j] - b[i][j]))
                    t[i][j].add(v - (a[i][j] - b[i][j]))
            elif 0 < i:
                buf = t[i-1][j]
                for v in buf:
                    t[i][j].add(v + (a[i][j] - b[i][j]))
                    t[i][j].add(v - (a[i][j] - b[i][j]))
            elif 0 < j:
                buf = t[i][j-1]
                for v in buf:
                    t[i][j].add(v + (a[i][j] - b[i][j]))
                    t[i][j].add(v - (a[i][j] - b[i][j]))
    ans = float("inf")
    for v in t[i][j]:
        ans = min(ans, abs(v))
    print(ans)

if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

input=sys.stdin.readline

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

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

dp=[[[False for k in range(0,min(80*(i+j+1)+1,80*(H-1-i+W-1-j+1)+1))] for i in range(0,W)] for j in range(0,H)]
dp[0][0][C[0][0]]=True
for i in range(0,H):
    for j in range(0,W):
        for k in range(0,min(80*(i+j+1)+1,80*(H-1-i+W-1-j+1)+1)):
            ans=False
            if i>=1:
                if k+C[i][j]<len(dp[i-1][j]):
                    ans= ans or dp[i-1][j][k+C[i][j]]
                if abs(k-C[i][j])<len(dp[i-1][j]):
                    ans= ans or dp[i-1][j][abs(k-C[i][j])]
            if j>=1:
                if k+C[i][j]<len(dp[i][j-1]):
                    ans= ans or dp[i][j-1][k+C[i][j]]
                if abs(k-C[i][j])<len(dp[i][j-1]):
                    ans= ans or dp[i][j-1][abs(k-C[i][j])]
            if (i,j)!=(0,0):
                dp[i][j][k]=ans

ans=100
for i in range(0,80+1):
    test=100
    if dp[H-1][W-1][i]:
        test=i
    if ans>test:
        ans=test

print(ans)

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

dp = [[[False for i in range(80 * h + 80 * w + 1)] for _ in range(1 + w)] for j in range(1 + h)]
# dp[i][j][k]=マスの(i,j)において絶対値をkにすることができるかどうか
dp[0][0][0] = True
dp[1][0][0] = True
dp[0][1][0] = True

# 初期値の設定
for i in range(1, h + 1):
    for j in range(1, w + 1):
      
        a_num = a[i - 1][j - 1]
        b_num = b[i - 1][j - 1]
        dif = a_num - b_num
        if i == 1:
            for k in range(80 * i + 80 * j - 79):
                if dp[1][j - 1][abs(k - dif)] or dp[1][j - 1][abs(k + dif)]:
                    dp[1][j][abs(k)] = True

        elif j == 1:
            for k in range(80 * i + 80 * j - 79):
                if dp[i - 1][1][abs(k - dif)] or dp[i - 1][1][abs(k + dif)]:
                    dp[i][1][k] = True

        else:
            for k in range(80 * i + 80 * j - 79):
                if dp[i - 1][j][abs(k - dif)] or dp[i - 1][j][abs(k + dif)] or dp[i][j - 1][abs(k - dif)] or dp[i][j - 1][abs(k + dif)]:
                    dp[i][j][k] = True

ans = 0
for i in range(80 * h + 80 * w + 1):
    if dp[h][w][i]:
        ans = i
        break

print(ans)

Submission status: Time Limit Exceeded

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

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

def build_grid(H, W, intv, _type, space=True, padding=False):
    # 入力がスペース区切りかどうか
    if space:
        _input = lambda: input().split()
    else:
        _input = lambda: input()
    _list = lambda: list(map(_type, _input()))
    # 余白の有無
    if padding:
        offset = 1
    else:
        offset = 0
    grid = list2d(H+offset*2, W+offset*2, intv)
    for i in range(offset, H+offset):
        row = _list()
        for j in range(offset, W+offset):
            grid[i][j] = row[j-offset]
    return grid

H, W = MAP()
MAXV = 160 * 80

grid1 =build_grid(H, W, 0, int)
grid2 =build_grid(H, W, 0, int)
grid = list2d(H+1, W+1, 0)
for i in range(H):
    for j in range(W):
        grid[i][j] = abs(grid1[i][j]-grid2[i][j])

# dp[i][j][k] := グリッド(i, j)に数値kで到達可能かどうか
dp = list3d(H+1, W+1, MAXV+1, 0)
dp[0][0][0] = 1
for i in range(H):
    for j in range(W):
        v = grid[i][j]
        for k in range(MAXV+1):
            # 枝刈り
            if (i+j+1) * 80 < k:
                break
            # 上からの遷移
            dp[i+1][j][abs(k-v)] |= dp[i][j][k]
            if k+v <= MAXV:
                dp[i+1][j][k+v] |= dp[i][j][k]
            # 左からの遷移
            dp[i][j+1][abs(k-v)] |= dp[i][j][k]
            if k+v <= MAXV:
                dp[i][j+1][k+v] |= dp[i][j][k]
ans = INF
for k in range(MAXV+1):
    if dp[H][W-1][k]:
        ans = min(ans, k)
    if dp[H-1][W][k]:
        ans = min(ans, k)
print(ans)