Problem p02586 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define forn(i, a, b) for (int i = a; i < b; i++)
#define fi first
#define se second
#define fast ios_base::sync_with_stdio(false);
using namespace std;

// for debugging
/*
g++ -D_GLIBCXX_ASSERTIONS -DDEBUG -ggdb3 -std=c++14
*/
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x)                                                                 \\
  {                                                                            \\
    ++recur_depth;                                                             \\
    auto x_ = x;                                                               \\
    --recur_depth;                                                             \\
    cerr << string(recur_depth, '\\t') << "\\e[91m" << __func__ << ":"           \\
         << __LINE__ << "\\t" << #x << " = " << x_ << "\\e[39m" << endl;         \\
  }
#else
#define dbg(x)
#endif
template <typename Ostream, typename Cont>
typename enable_if<is_same<Ostream, ostream>::value, Ostream &>::type
operator<<(Ostream &os, const Cont &v) {
  os << "[";
  for (auto &x : v) {
    os << x << ", ";
  }
  return os << "]";
}
template <typename Ostream, typename... Ts>
Ostream &operator<<(Ostream &os, const pair<Ts...> &p) {
  return os << "{" << p.first << ", " << p.second << "}";
}
// debugging ends here

typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
const int mxn = 3e3 + 1;
// const int len = 1e7;
ll dp[mxn][mxn][3];
ll vals[mxn][mxn];
int r, c;

ll find(int x, int y, int xl) {
  if (x < 0 || x >= r)
    return 0ll;
  if (y < 0 || y >= c)
    return 0ll;
  ll &ans = dp[x][y][xl];
  if (ans != -1) {
    return ans;
  }
  ans = 0;
  if (vals[x][y] != 0) {
    ans = max(ans, vals[x][y] + find(x + 1, y, 2));
  }
  if (xl && vals[x][y] != 0) {
    ans = max(ans, vals[x][y] + find(x, y + 1, xl - 1));
  }
  ans = max(ans, max(find(x + 1, y, 2), find(x, y + 1, xl)));
  // cout << x << ' ' << y << " " << xl << " " << " " << ans << endl;
  return ans;
}

void solve() {
  int k;
  cin >> r >> c >> k;
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < k; i++) {
    int x, y, val;
    cin >> x >> y;
    --x, --y;
    cin >> vals[x][y];
  }

  ll ans = find(0, 0, 2);

  cout << ans << endl;
}

int main() {
  fast;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  // int t;cin >> t;while(t--)
  solve();
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define forn(i, a, b) for (int i = a; i < b; i++)
#define fi first
#define se second
#define fast ios_base::sync_with_stdio(false);
using namespace std;

// for debugging
/*
g++ -D_GLIBCXX_ASSERTIONS -DDEBUG -ggdb3 -std=c++14
*/
int recur_depth = 0;
#ifdef DEBUG
#define dbg(x)                                                                 \\
  {                                                                            \\
    ++recur_depth;                                                             \\
    auto x_ = x;                                                               \\
    --recur_depth;                                                             \\
    cerr << string(recur_depth, '\\t') << "\\e[91m" << __func__ << ":"           \\
         << __LINE__ << "\\t" << #x << " = " << x_ << "\\e[39m" << endl;         \\
  }
#else
#define dbg(x)
#endif
template <typename Ostream, typename Cont>
typename enable_if<is_same<Ostream, ostream>::value, Ostream &>::type
operator<<(Ostream &os, const Cont &v) {
  os << "[";
  for (auto &x : v) {
    os << x << ", ";
  }
  return os << "]";
}
template <typename Ostream, typename... Ts>
Ostream &operator<<(Ostream &os, const pair<Ts...> &p) {
  return os << "{" << p.first << ", " << p.second << "}";
}
// debugging ends here

typedef long long int ll;
typedef vector<int> vi;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
const int mxn = 3e3 + 1;
// const int len = 1e7;
ll dp[mxn][mxn][3];
map<pii, ll> vals;
int r, c;

ll find(int x, int y, int xl) {
  if (x < 0 || x >= r)
    return 0ll;
  if (y < 0 || y >= c)
    return 0ll;
  ll &ans = dp[x][y][xl];
  if (ans != -1) {
    return ans;
  }
  ans = 0;
  if (vals.find({x, y}) != vals.end()) {
    ans = max(ans, vals[{x, y}] + find(x + 1, y, 2));
  }
  if (xl && vals.find({x, y}) != vals.end()) {
    ans = max(ans, vals[{x, y}] + find(x, y + 1, xl - 1));
  }
  ans = max(ans, max(find(x + 1, y, 2), find(x, y + 1, xl)));
  // cout << x << ' ' << y << " " << xl << " " << " " << ans << endl;
  return ans;
}

void solve() {
  int k;
  cin >> r >> c >> k;
  memset(dp, -1, sizeof(dp));
  for (int i = 0; i < k; i++) {
    int x, y, val;
    cin >> x >> y;
    --x, --y;
    cin >> vals[{x, y}];
  }

  ll ans = find(0, 0, 2);

  cout << ans << endl;
}

int main() {
  fast;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif

  // int t;cin >> t;while(t--)
  solve();
}