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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define mp make_pair
#define pb(x) push_back(x)
#define vi vector<int>
#define vs vector<string>
#define vll vector<long long>
#define vc vector<char>
#define vs vector<string>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pil pair<int, long long>
#define pli pair<long long, int>
#define pcc pair<char, char>
#define pdd pair<double, double>
#define mll map<long long, long long>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
#define flsh fflush(stdout)
#define gcd __gcd
#define clr(x) memset(x, 0, sizeof(x))
#define mod 1000000007LL
#define mod2 998244353LL
#define INF 1000000000007

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    o_tree;

const int N = 3005;

ll a[N][N];
ll dp[N][N][4];

void solve() {
  ll r, c, k;
  cin >> r >> c >> k;
  // clr(a);
  ll i, j;
  for (i = 0; i < k; i++) {
    ll x, y, v;
    cin >> x >> y >> v;
    a[x][y] = v;
  }
  // clr(dp);

  for (i = 1; i <= r; i++) {
    for (j = 1; j <= c; j++) {
      for (ll ii = 0; ii <= 3; ii++) {
        dp[i][j][ii] = max(dp[i][j][ii], dp[i][j - 1][ii]);
      }
      for (ll ii = 1; ii <= 3; ii++) {
        dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][ii] + a[i][j]);
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][ii]);
      }
      for (ll ii = 1; ii <= 3; ii++) {
        dp[i][j][ii] = max(dp[i][j][ii], dp[i][j - 1][ii - 1] + a[i][j]);
        dp[i][j][ii - 1] = max(dp[i][j][ii - 1], dp[i][j - 1][ii - 1]);
      }
    }
  }

  ll res = 0;
  for (i = 1; i <= 3; i++) {
    res = max(res, dp[r][c][i]);
  }
  cout << res << "\\n";
}

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

  if (fopen("input.txt", "r")) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }
  cout << setprecision(20);

  ll t = 1;

  // cin>>t;

  while (t--) {
    solve();
  }

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define mp make_pair
#define pb(x) push_back(x)
#define vi vector<int>
#define vs vector<string>
#define vll vector<long long>
#define vc vector<char>
#define vs vector<string>
#define pii pair<int, int>
#define pll pair<long long, long long>
#define pil pair<int, long long>
#define pli pair<long long, int>
#define pcc pair<char, char>
#define pdd pair<double, double>
#define mll map<long long, long long>
#define sz(v) ((int)(v).size())
#define all(v) (v).begin(), (v).end()
#define flsh fflush(stdout)
#define gcd __gcd
#define clr(x) memset(x, 0, sizeof(x))
#define mod 1000000007LL
#define mod2 998244353LL
#define INF 1000000000007

typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef tree<ll, null_type, less<ll>, rb_tree_tag,
             tree_order_statistics_node_update>
    o_tree;

void solve() {
  ll r, c, k;
  cin >> r >> c >> k;
  ll a[r + 5][c + 5];
  clr(a);
  ll i, j;
  for (i = 0; i < k; i++) {
    ll x, y, v;
    cin >> x >> y >> v;
    a[x][y] = v;
  }
  ll dp[r + 5][c + 5][4];
  clr(dp);

  for (i = 1; i <= r; i++) {
    for (j = 1; j <= c; j++) {
      for (ll ii = 1; ii <= 3; ii++) {
        dp[i][j][1] = max(dp[i][j][1], dp[i - 1][j][ii] + a[i][j]);
        dp[i][j][0] = max(dp[i][j][0], dp[i - 1][j][ii]);
      }
      for (ll ii = 1; ii <= 3; ii++) {
        dp[i][j][ii] = max(dp[i][j][ii], dp[i][j - 1][ii - 1] + a[i][j]);
        dp[i][j][ii - 1] = max(dp[i][j][ii - 1], dp[i][j - 1][ii - 1]);
      }
    }
  }

  ll res = 0;
  for (i = 0; i <= 3; i++) {
    res = max(res, dp[r][c][i]);
  }
  cout << res << "\\n";
}

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

  if (fopen("input.txt", "r")) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }
  cout << setprecision(20);

  ll t = 1;

  // cin>>t;

  while (t--) {
    solve();
  }

  return 0;
}