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

#include <bits/stdc++.h>
using namespace std;
long long grid[3001][3001];
long long dp[3001][3001][4];
int main() {
  long long r, c, k;
  cin >> r >> c >> k;
  for (long long i = 0; i < k; i++) {
    long long a, b, v;
    cin >> a >> b >> v;
    grid[a - 1][b - 1] = v;
  }
  for (long long i = 0; i < r; i++) {
    for (long long j = 0; j < c; j++) {
      if (i == 0 && j == 0) {
        dp[i][j][0] = 0;
        dp[i][j][1] = grid[i][j];
        dp[i][j][2] = 0;
        dp[i][j][3] = 0;
        continue;
      }
      if (i == 0) {
        dp[i][j][0] = 0;
        dp[i][j][1] = max(dp[i][j - 1][1], dp[i][j - 1][0] + grid[i][j]);
        dp[i][j][2] = max(dp[i][j - 1][2], dp[i][j - 1][1] + grid[i][j]);
        dp[i][j][3] = max(dp[i][j - 1][3], dp[i][j - 1][2] + grid[i][j]);
      } else if (j == 0) {
        dp[i][j][0] = max(max(dp[i - 1][j][0], dp[i - 1][j][1]),
                          max(dp[i - 1][j][2], dp[i - 1][j][3]));
        dp[i][j][1] = max(grid[i][j], dp[i][j][0] + grid[i][j]);
        dp[i][j][2] = 0;
        dp[i][j][3] = 0;
      } else {
        dp[i][j][0] =
            max(dp[i - 1][j][0], max(max(dp[i - 1][j][0], dp[i - 1][j][1]),
                                     max(dp[i - 1][j][2], dp[i - 1][j][3])));
        dp[i][j][1] = max(dp[i][j - 1][1], max(dp[i][j - 1][0] + grid[i][j],
                                               dp[i][j][0] + grid[i][j]));
        dp[i][j][2] = max(dp[i][j - 1][2], dp[i][j - 1][1] + grid[i][j]);
        dp[i][j][3] = max(dp[i][j - 1][3], dp[i][j - 1][2] + grid[i][j]);
      }
    }
  }
  long long ans = dp[r - 1][c - 1][0];
  for (long long k = 0; k < 4; k++)
    ans = max(ans, dp[r - 1][c - 1][k]);
  cout << ans << endl;
}

Error: -11

Fixed submission:

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

int main() {
  long long r, c, k;
  cin >> r >> c >> k;
  long long grid[3001][3001];
  memset(grid, 0, sizeof(grid));
  for (long long i = 0; i < k; i++) {
    long long a, b, v;
    cin >> a >> b >> v;
    grid[a - 1][b - 1] = v;
  }
  long long dp[3001][3001][4];
  memset(dp, 0, sizeof(dp));
  for (long long i = 0; i < r; i++) {
    for (long long j = 0; j < c; j++) {
      if (i == 0 && j == 0) {
        dp[i][j][0] = 0;
        dp[i][j][1] = grid[i][j];
        dp[i][j][2] = 0;
        dp[i][j][3] = 0;
        continue;
      }
      if (i == 0) {
        dp[i][j][0] = 0;
        dp[i][j][1] = max(dp[i][j - 1][1], dp[i][j - 1][0] + grid[i][j]);
        dp[i][j][2] = max(dp[i][j - 1][2], dp[i][j - 1][1] + grid[i][j]);
        dp[i][j][3] = max(dp[i][j - 1][3], dp[i][j - 1][2] + grid[i][j]);
      } else if (j == 0) {
        dp[i][j][0] = max(max(dp[i - 1][j][0], dp[i - 1][j][1]),
                          max(dp[i - 1][j][2], dp[i - 1][j][3]));
        dp[i][j][1] = max(grid[i][j], dp[i][j][0] + grid[i][j]);
        dp[i][j][2] = 0;
        dp[i][j][3] = 0;
      } else {
        dp[i][j][0] =
            max(dp[i - 1][j][0], max(max(dp[i - 1][j][0], dp[i - 1][j][1]),
                                     max(dp[i - 1][j][2], dp[i - 1][j][3])));
        dp[i][j][1] = max(dp[i][j - 1][1], max(dp[i][j - 1][0] + grid[i][j],
                                               dp[i][j][0] + grid[i][j]));
        dp[i][j][2] = max(dp[i][j - 1][2], dp[i][j - 1][1] + grid[i][j]);
        dp[i][j][3] = max(dp[i][j - 1][3], dp[i][j - 1][2] + grid[i][j]);
      }
    }
  }
  long long ans = dp[r - 1][c - 1][0];
  for (long long k = 0; k < 4; k++)
    ans = max(ans, dp[r - 1][c - 1][k]);
  cout << ans << endl;
}