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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, m, n) for (int i = m; i < n; i++)
const ll mod = 1e9 + 7;

int main() {
  int R, C, K;
  cin >> R >> C >> K;
  int r[K], c[K], v[K];
  rep(i, 0, K) { cin >> r[i] >> c[i] >> v[i]; }
  vector<vector<int>> item(R, vector<int>(C));
  /*int item[R][C];
  rep(i,0,R){
    rep(j,0,C){
      item[i][j]=0;
    }
  }*/
  rep(i, 0, K) { item[r[i] - 1][c[i] - 1] = v[i]; }
  vector<vector<vector<ll>>> dp(R, vector<vector<ll>>(C, vector<ll>(4)));
  /*ll dp[R][C][4];
  rep(i,0,R){
    rep(j,0,C){
      rep(k,0,4){
        dp[i][j][k]=0;
      }
    }
  }*/
  if (item[0][0] != 0)
    dp[0][0][1] = item[0][0];
  rep(i, 0, R) {
    rep(j, 0, C) {
      if (i + 1 < R) {
        dp[i + 1][j][0] =
            max(max(max(max(dp[i + 1][j][0], dp[i][j][0]), dp[i][j][1]),
                    dp[i][j][2]),
                dp[i][j][3]);
        if (item[i + 1][j] != 0) {
          dp[i + 1][j][1] =
              max(max(max(max(dp[i + 1][j][1], dp[i][j][0] + item[i + 1][j]),
                          dp[i][j][1] + item[i + 1][j]),
                      dp[i][j][2] + item[i + 1][j]),
                  dp[i][j][3] + item[i + 1][j]);
        }
      }
      if (j + 1 < C) {
        dp[i][j + 1][0] = max(dp[i][j + 1][0], dp[i][j][0]);
        dp[i][j + 1][1] = max(dp[i][j + 1][1], dp[i][j][1]);
        dp[i][j + 1][2] = max(dp[i][j + 1][2], dp[i][j][2]);
        dp[i][j + 1][3] = max(dp[i][j + 1][3], dp[i][j][3]);
        if (item[i][j + 1] != 0) {
          dp[i][j + 1][1] = max(dp[i][j + 1][1], dp[i][j][0] + item[i][j + 1]);
          dp[i][j + 1][2] = max(dp[i][j + 1][2], dp[i][j][1] + item[i][j + 1]);
          dp[i][j + 1][3] = max(dp[i][j + 1][3], dp[i][j][2] + item[i][j + 1]);
        }
      }
    }
  }
  cout << max(max(max(dp[R - 1][C - 1][0], dp[R - 1][C - 1][1]),
                  dp[R - 1][C - 1][2]),
              dp[R - 1][C - 1][3])
       << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
#define rep(i, m, n) for (int i = m; i < n; i++)
const ll mod = 1e9 + 7;

int main() {
  int R, C, K;
  cin >> R >> C >> K;
  int r[K], c[K], v[K];
  rep(i, 0, K) { cin >> r[i] >> c[i] >> v[i]; }
  int item[R][C];
  rep(i, 0, R) {
    rep(j, 0, C) { item[i][j] = 0; }
  }
  rep(i, 0, K) { item[r[i] - 1][c[i] - 1] = v[i]; }
  ll dp[R][C][4];
  rep(i, 0, R) {
    rep(j, 0, C) {
      rep(k, 0, 4) { dp[i][j][k] = 0; }
    }
  }
  if (item[0][0] != 0)
    dp[0][0][1] = item[0][0];
  rep(i, 0, R) {
    rep(j, 0, C) {
      if (i + 1 < R) {
        dp[i + 1][j][0] =
            max(max(max(max(dp[i + 1][j][0], dp[i][j][0]), dp[i][j][1]),
                    dp[i][j][2]),
                dp[i][j][3]);
        if (item[i + 1][j] != 0) {
          dp[i + 1][j][1] =
              max(max(max(max(dp[i + 1][j][1], dp[i][j][0] + item[i + 1][j]),
                          dp[i][j][1] + item[i + 1][j]),
                      dp[i][j][2] + item[i + 1][j]),
                  dp[i][j][3] + item[i + 1][j]);
        }
      }
      if (j + 1 < C) {
        dp[i][j + 1][0] = max(dp[i][j + 1][0], dp[i][j][0]);
        dp[i][j + 1][1] = max(dp[i][j + 1][1], dp[i][j][1]);
        dp[i][j + 1][2] = max(dp[i][j + 1][2], dp[i][j][2]);
        dp[i][j + 1][3] = max(dp[i][j + 1][3], dp[i][j][3]);
        if (item[i][j + 1] != 0) {
          dp[i][j + 1][1] = max(dp[i][j + 1][1], dp[i][j][0] + item[i][j + 1]);
          dp[i][j + 1][2] = max(dp[i][j + 1][2], dp[i][j][1] + item[i][j + 1]);
          dp[i][j + 1][3] = max(dp[i][j + 1][3], dp[i][j][2] + item[i][j + 1]);
        }
      }
    }
  }
  cout << max(max(max(dp[R - 1][C - 1][0], dp[R - 1][C - 1][1]),
                  dp[R - 1][C - 1][2]),
              dp[R - 1][C - 1][3])
       << endl;
}