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

// ######      ###      #######    #######    ##     #     #####        ###
// ##### // #     #    #   #          #        #       # #    #    #     # #   #
// ###  // #     #   #     #        #         #       #  #   #   #       #    #
// #    ###  //
// ######   #########      #          #       #   #  #   #           #########
// #   // #     #  #       #     #           #       #    # #   #    ####   # #
// #   // #     #  #       #    #            #       #     ##   #    #  #   # #
// //
// ######   #       #   #######    #######    #      #    #####  #   #       #
// #   //

// #pragma GCC optimize(2)
// #pragma GCC optimize(3)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> ppl;
#define ain(a, n)                                                              \\
  for (ll i = 0; i < (n); ++i)                                                 \\
    cin >> (a)[i];
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define loop(i, n) for (ll i = 0; i < (n); ++i)
#define FOR(i, a, b) for (ll i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (ll i = (a); i >= (b); --i)
#define cases                                                                  \\
  ll T = 0;                                                                    \\
  cin >> T;                                                                    \\
  while (T--)
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define END "\\n"
#define pb push_back
#define mp make_pair
#define go(c, itr) for (auto itr = (c).begin(); itr != (c).end(); ++itr)
// #define back(c,itr) for(auto itr=(c).rbegin(); itr!=(c).rend(); ++itr)
#define PI 3.14159265359
#define inf 9e18
#define MOD 1000000007
#define MODU 998244353
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define MAXN 100005
#define BLOCK 555
const string alpha = "abcdefghijklmnopqrstuvwxyz";
const ll N = 200005;
const long double epsilon = 1e-9;
inline ll myceil(ll a, ll b) {
  if (a % b == 0)
    return (a / b);
  return (a / b + 1);
}
ll binexp(ll a, ll b, ll m) {
  a %= m;
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
ll modinvfermat(ll a, ll m) { return binexp(a, m - 2, m); }
void task(bool flag) {
  if (flag)
    cout << "YES\\n";
  else
    cout << "NO\\n";
}
ll lcm(ll a, ll b) { return ((1LL * a * b) / (__gcd(a, b))); }

ll n, m, k;
vvl pres;
vector<vvl> dp(3005, vvl(3005, vl(3, -1)));
ll solve(ll i, ll j, ll lim) {
  if (i > n || j > m || lim < 0)
    return 0;
  if (dp[i][j][lim] != -1)
    return dp[i][j][lim];
  ll res1 = 0, res2 = 0;
  // Take
  if (pres[i][j]) {
    res1 = pres[i][j] + max(solve(i, j + 1, lim - 1), solve(i + 1, j, 2));
  }
  // Leave
  res2 = max(solve(i, j + 1, lim), solve(i + 1, j, 2));
  return dp[i][j][lim] = max(res1, res2);
}

signed main() {
  // #ifndef ONLINE_JUDGE
  // 	freopen("input.txt", "r", stdin);
  // 	freopen("output.txt", "w", stdout);
  // #endif
  fast cin >> n >> m >> k;
  pres = vvl(n + 1, vl(m + 1, 0));
  loop(i, k) {
    ll x, y, v;
    cin >> x >> y >> v;
    pres[x][y] = v;
  }
  cout << solve(1, 1, 2) << END;

  // #ifndef ONLINE_JUDGE
  // 	cout << "\\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "
  // sec\\n"; #endif
  return 0;
}

Error: TLE

Fixed submission:

// ######      ###      #######    #######    ##     #     #####        ###
// ##### // #     #    #   #          #        #       # #    #    #     # #   #
// ###  // #     #   #     #        #         #       #  #   #   #       #    #
// #    ###  //
// ######   #########      #          #       #   #  #   #           #########
// #   // #     #  #       #     #           #       #    # #   #    ####   # #
// #   // #     #  #       #    #            #       #     ##   #    #  #   # #
// //
// ######   #       #   #######    #######    #      #    #####  #   #       #
// #   //

// #pragma GCC optimize(2)
// #pragma GCC optimize(3)
// #pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
// #pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double lld;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
typedef pair<ll, ll> ppl;
#define ain(a, n)                                                              \\
  for (ll i = 0; i < (n); ++i)                                                 \\
    cin >> (a)[i];
#define DEBUG(x) cout << '>' << #x << ':' << x << endl;
#define loop(i, n) for (ll i = 0; i < (n); ++i)
#define FOR(i, a, b) for (ll i = (a); i <= (b); ++i)
#define FORD(i, a, b) for (ll i = (a); i >= (b); --i)
#define cases                                                                  \\
  ll T = 0;                                                                    \\
  cin >> T;                                                                    \\
  while (T--)
#define ff first
#define ss second
#define all(v) v.begin(), v.end()
#define END "\\n"
#define pb push_back
#define mp make_pair
#define go(c, itr) for (auto itr = (c).begin(); itr != (c).end(); ++itr)
// #define back(c,itr) for(auto itr=(c).rbegin(); itr!=(c).rend(); ++itr)
#define PI 3.14159265359
#define inf 9e18
#define MOD 1000000007
#define MODU 998244353
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);
#define MAXN 100005
#define BLOCK 555
const string alpha = "abcdefghijklmnopqrstuvwxyz";
const ll N = 200005;
const long double epsilon = 1e-9;
inline ll myceil(ll a, ll b) {
  if (a % b == 0)
    return (a / b);
  return (a / b + 1);
}
ll binexp(ll a, ll b, ll m) {
  a %= m;
  ll res = 1;
  while (b > 0) {
    if (b & 1)
      res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
ll modinvfermat(ll a, ll m) { return binexp(a, m - 2, m); }
void task(bool flag) {
  if (flag)
    cout << "YES\\n";
  else
    cout << "NO\\n";
}
ll lcm(ll a, ll b) { return ((1LL * a * b) / (__gcd(a, b))); }

ll n, m, k;
map<ppl, ll> pres;
vector<vvl> dp(3005, vvl(3005, vl(3, -1)));
ll solve(ll i, ll j, ll lim) {
  if (i > n || j > m || lim < 0)
    return 0;
  if (dp[i][j][lim] != -1)
    return dp[i][j][lim];
  ll res1 = 0, res2 = 0;
  // Take
  if (pres[mp(i, j)]) {
    res1 = pres[mp(i, j)] + max(solve(i, j + 1, lim - 1), solve(i + 1, j, 2));
  }
  // Leave
  res2 = max(solve(i, j + 1, lim), solve(i + 1, j, 2));
  return dp[i][j][lim] = max(res1, res2);
}

signed main() {
  // #ifndef ONLINE_JUDGE
  // 	freopen("input.txt", "r", stdin);
  // 	freopen("output.txt", "w", stdout);
  // #endif
  fast cin >> n >> m >> k;
  loop(i, k) {
    ll x, y, v;
    cin >> x >> y >> v;
    pres[mp(x, y)] = v;
  }
  cout << solve(1, 1, 2) << END;

  // #ifndef ONLINE_JUDGE
  // 	cout << "\\nTime Elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "
  // sec\\n"; #endif
  return 0;
}