prithivMLmods/Coder-Stat huggingface dataset:
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define pli pair<ll, int>
#define pil pair<int, ll>
#define mod 1000000007
#define mod1 1000000009
#define mod2 1000000021
#define INF 500001
#define MAX 1000000001
using namespace std;

/* 🐣🐥 */
int dp[3002][3002], cp;
char ans[3002];
int main() {
  char a[3002], b[3002];
  int al, bl, ap, bp;
  scanf("%s %s", a + 1, b + 1);
  al = strlen(a + 1), bl = strlen(b + 1);
  for (int i = 1; i <= al; i++) {
    for (int j = 1; j <= bl; j++) {
      dp[i][j] =
          max({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1] + (a[i] == b[j])});
    }
  }
  ap = al, bp = bl;
  while (ap >= 0 && bp >= 0) {
    if (dp[ap - 1][bp] == dp[ap][bp])
      ap--;
    else if (dp[ap][bp - 1] == dp[ap][bp])
      bp--;
    else {
      ans[cp++] = a[ap];
      ap--, bp--;
    }
  }

  for (int i = cp - 1; i >= 0; i--)
    printf("%c", ans[i]);

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define ll long long
#define pll pair<ll, ll>
#define pii pair<int, int>
#define pli pair<ll, int>
#define pil pair<int, ll>
#define mod 1000000007
#define mod1 1000000009
#define mod2 1000000021
#define INF 500001
#define MAX 1000000001
using namespace std;

/* 🐣🐥 */
int dp[3002][3002], cp;
char ans[3002];
int main() {
  char a[3002], b[3002];
  int al, bl, ap, bp;
  scanf("%s %s", a + 1, b + 1);
  al = strlen(a + 1), bl = strlen(b + 1);
  for (int i = 1; i <= al; i++) {
    for (int j = 1; j <= bl; j++) {
      dp[i][j] =
          max({dp[i - 1][j], dp[i][j - 1], dp[i - 1][j - 1] + (a[i] == b[j])});
    }
  }
  ap = al, bp = bl;
  while (ap >= 1 && bp >= 1) {
    if (dp[ap - 1][bp] == dp[ap][bp])
      ap--;
    else if (dp[ap][bp - 1] == dp[ap][bp])
      bp--;
    else {
      ans[cp++] = a[ap];
      ap--, bp--;
    }
  }

  for (int i = cp - 1; i >= 0; i--)
    printf("%c", ans[i]);

  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define MAX 3000
#define ll long long
#define mod (1000000000 + 7)
#define Fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);

using namespace std;

int T[MAX][MAX];

string LCS(string X, string Y, int m, int n) {
  if (m == 0 || n == 0)
    return string("");
  if (X[m - 1] == Y[n - 1]) {
    return LCS(X, Y, m - 1, n - 1) + X[m - 1];
  }
  if (T[m - 1][n] > T[m][n - 1])
    return LCS(X, Y, m - 1, n);

  else
    return LCS(X, Y, m, n - 1);
}

int main() {
  string s, t;
  cin >> s >> t;
  int l = s.length();
  int m = t.length();
  for (int j = 0; j <= m; j++)
    T[0][j] = 0;
  for (int i = 0; i <= l; i++)
    T[i][0] = 0;
  for (int i = 1; i <= l; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i - 1] == t[j - 1])
        T[i][j] = T[i - 1][j - 1] + 1;
      else
        T[i][j] = max(T[i - 1][j], T[i][j - 1]);
    }
  }
  cout << LCS(s, t, l, m);
}


Fixed submission:
#include <bits/stdc++.h>
#define MAX 3010
#define ll long long
#define mod (1000000000 + 7)
#define Fast                                                                   \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(NULL);

using namespace std;

int T[MAX][MAX];

string LCS(string X, string Y, int m, int n) {
  if (m == 0 || n == 0)
    return string("");
  if (X[m - 1] == Y[n - 1]) {
    return LCS(X, Y, m - 1, n - 1) + X[m - 1];
  }
  if (T[m - 1][n] > T[m][n - 1])
    return LCS(X, Y, m - 1, n);

  else
    return LCS(X, Y, m, n - 1);
}

int main() {
  string s, t;
  cin >> s >> t;
  int l = s.length();
  int m = t.length();
  for (int j = 0; j <= m; j++)
    T[0][j] = 0;
  for (int i = 0; i <= l; i++)
    T[i][0] = 0;
  for (int i = 1; i <= l; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i - 1] == t[j - 1])
        T[i][j] = T[i - 1][j - 1] + 1;
      else
        T[i][j] = max(T[i - 1][j], T[i][j - 1]);
    }
  }
  cout << LCS(s, t, l, m);
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define mkp make_pair
#define pr(num) cout << num << "\n"
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define INF 1000000000000000000
#define MOD 1000000007LL
#define MAX 100100
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef map<ll, ll> Map;

string s, t;
ll dp[3003][3003];

void printLcs(ll i, ll j) {
  if (i == 0 && j == 0)
    return;

  if (s[i - 1] == t[j - 1]) {
    printLcs(i - 1, j - 1);
    cout << s[i - 1];
  } else if (dp[i - 1][j] >= dp[i][j - 1]) {
    printLcs(i - 1, j);
  } else {
    printLcs(i, j - 1);
  }
}

