Language: C++


#include <iostream>
#include <iomanip> //! for setprecision(10)
#include <math.h>
#include <algorithm>
#include <functional> 
#include <string>
#include <vector>

#include <cstring>
#include <set>
#include <map>
#include <queue>
#include <utility>

using namespace std;

typedef long long LL;

#define dump(c) { for (auto it = c.begin(); it != c.end(); ++it) if (it == c.begin()) cout << *it; else cout << ' ' << *it; cout << endl; } 
#define rep(i,n) for (int i = 0; i < (n); ++i)

const int MOD = 1000000007;

int MAXD = 256000;

int dp[85][85][512001]; //! [h][w][diff] => reachable
int A[85][85];
int B[85][85];
int D[85][85];

int main()
{
	cin.tie(0);
	ios::sync_with_stdio(0);

    int H, W;
    cin >> H >> W;
    for (int y = 1; y <= H; ++y) {
        for (int x = 1; x <= W; ++x) {
            cin >> A[y][x];
        }
    }
    for (int y = 1; y <= H; ++y) {
        for (int x = 1; x <= W; ++x) {
            cin >> B[y][x];
        }
    }
    for (int y = 1; y <= H; ++y) {
        for (int x = 1; x <= W; ++x) {
            D[y][x] = abs(A[y][x]-B[y][x]);
        }
    }

    dp[1][1][D[1][1]] = true;

    for (int y = 1; y <= H; ++y) {
        for (int x = 1; x <= W; ++x) {
            if (y == 1 && x == 1) continue;
            int d = D[y][x];
            // cout << y << ' ' << x << ' ' << d << endl;
            for (int z = 0; z <= MAXD; ++z) {
                if (dp[y-1][x][z]) {
                    // cout << "   down from " << (y-1) << ' ' << x << ' ' << z << endl;
                    if (d<z) {
                        dp[y][x][z-d] = true;
                    } else {
                        dp[y][x][d-z] = true;
                    }
                    dp[y][x][d+z] = true;
                }
            }
            for (int z = 0; z <= MAXD; ++z) {
                if (dp[y][x-1][z]) {
                    // cout << "    right from " << y << ' ' << (x-1) << ' ' << z << endl;
                    if (d<z) {
                        dp[y][x][z-d] = true;
                    } else {
                        dp[y][x][d-z] = true;
                    }
                    dp[y][x][d+z] = true;
                }
            }
        }
    }

    // for (int y = 1; y <= H; ++y) {
    //     for (int x = 1; x <= W; ++x) {
    //         cout << "[";
    //         for (int z = 0; z <= MAXD; ++z) {
    //             if (dp[y][x][z]) {
    //                 cout << z << ",";
    //             }
    //         }
    //         cout << "] ";
    //     }
    //     cout << endl;
    // }

    int ans = -1;
    for (int z = 0; ans < 0 && z <= MAXD; ++z) {
        if (dp[H][W][z]) {
            ans = z;
        }
    }

    cout << ans << endl;

    return 0;
}


Compiler output:
/tmp/cce2nUvw.o: in function `main':
a.cc:(.text+0x8e): relocation truncated to fit: R_X86_64_PC32 against symbol `A' defined in .bss section in /tmp/cce2nUvw.o
a.cc:(.text+0xff): relocation truncated to fit: R_X86_64_PC32 against symbol `B' defined in .bss section in /tmp/cce2nUvw.o
a.cc:(.text+0x176): relocation truncated to fit: R_X86_64_PC32 against symbol `A' defined in .bss section in /tmp/cce2nUvw.o
a.cc:(.text+0x1ab): relocation truncated to fit: R_X86_64_PC32 against symbol `B' defined in .bss section in /tmp/cce2nUvw.o
a.cc:(.text+0x1ed): relocation truncated to fit: R_X86_64_PC32 against symbol `D' defined in .bss section in /tmp/cce2nUvw.o
a.cc:(.text+0x216): relocation truncated to fit: R_X86_64_PC32 against symbol `D' defined in .bss section in /tmp/cce2nUvw.o
a.cc:(.text+0x28e): relocation truncated to fit: R_X86_64_PC32 against symbol `D' defined in .bss section in /tmp/cce2nUvw.o
collect2: error: ld returned 1 exit status