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

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

#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<int, null_type, lessl<int>, rb_tree_tag,                                \\
       tree_order_statistics_node_update>
#define ll long long
#define int long long
#define double long double

#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()

#define ff first
#define ss second
#define mp make_pair

#define pb push_back
#define endl "\\n"

#define f(i, l, r) for (int i = l; i <= r; i++)
#define rf(i, r, l) for (int i = r; i >= l; i--)

#define bp __builtin_popcountll
#define inf 1e18

const int N = 3e3 + 5;
const int M = 1e9 + 7;
int dp[N][N];
int ans[N][N][4];
void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      dp[i][j] = 0;
      for (int p = 0; p < 4; p++)
        ans[i][j][p] = 0;
    }
  }
  while (k--) {
    int r1, c1, v1;
    cin >> r1 >> c1 >> v1;
    dp[r1][c1] = v1;
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      ans[i][j][0] = max(ans[i - 1][j][3], ans[i][j - 1][0]);
      ans[i][j][1] = max(dp[i][j] + ans[i - 1][j][3],
                         max(ans[i][j - 1][1], ans[i][j - 1][0] + dp[i][j]));
      ans[i][j][2] = max(dp[i][j] + ans[i - 1][j][3],
                         max(ans[i][j - 1][2], ans[i][j - 1][1] + dp[i][j]));
      ans[i][j][3] = max(dp[i][j] + ans[i - 1][j][3],
                         max(ans[i][j - 1][3], ans[i][j - 1][2] + dp[i][j]));
      ans[i][j][1] = max(ans[i][j][1], ans[i][j][0]);
      ans[i][j][2] = max(ans[i][j][2], ans[i][j][1]);
      ans[i][j][3] = max(ans[i][j][3], ans[i][j][2]);
      // cout<<ans[i][j][3]<<" ";
    }
    // cout<<endl;
  }
  int res = ans[n][m][3];
  cout << res << endl;
}

signed main() {
  FAST int t = 1;
  // cin>>t;
  for (int tc = 1; tc <= t; tc++) {
    // cout<<"Case #"<<tc<<": ";
    solve();
  }
}


Error: 0

Fixed submission:

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

#define FAST ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
#define ordered_set                                                            \\
  tree<int, null_type, lessl<int>, rb_tree_tag,                                \\
       tree_order_statistics_node_update>
#define ll long long
#define int long long
#define double long double

#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()

#define ff first
#define ss second
#define mp make_pair

#define pb push_back
#define endl "\\n"

#define f(i, l, r) for (int i = l; i <= r; i++)
#define rf(i, r, l) for (int i = r; i >= l; i--)

#define bp __builtin_popcountll
#define inf 1e18

const int N = 3e2 + 5;
const int M = 1e9 + 7;
int dp[N][N];
int ans[N][N][4];
void solve() {
  int n, m, k;
  cin >> n >> m >> k;
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N; j++) {
      dp[i][j] = 0;
      for (int p = 0; p < 4; p++)
        ans[i][j][p] = 0;
    }
  }
  while (k--) {
    int r1, c1, v1;
    cin >> r1 >> c1 >> v1;
    dp[r1][c1] = v1;
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      ans[i][j][0] = max(ans[i - 1][j][3], ans[i][j - 1][0]);
      ans[i][j][1] = max(dp[i][j] + ans[i - 1][j][3],
                         max(ans[i][j - 1][1], ans[i][j - 1][0] + dp[i][j]));
      ans[i][j][2] = max(dp[i][j] + ans[i - 1][j][3],
                         max(ans[i][j - 1][2], ans[i][j - 1][1] + dp[i][j]));
      ans[i][j][3] = max(dp[i][j] + ans[i - 1][j][3],
                         max(ans[i][j - 1][3], ans[i][j - 1][2] + dp[i][j]));
      ans[i][j][1] = max(ans[i][j][1], ans[i][j][0]);
      ans[i][j][2] = max(ans[i][j][2], ans[i][j][1]);
      ans[i][j][3] = max(ans[i][j][3], ans[i][j][2]);
      // cout<<ans[i][j][3]<<" ";
    }
    // cout<<endl;
  }
  int res = ans[n][m][3];
  cout << res << endl;
}

signed main() {
  FAST int t = 1;
  // cin>>t;
  for (int tc = 1; tc <= t; tc++) {
    // cout<<"Case #"<<tc<<": ";
    solve();
  }
}