Problem p02839 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
// #include<cmath>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)

int abs(int x) {
  if (x > 0)
    return x;
  else
    return -x;
}

int dp[80][80][12801];
int main() {
  int h, w;
  cin >> h >> w;
  // h = 80;
  // w = 80;
  int a[h][w], b[h][w];
  rep(i, h) rep(j, w) cin >> a[i][j];
  rep(i, h) rep(j, w) cin >> b[i][j];
  // rep(i, h)rep(j,w) a[i][j] = 80;
  // rep(i, h)rep(j,w) b[i][j] = 80;
  int mx = 1280001;

  rep(i, mx + 1) {
    if (abs(a[0][0] - b[0][0]) == i) {
      dp[0][0][i] = 1;
    } else {
      dp[0][0][i] = 0;
    }
  }

  rep(i, h) rep(j, w) rep(k, mx + 1) {
    int delta1 = abs(k - (a[i][j] - b[i][j]));
    int delta2 = abs(k + (a[i][j] - b[i][j]));
    if (delta1 > mx) {
      delta1 = delta2;
    }
    if (delta2 > mx) {
      delta2 = delta1;
    }

    if (i > 0 && j > 0) {
      if (dp[i - 1][j][delta1] == 1 || dp[i][j - 1][delta1] == 1 ||
          dp[i - 1][j][delta2] == 1 || dp[i][j - 1][delta2] == 1) {
        dp[i][j][k] = 1;
      } else {
        dp[i][j][k] = 0;
      }
    } else if (i > 0 && j == 0) {
      if (dp[i - 1][j][delta1] == 1 || dp[i - 1][j][delta2] == 1) {
        dp[i][j][k] = 1;
      } else {
        dp[i][j][k] = 0;
      }
    } else if (i == 0 && j > 0) {
      if (dp[i][j - 1][delta1] == 1 || dp[i][j - 1][delta2] == 1) {
        dp[i][j][k] = 1;
      } else {
        dp[i][j][k] = 0;
      }
    }
  }
  rep(i, mx + 1) {
    if (dp[h - 1][w - 1][i] == 1) {
      cout << i << endl;
      return 0;
    }
  }
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
// #include<cmath>

using namespace std;

#define rep(i, n) for (int i = 0; i < n; i++)

int abs(int x) {
  if (x > 0)
    return x;
  else
    return -x;
}

int dp[80][80][12801];
int main() {
  int h, w;
  cin >> h >> w;
  // h = 80;
  // w = 80;
  int a[h][w], b[h][w];
  rep(i, h) rep(j, w) cin >> a[i][j];
  rep(i, h) rep(j, w) cin >> b[i][j];
  // rep(i, h)rep(j,w) a[i][j] = 80;
  // rep(i, h)rep(j,w) b[i][j] = 80;
  int mx = (h + w) * 80;

  rep(i, mx + 1) {
    if (abs(a[0][0] - b[0][0]) == i) {
      dp[0][0][i] = 1;
    } else {
      dp[0][0][i] = 0;
    }
  }

  rep(i, h) rep(j, w) rep(k, mx + 1) {
    int delta1 = abs(k - (a[i][j] - b[i][j]));
    int delta2 = abs(k + (a[i][j] - b[i][j]));
    if (delta1 > mx) {
      delta1 = delta2;
    }
    if (delta2 > mx) {
      delta2 = delta1;
    }

    if (i > 0 && j > 0) {
      if (dp[i - 1][j][delta1] == 1 || dp[i][j - 1][delta1] == 1 ||
          dp[i - 1][j][delta2] == 1 || dp[i][j - 1][delta2] == 1) {
        dp[i][j][k] = 1;
      } else {
        dp[i][j][k] = 0;
      }
    } else if (i > 0 && j == 0) {
      if (dp[i - 1][j][delta1] == 1 || dp[i - 1][j][delta2] == 1) {
        dp[i][j][k] = 1;
      } else {
        dp[i][j][k] = 0;
      }
    } else if (i == 0 && j > 0) {
      if (dp[i][j - 1][delta1] == 1 || dp[i][j - 1][delta2] == 1) {
        dp[i][j][k] = 1;
      } else {
        dp[i][j][k] = 0;
      }
    }
  }
  rep(i, mx + 1) {
    if (dp[h - 1][w - 1][i] == 1) {
      cout << i << endl;
      return 0;
    }
  }
}