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 std;
using namespace __gnu_pbds;
template <typename key>
using ordered_set = tree<key, null_type, less<key>, rb_tree_tag,
                         tree_order_statistics_node_update>;
#define F first
#define S second
#define T second.first
#define D second.second
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
#define all(v) v.begin(), v.end()
#define OO 0x3f3f3f3fLL
#define INF 0x3f3f3f3f3f3f3f3fLL
#define sz(s) (int)(s.size())
#define RT(s) return cout << s, 0
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef pair<ll, pair<ll, ll>> piii;
const int N = 3e3 + 5, M = 20, mod = 1000000000;
const double EPS = 1e-10;

ll n, m, k;
ll A[N][N], mem[N][N][4];

ll solve(int x, int y, int rem) {
  if (x == n - 1 && y == m - 1) {
    if (rem)
      return A[x][y];
    return 0;
  }
  if (x == n || y == m) {
    return -INF;
  }

  ll &ret = mem[x][y][rem];
  if (~ret)
    return ret;

  if (rem) {
    ret = max(solve(x, y + 1, rem - 1), solve(x + 1, y, 3)) + A[x][y];
  }
  ret = max(ret, solve(x, y + 1, rem));
  ret = max(ret, solve(x + 1, y, 3));
  return ret;
}

int main() {
  FIO
          //    freopen("input.txt", "r", stdin);
          //    freopen("output.txt", "w", stdout);
          cin >>
      n >> m >> k;
  for (int i = 0; i < k; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    A[a - 1][b - 1] = c;
  }
  memset(mem, -1, sizeof mem);
  cout << solve(0, 0, 3);
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
template <typename key>
using ordered_set = tree<key, null_type, less<key>, rb_tree_tag,
                         tree_order_statistics_node_update>;
#define F first
#define S second
#define T second.first
#define D second.second
#define FIO                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);
#define all(v) v.begin(), v.end()
#define OO 0x3f3f3f3fLL
#define INF 0x3f3f3f3f3f3f3f3fLL
#define sz(s) (int)(s.size())
#define RT(s) return cout << s, 0
typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pii;
typedef pair<ll, pair<ll, ll>> piii;
const int N = 3e2 + 5, M = 20, mod = 1000000000;
const double EPS = 1e-10;

ll n, m, k;
ll A[N][N], mem[N][N][4];

ll solve(int x, int y, int rem) {
  if (x == n - 1 && y == m - 1) {
    if (rem)
      return A[x][y];
    return 0;
  }
  if (x == n || y == m) {
    return -INF;
  }

  ll &ret = mem[x][y][rem];
  if (~ret)
    return ret;

  if (rem) {
    ret = max(solve(x, y + 1, rem - 1), solve(x + 1, y, 3)) + A[x][y];
  }
  ret = max(ret, solve(x, y + 1, rem));
  ret = max(ret, solve(x + 1, y, 3));
  return ret;
}

int main() {
  FIO
          //    freopen("input.txt", "r", stdin);
          //    freopen("output.txt", "w", stdout);
          cin >>
      n >> m >> k;
  for (int i = 0; i < k; i++) {
    ll a, b, c;
    cin >> a >> b >> c;
    A[a - 1][b - 1] = c;
  }
  memset(mem, -1, sizeof mem);
  cout << solve(0, 0, 3);
}