Problem p02586 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <float.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = 0; i <= (n); i++)
using namespace std;
typedef long long ll;

const ll INF = 1LL << 62;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// 多次元 vector 生成
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
const int MOD = 1000000007;

const int max_v = 3001;

ll dp[max_v][max_v][4];

int main() {
  int R, C, K;
  cin >> R >> C >> K;

  vector<vector<ll>> rcv(R, vector<ll>(C, 0));
  rep(i, K) {
    ll tr, tc, tv;
    cin >> tr >> tc >> tv;
    tr--;
    tc--;
    rcv[tr][tc] = tv;
  }

  rep(i, max_v) {
    rep(j, max_v) {
      rep(k, 4) { dp[i][j][k] = -INF; }
    }
  }
  dp[0][0][0] = 0;
  dp[0][0][1] = rcv[0][0];

  rep(i, R) {
    rep(j, C) {
      rep(k, 4) {
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
        dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][k] + rcv[i + 1][j]);
        dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        if (k + 1 < 4) {
          dp[i][j + 1][k + 1] =
              max(dp[i][j + 1][k + 1], dp[i][j][k] + rcv[i][j + 1]);
        }
      }
    }
  }
  ll ans = -1;
  rep(i, 4) { ans = max(ans, dp[R - 1][C - 1][i]); }

  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#include <float.h>
#include <math.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define rrep(i, n) for (int i = 0; i <= (n); i++)
using namespace std;
typedef long long ll;

const ll INF = 1LL << 62;
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// 多次元 vector 生成
template <class T> vector<T> make_vec(size_t a) { return vector<T>(a); }
template <class T, class... Ts> auto make_vec(size_t a, Ts... ts) {
  return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
const int MOD = 1000000007;

const int max_v = 3001;

ll dp[max_v][max_v][4];

int main() {
  int R, C, K;
  cin >> R >> C >> K;

  vector<vector<ll>> rcv(R + 1, vector<ll>(C + 1, 0));
  rep(i, K) {
    ll tr, tc, tv;
    cin >> tr >> tc >> tv;
    tr--;
    tc--;
    rcv[tr][tc] = tv;
  }

  rep(i, max_v) {
    rep(j, max_v) {
      rep(k, 4) { dp[i][j][k] = -INF; }
    }
  }
  dp[0][0][0] = 0;
  dp[0][0][1] = rcv[0][0];

  rep(i, R) {
    rep(j, C) {
      rep(k, 4) {
        dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
        dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][k] + rcv[i + 1][j]);
        dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
        if (k + 1 < 4) {
          dp[i][j + 1][k + 1] =
              max(dp[i][j + 1][k + 1], dp[i][j][k] + rcv[i][j + 1]);
        }
      }
    }
  }
  ll ans = -1;
  rep(i, 4) { ans = max(ans, dp[R - 1][C - 1][i]); }

  cout << ans << endl;
}