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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

using int64 = long long;

#define all(_) begin(_), end(_)
#define rall(_) rbegin(_), rend(_)

int64 dp[3000][3000][4];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int r, c, k;
  cin >> r >> c >> k;
  int val[r][c];
  fill(val[0], val[r], 0);
  for (int i = 0; i < k; ++i) {
    int x, y, z;
    cin >> x >> y >> z;
    --x;
    --y;
    val[x][y] = z;
  }

  if (val[0][0] != 0)
    dp[0][0][1] = val[0][0];

  for (int i = 0; i < r; ++i) {
    for (int j = 0; j < c; ++j) {
      if (i == 0 && j == 0)
        continue;

      if (val[i][j] != 0) {
        if (i > 0) {
          for (int l = 0; l < 4; ++l)
            dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][l]);
          for (int l = 0; l < 4; ++l)
            dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][l] + val[i][j]);
        }
        if (j > 0) {
          dp[i][j][0] = max(dp[i][j][0], dp[i][j - 1][0]);
          for (int l = 1; l < 4; ++l) {
            dp[i][j][l] = max({dp[i][j][l], dp[i][j - 1][l],
                               dp[i][j - 1][l - 1] + val[i][j]});
          }
        }
      } else {
        if (i > 0) {
          for (int l = 0; l < 4; ++l)
            dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][l]);
        }
        if (j > 0) {
          for (int l = 0; l < 4; ++l)
            dp[i][j][l] = max(dp[i][j][l], dp[i][j - 1][l]);
        }
      }
    }
  }

  int64 ans = 0;
  for (int i = 0; i < 4; ++i)
    ans = max(ans, dp[r - 1][c - 1][i]);
  cout << ans << endl;

  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <chrono>
#include <cmath>
#include <complex>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <utility>
#include <vector>

using namespace std;

using int64 = long long;

#define all(_) begin(_), end(_)
#define rall(_) rbegin(_), rend(_)

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);

  int r, c, k;
  cin >> r >> c >> k;
  int val[r][c];
  fill(val[0], val[r], 0);
  for (int i = 0; i < k; ++i) {
    int x, y, z;
    cin >> x >> y >> z;
    --x;
    --y;
    val[x][y] = z;
  }

  int64 dp[r][c][4];
  for (int i = 0; i < r; ++i) {
    for (int j = 0; j < c; ++j) {
      for (int l = 0; l < 4; ++l) {
        dp[i][j][l] = 0;
      }
    }
  }

  if (val[0][0] != 0)
    dp[0][0][1] = val[0][0];

  for (int i = 0; i < r; ++i) {
    for (int j = 0; j < c; ++j) {
      if (i == 0 && j == 0)
        continue;

      if (val[i][j] != 0) {
        if (i > 0) {
          for (int l = 0; l < 4; ++l)
            dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][l]);
          for (int l = 0; l < 4; ++l)
            dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][l] + val[i][j]);
        }
        if (j > 0) {
          dp[i][j][0] = max(dp[i][j][0], dp[i][j - 1][0]);
          for (int l = 1; l < 4; ++l) {
            dp[i][j][l] = max({dp[i][j][l], dp[i][j - 1][l],
                               dp[i][j - 1][l - 1] + val[i][j]});
          }
        }
      } else {
        if (i > 0) {
          for (int l = 0; l < 4; ++l)
            dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][l]);
        }
        if (j > 0) {
          for (int l = 0; l < 4; ++l)
            dp[i][j][l] = max(dp[i][j][l], dp[i][j - 1][l]);
        }
      }
    }
  }

  int64 ans = 0;
  for (int i = 0; i < 4; ++i)
    ans = max(ans, dp[r - 1][c - 1][i]);
  cout << ans << endl;

  return 0;
}