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

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

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef pair<ll, ll> P;
typedef tuple<int, int, int> tpl;

#define ALL(a) (a).begin(), (a).end()
#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(c) reverse((c).begin(), (c).end())
#define EXIST(m, v) (m).find((v)) != (m).end()
#define LB(a, x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a, x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); --i)
#define RREP(i, n) RFOR(i, n, 0)

#define en "\\n"

constexpr double EPS = 1e-9;
constexpr double PI = 3.1415926535897932;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL << 60;
constexpr long long MOD = 1000000007; // 998244353;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

ll dp[3030][3030][4], item[3030][3030];
void Main() {
  ll R, C, K;
  cin >> R >> C >> K;
  REP(i, K) {
    ll r, c, v;
    cin >> r >> c >> v;
    item[r][c] = v;
  }

  FOR(i, 1, R + 1) FOR(j, 1, C + 1) {
    REP(k, 4) {
      chmax(dp[i][j][k], dp[i - 1][j][k]);
      chmax(dp[i][j][k], dp[i][j - 1][k]);
    }

    if (item[i][j] > 0) {
      chmax(dp[i][j][1], max(dp[i - 1][j][3], dp[i][j - 1][0]) + item[i][j]);
      REP(k, 2) { chmax(dp[i][j][k + 2], dp[i][j - 1][k + 1] + item[i][j]); }
      REP(k, 3) chmax(dp[i][j][k + 1], dp[i][j][k]);
    }
  }

  ll ans = 0;
  REP(i, 4) chmax(ans, dp[R][C][i]);
  cout << ans << en;
  return;
}

int main(void) {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(15);
  int t = 1; // cin>>t;
  REP(_, t) Main();
  return 0;
}

Error: 0

Fixed submission:

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

typedef long long ll;
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<long long> VL;
typedef vector<vector<long long>> VVL;
typedef pair<ll, ll> P;
typedef tuple<int, int, int> tpl;

#define ALL(a) (a).begin(), (a).end()
#define SORT(c) sort((c).begin(), (c).end())
#define REVERSE(c) reverse((c).begin(), (c).end())
#define EXIST(m, v) (m).find((v)) != (m).end()
#define LB(a, x) lower_bound((a).begin(), (a).end(), x) - (a).begin()
#define UB(a, x) upper_bound((a).begin(), (a).end(), x) - (a).begin()

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define RFOR(i, a, b) for (int i = (a)-1; i >= (b); --i)
#define RREP(i, n) RFOR(i, n, 0)

#define en "\\n"

constexpr double EPS = 1e-9;
constexpr double PI = 3.1415926535897932;
constexpr int INF = 2147483647;
constexpr long long LINF = 1LL << 60;
constexpr long long MOD = 1000000007; // 998244353;

template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

void Main() {
  ll R, C, K;
  cin >> R >> C >> K;
  ll item[R + 1][C + 1];
  REP(i, R + 1) REP(j, C + 1) item[i][j] = 0;
  REP(i, K) {
    ll r, c, v;
    cin >> r >> c >> v;
    item[r][c] = v;
  }

  ll dp[R + 1][C + 1][4];
  REP(i, R + 1) REP(j, C + 1) REP(k, 4) dp[i][j][k] = 0;
  FOR(i, 1, R + 1) FOR(j, 1, C + 1) {
    REP(k, 4) {
      chmax(dp[i][j][k], dp[i - 1][j][k]);
      chmax(dp[i][j][k], dp[i][j - 1][k]);
    }

    if (item[i][j] > 0) {
      chmax(dp[i][j][1], max(dp[i - 1][j][3], dp[i][j - 1][0]) + item[i][j]);
      REP(k, 2) { chmax(dp[i][j][k + 2], dp[i][j - 1][k + 1] + item[i][j]); }
      REP(k, 3) chmax(dp[i][j][k + 1], dp[i][j][k]);
    }
  }

  ll ans = 0;
  REP(i, 4) chmax(ans, dp[R][C][i]);
  cout << ans << en;
  return;
}

int main(void) {
  cin.tie(0);
  cout.tie(0);
  ios_base::sync_with_stdio(0);
  cout << fixed << setprecision(15);
  int t = 1; // cin>>t;
  REP(_, t) Main();
  return 0;
}