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

#include <bits/stdc++.h>
using namespace std;
#define int long long int
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
#define tr(it, a) for (auto it = a.begin(); it != a.end(); it++)
#define deb(x) cout << #x << "=" << x << endl
#define deb2(x, y) cout << #x << "=" << x << "," << #y << "=" << y << endl
#define fo(i, n) for (int i = 0; i < n; i++)
#define Fo(i, k, n)                                                            \\
  for (int i = k; k < n ? i < n : i > n; k < n ? i += 1 : i -= 1)
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<int, pair<int, int>> pipii;
typedef priority_queue<pii, vector<pii>, greater<pii>> minpq;
typedef priority_queue<pii> maxpq;
// const double PI = acos(-1);
const int mod = 1000000007;

// ----------------------DEBUG TEMPLATE--------------------------
void __print(int x) { cerr << x; }
void __print(float x) { cerr << x; }
void __print(double x) { cerr << x; }
void __print(long double x) { cerr << x; }
void __print(char x) { cerr << '\\'' << x << '\\''; }
void __print(const char *x) { cerr << '\\"' << x << '\\"'; }
void __print(const string &x) { cerr << '\\"' << x << '\\"'; }
void __print(bool x) { cerr << (x ? "true" : "false"); }

template <typename T, typename V> void __print(const pair<T, V> &x) {
  cerr << '{';
  __print(x.first);
  cerr << ',';
  __print(x.second);
  cerr << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cerr << '{';
  for (auto &i : x)
    cerr << (f++ ? "," : ""), __print(i);
  cerr << "}";
}
void _print() { cerr << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cerr << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define debug(x...)                                                            \\
  cerr << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define debug(x...)
#endif
// ----------------------DEBUG TEMPLATE--------------------------
int R, C;
int mat[3001][3001];
int dp[3001][3001][4];

int solve(int x, int y, int curr) {
  if (x > R || y > C) {
    return 0;
  }

  if (dp[x][y][curr] != -1) {
    return dp[x][y][curr];
  }
  if (mat[x][y] == 0) {
    dp[x][y][curr] = max(solve(x, y + 1, curr), solve(x + 1, y, 0));
    return dp[x][y][curr];
  } else {
    if (curr < 3) {
      int op1 = mat[x][y] + solve(x, y + 1, curr + 1);
      int op2 = solve(x, y + 1, curr);
      int op3 = mat[x][y] + solve(x + 1, y, 0);
      int op4 = solve(x + 1, y, 0);
      dp[x][y][curr] = max({op1, op2, op3, op4});
      return dp[x][y][curr];
    } else {
      dp[x][y][curr] = max(solve(x + 1, y, 0), solve(x, y + 1, curr));
      return dp[x][y][curr];
    }
  }
}

int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  int K;
  cin >> R >> C >> K;
  memset(mat, 0, sizeof(mat));
  memset(dp, -1, sizeof(dp));
  while (K--) {
    int r, c, val;
    cin >> r >> c >> val;
    mat[r][c] = val;
  }
  cout << solve(1, 1, 0) << endl;
}


Error: 0

Fixed submission:

// ctrl+shift+b
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using P = pair<ll, ll>;
using Graph = vector<vector<ll>>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define rep2(i, m, n) for (ll i = m; i < (ll)(n); i++)
#define rrep(i, n, m) for (ll i = n; i >= (ll)(m); i--)
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {0, 1, 1, 1, 0, -1, -1, -1};
const int ddy[8] = {1, 1, 0, -1, -1, -1, 0, 1};
const ll MOD = 1000000007;
const ll INF = 1000000000000000000L;
#ifdef __DEBUG

/**
 * For DEBUG
 * https://github.com/ta7uw/cpp-pyprint
 */
#include "cpp-pyprint/pyprint.h"

#endif

ll find_maximum(ll a[], ll n) {
  int c, max, index;

  max = a[0];
  index = 0;

  for (c = 1; c < n; c++) {
    if (a[c] > max) {
      index = c;
      max = a[c];
    }
  }

  return max;
}

void Main() {
  ll R, C, K;
  cin >> R >> C >> K;
  ll arr[R + 1][C + 1][4];
  rep(i, R + 1) {
    rep(j, C + 1) {
      rep(k, 4) { arr[i][j][k] = 0; }
    }
  }
  map<P, ll> x;
  rep(i, K) {
    ll r, c, v;
    cin >> r >> c >> v;
    x[make_pair(r, c)] = v;
  }
  rep2(i, 1, R + 1) {
    rep2(j, 1, C + 1) {
      rep(k, 4) {
        arr[i][j][k] = max(arr[i][j - 1][k], find_maximum(arr[i - 1][j], 4));
        if (k != 0) {
          P p = make_pair(i, j);
          if (x.find(p) != x.end()) {
            ll v = x[p];
            arr[i][j][k] = max(
                arr[i][j][k],
                max(find_maximum(arr[i - 1][j], 4), arr[i][j - 1][k - 1]) + v);
          }
        }
      }
    }
  }
  ll ans = -INF;
  rep(i, 4) {
    ll tmp = arr[R][C][i];
    if (tmp > ans) {
      ans = tmp;
    }
  }
  cout << ans << endl;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  Main();
  return 0;
}