Problem p02586 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
using pll = pair<ll, ll>;

#define INF (1LL << 60)
#define MOD 1000000007
#define PI 3.14159265358979323846

#define REP(i, m, n) for (ll(i) = (m), (i_len) = (n); (i) < (i_len); ++(i))
#define FORR(i, v) for (auto(i) : v)
#define ALL(x) (x).begin(), (x).end()
#define PR(x) cout << (x) << endl
#define PS(x) cout << (x) << " "
#define SZ(x) ((ll)(x).size())
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define REV(x) reverse(ALL((x)))
#define ASC(x) sort(ALL((x)))
#define DESC(x)                                                                \
  ASC((x));                                                                    \
  REV((x))
#define pb push_back
#define eb emplace_back

int main() {
  ll R, C, K;
  cin >> R >> C >> K;
  map<pair<ll, ll>, ll> S;
  REP(i, 0, K) {
    ll r, c, v;
    cin >> r >> c >> v;
    S[{r, c}] = v;
  }

  // vector<vector<vector<ll>>> dp(R+1, vector<vector<ll>>(C+1, vector<ll>(4)));
  ll dp[R + 1][C + 1][4];
  REP(i, 0, R + 1) REP(j, 0, C + 1) REP(k, 0, 4) dp[i][j][k] = 0;
  REP(i, 1, R + 1) {
    REP(j, 1, C + 1) {
      ll x = 0;
      if (S.count({i, j}) > 0)
        x = S[{i, j}];
      dp[i][j][0] = MAX(dp[i][j][0], dp[i][j - 1][0]);
      REP(k, 0, 4) dp[i][j][0] = MAX(dp[i][j][0], dp[i - 1][j][k]);
      REP(k, 1, 4) dp[i][j][k] = MAX(dp[i][j][0], dp[i][j - 1][k]);
      dp[i][j][1] = MAX(dp[i][j][1], dp[i][j - 1][0] + x);
      REP(k, 0, 4) dp[i][j][1] = MAX(dp[i][j][1], dp[i - 1][j][k] + x);
      REP(k, 2, 4) dp[i][j][k] = MAX(dp[i][j][k], dp[i][j - 1][k - 1] + x);
    }
  }

  ll ans = MAX(dp[R][C][0], dp[R][C][1]);
  ans = MAX(ans, dp[R][C][2]);
  ans = MAX(ans, dp[R][C][3]);
  PR(ans);

  return 0;
}

/*



*/

Fixed submission:
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using vec = vector<ll>;
using mat = vector<vec>;
using pll = pair<ll, ll>;

#define INF (1LL << 60)
#define MOD 1000000007
#define PI 3.14159265358979323846

#define REP(i, m, n) for (ll(i) = (m), (i_len) = (n); (i) < (i_len); ++(i))
#define FORR(i, v) for (auto(i) : v)
#define ALL(x) (x).begin(), (x).end()
#define PR(x) cout << (x) << endl
#define PS(x) cout << (x) << " "
#define SZ(x) ((ll)(x).size())
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define REV(x) reverse(ALL((x)))
#define ASC(x) sort(ALL((x)))
#define DESC(x)                                                                \
  ASC((x));                                                                    \
  REV((x))
#define pb push_back
#define eb emplace_back

int main() {
  ll R, C, K;
  cin >> R >> C >> K;
  map<pair<ll, ll>, ll> S;
  REP(i, 0, K) {
    ll r, c, v;
    cin >> r >> c >> v;
    S[{r, c}] = v;
  }

  vector<vector<vector<ll>>> dp(R + 1,
                                vector<vector<ll>>(C + 1, vector<ll>(4, 0)));
  REP(i, 1, R + 1) {
    REP(j, 1, C + 1) {
      ll x = 0;
      if (S.count({i, j}) > 0)
        x = S[{i, j}];
      dp[i][j][0] = MAX(dp[i][j][0], dp[i][j - 1][0]);
      REP(k, 0, 4) dp[i][j][0] = MAX(dp[i][j][0], dp[i - 1][j][k]);
      REP(k, 1, 4) dp[i][j][k] = MAX(dp[i][j][0], dp[i][j - 1][k]);
      dp[i][j][1] = MAX(dp[i][j][1], dp[i][j - 1][0] + x);
      REP(k, 0, 4) dp[i][j][1] = MAX(dp[i][j][1], dp[i - 1][j][k] + x);
      REP(k, 2, 4) dp[i][j][k] = MAX(dp[i][j][k], dp[i][j - 1][k - 1] + x);
    }
  }

  ll ans = MAX(dp[R][C][0], dp[R][C][1]);
  ans = MAX(ans, dp[R][C][2]);
  ans = MAX(ans, dp[R][C][3]);
  PR(ans);

  return 0;
}

/*



*/