Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace __gnu_pbds;

#define fr(i, j, n) for (long long i = j; i < (n); ++i)
#define bk(i, j, n) for (long long i = j; i >= n; --i)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define endl "\n"
#define MOD 1000000007
#define debug1(x) cout << #x << " " << x << endl;
#define debug2(x, y) cout << #x << " " << x << " " << #y << " " << y << endl;
#define debug3(x, y, z)                                                        \
  cout << #x << " " << x << " " << #y << " " << y << " " << #z << " " << z     \
       << endl;
#define srt(x) sort(x.begin(), x.end());
#define run                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ordered_set                                                            \
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>

typedef long long ll;
ll modpower(ll a, ll b, ll c) {
  ll res = 1;
  while (b) {
    if (b & 1LL)
      res = (res * a) % c;
    a = (a * a) % c;
    b >>= 1;
  }
  return res;
}

ll dp[3005][3005];

int main() {
  run;
  string s, t;
  cin >> s >> t;
  ll n1 = s.length();
  ll n2 = t.length();
  fr(i, 0, n1) {
    fr(j, 0, n2) {
      if (s[i] == t[j]) {
        if (i && j)
          dp[i][j] = 1 + dp[i - 1][j - 1];
        else
          dp[i][j] = 1;
        // max(dp[i-1][j],dp[i][j-1]);
      }
      if (i)
        dp[i][j] = max(dp[i - 1][j], dp[i][j]);
      if (j)
        dp[i][j] = max(dp[i][j - 1], dp[i][j]);
    }
  }

  ll len = dp[n1 - 1][n2 - 1];
  ll i = n1 - 1, j = n2 - 1;
  string u;
  while (len) {
    if (s[i] == t[j]) {
      u.pb(s[i]);
      i--, j--, len--;
    } else if (dp[i - 1][j] > dp[i][j - 1]) {
      i--;
    } else {
      j--;
    }
  }
  if (!u.length())
    return 0;
  bk(i, u.length() - 1, 0) { cout << u[i]; }

  // cout<<dp[n1-1][n2-1];
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <iostream>
#include <stdio.h>

using namespace std;
using namespace __gnu_pbds;

#define fr(i, j, n) for (long long i = j; i < (n); ++i)
#define bk(i, j, n) for (long long i = j; i >= n; --i)
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define endl "\n"
#define MOD 1000000007
#define debug1(x) cout << #x << " " << x << endl;
#define debug2(x, y) cout << #x << " " << x << " " << #y << " " << y << endl;
#define debug3(x, y, z)                                                        \
  cout << #x << " " << x << " " << #y << " " << y << " " << #z << " " << z     \
       << endl;
#define srt(x) sort(x.begin(), x.end());
#define run                                                                    \
  ios_base::sync_with_stdio(0);                                                \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ordered_set                                                            \
  tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update>

typedef long long ll;
ll modpower(ll a, ll b, ll c) {
  ll res = 1;
  while (b) {
    if (b & 1LL)
      res = (res * a) % c;
    a = (a * a) % c;
    b >>= 1;
  }
  return res;
}

ll dp[3005][3005];

int main() {
  run;
  string s, t;
  cin >> s >> t;
  ll n1 = s.length();
  ll n2 = t.length();
  fr(i, 0, n1) {
    fr(j, 0, n2) {
      if (s[i] == t[j]) {
        if (i && j)
          dp[i][j] = 1 + dp[i - 1][j - 1];
        else
          dp[i][j] = 1;
        // max(dp[i-1][j],dp[i][j-1]);
      }
      if (i)
        dp[i][j] = max(dp[i - 1][j], dp[i][j]);
      if (j)
        dp[i][j] = max(dp[i][j - 1], dp[i][j]);
    }
  }

  ll len = dp[n1 - 1][n2 - 1];
  ll i = n1 - 1, j = n2 - 1;
  string u;
  while (len) {
    if (s[i] == t[j]) {
      u.pb(s[i]);
      i--, j--, len--;
    } else if (i && j && dp[i - 1][j] > dp[i][j - 1] || !j) {
      i--;
    } else {
      j--;
    }
  }
  if (!u.length())
    return 0;
  bk(i, u.length() - 1, 0) { cout << u[i]; }

  // cout<<dp[n1-1][n2-1];
}