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

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

#define ll long long

ll a[3001][3001], dp[3001][3001][4];

int main() {
  ll i, j, n, m, k, x, y, v, p, ans;

  cin >> n >> m >> k;

  memset(a, 0, sizeof(a));
  for (i = 0; i < k; i += 1) {
    cin >> x >> y >> v;
    a[x][y] = v;
  }

  memset(dp, 0, sizeof(dp));

  // for(i=1; i<=n; i+=1)
  // {
  //     for(j=1; j<=m; j+=1)
  //         cout << a[i][j] << " ";
  //     cout << endl;
  // }

  for (i = 1; i <= n; i += 1) {
    for (j = 1; j <= m; j += 1) {
      // 0
      for (p = 0; p < 4; p += 1)
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][p]);
      dp[i][j][0] = max(dp[i][j][0], dp[i][j - 1][0]);

      // 1
      if (a[i][j] != 0) {
        for (p = 0; p < 4; p += 1)
          dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][p] + a[i][j]);

        dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][0] + a[i][j]);
      }
      dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][1]);

      // 2
      if (a[i][j] != 0 && dp[i][j - 1][1] != 0)
        dp[i][j][2] = max(dp[i][j][2], dp[i][j - 1][1] + a[i][j]);

      dp[i][j][2] = max(dp[i][j][2], dp[i][j - 1][2]);

      // 3
      if (a[i][j] != 0 && dp[i][j - 1][2] != 0)
        dp[i][j][3] = max(dp[i][j][3], dp[i][j - 1][2] + a[i][j]);

      dp[i][j][3] = max(dp[i][j][3], dp[i][j - 1][3]);
    }
  }

  ans = 0;
  for (i = 0; i < 4; i += 1)
    ans = max(ans, dp[n][m][i]);

  cout << ans << endl;

  // for(p=0; p<4; p+=1)
  // {
  //     cout << p << endl;
  //     for(i=1; i<=n; i+=1)
  //     {
  //         for(j=1; j<=m; j+=1)
  //             cout << dp[i][j][p] << " ";
  //         cout << endl;
  //     }
  // }
}

Error: 0

Fixed submission:

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

#define ll long long

int main() {
  ll i, j, n, m, k, x, y, v, p, ans;

  cin >> n >> m >> k;

  ll a[n + 1][m + 1], dp[n + 1][m + 1][4];

  memset(a, 0, sizeof(a));
  for (i = 0; i < k; i += 1) {
    cin >> x >> y >> v;
    a[x][y] = v;
  }

  memset(dp, 0, sizeof(dp));

  // for(i=1; i<=n; i+=1)
  // {
  //     for(j=1; j<=m; j+=1)
  //         cout << a[i][j] << " ";
  //     cout << endl;
  // }

  for (i = 1; i <= n; i += 1) {
    for (j = 1; j <= m; j += 1) {
      // 0
      for (p = 0; p < 4; p += 1)
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][p]);
      dp[i][j][0] = max(dp[i][j][0], dp[i][j - 1][0]);

      // 1
      if (a[i][j] != 0) {
        for (p = 0; p < 4; p += 1)
          dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][p] + a[i][j]);

        dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][0] + a[i][j]);
      }
      dp[i][j][1] = max(dp[i][j][1], dp[i][j - 1][1]);

      // 2
      if (a[i][j] != 0 && dp[i][j - 1][1] != 0)
        dp[i][j][2] = max(dp[i][j][2], dp[i][j - 1][1] + a[i][j]);

      dp[i][j][2] = max(dp[i][j][2], dp[i][j - 1][2]);

      // 3
      if (a[i][j] != 0 && dp[i][j - 1][2] != 0)
        dp[i][j][3] = max(dp[i][j][3], dp[i][j - 1][2] + a[i][j]);

      dp[i][j][3] = max(dp[i][j][3], dp[i][j - 1][3]);
    }
  }

  ans = 0;
  for (i = 0; i < 4; i += 1)
    ans = max(ans, dp[n][m][i]);

  cout << ans << endl;

  // for(p=0; p<4; p+=1)
  // {
  //     cout << p << endl;
  //     for(i=1; i<=n; i+=1)
  //     {
  //         for(j=1; j<=m; j+=1)
  //             cout << dp[i][j][p] << " ";
  //         cout << endl;
  //     }
  // }
}