int main(void) {
  cin >> s >> t;
  ll i, j;
  ll slen = s.size();
  ll tlen = t.size();

  for (i = 1; i <= slen; i++) {
    for (j = 1; j <= tlen; j++) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }

  printLcs(slen, tlen);
  cout << "\n";
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
#define mkp make_pair
#define pr(num) cout << num << "\n"
#define max(a, b) ((a) > (b) ? (a) : (b))
#define min(a, b) ((a) < (b) ? (a) : (b))
#define INF 1000000000000000000
#define MOD 1000000007LL
#define MAX 100100
using namespace std;
typedef long long ll;
typedef pair<ll, ll> P;
typedef map<ll, ll> Map;

string s, t;
ll dp[3003][3003];

void printLcs(ll i, ll j) {
  if (i == 0 || j == 0)
    return;

  if (s[i - 1] == t[j - 1]) {
    printLcs(i - 1, j - 1);
    cout << s[i - 1];
  } else if (dp[i - 1][j] >= dp[i][j - 1]) {
    printLcs(i - 1, j);
  } else {
    printLcs(i, j - 1);
  }
}

int main(void) {
  cin >> s >> t;
  ll i, j;
  ll slen = s.size();
  ll tlen = t.size();

  for (i = 1; i <= slen; i++) {
    for (j = 1; j <= tlen; j++) {
      if (s[i - 1] == t[j - 1])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      else
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
    }
  }

  printLcs(slen, tlen);
  cout << "\n";
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
/*COMPETITIVE PROGRAMMING C++ TEMPLATE */

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " = " << x << "\n"
#define MOD 1000000007

const long double PI = 3.141592653589793236L;

typedef long long int ll;
typedef long double ld;

using namespace std;

void solve() {
  string s;
  string t;
  cin >> s >> t;

  int n = s.size();
  int m = t.size();

  vector<vector<int>> dp(n + 1, vector<int>(m));

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }

  string ans = "";

  int i = n, j = m;

  while (i && j) {
    if (s[i - 1] == t[j - 1]) {
      ans += s[i - 1];
      i--;
      j--;
    } else {
      if (dp[i][j - 1] > dp[i - 1][j]) {
        j--;
      } else {
        i--;
      }
    }
  }

  reverse(all(ans));

  cout << ans << "\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int T = 1;
  // cin >> T;

  while (T--) {
    solve();
  }

  return 0;
}


Fixed submission:
/*COMPETITIVE PROGRAMMING C++ TEMPLATE */

#include <algorithm>
#include <climits>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#define all(x) x.begin(), x.end()
#define debug(x) cout << #x << " = " << x << "\n"
#define MOD 1000000007

const long double PI = 3.141592653589793236L;

typedef long long int ll;
typedef long double ld;

using namespace std;

void solve() {
  string s;
  string t;
  cin >> s >> t;

  int n = s.size();
  int m = t.size();

  vector<vector<int>> dp(n + 1, vector<int>(m + 1));

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }

  string ans = "";

  int i = n, j = m;

  while (i && j) {
    if (s[i - 1] == t[j - 1]) {
      ans += s[i - 1];
      i--;
      j--;
    } else {
      if (dp[i][j - 1] > dp[i - 1][j]) {
        j--;
      } else {
        i--;
      }
    }
  }

  reverse(all(ans));

  cout << ans << "\n";
}

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  int T = 1;
  // cin >> T;

  while (T--) {
    solve();
  }

  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
/* Simplicity and Goodness */

