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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define PER(i, a, b) for (ll i = (a); i >= (b); i--)
#define rep(i, n) REP(i, 0, n)
#define per(i, n) PER(i, n, 0)
#define ALL(a) (a).begin(), (a).end()
const ll INF = 1e18 + 18;
const ll MAX = 200000;
const ll MOD = 1000000007;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl;
#define YES(n) cout << ((n) ? "YES" : "NO") << endl;

ll dp[3010][3010][4];

int main() {
  ll R, C, K;
  cin >> R >> C >> K;
  vector<ll> RR(K), CC(K), VV(K);
  vector<vector<ll>> Check(R, vector<ll>(C, 0));
  rep(i, K) {
    cin >> RR[i] >> CC[i] >> VV[i];
    RR[i]--;
    CC[i]--;
    Check[RR[i]][CC[i]] = VV[i];
  }
  rep(i, R + 1) {
    rep(j, C + 1) {
      rep(k, 4) { dp[i][j][k] = -1 * INF; }
    }
  }
  /*  rep(i, R){
  rep(j, C){
    cout<<Check[i][j]<<" ";
  }
    cout<<endl;}*/

  dp[0][0][0] = 0;
  if (Check[0][0] > 0)
    dp[0][0][1] = Check[0][0];

  rep(i, R) {
    rep(j, C) {
      if (i > 0) {
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][0]);
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][1]);
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][2]);
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][3]);
        if (Check[i][j] > 0) {
          dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][0] + Check[i][j]);
          dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][1] + Check[i][j]);
          dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][2] + Check[i][j]);
          dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][3] + Check[i][j]);
        }
      }
      if (j > 0) {
        dp[i][j][0] = max(dp[i][j][0], dp[i][j - 1][0]);
        dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][1]);
        dp[i][j][2] = max(dp[i][j][2], dp[i][j - 1][2]);
        dp[i][j][3] = max(dp[i][j][3], dp[i][j - 1][3]);
        if (Check[i][j] > 0) {
          dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][0] + Check[i][j]);
          dp[i][j][2] = max(dp[i][j][2], dp[i][j - 1][1] + Check[i][j]);
          dp[i][j][3] = max(dp[i][j][3], dp[i][j - 1][2] + Check[i][j]);
        }
      }
      // cout<<i<<" "<<j<<"   "<<dp[i][j][0]<<" "<<dp[i][j][1]<<"
      // "<<dp[i][j][2]<<" "<<dp[i][j][3]<<endl;
    }
  }
  ll Ans = max(dp[R - 1][C - 1][0], dp[R - 1][C - 1][1]);
  ll Anss = max(dp[R - 1][C - 1][2], dp[R - 1][C - 1][3]);
  cout << max(Ans, Anss) << endl;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i, a, b) for (ll i = (a); i < (b); i++)
#define PER(i, a, b) for (ll i = (a); i >= (b); i--)
#define rep(i, n) REP(i, 0, n)
#define per(i, n) PER(i, n, 0)
#define ALL(a) (a).begin(), (a).end()
const ll INF = 1e18 + 18;
const ll MAX = 200000;
const ll MOD = 1000000007;
#define Yes(n) cout << ((n) ? "Yes" : "No") << endl;
#define YES(n) cout << ((n) ? "YES" : "NO") << endl;

int main() {
  ll R, C, K;
  cin >> R >> C >> K;
  vector<ll> RR(K), CC(K), VV(K);
  vector<vector<ll>> Check(R, vector<ll>(C, 0));
  rep(i, K) {
    cin >> RR[i] >> CC[i] >> VV[i];
    RR[i]--;
    CC[i]--;
    Check[RR[i]][CC[i]] = VV[i];
  }
  ll dp[3010][3010][4];
  rep(i, R + 1) {
    rep(j, C + 1) {
      rep(k, 4) { dp[i][j][k] = -1 * INF; }
    }
  }
  /*  rep(i, R){
  rep(j, C){
    cout<<Check[i][j]<<" ";
  }
    cout<<endl;}*/

  dp[0][0][0] = 0;
  if (Check[0][0] > 0)
    dp[0][0][1] = Check[0][0];

  rep(i, R) {
    rep(j, C) {
      if (i > 0) {
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][0]);
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][1]);
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][2]);
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][3]);
        if (Check[i][j] > 0) {
          dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][0] + Check[i][j]);
          dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][1] + Check[i][j]);
          dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][2] + Check[i][j]);
          dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][3] + Check[i][j]);
        }
      }
      if (j > 0) {
        dp[i][j][0] = max(dp[i][j][0], dp[i][j - 1][0]);
        dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][1]);
        dp[i][j][2] = max(dp[i][j][2], dp[i][j - 1][2]);
        dp[i][j][3] = max(dp[i][j][3], dp[i][j - 1][3]);
        if (Check[i][j] > 0) {
          dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][0] + Check[i][j]);
          dp[i][j][2] = max(dp[i][j][2], dp[i][j - 1][1] + Check[i][j]);
          dp[i][j][3] = max(dp[i][j][3], dp[i][j - 1][2] + Check[i][j]);
        }
      }
      // cout<<i<<" "<<j<<"   "<<dp[i][j][0]<<" "<<dp[i][j][1]<<"
      // "<<dp[i][j][2]<<" "<<dp[i][j][3]<<endl;
    }
  }
  ll Ans = max(dp[R - 1][C - 1][0], dp[R - 1][C - 1][1]);
  ll Anss = max(dp[R - 1][C - 1][2], dp[R - 1][C - 1][3]);
  cout << max(Ans, Anss) << endl;
}