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

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define printV(v)                                                              \\
  for (int i = 0; i < (int)v.size(); i++) {                                    \\
    cout << v[i] << " ";                                                       \\
  }                                                                            \\
  cout << endl
#define printRev(v)                                                            \\
  for (int i = (int)v.size() - 1; i >= 0; i--) {                               \\
    cout << v[i] << " ";                                                       \\
  }                                                                            \\
  cout << endl
#define sortV(v) sort(v.begin(), v.end())
#define revV(v) reverse(v.begin(), v.end())

ll a, b, c, d, e, f, g, n, m, k, t, x, h, w, p, q, y;
string str = "";
ll mod = 1e9 + 7;
const int N = 3009;
ll dp0[N][N];
ll dp1[N][N];
ll dp2[N][N];
ll dp3[N][N];
ll grid[N][N];

void solve() {
  ll R, C;
  cin >> R >> C >> k;
  for (int i = 1; i <= k; i++) {
    cin >> a >> b >> x;
    grid[a][b] = x;
  }

  ll answer = 0;

  for (int i = 1; i <= R; i++) {
    for (int ii = 1; ii <= C; ii++) {
      dp0[i][ii] = max(dp0[i][ii], max(max(dp0[i - 1][ii], dp1[i - 1][ii]),
                                       max(dp2[i - 1][ii], dp3[i - 1][ii])));
    }
    for (int j = 1; j <= C; j++) {
      dp1[i][j] = max(max(dp1[i][j - 1], dp1[i][j]), grid[i][j] + dp0[i][j]);
      dp2[i][j] =
          max(max(dp2[i][j - 1], dp2[i][j]), grid[i][j] + dp1[i][j - 1]);
      dp3[i][j] =
          max(max(dp3[i][j - 1], dp3[i][j]), grid[i][j] + dp2[i][j - 1]);
    }
  }

  for (int i = 1; i <= R; i++) {
    for (int j = 1; j <= C; j++) {
      answer = max(answer, max(dp0[i][j], dp1[i][j]));
      answer = max(answer, max(dp2[i][j], dp3[i][j]));
    }
  }

  /*
                  for(int i=1;i<=R;i++){
                      for(int j=1;j<=C;j++){
                          cout << dp0[i][j] << " ";
                          //cout << " (" << i << "," << j << ") ";
                      }cout << endl;
                  }

  */

  cout << answer << endl;
}

int main() {
  ios_base::sync_with_stdio(false);

  int cases = 1;
  // cin >> cases;

  while (cases--)
    solve();
  return 0;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define ll long long
#define pb push_back
#define printV(v)                                                              \\
  for (int i = 0; i < (int)v.size(); i++) {                                    \\
    cout << v[i] << " ";                                                       \\
  }                                                                            \\
  cout << endl
#define printRev(v)                                                            \\
  for (int i = (int)v.size() - 1; i >= 0; i--) {                               \\
    cout << v[i] << " ";                                                       \\
  }                                                                            \\
  cout << endl
#define sortV(v) sort(v.begin(), v.end())
#define revV(v) reverse(v.begin(), v.end())

ll a, b, c, d, e, f, g, n, m, k, t, x, h, w, p, q, y;
string str = "";
ll mod = 1e9 + 7;
// const int N = 3009;

void solve() {
  ll R, C;
  cin >> R >> C >> k;
  ll N = max(R, C) + 9;
  ll dp0[N][N];
  ll dp1[N][N];
  ll dp2[N][N];
  ll dp3[N][N];
  ll grid[N][N];
  N--;
  for (int i = 0; i <= N; i++)
    for (int j = 0; j <= N; j++) {
      dp0[i][j] = dp1[i][j] = dp2[i][j] = dp3[i][j] = grid[i][j] = 0;
    }
  for (int i = 1; i <= k; i++) {
    cin >> a >> b >> x;
    grid[a][b] = x;
  }

  ll answer = 0;

  for (int i = 1; i <= R; i++) {
    for (int ii = 1; ii <= C; ii++) {
      dp0[i][ii] = max(dp0[i][ii], max(max(dp0[i - 1][ii], dp1[i - 1][ii]),
                                       max(dp2[i - 1][ii], dp3[i - 1][ii])));
    }
    for (int j = 1; j <= C; j++) {
      dp1[i][j] = max(max(dp1[i][j - 1], dp1[i][j]), grid[i][j] + dp0[i][j]);
      dp2[i][j] =
          max(max(dp2[i][j - 1], dp2[i][j]), grid[i][j] + dp1[i][j - 1]);
      dp3[i][j] =
          max(max(dp3[i][j - 1], dp3[i][j]), grid[i][j] + dp2[i][j - 1]);
    }

    for (int i = 1; i <= R; i++) {
      for (int j = 1; j <= C; j++) {
        answer = max(answer, max(dp0[i][j], dp1[i][j]));
        answer = max(answer, max(dp2[i][j], dp3[i][j]));
      }
    }
  }
  cout << answer << endl;
}

int main() {
  ios_base::sync_with_stdio(false);

  int cases = 1;
  // cin >> cases;

  while (cases--)
    solve();
  return 0;
}