Problem p03165 (C++)

Buggy submission (status: Time Limit Exceeded):
/*
    Author: Alam Khan
    AUST CSE 40th Batch

*/

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
typedef deque<ll> dl;
typedef stack<ll> stl;
typedef set<ll> sl;
typedef map<string, ll> msl;
typedef map<ll, ll> mll;
#define sf(n) scanf("%lld", &n)
#define sff(n, m) scanf("%lld %lld", &n, &m)
#define sfff(n, m, r) scanf("%lld %lld %lld", &n, &m, &r)
#define sfs(n) scanf("%s", n)
#define pf(n) printf("%lld\n", n)
#define pff(n, m) printf("%lld %lld\n", n, m)
#define pfff(n, m, r) printf("%lld %lld %lld\n", n, m, r)
#define pfs(n) printf("%s\n", n)
#define pfcs(i, n) printf("Case %lld: %lld\n", i, n)
#define pb push_back
#define prf printf
#define inf 2e18
#define low -1000000000000
#define PI acos(-1.0)
#define rep1(i, n) for (i = 1; i < n; i++)
#define rep0(i, n) for (i = 0; i < n; i++)
#define rep(i, a, n) for (i = a; i < n; i++)
#define repe1(i, n) for (i = 1; i <= n; i++)
#define repe0(i, n) for (i = 0; i <= n; i++)
#define repe(i, a, n) for (i = a; i <= n; i++)
#define endl "\n"
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define fr freopen("input.txt", "r", stdin)
#define fw freopen("output.txt", "w", stdout)
ll dp[3009][3009];
string a, b;

ll LCS(ll i, ll j) {
  if (i == 0 || j == 0) {
    return dp[i][j] = 0;
  }
  if (a[i - 1] == b[j - 1]) {
    return dp[i][j] = 1 + LCS(i - 1, j - 1);
  } else {
    return dp[i][j] = max(LCS(i - 1, j), LCS(i, j - 1));
  }
}
void path(ll i, ll j) {
  if (i == 0 || j == 0)
    return;
  if (a[i - 1] == b[j - 1]) {
    path(i - 1, j - 1);
    cout << a[i - 1];
  } else {
    if (dp[i][j] == dp[i - 1][j]) {
      path(i - 1, j);
    } else
      path(i, j - 1);
  }
}
int main() {
  ll i, n, t, k, j, x = 0, y = 0, m;
  cin >> a >> b;
  memset(dp, -1, sizeof dp);
  n = a.size();
  m = b.size();
  x = LCS(n, m);
  path(n, m);
  return 0;
}


Fixed submission:
/*
    Author: Alam Khan
    AUST CSE 40th Batch

*/

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef vector<ll> vl;
typedef deque<ll> dl;
typedef stack<ll> stl;
typedef set<ll> sl;
typedef map<string, ll> msl;
typedef map<ll, ll> mll;
#define sf(n) scanf("%lld", &n)
#define sff(n, m) scanf("%lld %lld", &n, &m)
#define sfff(n, m, r) scanf("%lld %lld %lld", &n, &m, &r)
#define sfs(n) scanf("%s", n)
#define pf(n) printf("%lld\n", n)
#define pff(n, m) printf("%lld %lld\n", n, m)
#define pfff(n, m, r) printf("%lld %lld %lld\n", n, m, r)
#define pfs(n) printf("%s\n", n)
#define pfcs(i, n) printf("Case %lld: %lld\n", i, n)
#define pb push_back
#define prf printf
#define inf 2e18
#define low -1000000000000
#define PI acos(-1.0)
#define rep1(i, n) for (i = 1; i < n; i++)
#define rep0(i, n) for (i = 0; i < n; i++)
#define rep(i, a, n) for (i = a; i < n; i++)
#define repe1(i, n) for (i = 1; i <= n; i++)
#define repe0(i, n) for (i = 0; i <= n; i++)
#define repe(i, a, n) for (i = a; i <= n; i++)
#define endl "\n"
#define FAST                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);
#define fr freopen("input.txt", "r", stdin)
#define fw freopen("output.txt", "w", stdout)
ll dp[3009][3009];
string a, b;

ll LCS(ll i, ll j) {
  if (dp[i][j] != -1) {
    return dp[i][j];
  }
  if (i == 0 || j == 0) {
    return dp[i][j] = 0;
  }
  if (a[i - 1] == b[j - 1]) {
    return dp[i][j] = 1 + LCS(i - 1, j - 1);
  } else {
    return dp[i][j] = max(LCS(i - 1, j), LCS(i, j - 1));
  }
}
void path(ll i, ll j) {
  if (i == 0 || j == 0)
    return;
  if (a[i - 1] == b[j - 1]) {
    path(i - 1, j - 1);
    cout << a[i - 1];
  } else {
    if (dp[i][j] == dp[i - 1][j]) {
      path(i - 1, j);
    } else
      path(i, j - 1);
  }
}
int main() {
  ll i, n, t, k, j, x = 0, y = 0, m;
  cin >> a >> b;
  memset(dp, -1, sizeof dp);
  n = a.size();
  m = b.size();
  x = LCS(n, m);
  path(n, m);
  return 0;
}