Problem p02586 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define ll long long
#define all(v) (v).begin(), (v).end()
#define vi vector<ll>
#define vii vector<pair<ll, ll>>
#define ii pair<ll, ll>
#define sz(v) ((int)((v).size()))
#define lp(i, a, b) for (int i = a; i < b; i++)
#define pb push_back
#define pf push_front
#define F first
#define S second
#define endl "\n"
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
const double PI = 3.14159265358979323846;
const ll inf = 2e9, MOD = 998244353, MAX = 3e2 + 2;
int r, c, k, grid[MAX][MAX];
ll dp[MAX][MAX][4];

ll sol(int x, int y, int cnt) {
  if (x >= r || y >= c)
    return -1e15;
  if (x == r - 1 && y == c - 1) {
    if (cnt < 3)
      return grid[r - 1][c - 1];
    return 0;
  }
  ll &ret = dp[x][y][cnt];
  if (~ret)
    return ret;
  ret = 0;
  if (cnt < 3) {
    ret = max(sol(x, y + 1, cnt + (grid[x][y] != 0)) + grid[x][y],
              max(ret, sol(x + 1, y, 0) + grid[x][y]));
  }
  ret = max(ret, max(sol(x + 1, y, 0), sol(x, y + 1, cnt)));
  return ret;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  IOS;
  cin >> r >> c >> k;
  lp(i, 0, k) {
    int a, b, v;
    cin >> a >> b >> v;
    grid[a - 1][b - 1] = v;
  }
  memset(dp, -1, sizeof(dp));
  cout << sol(0, 0, 0);
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define ll long long
#define all(v) (v).begin(), (v).end()
#define vi vector<ll>
#define vii vector<pair<ll, ll>>
#define ii pair<ll, ll>
#define sz(v) ((int)((v).size()))
#define lp(i, a, b) for (int i = a; i < b; i++)
#define pb push_back
#define pf push_front
#define F first
#define S second
#define endl "\n"
#define IOS                                                                    \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
const double PI = 3.14159265358979323846;
const ll inf = 2e9, MOD = 998244353, MAX = 3e3 + 2;
int r, c, k, grid[MAX][MAX];
ll dp[MAX][MAX][4];

ll sol(int x, int y, int cnt) {
  if (x >= r || y >= c)
    return -1e15;
  if (x == r - 1 && y == c - 1) {
    if (cnt < 3)
      return grid[r - 1][c - 1];
    return 0;
  }
  ll &ret = dp[x][y][cnt];
  if (~ret)
    return ret;
  ret = 0;
  if (cnt < 3) {
    ret = max(sol(x, y + 1, cnt + (grid[x][y] != 0)) + grid[x][y],
              max(ret, sol(x + 1, y, 0) + grid[x][y]));
  }
  ret = max(ret, max(sol(x + 1, y, 0), sol(x, y + 1, cnt)));
  return ret;
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  IOS;
  cin >> r >> c >> k;
  lp(i, 0, k) {
    int a, b, v;
    cin >> a >> b >> v;
    grid[a - 1][b - 1] = v;
  }
  memset(dp, -1, sizeof(dp));
  cout << sol(0, 0, 0);
}