Problem p02586 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

typedef long long ll;
const ll MOD = 1e9 + 7;
const long long INF = 1LL << 60;
const double PI = 3.14159265358979323846;
const int NMAX = 100005;
using namespace std;

ll mp[5001][5001];
ll dp[5001][5001][4];

int main() {
  ll r, c, k;
  cin >> r >> c >> k;
  for (int i = 0; i <= r; i++) {
    for (int j = 0; j <= c; j++) {
      mp[i][j] = 0;
    }
  }
  for (int i = 0; i < k; i++) {
    ll x, y, v;
    cin >> x >> y >> v;
    mp[x - 1][y - 1] = v;
  }

  for (int j = 0; j <= r; j++) {
    for (int i = 0; i <= c; i++) {
      for (int l = 0; l < 4; l++) {
        dp[j][i][l] = 0;
      }
    }
  }

  for (int j = 0; j < r; j++) {
    for (int i = 0; i < c; i++) {
      dp[j][i + 1][0] = max(dp[j][i + 1][0], dp[j][i][0]);
      dp[j][i + 1][1] = max(dp[j][i + 1][1], dp[j][i][1]);
      dp[j][i + 1][1] = max(dp[j][i + 1][1], dp[j][i][0] + mp[j][i]);
      dp[j][i + 1][2] = max(dp[j][i + 1][2], dp[j][i][2]);
      dp[j][i + 1][2] = max(dp[j][i + 1][2], dp[j][i][1] + mp[j][i]);
      dp[j][i + 1][3] = max(dp[j][i + 1][3], dp[j][i][3]);
      dp[j][i + 1][3] = max(dp[j][i + 1][3], dp[j][i][2] + mp[j][i]);

      dp[j + 1][i][0] = max(dp[j][i][3], dp[j][i][2] + mp[j][i]);
      dp[j + 1][i][1] = max(dp[j][i][3], dp[j][i][2] + mp[j][i]);
      dp[j + 1][i][2] = max(dp[j][i][3], dp[j][i][2] + mp[j][i]);
      dp[j + 1][i][3] = max(dp[j][i][3], dp[j][i][2] + mp[j][i]);
    }
  }

  ll ans = 0;
  ans = max(dp[r][c - 1][3], dp[r - 1][c][3]);
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>

typedef long long ll;
const ll MOD = 1e9 + 7;
const long long INF = 1LL << 60;
const double PI = 3.14159265358979323846;
const int NMAX = 100005;
using namespace std;

int main() {
  ll r, c, k;
  cin >> r >> c >> k;
  vector<vector<ll>> mp(r + 1, vector<ll>(c + 1, 0));
  for (int i = 0; i < k; i++) {
    ll x, y, v;
    cin >> x >> y >> v;
    mp[x - 1][y - 1] = v;
  }

  ll dp[r + 1][c + 1][4];

  for (int j = 0; j <= r; j++) {
    for (int i = 0; i <= c; i++) {
      for (int l = 0; l < 4; l++) {
        dp[j][i][l] = 0;
      }
    }
  }

  for (int j = 0; j < r; j++) {
    for (int i = 0; i < c; i++) {
      dp[j][i + 1][0] = max(dp[j][i + 1][0], dp[j][i][0]);
      dp[j][i + 1][1] = max(dp[j][i + 1][1], dp[j][i][1]);
      dp[j][i + 1][1] = max(dp[j][i + 1][1], dp[j][i][0] + mp[j][i]);
      dp[j][i + 1][2] = max(dp[j][i + 1][2], dp[j][i][2]);
      dp[j][i + 1][2] = max(dp[j][i + 1][2], dp[j][i][1] + mp[j][i]);
      dp[j][i + 1][3] = max(dp[j][i + 1][3], dp[j][i][3]);
      dp[j][i + 1][3] = max(dp[j][i + 1][3], dp[j][i][2] + mp[j][i]);

      dp[j + 1][i][0] = max(dp[j][i][3], dp[j][i][2] + mp[j][i]);
      dp[j + 1][i][1] = max(dp[j][i][3], dp[j][i][2] + mp[j][i]);
      dp[j + 1][i][2] = max(dp[j][i][3], dp[j][i][2] + mp[j][i]);
      dp[j + 1][i][3] = max(dp[j][i][3], dp[j][i][2] + mp[j][i]);
    }
  }

  ll ans = 0;
  ans = max(dp[r][c - 1][3], dp[r - 1][c][3]);
  cout << ans << endl;
  return 0;
}