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

#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define MOD 1000000007
#define endl "\\n"
#define pb push_back
#define mp make_pair
#define ff first
#define all(c) (c).begin(), (c).end()
#define ss second
#define MAXX 9000000000000000000
#define pii pair<int, int>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vpi vector<pair<int, int>>
#define vvpi vector<vector<pair<int, int>>>
#define Inp(a, n)                                                              \\
  for (int i = 0; i < n; i++)                                                  \\
  cin >> a[i]
#define Out(a, n, ch)                                                          \\
  for (int i = 0; i < n; i++)                                                  \\
    cout << a[i] << ch;                                                        \\
  cout << endl;

int a[3005][3005], dp[3005][3005][4];

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int K, R, C, ans = 0;
  cin >> R >> C >> K;
  for (int i = 0; i <= R; i++) {
    for (int j = 0; j <= C; j++) {
      for (int k = 0; k < 4; k++)
        dp[i][j][k] = 0;
      a[i][j] = 0;
    }
  }
  for (int i = 0; i < K; i++) {
    int r, c, v;
    cin >> r >> c >> v;
    a[r][c] = v;
  }
  for (int i = 1; i <= R; i++) {
    for (int j = 1; j <= C; j++) {
      if (a[i][j] != 0) {
        // from top
        int mtop = 0;
        for (int k = 0; k < 4; k++)
          mtop = max(mtop, dp[i - 1][j][k]);
        dp[i][j][3] = max(dp[i][j][3], mtop); // max from this ;
        dp[i][j][2] = max(dp[i][j][2], mtop) + a[i][j];

        // from left
        dp[i][j][3] = max(dp[i][j][3], dp[i][j - 1][3]);
        dp[i][j][2] = max(dp[i][j][2], dp[i][j - 1][3] + a[i][j]);
        dp[i][j][2] = max(dp[i][j][2], dp[i][j - 1][2]);
        dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][2] + a[i][j]);
        dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][1]);
        dp[i][j][0] = max(dp[i][j][0], dp[i][j - 1][1] + a[i][j]);
        dp[i][j][0] = max(dp[i][j][0], dp[i][j - 1][0]);
      } else {
        for (int k = 0; k < 4; k++)
          dp[i][j][k] = max(dp[i - 1][j][k], dp[i][j - 1][k]);
      }
    }
  }
  for (int i = 0; i < 4; i++)
    ans = max(ans, dp[R][C][i]);
  cout << ans << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define MOD 1000000007
#define endl "\\n"
#define pb push_back
#define mp make_pair
#define ff first
#define all(c) (c).begin(), (c).end()
#define ss second
#define MAXX 9000000000000000000
#define pii pair<int, int>
#define vi vector<int>
#define vvi vector<vector<int>>
#define vpi vector<pair<int, int>>
#define vvpi vector<vector<pair<int, int>>>
#define Inp(a, n)                                                              \\
  for (int i = 0; i < n; i++)                                                  \\
  cin >> a[i]
#define Out(a, n, ch)                                                          \\
  for (int i = 0; i < n; i++)                                                  \\
    cout << a[i] << ch;                                                        \\
  cout << endl;

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int t = 1;
  // cin>>t;

  while (t--) {
    int K, R, C, ans = 0;
    cin >> R >> C >> K;
    int a[R + 5][C + 5], dp[R + 5][C + 5][4];
    for (int i = 0; i <= R; i++) {
      for (int j = 0; j <= C; j++) {
        for (int k = 0; k < 4; k++)
          dp[i][j][k] = 0;
        a[i][j] = 0;
      }
    }
    for (int i = 0; i < K; i++) {
      int r, c, v;
      cin >> r >> c >> v;
      a[r][c] = v;
    }
    for (int i = 1; i <= R; i++) {
      for (int j = 1; j <= C; j++) {
        if (a[i][j] != 0) {
          // from top
          int mtop = 0;
          for (int k = 0; k < 4; k++)
            mtop = max(mtop, dp[i - 1][j][k]);
          dp[i][j][3] = max(dp[i][j][3], mtop); // max from this ;
          dp[i][j][2] = max(dp[i][j][2], mtop) + a[i][j];

          // from left
          dp[i][j][3] = max(dp[i][j][3], dp[i][j - 1][3]);
          dp[i][j][2] = max(dp[i][j][2], dp[i][j - 1][3] + a[i][j]);
          dp[i][j][2] = max(dp[i][j][2], dp[i][j - 1][2]);
          dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][2] + a[i][j]);
          dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][1]);
          dp[i][j][0] = max(dp[i][j][0], dp[i][j - 1][1] + a[i][j]);
          dp[i][j][0] = max(dp[i][j][0], dp[i][j - 1][0]);
        } else {
          for (int k = 0; k < 4; k++)
            dp[i][j][k] = max(dp[i - 1][j][k], dp[i][j - 1][k]);
        }
      }
    }
    for (int i = 0; i < 4; i++)
      ans = max(ans, dp[R][C][i]);
    cout << ans << endl;
  }

  return 0;
}