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

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define LL_MAX LLONG_MAX
#define ULL_MAX ULLONG_MAX
#define ull unsigned long long
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define pb(x) push_back(x)
#define re() pop_back()
#define eb(x) emplace_back(x)
#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define fore(i, l, r) for (int i = int(l); i < int(r); ++i)
#define all(a) a.begin(), a.end()
#define msort(a) sort(a.begin(), a.end())
#define pii pair<int, int>
#define sz(x) int(x.size())
#define mk make_pair
#define endl '\\n'
const int maxn = 3004;
ll dp[maxn][maxn][4];

void solve() {
  ll n, m;
  cin >> n >> m;

  vector<vector<ll>> a(n + 1, vector<ll>(m + 1, 0));
  int k;
  cin >> k;
  while (k--) {
    int x, y, v;
    cin >> x >> y >> v;
    a[x][y] = v;
  }

  memset(dp, 0, sizeof dp);

  for (int i = 1; i < n + 1; i++) {
    for (int j = 1; j < m + 1; j++) {
      ll val = 0;
      for (int k = 0; k < 4; k++) {
        val = max(val, dp[i - 1][j][k]);
      }
      if (a[i][j] == 0) {
        dp[i][j][0] = max(dp[i][j - 1][0], val);
        dp[i][j][1] = dp[i][j - 1][1];
        dp[i][j][2] = dp[i][j - 1][2];
        dp[i][j][3] = dp[i][j - 1][3];
      } else {
        dp[i][j][0] = max(dp[i - 1][j][3], dp[i][j - 1][0]);
        dp[i][j][1] =
            max(val + a[i][j], max(dp[i][j - 1][0] + a[i][j], dp[i][j - 1][1]));
        dp[i][j][2] = max(dp[i][j - 1][1] + a[i][j], dp[i][j - 1][2]);
        dp[i][j][3] = max(dp[i][j - 1][2] + a[i][j], dp[i][j - 1][3]);
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i < 4; i++) {
    ans = max(ans, dp[n][m][i]);
  }

  cout << ans << endl;
}

int main() {
  IOS;
  ll t = 1; // cin>>t;
  while (t--) {
    solve();
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define LL_MAX LLONG_MAX
#define ULL_MAX ULLONG_MAX
#define ull unsigned long long
#define IOS                                                                    \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define pb(x) push_back(x)
#define re() pop_back()
#define eb(x) emplace_back(x)
#define forn(i, n) for (int i = 0; i < int(n); ++i)
#define fore(i, l, r) for (int i = int(l); i < int(r); ++i)
#define all(a) a.begin(), a.end()
#define msort(a) sort(a.begin(), a.end())
#define pii pair<int, int>
#define sz(x) int(x.size())
#define mk make_pair
#define endl '\\n'

void solve() {
  ll n, m;
  cin >> n >> m;

  vector<vector<ll>> a(n + 1, vector<ll>(m + 1, 0));
  int k;
  cin >> k;
  while (k--) {
    int x, y, v;
    cin >> x >> y >> v;
    a[x][y] = v;
  }

  ll dp[n + 1][m + 1][4];
  memset(dp, 0, sizeof dp);

  for (int i = 1; i < n + 1; i++) {
    for (int j = 1; j < m + 1; j++) {
      ll val = 0;
      for (int k = 0; k < 4; k++) {
        val = max(val, dp[i - 1][j][k]);
      }
      if (a[i][j] == 0) {
        dp[i][j][0] = max(dp[i][j - 1][0], val);
        dp[i][j][1] = dp[i][j - 1][1];
        dp[i][j][2] = dp[i][j - 1][2];
        dp[i][j][3] = dp[i][j - 1][3];
      } else {
        dp[i][j][0] = max(dp[i - 1][j][3], dp[i][j - 1][0]);
        dp[i][j][1] =
            max(val + a[i][j], max(dp[i][j - 1][0] + a[i][j], dp[i][j - 1][1]));
        dp[i][j][2] = max(dp[i][j - 1][1] + a[i][j], dp[i][j - 1][2]);
        dp[i][j][3] = max(dp[i][j - 1][2] + a[i][j], dp[i][j - 1][3]);
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i < 4; i++) {
    ans = max(ans, dp[n][m][i]);
  }

  cout << ans << endl;
}

int main() {
  IOS;
  ll t = 1; // cin>>t;
  while (t--) {
    solve();
  }
}