Problem p02586 (C++)

Buggy submission (status: Runtime Error):
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
using ll = long long;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ld long double
#define al(a) (a).begin(), (a).end()
#define mk make_pair
#define check cout << "?" << endl;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  if (v.empty()) {
    os << "{ }";
    return os;
  }
  os << "{" << v.front();
  for (auto itr = ++v.begin(); itr != v.end(); itr++) {
    os << ", " << *itr;
  }
  os << "}";
  return os;
}

ll MOD = 1000000007;
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;

int main() {
  ll R, C, k;
  cin >> R >> C >> k;
  vector<vector<ll>> m(R, vector<ll>(C, -1));
  rep(i, k) {
    ll r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    m[r][c] = v;
  }
  ll dp[4][R + 1][C];
  rep(i, 4) rep(j, R + 1) rep(K, C) dp[i][j][K] = -1;
  dp[0][0][0] = 0;
  rep(i, R) rep(j, C) for (int K = 3; K >= 0; K--) {
    if (dp[K][i][j] == -1)
      continue;
    if (j != C - 1)
      dp[K][i][j + 1] = max(dp[K][i][j + 1], dp[K][i][j]);
    dp[0][i + 1][j] = max(dp[0][i + 1][j], dp[K][i][j]);
    if (m[i][j] != -1) {
      if (K < 3 && dp[K + 1][i][j] < dp[K][i][j] + m[i][j]) {
        dp[K + 1][i][j] = dp[K][i][j] + m[i][j];
        if (j != C - 1)
          dp[K + 1][i][j + 1] = max(dp[K + 1][i][j + 1], dp[K + 1][i][j]);
        dp[0][i + 1][j] = max(dp[0][i + 1][j], dp[K + 1][i][j]);
      }
    }
  }
  ll ans = 0;
  rep(i, 4) rep(j, C) ans = max(ans, dp[i][R][j]);
  cout << ans << endl;
}

Fixed submission:
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
// #include <atcoder/all>
// using namespace atcoder;
using ll = long long;
#define pp pair<int, int>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define ld long double
#define al(a) (a).begin(), (a).end()
#define mk make_pair
#define check cout << "?" << endl;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  if (v.empty()) {
    os << "{ }";
    return os;
  }
  os << "{" << v.front();
  for (auto itr = ++v.begin(); itr != v.end(); itr++) {
    os << ", " << *itr;
  }
  os << "}";
  return os;
}

ll MOD = 1000000007;
ll mod = 998244353;
int inf = 1000001000;
ll INF = 1e18 + 5;

int main() {
  ll R, C, k;
  cin >> R >> C >> k;
  vector<vector<ll>> m(R, vector<ll>(C, -1));
  rep(i, k) {
    ll r, c, v;
    cin >> r >> c >> v;
    r--;
    c--;
    m[r][c] = v;
  }
  vector<vector<vector<ll>>> dp(4,
                                vector<vector<ll>>(R + 1, vector<ll>(C, -1)));
  dp[0][0][0] = 0;
  rep(i, R) rep(j, C) for (int K = 3; K >= 0; K--) {
    if (dp[K][i][j] == -1)
      continue;
    if (j != C - 1)
      dp[K][i][j + 1] = max(dp[K][i][j + 1], dp[K][i][j]);
    dp[0][i + 1][j] = max(dp[0][i + 1][j], dp[K][i][j]);
    if (m[i][j] != -1) {
      if (K < 3 && dp[K + 1][i][j] < dp[K][i][j] + m[i][j]) {
        dp[K + 1][i][j] = dp[K][i][j] + m[i][j];
        if (j != C - 1)
          dp[K + 1][i][j + 1] = max(dp[K + 1][i][j + 1], dp[K + 1][i][j]);
        dp[0][i + 1][j] = max(dp[0][i + 1][j], dp[K + 1][i][j]);
      }
    }
  }
  ll ans = 0;
  rep(i, 4) rep(j, C) ans = max(ans, dp[i][R][j]);
  cout << ans << endl;
}