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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define INF 2e9
#define MOD 1000000007
// #define MOD 998244353
#define LINF (long long)4e18
#define jck 3.141592

const double EPS = 1e-10;

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;

ll dp[3010][3010][4];

int main() {
  int R, C, K;
  cin >> R >> C >> K;
  vector<int> r(K), c(K), v(K);
  vector<vector<int>> f(R, vector<int>(C));
  rep(i, K) {
    cin >> r[i] >> c[i] >> v[i];
    r[i]--;
    c[i]--;
    f[r[i]][c[i]] = v[i];
  }
  rep(i, 3010) rep(j, 3010) rep(k, 4) { dp[i][j][k] = -1; }
  if (f[0][0])
    dp[1][1][1] = f[0][0];
  dp[1][1][0] = 0;

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


Error: -11

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define repr(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repr(i, 0, n)
#define INF 2e9
#define MOD 1000000007
// #define MOD 998244353
#define LINF (long long)4e18
#define jck 3.141592

const double EPS = 1e-10;

using ll = long long;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;

ll dp[3010][3010][4];

int main() {
  int R, C, K;
  cin >> R >> C >> K;
  vector<int> r(K), c(K), v(K);
  vector<vector<int>> f(R, vector<int>(C));
  rep(i, K) {
    cin >> r[i] >> c[i] >> v[i];
    r[i]--;
    c[i]--;
    f[r[i]][c[i]] = v[i];
  }
  rep(i, 3010) rep(j, 3010) rep(k, 4) { dp[i][j][k] = -1; }
  rep(i, K) {
    if (r[i] == 0 && c[i] == 0) {
      dp[1][1][1] = v[i];
      break;
    }
  }
  vector<vector<int>> p(R);
  rep(i, K) { p[r[i]].push_back(c[i]); }
  dp[1][1][0] = 0;

  rep(i, R + 1) rep(j, C + 1) rep(k, 4) {
    if (dp[i][j][k] == -1)
      continue;
    if (k < 3) {
      rep(l, p[i - 1].size()) {
        if (j - 1 >= p[i - 1][l])
          continue;
        dp[i][p[i - 1][l] + 1][k + 1] = max(
            dp[i][p[i - 1][l] + 1][k + 1], dp[i][j][k] + f[i - 1][p[i - 1][l]]);
      }
    }
    if (i < R && f[i][j - 1] > 0)
      dp[i + 1][j][1] = max(dp[i + 1][j][1], dp[i][j][k] + f[i][j - 1]);
    if (i < R)
      dp[i + 1][j][0] = max(dp[i + 1][j][0], dp[i][j][k]);
  }
  ll ans = 0;
  repr(i, R - 2, R + 1) repr(j, C - 2, C + 1) rep(k, 4) {
    ans = max(ans, dp[i][j][k]);
  }
  // rep(i,R)rep(j,C)rep(k,4) cout << i+1 << " " << j+1 << " " <<
  // dp[i+1][j+1][k] << endl;
  cout << ans << endl;
}