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

#include <bits/stdc++.h>
#include <random>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)

using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using ull = unsigned long long;
const ll K_MAX = 1e5 + 5;
const ll Lmax = ll(1e18);
const ll INF = 1e15 + 4;
const int Nmax = 2e5 + 1;
const int Nd = 61;
ll dfs[3003][3003][4];
ll v[3003][3003];

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  // vector<P> rc(k);
  rep(i, r + 2) { // dfsの初期化
    rep(j, c + 2) {
      v[i][j] = 0;
      rep(l, 4) { dfs[i][j][l] = 0; }
    }
  }

  rep(i, k) {
    int x, y;
    cin >> x >> y;
    cin >> v[x][y];
  }
  int n = r + c - 1;

  dfs[1][1][0] = 0;
  for (int in = 2; in <= n; in++) { // step n回
    int imax = min(in, r);
    for (int i = 1; i <= imax; i++) { //
      int j = in - i;
      /// 拾わないケース///
      dfs[i + 1][j][0] = max(dfs[i + 1][j][0], dfs[i][j][0]);
      dfs[i + 1][j][0] = max(dfs[i + 1][j][0], dfs[i][j][1]);
      dfs[i + 1][j][0] = max(dfs[i + 1][j][0], dfs[i][j][2]);
      dfs[i + 1][j][0] = max(dfs[i + 1][j][0], dfs[i][j][3]);
      dfs[i][j + 1][0] = max(dfs[i][j + 1][0], dfs[i][j][0]);
      dfs[i][j + 1][1] = max(dfs[i][j + 1][1], dfs[i][j][1]);
      dfs[i][j + 1][2] = max(dfs[i][j + 1][2], dfs[i][j][2]);
      dfs[i][j + 1][3] = max(dfs[i][j + 1][3], dfs[i][j][3]);
      if (v[i][j] != 0) { // v[i][j]!=0;
        /// ひろうケース
        dfs[i + 1][j][0] = max(dfs[i + 1][j][0], dfs[i][j][0] + v[i][j]);
        dfs[i + 1][j][0] = max(dfs[i + 1][j][0], dfs[i][j][1] + v[i][j]);
        dfs[i + 1][j][0] = max(dfs[i + 1][j][0], dfs[i][j][2] + v[i][j]);
        dfs[i][j + 1][1] = max(dfs[i][j + 1][1], dfs[i][j][0] + v[i][j]);
        dfs[i][j + 1][2] = max(dfs[i][j + 1][2], dfs[i][j][1] + v[i][j]);
        dfs[i][j + 1][3] = max(dfs[i][j + 1][3], dfs[i][j][2] + v[i][j]);
      }
    }
  }
  ll ans = max(
      dfs[r][c][0] + v[r][c],
      max(dfs[r][c][1] + v[r][c], max(dfs[r][c][2] + v[r][c], dfs[r][c][3])));
  cout << ans << endl;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
#include <random>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep1(i, n) for (int i = 1; i <= (n); ++i)

using namespace std;
using ll = long long;
using P = pair<ll, ll>;
using ull = unsigned long long;
const ll K_MAX = 1e5 + 5;
const ll Lmax = ll(1e18);
const ll INF = 1e15 + 4;
const int Nmax = 2e5 + 1;
const int Nd = 61;
ll dfs[3003][3003][4];
ll v[3003][3003];

int main() {
  int r, c, k;
  cin >> r >> c >> k;
  // vector<P> rc(k);
  rep(i, r) { // dfsの初期化
    rep(j, c) {
      v[i][j] = 0;
      rep(l, 4) { dfs[i][j][l] = 0; }
    }
  }

  rep(i, k) {
    int x, y;
    cin >> x >> y;
    cin >> v[x][y];
  }
  int n = r * c;

  dfs[1][1][0] = 0;
  for (int in = 0; in <= n; in++) { // step n回
    int imax = min(in, r);
    for (int i = 0; i <= imax; i++) { //
      int j = in - i;
      /// 拾わないケース///
      dfs[i + 1][j][0] = max(dfs[i + 1][j][0], dfs[i][j][0]);
      dfs[i + 1][j][0] = max(dfs[i + 1][j][0], dfs[i][j][1]);
      dfs[i + 1][j][0] = max(dfs[i + 1][j][0], dfs[i][j][2]);
      dfs[i + 1][j][0] = max(dfs[i + 1][j][0], dfs[i][j][3]);
      dfs[i][j + 1][0] = max(dfs[i][j + 1][0], dfs[i][j][0]);
      dfs[i][j + 1][1] = max(dfs[i][j + 1][1], dfs[i][j][1]);
      dfs[i][j + 1][2] = max(dfs[i][j + 1][2], dfs[i][j][2]);
      dfs[i][j + 1][3] = max(dfs[i][j + 1][3], dfs[i][j][3]);
      if (v[i][j] != 0) { // v[i][j]!=0;
        /// ひろうケース
        dfs[i + 1][j][0] = max(dfs[i + 1][j][0], dfs[i][j][0] + v[i][j]);
        dfs[i + 1][j][0] = max(dfs[i + 1][j][0], dfs[i][j][1] + v[i][j]);
        dfs[i + 1][j][0] = max(dfs[i + 1][j][0], dfs[i][j][2] + v[i][j]);
        dfs[i][j + 1][1] = max(dfs[i][j + 1][1], dfs[i][j][0] + v[i][j]);
        dfs[i][j + 1][2] = max(dfs[i][j + 1][2], dfs[i][j][1] + v[i][j]);
        dfs[i][j + 1][3] = max(dfs[i][j + 1][3], dfs[i][j][3] + v[i][j]);
      }
    }
  }
  ll ans = max(
      dfs[r][c][0] + v[r][c],
      max(dfs[r][c][1] + v[r][c], max(dfs[r][c][2] + v[r][c], dfs[r][c][3])));
  cout << ans << endl;
}