Problem p02839 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

int main() {
  long long h, w;
  cin >> h >> w;
  vector<vector<long long>> a(h + 1, vector<long long>(w + 1, 0));
  vector<vector<long long>> b(h + 1, vector<long long>(w + 1, 0));
  vector<vector<long long>> sa(h + 1, vector<long long>(w + 1, 0));
  for (int i = 1; i < h + 1; i++) {
    for (int j = 1; j < w + 1; j++) {
      cin >> a.at(i).at(j);
    }
  }
  for (int i = 1; i < h + 1; i++) {
    for (int j = 1; j < w + 1; j++) {
      cin >> b.at(i).at(j);
      sa.at(i).at(j) = a.at(i).at(j) - b.at(i).at(j);
      if (sa.at(i).at(j) < 0)
        sa.at(i).at(j) *= -1;
    }
  }
  long long k = 80 * 80 * 5;
  vector<vector<vector<int>>> dp(h + 1,
                                 vector<vector<int>>(w + 1, vector<int>(k, 0)));
  for (int i = 1; i < h + 1; i++) {
    for (int j = 1; j < w + 1; j++) {
      for (long long l = 0; l < k - 80; l++) {
        if (i == 1 & j == 1) {
          dp.at(i).at(j).at(sa.at(i).at(j)) = 1;
          continue;
        }
        long long kpi, kpj, kmi, kmj;
        kpi = l + sa.at(i).at(j);
        kpj = l + sa.at(i).at(j);
        kmi = l - sa.at(i).at(j);
        kmj = l - sa.at(i).at(j);
        if (kmi < 0)
          kmi *= -1;
        if (kmj < 0)
          kmj *= -1;
        if (dp.at(i - 1).at(j).at(kpi) == 1 | dp.at(i - 1).at(j).at(kmi) == 1 |
            dp.at(i).at(j - 1).at(kpi) == 1 | dp.at(i).at(j - 1).at(kmi) == 1) {
          dp.at(i).at(j).at(l) = 1;
        }
      }
    }
  }
  for (int i = 0; i < k; i++) {
    if (dp.at(h).at(w).at(i) == 1) {
      cout << i;
      return 0;
    }
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  long long h, w;
  cin >> h >> w;
  vector<vector<long long>> a(h + 1, vector<long long>(w + 1, 0));
  vector<vector<long long>> b(h + 1, vector<long long>(w + 1, 0));
  vector<vector<long long>> sa(h + 1, vector<long long>(w + 1, 0));
  for (int i = 1; i < h + 1; i++) {
    for (int j = 1; j < w + 1; j++) {
      cin >> a.at(i).at(j);
    }
  }
  for (int i = 1; i < h + 1; i++) {
    for (int j = 1; j < w + 1; j++) {
      cin >> b.at(i).at(j);
      sa.at(i).at(j) = a.at(i).at(j) - b.at(i).at(j);
      if (sa.at(i).at(j) < 0)
        sa.at(i).at(j) *= -1;
    }
  }
  long long k = 80 * 80 * 3;
  vector<vector<vector<int>>> dp(h + 1,
                                 vector<vector<int>>(w + 1, vector<int>(k, 0)));
  for (int i = 1; i < h + 1; i++) {
    for (int j = 1; j < w + 1; j++) {
      for (long long l = 0; l < k - 80; l++) {
        if (i == 1 & j == 1) {
          dp.at(i).at(j).at(sa.at(i).at(j)) = 1;
          continue;
        }
        long long kpi, kpj, kmi, kmj;
        kpi = l + sa.at(i).at(j);
        kpj = l + sa.at(i).at(j);
        kmi = l - sa.at(i).at(j);
        kmj = l - sa.at(i).at(j);
        if (kmi < 0)
          kmi *= -1;
        if (kmj < 0)
          kmj *= -1;
        if (dp.at(i - 1).at(j).at(kpi) == 1 | dp.at(i - 1).at(j).at(kmi) == 1 |
            dp.at(i).at(j - 1).at(kpi) == 1 | dp.at(i).at(j - 1).at(kmi) == 1) {
          dp.at(i).at(j).at(l) = 1;
        }
      }
    }
  }
  for (int i = 0; i < k; i++) {
    if (dp.at(h).at(w).at(i) == 1) {
      cout << i;
      return 0;
    }
  }
}