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

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

typedef long long ll;

const int INF = 1 << 30;
const ll LLINF = 1LL << 60;

int mod = 1000000007;

ll items[3000 + 1][3000 + 1];
ll dp[4][3000 + 1][3000 + 1];

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int R, C, K;
  cin >> R >> C >> K;
  rep(i, R + 1) rep(j, C + 1) items[i][j] = 0;
  rep(i, K) {
    int r, c;
    cin >> r >> c;
    ll v;
    cin >> v;
    items[r - 1][c - 1] = v;
  }

  rep(i, 4) rep(j, R + 1) rep(k, C + 1) dp[i][j][k] = -LLINF;
  dp[0][0][0] = 0;
  for (int r = 0; r <= R; r++) {
    for (int c = 0; c <= C; c++) {
      for (int have = 0; have <= 3; have++) {
        if (have == 3) {
          if (r + 1 <= R)
            dp[0][r + 1][c] = max(dp[0][r + 1][c], dp[3][r][c]);
          if (c + 1 <= C)
            dp[3][r][c + 1] = max(dp[3][r][c + 1], dp[3][r][c]);
        } else {
          if (r + 1 <= R) {
            if (items[r][c] != 0) {
              dp[0][r + 1][c] =
                  max(dp[0][r + 1][c], dp[have][r][c] + items[r][c]);
            } else
              dp[0][r + 1][c] = max(dp[0][r + 1][c], dp[have][r][c]);
          }
          if (c + 1 <= C) {
            if (items[r][c] != 0) {
              dp[have][r][c + 1] = max(dp[have][r][c + 1], dp[have][r][c]);
              dp[have + 1][r][c + 1] =
                  max(dp[have + 1][r][c + 1], dp[have][r][c] + items[r][c]);
            } else
              dp[have][r][c + 1] = max(dp[have][r][c + 1], dp[have][r][c]);
          }
        }
      }
    }
  }
  ll ans = 0;
  rep(have, 4) ans = max(ans, dp[have][R][C]);
  cout << ans << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

typedef long long ll;

const int INF = 1 << 30;
const ll LLINF = 1LL << 60;

int mod = 1000000007;

int main(void) {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  int R, C, K;
  cin >> R >> C >> K;
  ll items[R + 1][C + 1];
  rep(i, R + 1) rep(j, C + 1) items[i][j] = 0;
  rep(i, K) {
    int r, c;
    cin >> r >> c;
    ll v;
    cin >> v;
    items[r - 1][c - 1] = v;
  }
  ll dp[4][R + 1][C + 1];
  rep(i, 4) rep(j, R + 1) rep(k, C + 1) dp[i][j][k] = -LLINF;
  dp[0][0][0] = 0;
  for (int r = 0; r <= R; r++) {
    for (int c = 0; c <= C; c++) {
      for (int have = 0; have <= 3; have++) {
        if (have == 3) {
          if (r + 1 <= R)
            dp[0][r + 1][c] = max(dp[0][r + 1][c], dp[3][r][c]);
          if (c + 1 <= C)
            dp[3][r][c + 1] = max(dp[3][r][c + 1], dp[3][r][c]);
        } else {
          if (r + 1 <= R) {
            if (items[r][c] != 0) {
              dp[0][r + 1][c] =
                  max(dp[0][r + 1][c], dp[have][r][c] + items[r][c]);
            } else
              dp[0][r + 1][c] = max(dp[0][r + 1][c], dp[have][r][c]);
          }
          if (c + 1 <= C) {
            if (items[r][c] != 0) {
              dp[have][r][c + 1] = max(dp[have][r][c + 1], dp[have][r][c]);
              dp[have + 1][r][c + 1] =
                  max(dp[have + 1][r][c + 1], dp[have][r][c] + items[r][c]);
            } else
              dp[have][r][c + 1] = max(dp[have][r][c + 1], dp[have][r][c]);
          }
        }
      }
    }
  }
  ll ans = 0;
  rep(have, 4) ans = max(ans, dp[have][R][C]);
  cout << ans << endl;
  return 0;
}