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

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define int long long
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define fore(i, a) for (auto &i : a)
#define REP(i, n) for (int i = 0; i < n; i++)
#define eREP(i, n) for (int i = 0; i <= n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define eFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define SORT(c) sort((c).begin(), (c).end())
#define rSORT(c) sort((c).rbegin(), (c).rend())
#define LB(x, a) lower_bound((x).begin(), (x).end(), (a))
#define UB(x, a) upper_bound((x).begin(), (x).end(), (a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
#define eps 1e-12
// priority_queue<int,vector<int>, greater<int> > q2;

int V[3010][3010];
int dp[3010][3010][10];

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int R, C, K;
  cin >> R >> C >> K;

  REP(i, 3005) REP(j, 3005) V[i][j] = -1;
  REP(i, K) {
    int r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    V[r][c] = v;
  }

  REP(i, 3005) REP(j, 3005) REP(k, 5) dp[i][j][k] = 0;
  REP(i, R) {
    REP(j, C) {
      eREP(k, 3) {

        if (k != 3 && V[i][j] != -1) {
          if (k == 0)
            chmax(dp[i][j][k + 1], dp[i][j][k] + V[i][j]);

          if (j != 0 && j >= k)
            chmax(dp[i][j][k + 1], dp[i][j - 1][k] + V[i][j]);
        }
        if (i != R - 1) {
          chmax(dp[i + 1][j][0], dp[i][j][k]);
        }
        if (j != C - 1)
          chmax(dp[i][j + 1][k], dp[i][j][k]);
      }
    }
  }
  int ans = -INF;
  REP(k, 4) chmax(ans, dp[R - 1][C - 1][k]);
  cout << ans << endl;
  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define int long long
#define double long double
typedef vector<int> VI;
typedef pair<int, int> pii;
typedef vector<pii> VP;
typedef vector<string> VS;
typedef priority_queue<int> PQ;
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
#define fore(i, a) for (auto &i : a)
#define REP(i, n) for (int i = 0; i < n; i++)
#define eREP(i, n) for (int i = 0; i <= n; i++)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define eFOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define SORT(c) sort((c).begin(), (c).end())
#define rSORT(c) sort((c).rbegin(), (c).rend())
#define LB(x, a) lower_bound((x).begin(), (x).end(), (a))
#define UB(x, a) upper_bound((x).begin(), (x).end(), (a))
#define INF 1000000000
#define LLINF 9223372036854775807
#define mod 1000000007
#define eps 1e-12
// priority_queue<int,vector<int>, greater<int> > q2;

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int R, C, K;
  cin >> R >> C >> K;
  int V[3010][3010];
  int dp[3010][3010][10];
  REP(i, 3005) REP(j, 3005) V[i][j] = -1;
  REP(i, K) {
    int r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    V[r][c] = v;
  }

  REP(i, 3005) REP(j, 3005) REP(k, 5) dp[i][j][k] = 0;
  REP(i, R) {
    REP(j, C) {
      eREP(k, 3) {

        if (k != 3 && V[i][j] != -1) {
          if (k == 0)
            chmax(dp[i][j][k + 1], dp[i][j][k] + V[i][j]);

          if (j != 0 && j >= k)
            chmax(dp[i][j][k + 1], dp[i][j - 1][k] + V[i][j]);
        }
        if (i != R - 1) {
          chmax(dp[i + 1][j][0], dp[i][j][k]);
        }
        if (j != C - 1)
          chmax(dp[i][j + 1][k], dp[i][j][k]);
      }
    }
  }
  int ans = -INF;
  REP(k, 4) chmax(ans, dp[R - 1][C - 1][k]);
  cout << ans << endl;
  return 0;
}