Problem p02586 (C++) — buggy submission, verdict: 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;

/*
struct point{
    ll start, index, point;
};

bool comp(point a, point b){
    if (a.start == b.start){
        return a.index < b.index;
    }
    else{
        return a.start < b.start;
    }
}
*/
const int max_v = 3001;

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

int main() {
  int R, C, K;
  cin >> R >> C >> K;
  // vector<ll> r(K), c(K), v(K);
  // rep(i,K) cin >> r[i] >> c[i] >> v[i];

  /*
  vector<point> rcv(K);
  rep(i,K){
      ll tr,tc,tv;
      cin >> tr >> tc >> tv;
      point p = {tr, tc, tv};
      rcv[i] = p;
  }
  */
  vector<vector<ll>> rcv(R + 5, vector<ll>(C + 5, 0));
  rep(i, K) {
    ll tr, tc, tv;
    cin >> tr >> tc >> tv;
    tr--;
    tc--;
    rcv[tr][tc] = tv;
    // cout << tr << " " << tc << " " << tv;
  }

  // cout << rcv.size() << endl;

  // sort(rcv.begin(), rcv.end(), comp);

  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) {
        // cout << i << " " << j << " " << k << endl;
        if (i + 1 < R && dp[i][j][k] != INF) {
          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]);
          // cout << i << " " << j << endl;
          // cout << dp[i+1][j][0] <<  " " << dp[i+1][j][1] << endl;
        }
        if (j + 1 < C && dp[i][j][k] != INF) {
          dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
          if (k + 1 >= 4)
            continue;
          // cout << i << " " << j << " " << k << endl;
          // cout << rcv[i][k] << endl;
          dp[i][j + 1][k + 1] =
              max(dp[i][j + 1][k + 1], dp[i][j][k] + rcv[i][j + 1]);
          /*
          if (dp[i][j+1][k+1] == 12){
              cout << i << " " << j+1 << " " << k+1 << endl;
          }
          */
        }
      }
    }
  }
  ll ans = -1;
  rep(i, 4) {
    // cout << dp[R-1][C-1][i] << endl;
    ans = max(ans, dp[R - 1][C - 1][i]);
  }

  cout << ans << endl;
}

Error: -11

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;

/*
struct point{
    ll start, index, point;
};

bool comp(point a, point b){
    if (a.start == b.start){
        return a.index < b.index;
    }
    else{
        return a.start < b.start;
    }
}
*/
const int max_v = 3001;

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

int main() {
  int R, C, K;
  cin >> R >> C >> K;
  // vector<ll> r(K), c(K), v(K);
  // rep(i,K) cin >> r[i] >> c[i] >> v[i];

  /*
  vector<point> rcv(K);
  rep(i,K){
      ll tr,tc,tv;
      cin >> tr >> tc >> tv;
      point p = {tr, tc, tv};
      rcv[i] = p;
  }
  */
  vector<vector<ll>> rcv(R * C + 1, vector<ll>(4, 0));
  rep(i, K) {
    ll tr, tc, tv;
    cin >> tr >> tc >> tv;
    tr--;
    tc--;
    rcv[tr][tc] = tv;
    // cout << tr << " " << tc << " " << tv;
  }

  // cout << rcv.size() << endl;

  // sort(rcv.begin(), rcv.end(), comp);

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

  rep(i, R) {
    rep(j, C) {
      rep(k, 4) {
        // cout << i << " " << j << " " << k << endl;
        if (i + 1 < max_v) {
          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]);
          // cout << i << " " << j << endl;
          // cout << dp[i+1][j][0] <<  " " << dp[i+1][j][1] << endl;
        }
        if (j + 1 < max_v) {
          dp[i][j + 1][k] = max(dp[i][j + 1][k], dp[i][j][k]);
          if (k + 1 >= 4)
            continue;
          // cout << i << " " << j << " " << k << endl;
          // cout << rcv[i][k] << endl;
          dp[i][j + 1][k + 1] =
              max(dp[i][j + 1][k + 1], dp[i][j][k] + rcv[i][j + 1]);
          /*
          if (dp[i][j+1][k+1] == 12){
              cout << i << " " << j+1 << " " << k+1 << endl;
          }
          */
        }
      }
    }
  }
  ll ans = -1;
  rep(i, 4) {
    // cout << dp[R-1][C-1][i] << endl;
    ans = max(ans, dp[R - 1][C - 1][i]);
  }

  cout << ans << endl;
}