Language: C++

// https://atcoder.jp/contests/abc147/tasks/abc147_e

#include "algorithm"
#include "cmath"
#include "functional"
#include "iomanip"
#include "iostream"
#include "map"
#include "numeric"
#include "queue"
#include "set"
#include "string"
#include "vector"
#define rep(i, to) for (ll i = 0; i < (to); ++i)
#define rep1(i, to) for (ll i = 1; i <= (to); ++i)
#define repf(i, from, to) for (ll i = from; i < (to); ++i)
#define repr(i, from) for (ll i = from - 1; i >= 0; --i)
#define all(vec) vec.begin(), vec.end()
#define fi first
#define se second
using namespace std;
typedef long long ll;
template <typename T>
using V = vector<T>;
using VL = V<ll>;
using VVL = V<VL>;
template <typename T, typename U>
using P = pair<T, U>;
using PL = P<ll, ll>;
using VPL = V<PL>;
template <typename T>
inline bool chmax(T& a, T b);
template <typename T>
inline bool chmin(T& a, T b);
void print_ints(vector<ll> v);
template <typename T>
void drop(T a);

const ll INF = 1e18;

void solve() {
  ll H, W;
  cin >> H >> W;

  VVL as(H, VL(W));
  rep(h, H) rep(w, W) cin >> as[h][w];

  VVL bs(H, VL(W));
  rep(h, H) rep(w, W) cin >> bs[h][w];

  ll abs_max = 0;
  rep(h, H) rep(w, W) { chmax(abs_max, abs(as[h][w] - bs[h][w])); }

  ll abs_sum_max = (W + H - 1) * abs_max;

  ll res = INF;

  // dp[h][w][k]: h, w までの経路で偏りが k となるか
  // k はマイナスも扱うために abs_sum_max を足した値としておく
  V<V<V<bool>>> dp(H, V<V<bool>>(W, V<bool>(2 * abs_sum_max + 1)));
  dp[0][0][abs_sum_max + as[0][0] - bs[0][0]] = true;
  dp[0][0][abs_sum_max + bs[0][0] - as[0][0]] = true;

  // 配る DP
  rep(h, H) rep(w, W) rep(k, 2 * abs_sum_max + 1) {
    if (!dp[h][w][k]) continue;

    rep(h_diff, 2) rep(w_diff, 2) {
      if (h_diff == w_diff) continue;
      if (h + h_diff >= H || w + w_diff >= W) continue;

      ll a = as[h + h_diff][w + w_diff];
      ll b = bs[h + h_diff][w + w_diff];

      dp[h + h_diff][w + w_diff][k + a - b] = true;
      dp[h + h_diff][w + w_diff][k + b - a] = true;
    }
  }

  rep(k, abs_sum_max + 1) if (dp[H - 1][W - 1][abs_sum_max + k]) drop(k);
}

// bitset
void solve2() {
  ll H, W;
  cin >> H >> W;

  VVL as(H, VL(W));
  rep(h, H) rep(w, W) cin >> as[h][w];

  VVL bs(H, VL(W));
  rep(h, H) rep(w, W) cin >> bs[h][w];

  ll abs_max = 0;
  rep(h, H) rep(w, W) { chmax(abs_max, abs(as[h][w] - bs[h][w])); }

  ll abs_sum_max = (W + H - 1) * abs_max;

  ll res = INF;

  // dp[h][w][k]: h, w までの経路で偏りが k となるか
  // k はマイナスも扱うために abs_sum_max を足した値としておく
  // (80 + 80 - 1) * 80 = 12720
  using bits = bitset<12720>;
  V<V<bits>> dp(H, V<bits>(W, bits()));
  dp[0][0][abs_sum_max + as[0][0] - bs[0][0]] = true;
  dp[0][0][abs_sum_max + bs[0][0] - as[0][0]] = true;

  // 配る DP
  rep(h, H) rep(w, W) rep(k, 2 * abs_sum_max + 1) {
    if (!dp[h][w][k]) continue;

    rep(h_diff, 2) rep(w_diff, 2) {
      if (h_diff == w_diff) continue;
      if (h + h_diff >= H || w + w_diff >= W) continue;

      ll a = as[h + h_diff][w + w_diff];
      ll b = bs[h + h_diff][w + w_diff];

      dp[h + h_diff][w + w_diff][k + a - b] = true;
      dp[h + h_diff][w + w_diff][k + b - a] = true;
    }
  }

  rep(k, abs_sum_max + 1) if (dp[H - 1][W - 1][abs_sum_max + k]) drop(k);
}

struct exit_exception : public std::exception {
  const char* what() const throw() { return "Exited"; }
};

#ifndef TEST
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  try {
    solve2();
  } catch (exit_exception& e) {
  }

  return 0;
}
#endif

template <typename T>
inline bool chmax(T& a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}

template <typename T>
inline bool chmin(T& a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

void print_ints(vector<ll> v) {
  rep(i, v.size()) {
    if (i > 0) {
      cout << " ";
    }

    cout << v[i];
  }

  cout << endl;
}

template <typename T>
void drop(T res) {
  cout << res << endl;
  throw exit_exception();
}


Compiler output:
a.cc: In function 'void solve2()':
a.cc:104:16: error: 'bitset' does not name a type
  104 |   using bits = bitset<12720>;
      |                ^~~~~~
a.cc:105:7: error: 'bits' was not declared in this scope; did you mean 'bs'?
  105 |   V<V<bits>> dp(H, V<bits>(W, bits()));
      |       ^~~~
      |       bs
a.cc:105:7: error: template argument 1 is invalid
a.cc:105:11: error: template argument 1 is invalid
  105 |   V<V<bits>> dp(H, V<bits>(W, bits()));
      |           ^~
a.cc:105:38: error: expression list treated as compound expression in initializer [-fpermissive]
  105 |   V<V<bits>> dp(H, V<bits>(W, bits()));
      |                                      ^
a.cc:106:5: error: invalid types 'int[int]' for array subscript
  106 |   dp[0][0][abs_sum_max + as[0][0] - bs[0][0]] = true;
      |     ^
a.cc:107:5: error: invalid types 'int[int]' for array subscript
  107 |   dp[0][0][abs_sum_max + bs[0][0] - as[0][0]] = true;
      |     ^
a.cc:111:12: error: invalid types 'int[ll {aka long long int}]' for array subscript
  111 |     if (!dp[h][w][k]) continue;
      |            ^
a.cc:120:9: error: invalid types 'int[ll {aka long long int}]' for array subscript
  120 |       dp[h + h_diff][w + w_diff][k + a - b] = true;
      |         ^
a.cc:121:9: error: invalid types 'int[ll {aka long long int}]' for array subscript
  121 |       dp[h + h_diff][w + w_diff][k + b - a] = true;
      |         ^
a.cc:125:33: error: invalid types 'int[ll {aka long long int}]' for array subscript
  125 |   rep(k, abs_sum_max + 1) if (dp[H - 1][W - 1][abs_sum_max + k]) drop(k);
      |                                 ^