Problem p02586 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define INF (int)1e9 + 4
#define INFL (ll)1e18 + 4
#define md (int)1e9 + 7
#define mx 10000000
#define mk make_pair
#define pi pair<int, int>
#define ss second
#define ff first
#define pb emplace_back
#define onell(x) __builtin_popcountll(x)
#define one(x) __builtin_popcount(x)
#define all(s) s.begin(), s.end()
#define rall(x) (x).rbegin(), (x).rend()
#define trav(x, a) for (auto &x : a)
#define fr(i, a, b) for (int i = a; i <= b; i++)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = a; i > b; i--)
#define rf(i, a, b) for (int i = a; i >= b; i--)
#define sz(x) ((long long)x.size())
#define mez(s) memset(s, 0, sizeof s)
#define mex(s) memset(s, -1, sizeof s)
#define metr(s) memset(s, true, sizeof s)
typedef vector<int> vi;
typedef vector<ll> vl;
typedef map<int, int> mp;
typedef queue<int> que;
typedef vector<pi> vp;
int n, m, k;
const int mxn = 350;
int gh[mxn][mxn];
ll dp[mxn][mxn][4];
ll dfs(int i, int j, int k) {
  if (i > n)
    return 0;
  if (j > m)
    return 0;
  ll ans = 0;
  if (dp[i][j][k] != -1)
    return dp[i][j][k];
  if (k > 0) {
    ans = max(ans, gh[i][j] + dfs(i, j + 1, k - 1));
    ans = max(ans, gh[i][j] + dfs(i + 1, j, 3));
  }
  ans = max(ans, dfs(i + 1, j, 3));
  ans = max(ans, dfs(i, j + 1, k));
  return dp[i][j][k] = ans;
}
void solve() {
  int x = 0, y = 0, z = 0;
  cin >> n >> m >> k;
  mex(dp), mez(gh);
  while (k--) {
    cin >> x >> y >> z;
    gh[x][y] = z;
  }
  ll ans = dfs(1, 1, 3);
  cout << ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define ld long double
#define INF (int)1e9 + 4
#define INFL (ll)1e18 + 4
#define md (int)1e9 + 7
#define mx 10000000
#define mk make_pair
#define pi pair<int, int>
#define ss second
#define ff first
#define pb emplace_back
#define onell(x) __builtin_popcountll(x)
#define one(x) __builtin_popcount(x)
#define all(s) s.begin(), s.end()
#define rall(x) (x).rbegin(), (x).rend()
#define trav(x, a) for (auto &x : a)
#define fr(i, a, b) for (int i = a; i <= b; i++)
#define rep(i, a, b) for (int i = a; i < b; i++)
#define per(i, a, b) for (int i = a; i > b; i--)
#define rf(i, a, b) for (int i = a; i >= b; i--)
#define sz(x) ((long long)x.size())
#define mez(s) memset(s, 0, sizeof s)
#define mex(s) memset(s, -1, sizeof s)
#define metr(s) memset(s, true, sizeof s)
typedef vector<int> vi;
typedef vector<ll> vl;
typedef map<int, int> mp;
typedef queue<int> que;
typedef vector<pi> vp;
int n, m, k;
const int mxn = 3010;
int gh[mxn][mxn];
ll dp[mxn][mxn][4];
ll dfs(int i, int j, int k) {
  if (i > n)
    return 0;
  if (j > m)
    return 0;
  ll ans = 0;
  if (dp[i][j][k] != -1)
    return dp[i][j][k];
  if (k > 0) {
    ans = max(ans, gh[i][j] + dfs(i, j + 1, k - 1));
    ans = max(ans, gh[i][j] + dfs(i + 1, j, 3));
  }
  ans = max(ans, dfs(i + 1, j, 3));
  ans = max(ans, dfs(i, j + 1, k));
  return dp[i][j][k] = ans;
}
void solve() {
  int x = 0, y = 0, z = 0;
  cin >> n >> m >> k;
  mex(dp), mez(gh);
  while (k--) {
    cin >> x >> y >> z;
    gh[x][y] = z;
  }
  ll ans = dfs(1, 1, 3);
  cout << ans;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int t = 1;
  // cin>>t;
  while (t--) {
    solve();
  }
  return 0;
}