Language: C++


#include <iostream>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#include <map>
#include <unordered_map>
#include <set>
#include <deque>

long long mod = 1e9+7;
class Mint {
public:
  long long x;
  Mint(){}; // 引数なしでも定義できるように引数なしコンストラクタも用意しておく
  Mint(long long a){
    x = a%mod;
    while(x<0){
      x += mod;
    }
  };
  Mint& operator += (const Mint &a){
    x+=a.x;
    x%=mod;
    return *this;
  }
  Mint& operator -= (const Mint &a){
    x += (mod - a.x);
    x%=mod;
    return *this;
  }
  Mint& operator *= (const Mint &a){
    x*=a.x;
    x%=mod;
    return *this;
  }

  // a^n mod を計算する
  // Useaeg: Mint z = y.pow(y, n);
  Mint pow(Mint a, long long n) const {
    Mint y(1);
    Mint b(a);
    while (n > 0) {
      if (n & 1) y *= b;
      b *= b;
      n >>= 1;
    }
    return y;
  }

  // a^{-1} mod を計算する
  Mint modinv(const Mint &a) const {
    return pow(a, mod-2);
  }

  Mint& operator /= (const Mint &a) {
    x *= modinv(a).x;
    x %= mod;
    return *this;
  }

  Mint operator + (Mint &a) const {
    Mint y(*this);
    y += a;
    return y;
  }

  Mint operator - (Mint &a) const {
    Mint y(*this);
    y -= a;
    return y;
  }

  Mint operator * (Mint &a) const {
    Mint y(*this);
    y *= a;
    return y;
  }

  Mint operator / (Mint &a) const {
    Mint y(*this);
    y/=a;
    return y.x;
  }

  // nCk @mod を計算する
  Mint nCk(Mint &n, const long long k) const {
    Mint y = Mint(1);
    for (Mint i(0); (i.x)<k; i.x++){
      y *= (n-i);
      y /= i;
    }
    return y;
  }

  // nPk @mod を計算する
  Mint nPk(Mint &n, long long k) const {
    Mint y(1);
    for (Mint i(0); (i.x)<k; i.x++){
      y *= (n-i);
    }
    return y;
  }
};

struct node {
  int v;
  int supply;
  int amari;
};

class MyComparison {
public:
  bool operator()(node n1, node n2) {
    return (n1.supply!=n2.supply)?(n1.supply>n2.supply):(n1.amari<n2.amari);
  }
};

bool dp[84][84][1100000];
int A[84][84];
int B[84][84];
int main(){
  int H, W;
  std::cin >> H >> W;
  for (int i=0; i<H; i++) {
    for (int j=0; j<W; j++) {
      std::cin >> A[i][j];
    }
  }
  for (int i=0; i<H; i++) {
    for (int j=0; j<W; j++) {
      std::cin >> B[i][j];
    }
  }
  int MAX = 1100000;
  for (int i=0; i<84; i++) {
    for (int j=0; j<W; j++) {
      for (int k=0; k<MAX; k++){
        dp[i][j][k] = false;
      }
    }
  }
  int offset = MAX/2;
  dp[0][0][+A[0][0]-B[0][0]+offset] = true;
  dp[0][0][-A[0][0]+B[0][0]+offset] = true;
  for (int i=0; i<H; i++) {
    for (int j=0; j<W; j++) {
      if (i>0) {
        // (i-1, j) -> (i, j)
        for (int k=0; k<MAX; k++) {
          if (dp[i-1][j][k]) {
            dp[i][j][k+A[i][j]-B[i][j]]=true;
            dp[i][j][k-A[i][j]+B[i][j]]=true;
          }
        }
      }
      if (j>0) {
        // (i, j-1) -> (i, j)
        for (int k=0; k<MAX; k++) {
          if (dp[i][j-1][k]) {
            dp[i][j][k+A[i][j]-B[i][j]]=true;
            dp[i][j][k-A[i][j]+B[i][j]]=true;
          }
        }
      }
    }
  }
  int ans = 1001001001;
  for (int k=0; k<MAX; k++) {
    if (dp[H-1][W-1][k]){
      ans = std::min(ans, abs(k-offset));
    }
  }
  std::cout << ans << std::endl;
  return 0;
}

Compiler output:
/tmp/ccKm5ILf.o: in function `main':
a.cc:(.text+0x71): relocation truncated to fit: R_X86_64_PC32 against symbol `A' defined in .bss section in /tmp/ccKm5ILf.o
a.cc:(.text+0xe3): relocation truncated to fit: R_X86_64_PC32 against symbol `B' defined in .bss section in /tmp/ccKm5ILf.o
a.cc:(.text+0x19b): relocation truncated to fit: R_X86_64_PC32 against symbol `A' defined in .bss section in /tmp/ccKm5ILf.o
a.cc:(.text+0x1a1): relocation truncated to fit: R_X86_64_PC32 against symbol `B' defined in .bss section in /tmp/ccKm5ILf.o
a.cc:(.text+0x1bb): relocation truncated to fit: R_X86_64_PC32 against symbol `B' defined in .bss section in /tmp/ccKm5ILf.o
a.cc:(.text+0x1c1): relocation truncated to fit: R_X86_64_PC32 against symbol `A' defined in .bss section in /tmp/ccKm5ILf.o
a.cc:(.text+0x273): relocation truncated to fit: R_X86_64_PC32 against symbol `A' defined in .bss section in /tmp/ccKm5ILf.o
a.cc:(.text+0x2af): relocation truncated to fit: R_X86_64_PC32 against symbol `B' defined in .bss section in /tmp/ccKm5ILf.o
a.cc:(.text+0x319): relocation truncated to fit: R_X86_64_PC32 against symbol `A' defined in .bss section in /tmp/ccKm5ILf.o
a.cc:(.text+0x356): relocation truncated to fit: R_X86_64_PC32 against symbol `B' defined in .bss section in /tmp/ccKm5ILf.o
a.cc:(.text+0x420): additional relocation overflows omitted from the output
collect2: error: ld returned 1 exit status