#include <bits/stdc++.h>
using namespace std;
#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define pri(n) printf("%d ", (int)(n))
#define prin(n) printf("%d\n", (int)(n))
#define lpri(n) printf("%lld ", n)
#define lprin(n) printf("%lld\n", n)
#define deb(x) cout << #x << " = " << x << ' ';
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define tc                                                                     \
  int tt;                                                                      \
  scn(tt);                                                                     \
  while (tt--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
using ll = long long;
const int inf = INT_MAX;
const int ninf = INT_MIN;
const int mod = 998244353;
const int N = 2e5 + 2;

int dp[1002][1002];

int main() {
  string s, ts;
  cin >> s >> ts;

  int n = (int)s.size(), m = (int)ts.size();

  dp[0][0] = 0;

  rep(i, 1, n + 1) {
    rep(j, 1, m + 1) {
      if (s[i - 1] == ts[j - 1]) {
        dp[i][j] = 1 + dp[i - 1][j - 1];
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }

  string ans = "";

  int i = n, j = m;
  while (i > 0 and j > 0) {
    if (dp[i][j] > dp[i - 1][j] and dp[i][j] > dp[i][j - 1]) {
      ans += s[i - 1];
      j--;
      i--;
    } else if (dp[i][j] == dp[i - 1][j]) {
      i--;
    } else {
      j--;
    }
  }

  reverse(ans.begin(), ans.end());
  cout << ans;
  return 0;
}

Fixed submission:
/* Simplicity and Goodness */

#include <bits/stdc++.h>
using namespace std;
#define scn(n) scanf("%d", &n)
#define lscn(n) scanf("%lld", &n)
#define pri(n) printf("%d ", (int)(n))
#define prin(n) printf("%d\n", (int)(n))
#define lpri(n) printf("%lld ", n)
#define lprin(n) printf("%lld\n", n)
#define deb(x) cout << #x << " = " << x << ' ';
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define tc                                                                     \
  int tt;                                                                      \
  scn(tt);                                                                     \
  while (tt--)
#define pb push_back
#define mp make_pair
#define F first
#define S second
using ll = long long;
const int inf = INT_MAX;
const int ninf = INT_MIN;
const int mod = 998244353;
const int N = 2e5 + 2;

int dp[3002][3002];

int main() {
  string s, ts;
  cin >> s >> ts;

  int n = (int)s.size(), m = (int)ts.size();

  dp[0][0] = 0;

  rep(i, 1, n + 1) {
    rep(j, 1, m + 1) {
      if (s[i - 1] == ts[j - 1]) {
        dp[i][j] = 1 + dp[i - 1][j - 1];
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }

  string ans = "";

  int i = n, j = m;
  while (i > 0 and j > 0) {
    if (dp[i][j] > dp[i - 1][j] and dp[i][j] > dp[i][j - 1]) {
      ans += s[i - 1];
      j--;
      i--;
    } else if (dp[i][j] == dp[i - 1][j]) {
      i--;
    } else {
      j--;
    }
  }

  reverse(ans.begin(), ans.end());
  cout << ans;
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

string lcs(string s1, string s2) {
  string ret;
  int n = s1.length(), m = s2.length();
  vector<vector<int>> dp(n + 1, vector<int>(m + 1));
  for (int i = 0; i < n; i++) {
    dp[i][0] = 0;
  }
  for (int i = 0; i < m; i++) {
    dp[0][i] = 0;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s1[i - 1] == s2[j - 1]) {
        dp[i][j] = 1 + dp[i - 1][j - 1];
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  int i = n, j = m;
  while (i > 0 && j > 0) {
    while (dp[i][j] == dp[i][j - 1])
      j--;
    while (dp[i][j] == dp[i - 1][j])
      i--;
    ret.push_back(s2[j - 1]);
    i--;
    j--;
  }
  reverse(ret.begin(), ret.end());
  return ret;
}

int main() {
  string s1, s2;
  cin >> s1 >> s2;
  cout << lcs(s1, s2);
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

string lcs(string s1, string s2) {
  string ret;
  int n = s1.length(), m = s2.length();
  vector<vector<int>> dp(n + 1, vector<int>(m + 1));
  for (int i = 0; i < n; i++) {
    dp[i][0] = 0;
  }
  for (int i = 0; i < m; i++) {
    dp[0][i] = 0;
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (s1[i - 1] == s2[j - 1]) {
        dp[i][j] = 1 + dp[i - 1][j - 1];
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  int i = n, j = m;
  while (ret.length() < dp[n][m]) {
    while (dp[i][j] == dp[i][j - 1])
      j--;
    while (dp[i][j] == dp[i - 1][j])
      i--;
    ret.push_back(s2[j - 1]);
    i--;
    j--;
  }
  reverse(ret.begin(), ret.end());
  return ret;
}

int main() {
  string s1, s2;
  cin >> s1 >> s2;
  cout << lcs(s1, s2);
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s, t;
  cin >> s >> t;
  ll dp[t.size() + 1][s.size() + 1];
  for (ll i = 0; i <= s.size(); i++)
    dp[0][i] = 0;
  for (ll i = 0; i <= t.size(); i++)
    dp[i][0] = 0;
  for (ll i = 1; i <= t.size(); i++) {
    for (ll j = 1; j <= s.size(); j++) {
      if (t[i - 1] == s[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  vector<char> ans;
  if (dp[t.size()][s.size()] == 0) {
    cout << "";
    return 0;
  }
  ll h = s.size(), v = t.size();
  while (h != 0 || v != 0) {
    if (s[h - 1] == t[v - 1]) {
      ans.push_back(s[h - 1]);
      h--;
      v--;
    } else if (dp[v][h - 1] > dp[v - 1][h]) {
      h--;
    } else {
      v--;
    }
  }
  for (ll i = ans.size() - 1; i >= 0; i--)
    cout << ans[i];
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  string s, t;
  cin >> s >> t;
  ll dp[t.size() + 1][s.size() + 1];
  for (ll i = 0; i <= s.size(); i++)
    dp[0][i] = 0;
  for (ll i = 0; i <= t.size(); i++)
    dp[i][0] = 0;
  for (ll i = 1; i <= t.size(); i++) {
    for (ll j = 1; j <= s.size(); j++) {
      if (t[i - 1] == s[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }
  vector<char> ans;
  if (dp[t.size()][s.size()] == 0) {
    cout << "";
    return 0;
  }
  ll h = s.size(), v = t.size();
  while (h != 0 && v != 0) {
    if (s[h - 1] == t[v - 1]) {
      ans.push_back(s[h - 1]);
      h--;
      v--;
    } else if (dp[v][h - 1] > dp[v - 1][h]) {
      h--;
    } else {
      v--;
    }
  }
  for (ll i = ans.size() - 1; i >= 0; i--)
    cout << ans[i];
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define fore(i, s, e) for (int i = s; i < e; i++)

typedef long long ll;

const int N = 3005;

ll dp[N][N];
string s, t, r;

ll calc(int i, int j) {
  if (i == s.size() || j == t.size())
    return 0;

  ll &r = dp[i][j];

  if (r != -1)
    return r;

  if (s[i] == t[j])
    return r = 1 + calc(i + 1, j + 1);
  else
    return r = max(calc(i + 1, j), calc(i, j + 1));
}

int main() {
  cin >> s >> t;
  memset(dp, -1, sizeof dp);
  calc(0, 0);
  int i = 0, j = 0;
  if (dp[0][0] != 0)
    while (i < s.size() || j < t.size()) {
      if (s[i] == t[j])
        r += s[i], i++, j++;
      else if (dp[i + 1][j] > dp[i][j + 1])
        i++;
      else
        j++;
    }
  cout << r << endl;
  return 0;
}

// https://atcoder.jp/contests/dp/tasks/dp_f

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define fore(i, s, e) for (int i = s; i < e; i++)

typedef long long ll;

const int N = 3005;

ll dp[N][N];
string s, t, r;

ll calc(int i, int j) {
  if (i == s.size() || j == t.size())
    return 0;

  ll &r = dp[i][j];

  if (r != -1)
    return r;

  if (s[i] == t[j])
    return r = 1 + calc(i + 1, j + 1);
  else
    return r = max(calc(i + 1, j), calc(i, j + 1));
}

int main() {
  cin >> s >> t;
  memset(dp, -1, sizeof dp);
  calc(0, 0);
  int i = 0, j = 0;
  while (i < s.size() && j < t.size()) {
    if (s[i] == t[j])
      r += s[i], i++, j++;
    else if (dp[i + 1][j] > dp[i][j + 1])
      i++;
    else
      j++;
  }
  cout << r << endl;
  return 0;
}

// https://atcoder.jp/contests/dp/tasks/dp_f
Problem p03165 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define sz(x) (int)x.size()
#define yes cout << "Yes\n"
#define no cout << "No\n"
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define ft first
#define sd second
typedef pair<int, int> pii;
typedef long double ld;
typedef vector<int> vi;
typedef vector<bool> vb;
mt19937_64 rnd(time(0));

void start() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // cout << fixed << setprecision(10);
  //  cerr << fixed << setprecision(10);
#ifdef MY
  freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
#endif
}

signed main() {
  start();
  string s, t;
  cin >> s >> t;

  vector<vi> d(sz(s) + 1, vi(sz(t) + 1));
  s = ' ' + s;
  t = ' ' + t;
  // d[i][j] - длина наибольшей общей подпоследовательности где взяли
  //  <= i элементов из s и <= j элементов из t
  //  i

  for (int i = 1; i < sz(s); ++i) {
    for (int j = 1; j < sz(t); ++j) {
      d[i][j] = max(d[i - 1][j], d[i][j - 1]);
      if (s[i] == t[j])
        d[i][j] = max(d[i][j], d[i - 1][j - 1] + 1);
    }
  }
  int x = sz(s) - 1, y = sz(t) - 1;
  if (d[x][y]) {
    string ans;
    while (x && y) {
      if (s[x] == t[y]) {
        ans.push_back(s[x]);
        x--;
        y--;
      } else {
        if (d[x][y] == d[x - 1][y]) {
          x--;
        } else {
          y--;
        }
      }
    }
    reverse(all(ans));
    cout << ans;
  }
  for (int i = 0; i < sz(s); ++i) {
    for (int j = 0; j < sz(t); ++j)
      cerr << d[i][j] << ' ';
    cerr << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define int long long
#define sz(x) (int)x.size()
#define yes cout << "Yes\n"
#define no cout << "No\n"
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define ft first
#define sd second
typedef pair<int, int> pii;
typedef long double ld;
typedef vector<int> vi;
typedef vector<bool> vb;
mt19937_64 rnd(time(0));

void start() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  // cout << fixed << setprecision(10);
  //  cerr << fixed << setprecision(10);
#ifdef MY
  freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
#endif
}

signed main() {
  start();
  string s, t;
  cin >> s >> t;

  vector<vi> d(sz(s) + 1, vi(sz(t) + 1));
  s = ' ' + s;
  t = ' ' + t;
  // d[i][j] - длина наибольшей общей подпоследовательности где взяли
  //  <= i элементов из s и <= j элементов из t
  //  i

  for (int i = 1; i < sz(s); ++i) {
    for (int j = 1; j < sz(t); ++j) {
      d[i][j] = max(d[i - 1][j], d[i][j - 1]);
      if (s[i] == t[j])
        d[i][j] = max(d[i][j], d[i - 1][j - 1] + 1);
    }
  }
  int x = sz(s) - 1, y = sz(t) - 1;
  if (d[x][y]) {
    string ans;
    while (x && y) {
      if (s[x] == t[y]) {
        ans.push_back(s[x]);
        x--;
        y--;
      } else {
        if (d[x][y] == d[x - 1][y]) {
          x--;
        } else {
          y--;
        }
      }
    }
    reverse(all(ans));
    cout << ans;
  }
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define maxn 1005

using namespace std;
char s[maxn], t[maxn], ans[maxn];
int n, m, cc, dp[maxn][maxn], px[maxn][maxn], py[maxn][maxn];

void dfs(int x, int y) {
  if (x == 0 && y == 0)
    return;
  int xx = px[x][y], yy = py[x][y];
  if (xx < x && yy < y)
    ans[++cc] = s[xx];
  dfs(xx, yy);
}

int main() {
  cin >> s >> t;
  n = strlen(s);
  m = strlen(t);
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= m; j++) {
      if (!i && !j)
        continue;
      dp[i][j] = -1;
      if (i > 0 && dp[i][j] < dp[i - 1][j]) {
        px[i][j] = i - 1;
        py[i][j] = j;
        dp[i][j] = dp[i - 1][j];
      }
      if (j > 0 && dp[i][j] < dp[i][j - 1]) {
        px[i][j] = i;
        py[i][j] = j - 1;
        dp[i][j] = dp[i][j - 1];
      }
      if (i > 0 && j > 0 &&
          dp[i][j] < dp[i - 1][j - 1] + (s[i - 1] == t[j - 1])) {
        px[i][j] = i - 1;
        py[i][j] = j - 1;
        dp[i][j] = dp[i - 1][j - 1] + (s[i - 1] == t[j - 1]);
      }
    }
  dfs(n, m);
  for (int i = cc; i; i--)
    printf("%c", ans[i]);
  puts("");
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define maxn 3005

using namespace std;
char s[maxn], t[maxn], ans[maxn];
int n, m, cc, dp[maxn][maxn], px[maxn][maxn], py[maxn][maxn];

void dfs(int x, int y) {
  if (x == 0 && y == 0)
    return;
  int xx = px[x][y], yy = py[x][y];
  if (xx < x && yy < y)
    ans[++cc] = s[xx];
  dfs(xx, yy);
}

int main() {
  cin >> s >> t;
  n = strlen(s);
  m = strlen(t);
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= m; j++) {
      if (!i && !j)
        continue;
      dp[i][j] = -1;
      if (i > 0 && dp[i][j] < dp[i - 1][j]) {
        px[i][j] = i - 1;
        py[i][j] = j;
        dp[i][j] = dp[i - 1][j];
      }
      if (j > 0 && dp[i][j] < dp[i][j - 1]) {
        px[i][j] = i;
        py[i][j] = j - 1;
        dp[i][j] = dp[i][j - 1];
      }
      if (i > 0 && j > 0 &&
          dp[i][j] < dp[i - 1][j - 1] + (s[i - 1] == t[j - 1])) {
        px[i][j] = i - 1;
        py[i][j] = j - 1;
        dp[i][j] = dp[i - 1][j - 1] + (s[i - 1] == t[j - 1]);
      }
    }
  dfs(n, m);
  for (int i = cc; i; i--)
    printf("%c", ans[i]);
  puts("");
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;

string s1, s2;
int dp[3000][3000];
pii prv[3000][3000];
string ans;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> s1 >> s2;
  for (int i = 1; i <= s1.size(); i++)
    for (int j = 1; j <= s2.size(); j++) {
      if (s1[i - 1] == s2[j - 1])
        dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
      else
        dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);

      if (dp[i][j] == dp[i - 1][j - 1] + 1)
        prv[i][j] = {i - 1, j - 1};
      if (dp[i][j] == dp[i][j - 1])
        prv[i][j] = {i, j - 1};
      if (dp[i][j] == dp[i - 1][j])
        prv[i][j] = {i - 1, j};
    }

  int pos1 = s1.size(), pos2 = s2.size();
  while (pos1 != 0 && pos2 != 0) {
    if (prv[pos1][pos2] == pii(pos1 - 1, pos2 - 1)) {
      ans += s1[pos1 - 1];
      pos1--, pos2--;
    } else if (prv[pos1][pos2] == pii(pos1 - 1, pos2))
      pos1--;
    else if (prv[pos1][pos2] == pii(pos1, pos2 - 1))
      pos2--;
  }
  reverse(ans.begin(), ans.end());
  cout << ans << '\n';
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef pair<ll, ll> pll;

string s1, s2;
int dp[3001][3001];
pii prv[3001][3001];
string ans;

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cin >> s1 >> s2;
  for (int i = 1; i <= s1.size(); i++)
    for (int j = 1; j <= s2.size(); j++) {
      if (s1[i - 1] == s2[j - 1])
        dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
      else
        dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]);

      if (dp[i][j] == dp[i - 1][j - 1] + 1)
        prv[i][j] = {i - 1, j - 1};
      if (dp[i][j] == dp[i][j - 1])
        prv[i][j] = {i, j - 1};
      if (dp[i][j] == dp[i - 1][j])
        prv[i][j] = {i - 1, j};
    }

  int pos1 = s1.size(), pos2 = s2.size();
  while (pos1 != 0 && pos2 != 0) {
    if (prv[pos1][pos2] == pii(pos1 - 1, pos2 - 1)) {
      ans += s1[pos1 - 1];
      pos1--, pos2--;
    } else if (prv[pos1][pos2] == pii(pos1 - 1, pos2))
      pos1--;
    else if (prv[pos1][pos2] == pii(pos1, pos2 - 1))
      pos2--;
  }
  reverse(ans.begin(), ans.end());
  cout << ans << '\n';
  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int main(int argc, char *argv[]) {
  string s, t;
  cin >> s;
  cin >> t;
  // int dp[3003][3003];
  int dp[3003][300];

  std::fill(dp[0], dp[0] + 3003, 0);
  for (int i = 1; i <= s.size(); i++) {
    std::fill(dp[i], dp[i] + 300, 0);
    for (int j = 1; j <= t.size(); j++) {
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }

  int i = s.size(), j = t.size();
  string ret = "";
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      ret += s[i - 1];
      i--, j--;
    } else if (dp[i][j - 1] > dp[i - 1][j]) {
      j--;
    } else {
      i--;
    }
  }

  reverse(ret.begin(), ret.end());
  puts(ret.c_str());

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

int main(int argc, char *argv[]) {
  string s, t;
  cin >> s;
  cin >> t;
  int dp[3003][3003];

  std::fill(dp[0], dp[0] + 3003, 0);
  for (int i = 1; i <= s.size(); i++) {
    std::fill(dp[i], dp[i] + 300, 0);
    for (int j = 1; j <= t.size(); j++) {
      if (s[i - 1] == t[j - 1]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      } else {
        dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
      }
    }
  }

  int i = s.size(), j = t.size();
  string ret = "";
  while (i > 0 && j > 0) {
    if (s[i - 1] == t[j - 1]) {
      ret += s[i - 1];
      i--, j--;
    } else if (dp[i][j - 1] > dp[i - 1][j]) {
      j--;
    } else {
      i--;
    }
  }

  reverse(ret.begin(), ret.end());
  puts(ret.c_str());

  return 0;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define umap unordered_map
#define flush fflush(stdout)
#define all(v) v.begin(), v.end()
#define st first
#define nd second
#define ln '\n'
#define MAX 1000000009
#define MOD 1000000007
#define N 3005
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
string s1, s2;
int LCS[N][N];
int main() {
#ifndef ONLINE_JUDGE
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> s1 >> s2;
  for (int i = 0; i < s1.size(); i++) {
    for (int j = 0; j < s2.size(); j++) {
      if (s1[i] == s2[j]) {
        LCS[i][j] = 1 + (j == 0 || i == 0 ? 0 : LCS[i - 1][j - 1]);
      } else {
        LCS[i][j] =
            max((i == 0 ? 0 : LCS[i - 1][j]), (j == 0 ? 0 : LCS[i][j - 1]));
      }
    }
  }
  int i = s1.size() - 1, j = s2.size() - 1;
  string ans = "";
  while (i != -1 && j != -1) {
    if (s1[i] == s2[j]) {
      ans += s1[i];
      i--;
      j--;
    } else if (LCS[i - 1][j] > LCS[i][j - 1])
      i--;
    else
      j--;
  }
  reverse(all(ans));
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define umap unordered_map
#define flush fflush(stdout)
#define all(v) v.begin(), v.end()
#define st first
#define nd second
#define ln '\n'
#define MAX 1000000009
#define MOD 1000000007
#define N 3005
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
string s1, s2;
int LCS[N][N];
int main() {
#ifndef ONLINE_JUDGE
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> s1 >> s2;
  for (int i = 0; i < s1.size(); i++) {
    for (int j = 0; j < s2.size(); j++) {
      if (s1[i] == s2[j]) {
        LCS[i][j] = 1 + (j == 0 || i == 0 ? 0 : LCS[i - 1][j - 1]);
      } else {
        LCS[i][j] =
            max((i == 0 ? 0 : LCS[i - 1][j]), (j == 0 ? 0 : LCS[i][j - 1]));
      }
    }
  }
  int i = s1.size() - 1, j = s2.size() - 1;
  string ans = "";
  while (i != -1 && j != -1) {
    if (s1[i] == s2[j]) {
      ans += s1[i];
      i--;
      j--;
    } else if ((i == 0 ? 0 : LCS[i - 1][j]) > (j == 0 ? 0 : LCS[i][j - 1]))
      i--;
    else
      j--;
  }
  reverse(all(ans));
  cout << ans;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
//                                                   ¯\_(ツ)_/¯
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>

#define pb push_back
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define ll long long
#define pi pair<int, int>
const int N = 1e5, MOD = 998244353;

using namespace std;

string a, b;
int dp[3005][3005];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
#ifndef ONLINE_JUDGE
  freopen("tst.txt", "r", stdin);
#endif

  cin >> a >> b;

  for (int i = 0; i < sz(a); i++) {

    for (int j = 0; j < sz(b); j++) {

      if (a[i] == b[j])
        dp[i + 1][j + 1] = 1 + dp[i][j];
      else
        dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
    }
  }

  string ans;

  pair<int, int> curr = {sz(a), sz(b)};

  while (curr.first != 0 && curr.second != 0) {

    int i = curr.first, j = curr.second;

    if (a[i - 1] == b[j - 1])
      ans.pb(a[i - 1]), curr = {i - 1, j - 1};
    else {
      if (dp[i - 1][j] > dp[i][j - 1])
        curr = {i - 1, j};
      else
        curr = {i, j - 1};
    }
  }

  reverse(all(ans));

  cout << ans << '\n';
}

Fixed submission:
//                                                   ¯\_(ツ)_/¯
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>

#define pb push_back
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(), (x).end()
#define ll long long
#define pi pair<int, int>
const int N = 1e5, MOD = 998244353;

using namespace std;

string a, b;
int dp[3005][3005];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);

  cin >> a >> b;

  for (int i = 0; i < sz(a); i++) {

    for (int j = 0; j < sz(b); j++) {

      if (a[i] == b[j])
        dp[i + 1][j + 1] = 1 + dp[i][j];
      else
        dp[i + 1][j + 1] = max(dp[i][j + 1], dp[i + 1][j]);
    }
  }

  string ans;

  pair<int, int> curr = {sz(a), sz(b)};

  while (curr.first != 0 && curr.second != 0) {

    int i = curr.first, j = curr.second;

    if (a[i - 1] == b[j - 1])
      ans.pb(a[i - 1]), curr = {i - 1, j - 1};
    else {
      if (dp[i - 1][j] > dp[i][j - 1])
        curr = {i - 1, j};
      else
        curr = {i, j - 1};
    }
  }

  reverse(all(ans));

  cout << ans << '\n';
}
Problem p03165 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ln '\n';
int lim = 1e6;
int inf = 1e9 + 7;
ll dp[3001][3001];
pair<int, int> nextdp[3001][3001];
ll solve(int i, int j, int n1, int n2, string s1, string s2) {
  if (i == n1 or j == n2) {
    return 0;
  }
  if (dp[i][j] != -1) {
    return dp[i][j];
  } else {
    ll a1 = -1;
    if (s1[i] == s2[j]) {
      a1 = 1 + solve(i + 1, j + 1, n1, n2, s1, s2);
      nextdp[i][j] = make_pair(i + 1, j + 1);
      // final.push_back(s1[i]);
      dp[i][j] = a1;
      return a1;
    }
    ll a2 = solve(i + 1, j, n1, n2, s1, s2);
    ll a3 = solve(i, j + 1, n1, n2, s1, s2);
    if (a2 > a3) {
      dp[i][j] = a2;
      nextdp[i][j] = make_pair(i + 1, j);
    } else {
      dp[i][j] = a3;
      nextdp[i][j] = make_pair(i, j + 1);
    }
    return dp[i][j];
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  string s, t;
  cin >> s >> t;
  int n1 = s.size(), n2 = t.size();
  for (int i = 0; i < 3001; i++) {
    for (int j = 0; j < 3001; j++) {
      dp[i][j] = -1;
      nextdp[i][j] = make_pair(-1, -1);
    }
  }
  ll ans = solve(0, 0, n1, n2, s, t);
  int i = 0, j = 0;
  string final = "";
  while (true) {
    if (i == n1 or j == n2) {
      break;
    }
    if (s[i] == t[j]) {
      final += string(1, s[i]);
    }
    pair<int, int> p = nextdp[i][j];
    i = p.first;
    j = p.second;
  }
  cout << final;
  cout << ln;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define ln '\n';
int lim = 1e6;
int inf = 1e9 + 7;
ll dp[3001][3001];
pair<int, int> nextdp[3001][3001];
ll solve(int i, int j, int n1, int n2, string &s1, string &s2) {
  if (i == n1 or j == n2) {
    return 0;
  }
  if (dp[i][j] != -1) {
    return dp[i][j];
  } else {
    ll a1 = -1;
    if (s1[i] == s2[j]) {
      a1 = 1 + solve(i + 1, j + 1, n1, n2, s1, s2);
      nextdp[i][j] = make_pair(i + 1, j + 1);
      // final.push_back(s1[i]);
      dp[i][j] = a1;
      return a1;
    }
    ll a2 = solve(i + 1, j, n1, n2, s1, s2);
    ll a3 = solve(i, j + 1, n1, n2, s1, s2);
    if (a2 > a3) {
      dp[i][j] = a2;
      nextdp[i][j] = make_pair(i + 1, j);
    } else {
      dp[i][j] = a3;
      nextdp[i][j] = make_pair(i, j + 1);
    }
    return dp[i][j];
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  string s, t;
  cin >> s >> t;
  int n1 = s.size(), n2 = t.size();
  for (int i = 0; i < 3001; i++) {
    for (int j = 0; j < 3001; j++) {
      dp[i][j] = -1;
      nextdp[i][j] = make_pair(-1, -1);
    }
  }
  ll ans = solve(0, 0, n1, n2, s, t);
  int i = 0, j = 0;
  string final = "";
  while (true) {
    if (i == n1 or j == n2) {
      break;
    }
    if (s[i] == t[j]) {
      final += string(1, s[i]);
    }
    pair<int, int> p = nextdp[i][j];
    i = p.first;
    j = p.second;
  }
  cout << final;
  cout << ln;
}
Problem p03165 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
#include <string>

using namespace std;

int n1, n2;
short dp[3000][3000];
char bt[3000][3000];

int main() {
  char a[3000], b[3000];
  cin >> a;
  cin >> b;
  n1 = strlen(a);
  n2 = strlen(b);

  for (int i = 0; i <= n1; i++) {
    for (int j = 0; j <= n2; j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
      } else {
        if (dp[i - 1][j] > dp[i][j - 1]) {
          bt[i][j] = 0;
          dp[i][j] = dp[i - 1][j];
        } else {
          bt[i][j] = 1;
          dp[i][j] = dp[i][j - 1];
        }

        if (a[i - 1] == b[j - 1]) {
          if (dp[i - 1][j - 1] + 1 > dp[i][j]) {
            dp[i][j] = dp[i - 1][j - 1] + 1;
            bt[i][j] = 2;
          }
        }
      }
    }
  }

  int answer = dp[n1][n2];

  if (answer < 1) {
    cout << "";
    return 0;
  }

  vector<char> res;
  int i = n1;
  int j = n2;
  while (i > 0 && j > 0) {
    if (bt[i][j] == 0) {
      i--;
    } else if (bt[i][j] == 1) {
      j--;
    } else if (bt[i][j] == 2) {
      res.push_back(a[i - 1]);
      i--;
      j--;
    }
  }
  reverse(res.begin(), res.end());
  for (auto c : res) {
    cout << c;
  }
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
#include <string>

using namespace std;

int n1, n2;
short dp[3001][3001];
char bt[3001][3001];

int main() {
  char a[3000], b[3000];
  cin >> a;
  cin >> b;
  n1 = strlen(a);
  n2 = strlen(b);

  for (int i = 0; i <= n1; i++) {
    for (int j = 0; j <= n2; j++) {
      if (i == 0 || j == 0) {
        dp[i][j] = 0;
      } else {
        if (dp[i - 1][j] > dp[i][j - 1]) {
          bt[i][j] = 0;
          dp[i][j] = dp[i - 1][j];
        } else {
          bt[i][j] = 1;
          dp[i][j] = dp[i][j - 1];
        }

        if (a[i - 1] == b[j - 1]) {
          if (dp[i - 1][j - 1] + 1 > dp[i][j]) {
            dp[i][j] = dp[i - 1][j - 1] + 1;
            bt[i][j] = 2;
          }
        }
      }
    }
  }

  int answer = dp[n1][n2];

  if (answer < 1) {
    cout << "";
    return 0;
  }

  vector<char> res;
  int i = n1;
  int j = n2;
  while (i > 0 && j > 0) {
    if (bt[i][j] == 0) {
      i--;
    } else if (bt[i][j] == 1) {
      j--;
    } else if (bt[i][j] == 2) {
      res.push_back(a[i - 1]);
      i--;
      j--;
    }
  }
  reverse(res.begin(), res.end());
  for (auto c : res) {
    cout << c;
  }
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll INF = 1e18;
constexpr ll MOD = 1e9 + 7;
constexpr double PI = 3.14159265358979323846;
constexpr int dx[] = {0, 0, 1, -1};
constexpr int dy[] = {1, -1, 0, 0};

int n, m, x[112345], y[112345];
V g[112345];
int dist[112345];

int dfs(int i) {
  if (dist[i] != -1)
    return dist[i];

  if (!g[i].size())
    return 0;

  int res = 0;

  REP(j, g[i].size())
  res = max(res, dfs(g[i][j]));

  return res + 1;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> m;

  REP(i, m) {
    cin >> x[i] >> y[i];

    g[x[i]].push_back(y[i]);
  }

  memset(dist, -1, sizeof(dist));

  int res = 0;

  for (int i = 1; i <= n; i++)
    res = max(res, dfs(i));

  cout << res << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) FOR(i, 0, n)
#define SORT(c) sort((c).begin(), (c).end())

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> V;
typedef map<int, int> M;

constexpr ll INF = 1e18;
constexpr ll MOD = 1e9 + 7;
constexpr double PI = 3.14159265358979323846;
constexpr int dx[] = {0, 0, 1, -1};
constexpr int dy[] = {1, -1, 0, 0};

int n, m, x[112345], y[112345];
V g[112345];
int dist[112345];

int dfs(int i) {
  if (dist[i] != -1)
    return dist[i];

  if (!g[i].size())
    return 0;

  int res = 0;

  REP(j, g[i].size())
  res = max(res, dfs(g[i][j]));

  dist[i] = res + 1;

  return res + 1;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> m;

  REP(i, m) {
    cin >> x[i] >> y[i];

    g[x[i]].push_back(y[i]);
  }

  memset(dist, -1, sizeof(dist));

  int res = 0;

  for (int i = 1; i <= n; i++)
    res = max(res, dfs(i));

  cout << res << endl;

  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
// includes
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

// macros
#define ll long long int
#define pb push_back
#define mk make_pair
#define pq priority_queue
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)

using namespace std;

//  types
typedef pair<int, int> P;
typedef pair<ll, int> Pl;
typedef pair<ll, ll> Pll;
typedef pair<double, double> Pd;

// constants
const int inf = 1e9;
const ll linf = 1LL << 50;
const double EPS = 1e-10;

// solve
vector<int> edge[100001];
ll dp[100001];

ll dfs(int x) {
  if (edge[x].size() == 0)
    return 0;
  if (dp[x] >= 0)
    return dp[x];
  ll res = 0;
  rep(i, edge[x].size()) { res = max(res, 1 + dfs(edge[x][i])); }
  return res;
}

int main(int argc, char const *argv[]) {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    edge[x].pb(y);
  }

  fill(dp, dp + n + 1, -1);

  ll res = 0;
  rep(i, n) { res = max(res, dfs(i)); }
  cout << res << endl;
  return 0;
}


Fixed submission:
// includes
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <cstdint>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

// macros
#define ll long long int
#define pb push_back
#define mk make_pair
#define pq priority_queue
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) FOR(i, 0, n)

using namespace std;

//  types
typedef pair<int, int> P;
typedef pair<ll, int> Pl;
typedef pair<ll, ll> Pll;
typedef pair<double, double> Pd;

// constants
const int inf = 1e9;
const ll linf = 1LL << 50;
const double EPS = 1e-10;

// solve
vector<int> edge[100001];
ll dp[100001];

ll dfs(int x) {
  if (edge[x].size() == 0)
    return 0;
  if (dp[x] >= 0)
    return dp[x];
  ll res = 0;
  rep(i, edge[x].size()) { res = max(res, 1 + dfs(edge[x][i])); }
  return dp[x] = res;
}

int main(int argc, char const *argv[]) {
  int n, m;
  cin >> n >> m;
  rep(i, m) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    edge[x].pb(y);
  }

  fill(dp, dp + n + 1, -1);

  ll res = 0;
  rep(i, n) { res = max(res, dfs(i)); }
  cout << res << endl;
  return 0;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int f(vector<vector<int>> &E, vector<int> &dp, int s) {
  if (dp[s] != -1) {
    return dp[s];
  } else {
    int ans = 0;
    for (int i = 0; i < E[s].size(); i++) {
      ans = max(ans, f(E, dp, E[s][i]) + 1);
    }
    return ans;
  }
}
int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> E(N);
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    E[x].push_back(y);
  }
  vector<int> dp(N, -1);
  int ans = 0;
  for (int i = 0; i < N; i++) {
    ans = max(ans, f(E, dp, i));
  }
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int f(vector<vector<int>> &E, vector<int> &dp, int s) {
  if (dp[s] != -1) {
    return dp[s];
  } else {
    int ans = 0;
    for (int i = 0; i < E[s].size(); i++) {
      ans = max(ans, f(E, dp, E[s][i]) + 1);
    }
    dp[s] = ans;
    return ans;
  }
}
int main() {
  int N, M;
  cin >> N >> M;
  vector<vector<int>> E(N);
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    E[x].push_back(y);
  }
  vector<int> dp(N, -1);
  int ans = 0;
  for (int i = 0; i < N; i++) {
    ans = max(ans, f(E, dp, i));
  }
  cout << ans << endl;
}
Problem p03166 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <vector>
using namespace std;

int N, M;
vector<int> next_verts[100000];
vector<int> longest_path_length_from_vert(100000, -1);

int calc_longest_path_from(int now) {
  if (longest_path_length_from_vert[now] >= 0) {
    return longest_path_length_from_vert[now];
  }
  if (next_verts[now].size() == 0) {
    longest_path_length_from_vert[now] = 0;
    return 0;
  }

  int ret = 0;
  for (auto itr = next_verts[now].begin(); itr != next_verts[now].end();
       itr++) {
    ret = max(ret, calc_longest_path_from(*itr));
  }
  return ret + 1;
}

int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    next_verts[x].push_back(y);
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    longest_path_length_from_vert[i] = calc_longest_path_from(i);
    ans = max(ans, longest_path_length_from_vert[i]);
  }

  cout << ans << endl;
}


Fixed submission:
#include <iostream>
#include <vector>
using namespace std;

int N, M;
vector<int> next_verts[100000];
vector<int> longest_path_length_from_vert(100000, -1);

int calc_longest_path_from(int now) {
  if (longest_path_length_from_vert[now] >= 0) {
    return longest_path_length_from_vert[now];
  }
  if (next_verts[now].size() == 0) {
    longest_path_length_from_vert[now] = 0;
    return 0;
  }

  int ret = 0;
  for (auto itr = next_verts[now].begin(); itr != next_verts[now].end();
       itr++) {
    ret = max(ret, calc_longest_path_from(*itr));
  }
  longest_path_length_from_vert[now] = ret + 1;
  return ret + 1;
}

int main() {
  cin >> N >> M;
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    x--;
    y--;
    next_verts[x].push_back(y);
  }
  int ans = 0;
  for (int i = 0; i < N; i++) {
    longest_path_length_from_vert[i] = calc_longest_path_from(i);
    ans = max(ans, longest_path_length_from_vert[i]);
  }

  cout << ans << endl;
}