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

#include <bits/stdc++.h>
using namespace std;
/*
 *  JUST KEEP GOING
 */
#define endl '\\n'
#define ll long long
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define sz(v) ((int)((v).size()))
#define clr(v, d) memset(v, d, sizeof(v))
#define rep(i, v) for (int i = 0; i < sz(v); ++i)
#define For(i, s) for (auto i : s)
#define Forr(i, s) for (auto &i : s)
#define lp(i, n) for (int i = 0; i < (int)(n); ++i)
#define lpi(i, j, n) for (int i = (j); i < (int)(n); ++i)
#define lpd(i, j, n) for (int i = (j); i >= (int)(n); --i)
#define vi vector<int>
#define vl vector<ll>
#define ii pair<int, int>
#define vii vector<ii>
#define vll vector<pair<ll, ll>>
#define PI double PPPPPP = 3.14159265358979323846;
#define bit(mask, i) ((mask >> i) & 1)

void run() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 3000 + 5, mod = 1e9 + 7;
int r, c, k;

ll dp[N][N][5], arr[N][N];
ll solve(int i, int j, int cnt) {
  if (i >= r || j >= c) {
    return -1e17;
  }
  if (i == r - 1 && j == c - 1) {
    return (cnt < 3 ? arr[i][j] : 0);
  }
  ll &ret = dp[i][j][cnt];
  if (~ret)
    return ret;
  ret = 0;
  if (cnt < 3) {
    ret = max(ret, solve(i, j + 1, cnt + 1) + arr[i][j]);
    ret = max(ret, solve(i + 1, j, 0) + arr[i][j]);
  }
  ret = max(ret, solve(i + 1, j, 0));
  ret = max(ret, solve(i, j + 1, cnt));
  return ret;
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  run();
  cin >> r >> c >> k;
  for (int i = 0; i < k; ++i) {
    int a, b, v;
    cin >> a >> b >> v;
    arr[a - 1][b - 1] = v;
  }
  clr(dp, -1);
  cout << solve(0, 0, 0) << endl;
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
/*
 *  JUST KEEP GOING
 */
#define endl '\\n'
#define ll long long
#define all(v) ((v).begin()), ((v).end())
#define allr(v) ((v).rbegin()), ((v).rend())
#define sz(v) ((int)((v).size()))
#define clr(v, d) memset(v, d, sizeof(v))
#define rep(i, v) for (int i = 0; i < sz(v); ++i)
#define For(i, s) for (auto i : s)
#define Forr(i, s) for (auto &i : s)
#define lp(i, n) for (int i = 0; i < (int)(n); ++i)
#define lpi(i, j, n) for (int i = (j); i < (int)(n); ++i)
#define lpd(i, j, n) for (int i = (j); i >= (int)(n); --i)
#define vi vector<int>
#define vl vector<ll>
#define ii pair<int, int>
#define vii vector<ii>
#define vll vector<pair<ll, ll>>
#define PI double PPPPPP = 3.14159265358979323846;
#define bit(mask, i) ((mask >> i) & 1)

void run() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr), cout.tie(nullptr);
}
const int N = 3000 + 5, mod = 1e9 + 7;
int r, c, k;

ll dp[N][N][5], arr[N][N];
ll solve(int i, int j, int cnt) {
  if (i >= r || j >= c) {
    return -1e17;
  }
  if (i == r - 1 && j == c - 1) {
    return (cnt < 3 ? arr[i][j] : 0);
  }
  ll &ret = dp[i][j][cnt];
  if (~ret)
    return ret;
  ret = 0;
  if (cnt < 3) {
    ret = max(ret, solve(i, j + 1, cnt + 1) + arr[i][j]);
    ret = max(ret, solve(i + 1, j, 0) + arr[i][j]);
  }
  ret = max(ret, solve(i + 1, j, 0));
  ret = max(ret, solve(i, j + 1, cnt));
}
int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  run();
  cin >> r >> c >> k;
  for (int i = 0; i < k; ++i) {
    int a, b, v;
    cin >> a >> b >> v;
    arr[a - 1][b - 1] = v;
  }
  clr(dp, -1);
  cout << solve(0, 0, 0) << endl;
}