alexjercan/bugnet huggingface dataset:
Problem p00495 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int dp[2][5001];
int main() {
  int n, m, a[5001], b[5000];
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= m; i++)
    scanf("%d", &b[i]);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      dp[1][j] = dp[0][j];
      if (a[i] == b[j])
        dp[1][j] = dp[0][j - 1] + 1;
      if (i == n)
        ans = max(ans, dp[1][j]);
    }
    for (int j = 1; j <= m; j++) {
      dp[0][j] = dp[1][j];
    }
  }
  printf("%d\\n", ans);
  return (0);
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int dp[5001][5001];
int main() {
  int n, m, a[5001], b[5000];
  scanf("%d %d", &n, &m);
  for (int i = 1; i <= n; i++)
    scanf("%d", &a[i]);
  for (int i = 1; i <= m; i++)
    scanf("%d", &b[i]);
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      dp[i][j] = dp[i - 1][j];
      if (a[i] == b[j])
        dp[i][j] = dp[i - 1][j - 1] + 1;
      if (i == n)
        ans = max(ans, dp[i][j]);
    }
  }
  printf("%d\\n", ans);
  return (0);
}
Problem p00495 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int dp[5001];
int dp1[5001];
int A, B;
int AA[5000];
int BB[5000];
int ans = 0;
int main() {
  scanf("%d%d", &A, &B);
  for (int i = 0; i < A; i++)
    scanf("%d", &AA[i]);
  for (int i = 0; i < B; i++)
    scanf("%d", &BB[i]);
  for (int j = 1; j <= B; j++) {
    int ma = 0;
    for (int i = 1; i <= A; i++) {
      ma = max(ma, dp[i - 1]);
      if (AA[i - 1] == BB[j - 1])
        dp1[i] = ma + 1;
      else
        dp1[i] = 0;
    }
    for (int i = 1; i <= A; i++) {
      dp[i] = dp1[i];
      ans = max(ans, dp1[i]);
    }
  }
  printf("%d\\n", ans);
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
int dp[5001][5001];
int A, B;
int AA[5000];
int BB[5000];
int ans = 0;
int main() {
  scanf("%d%d", &A, &B);
  for (int i = 0; i < A; i++)
    scanf("%d", &AA[i]);
  for (int i = 0; i < B; i++)
    scanf("%d", &BB[i]);
  for (int j = 1; j <= B; j++) {
    int ma = 0;
    for (int i = 1; i <= A; i++) {
      ma = max(ma, dp[j - 1][i - 1]);
      if (AA[i - 1] == BB[j - 1])
        dp[j][i] = ma + 1;
    }
  }
  for (int j = 0; j <= B; j++) {
    for (int i = 0; i <= A; i++) {
      ans = max(ans, dp[j][i]);
    }
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p00495 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
int A[5001], B[5001];
int main() {
  int a, b;
  cin >> a >> b;
  for (int i = 0; i < a; i++)
    cin >> A[i];
  for (int i = 0; i < b; i++)
    cin >> B[i];
  int next = 0, maxl = 0;
  for (int i = 0; i < b; i++) {
    next = i;
    int cnt = 0;
    for (int j = 0; j < a; j++)
      if (A[j] == B[next]) {
        cnt++;
        next++;
      }
    maxl = max(maxl, cnt);
  }
  cout << maxl << endl;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int A[5005], B[5005];
int D[5005][5005];
int main() {
  int a, b;
  cin >> a >> b;
  for (int i = 1; i <= a; i++)
    cin >> A[i];
  for (int i = 1; i <= b; i++)
    cin >> B[i];
  for (int i = 0; i <= a; i++)
    for (int j = 0; j <= b; j++)
      D[i][j] = 0;
  for (int i = 1; i <= b; i++) {
    int cnt = 0;
    bool begin = true;
    for (int j = 1; j <= a; j++) {
      if (B[i] == A[j] && begin)
        D[i][j] = 1;
      else if (B[i] == A[j] && begin == false)
        D[i][j] = cnt + 1;
      if (B[i - 1] == A[j]) {
        cnt = D[i - 1][j];
        begin = false;
      }
      // cout<<"D["<<i<<"]["<<j<<"] = "<<D[i][j]<<endl;
    }
  }
  int maxl = 0;
  for (int i = 1; i <= b; i++) {
    for (int j = 1; j <= a; j++) {
      maxl = max(maxl, D[i][j]);
    }
  }
  cout << maxl << endl;
}
Problem p00495 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

int main(void) {
  int n1, n2;
  cin >> n1 >> n2;
  vector<int> a(n1), b(n2);
  rep(i, n1) cin >> a[i];
  rep(i, n2) cin >> b[i];

  int ans = 0;
  for (int i = 0; i < n2;
       ++i) { // b?????£?¶???¨??????top??????????????\\????????????????±???????
    int tmp = 0, next = 0;
    bool flag = false;
    for (int j = i; j < n2; ++j) {
      for (int k = next; k < n1;
           ++k) { // a????????§?????´????????????????????????????????????
        if (b[j] == a[k]) {
          tmp++;
          next = k + 1;
          ans = max(ans, tmp);
          break;
        }
        if (k == n1 - 1) {
          flag = true;
          break;
        }
      }
      if (flag)
        break;
    }
  }
  printf("%d\\n", ans);
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)

int dp[5010][5010];

int main(void) {
  int n1, n2;
  cin >> n1 >> n2;
  vector<int> a(n1), b(n2);
  rep(i, n1) cin >> a[i];
  rep(i, n2) cin >> b[i];
  rep(i, 5010) rep(j, 5010) dp[i][j] = 0;

  for (int i = 1; i <= n1; ++i) {
    for (int j = 1; j <= n2; ++j) {
      if (a[i] == b[j])
        dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
      else
        dp[i][j] = max(dp[i][j], max(dp[i - 1][j], dp[i][j - 1]));
    }
  }
  printf("%d\\n", dp[n1][n2]);
  return 0;
}
Problem p00495 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = 1; i <= n; i++)
#define drep(i, n) for (int i = n; i >= 0; i--)
#define INF 100000005
#define MAX 100000001
#define dmp make_pair
#define dpb push_back
#define fi first
#define se second
using namespace std;
//__gcd(a,b), __builtin_popcount(a);

int a[5001], b[5001], dp[5001][2];
int main() {
  int n, m, t = 0, ans = 0;
  scanf("%d%d", &n, &m);
  rrep(i, n) scanf("%d", &a[i]);
  rrep(i, m) scanf("%d", &b[i]);
  for (int j = 1; j <= m; j++) {
    for (int i = 1; i <= n; i++) {
      if (a[i] == b[j])
        dp[i][j % 2] = dp[i - 1][(j - 1) % 2] + 1;
      else
        dp[i][j % 2] = dp[i - 1][j % 2];
      ans = max(ans, dp[i][j % 2]);
    }
  }
  printf("%d\\n", ans);
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = 1; i <= n; i++)
#define drep(i, n) for (int i = n; i >= 0; i--)
#define INF 100000005
#define MAX 100000001
#define dmp make_pair
#define dpb push_back
#define fi first
#define se second
using namespace std;
//__gcd(a,b), __builtin_popcount(a);

int a[5001], b[5001], dp[5001][5001];
int main() {
  int n, m, ans = 0;
  scanf("%d%d", &n, &m);
  rrep(i, n) scanf("%d", &a[i]);
  rrep(i, m) scanf("%d", &b[i]);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (a[i] == b[j])
        dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1);
      else
        dp[i][j] = max(dp[i][j], dp[i - 1][j]);
      ans = max(ans, dp[i][j]);
    }
  }
  printf("%d\\n", ans);
  return 0;
}
Problem p00495 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

int m, n, a[5000], b[5000];

short dp[5000][5000];
int dfs(int i, int j) {
  if (i == m || j == n)
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  if (a[i] == b[j])
    return dp[i][j] = dfs(i + 1, j + 1) + 1;
  else
    return dp[i][j] = dfs(i + 1, j);
}

int main() {
  scanf("%d%d", &m, &n);
  rep(i, m) scanf("%d", a + i);
  rep(j, n) scanf("%d", b + j);

  memset(dp, -1, sizeof dp);
  int ans = 0;
  rep(j, n) ans = max(ans, dfs(0, j));
  printf("%d\\n", ans);

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>

#define rep(i, n) for (int i = 0; i < (n); i++)

using namespace std;

int m, n, a[5000], b[5000];

int dp[5000][5000];
int dfs(int i, int j) {
  if (i == m || j == n)
    return 0;
  if (dp[i][j] != -1)
    return dp[i][j];
  if (a[i] == b[j])
    return dp[i][j] = dfs(i + 1, j + 1) + 1;
  else
    return dp[i][j] = dfs(i + 1, j);
}

int main() {
  scanf("%d%d", &m, &n);
  rep(i, m) scanf("%d", a + i);
  rep(j, n) scanf("%d", b + j);

  memset(dp, -1, sizeof dp);
  int ans = 0;
  rep(j, n) ans = max(ans, dfs(0, j));
  printf("%d\\n", ans);

  return 0;
}
Problem p00495 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, a) range(i, 0, a)
using namespace std;

int main() {
  vector<int> a, b;
  int in;
  cin >> in;
  a = vector<int>(in);
  cin >> in;
  b = vector<int>(in);
  rep(i, a.size()) cin >> a[i];
  rep(i, b.size()) cin >> b[i];

  int ans = 0;
  for (int i = 0; i < b.size(); i++) {
    int cnt = 0, preA = -1;
    rep(j, a.size()) {
      if (b[i] == a[j]) {
        preA = j;
        cnt = 1;
        break;
      }
    }
    if (preA == -1)
      continue;
    int tmpi = i;
    i++;
    for (int j = preA + 1; j < a.size() && i < b.size(); j++) {
      if (b[i] == a[j]) {
        i++;
        cnt++;
      }
    }
    ans = max(ans, cnt);
    i = tmpi;
  }
  cout << ans << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, a) range(i, 0, a)
using namespace std;

int main() {
  vector<int> a, b;
  int in;
  cin >> in;
  a = vector<int>(in);
  cin >> in;
  b = vector<int>(in);
  rep(i, a.size()) cin >> a[i];
  rep(i, b.size()) cin >> b[i];

  int ans = 0, preA, cnt = 0;
  bool flag = false, pre = false;
  for (int i = 0; i < b.size(); i++) {
    flag = false;
    if (pre) {
      range(j, preA + 1, a.size()) {
        if (b[i] == a[j]) {
          flag = true;
          preA = j;
          cnt++;
          break;
        }
      }
    } else {
      ans = max(ans, cnt);
      cnt = 0;
      rep(j, a.size()) {
        if (b[i] == a[j]) {
          flag = true;
          preA = j;
          cnt++;
          break;
        }
      }
    }
    pre = flag;
    i -= !flag;
  }
  cout << ans << endl;

  return 0;
}
Problem p00495 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dx[] = {1, -1, 0, 0, 1, -1, -1, 1};

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define mp make_pair
#define fi first
#define sc second
int A, B;
int a[5000], b[5000];
short dp[5001][5001];
int main() {
  scanf("%d%d", &A, &B);
  REP(i, A)
  scanf("%d", a + i);
  REP(i, B)
  scanf("%d", b + i);

  bool f = false;
  REP(i, A) {
    f = (f || a[i] == b[0]);
    dp[0][i] = f;
  }
  f = false;
  REP(i, B) { dp[i][0] = (a[0] == b[i]); }
  FOR(i, 1, B) {
    FOR(j, 1, A) {
      dp[i][j] = dp[i][j - 1];
      if (a[j] == b[i])
        dp[i][j] = max((short)(dp[i - 1][j - 1] + 1), dp[i][j]);
    }
  }
  short maxi = 0;
  REP(i, B)
  REP(j, A)
  maxi = max(maxi, dp[i][j]);
  printf("%d\\n", maxi);
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
#define pb push_back
int dy[] = {0, 0, 1, -1, 1, 1, -1, -1};
int dx[] = {1, -1, 0, 0, 1, -1, -1, 1};

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define RREP(i, n) for (int i = (n)-1; i >= 0; i--)
#define mp make_pair
#define fi first
#define sc second
int A, B, a[5000], b[5000];
int dp[5000][5000];
int main() {
  scanf("%d%d", &A, &B);
  REP(i, A)
  scanf("%d", a + i);
  REP(i, B)
  scanf("%d", b + i);

  bool f = false;
  REP(i, A) {
    f = (f || a[i] == b[0]);
    dp[0][i] = f;
  }
  f = false;
  REP(i, B) { dp[i][0] = (a[0] == b[i]); }
  int maxi = 0;
  FOR(i, 1, B) {
    FOR(j, 1, A) {
      dp[i][j] = dp[i][j - 1];
      if (a[j] == b[i])
        dp[i][j] = max(dp[i - 1][j - 1] + 1, dp[i][j]);
      maxi = max(maxi, dp[i][j]);
    }
  }
  printf("%d\\n", maxi);
  return 0;
}
Problem p00495 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.answer"

int main() {
  int A, B;
  cin >> A >> B;
  vector<int> as(A), bs(B);
  for (int i = 0; i < A; ++i) {
    cin >> as[i];
  }
  for (int i = 0; i < B; ++i) {
    cin >> bs[i];
  }
  int ans = 0;
  for (int f = 0; f < B; ++f) {
    int na = 0;
    int nans = 0;
    for (int nb = f; nb < B; ++nb) {
      bool ok = false;
      while (1) {
        if (na == A) {
          break;
        } else if (as[na] == bs[nb]) {
          na++;
          ok = true;
          break;
        } else {
          na++;
        }
      }
      if (!ok)
        break;
      else {
        nans += 1;
      }
    }
    ans = max(ans, nans);
  }
  cout << ans << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.answer"

int memo[5002][5002];
int main() {
  int A, B;
  cin >> A >> B;
  vector<int> as(A), bs(B);
  for (int i = 0; i < A; ++i) {
    cin >> as[i];
  }
  for (int i = 0; i < B; ++i) {
    cin >> bs[i];
  }
  memset(memo, 0, sizeof(memo));
  int ans = 0;
  for (int f = 0; f < B; ++f) {
    int na = 0;
    int nans = 0;
    for (int nb = f; nb < B; ++nb) {
      bool ok = false;
      while (1) {
        if (na == A) {
          break;
        } else if (as[na] == bs[nb]) {
          na++;
          ok = true;
          break;
        } else {
          na++;
        }
      }
      if (!ok)
        break;
      else {
        nans += 1;
      }
    }
    ans = max(ans, nans);
  }
  cout << ans << endl;
  return 0;
}
Problem p00495 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int a[5010], b[5010];

signed main() {
  int n, m;
  cin >> n >> m;
  rep(i, 0, n) { cin >> a[i]; }
  rep(i, 0, m) { cin >> b[i]; }
  int MAX = 0, ind;
  rep(i, 0, m) {
    ind = i;
    rep(j, 0, n) {
      if (a[j] == b[ind]) {
        ind++;
      }
    }
    MAX = max(MAX, ind - i);
  }
  cout << MAX << endl;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, n) for (int i = a; i < n; i++)
#define repb(i, a, b) for (int i = a; i >= b; i--)
#define all(a) a.begin(), a.end()
#define o(a) cout << a << endl
#define int long long
#define fi first
#define se second
using namespace std;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

int dp[5010][5001];
int a[5010], b[5010];

signed main() {
  int n, m;
  cin >> n >> m;
  rep(i, 0, n) { cin >> a[i]; }
  rep(i, 0, m) { cin >> b[i]; }
  int MAX = 0;
  rep(i, 0, m) {
    rep(j, 0, n) {
      if (j != 0) {
        dp[i + 1][j] = dp[i + 1][j - 1];
        if (b[i] == a[j]) {
          dp[i + 1][j] = dp[i][j - 1] + 1;
        }
      } else if (b[i] == a[j]) {
        dp[i + 1][j] = 1;
      }
      MAX = max(MAX, dp[i + 1][j]);
    }
  }
  cout << MAX << endl;
}
Problem p00495 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <vector>
using namespace std;

const int max_a = 5010;
const int max_b = 5010;

int solve(const vector<int> &a, const vector<int> &b) {
  int ret = 0;
  int n1 = a.size();
  int n2 = b.size();
  static int dp[max_a][max_b];

  for (int i = 0; i <= n1; ++i) {
    for (int j = 0; j <= n2; ++j) {
      dp[i][j] = 0;
    }
  }

  for (int i = 0; i < n1; ++i) {
    for (int j = 0; j < n2; ++j) {
      if (a[i] == b[j]) {
        dp[i + 1][j + 1] = dp[i][j] + 1;
      } else {
        // if( dp[i][j+1] > dp[i+1][j] )
        dp[i + 1][j + 1] = dp[i][j + 1];
        // max( dp[i+1][j], dp[i][j+1] );
      }
    }
  }

  for (int i = 0; i <= n2; ++i) {
    ret = max(ret, dp[n1][i]);
  }

  return ret;
}

int main() {
  int A, B;
  while (cin >> A >> B) {
    if (A <= 0 || B <= 0 || A > 5000 || B > 5000)
      break;
    vector<int> vA, vB;
    for (int i = 0; i < A; ++i) {
      int t;
      cin >> t;
      vA.push_back(t);
    }
    for (int i = 0; i < B; ++i) {
      int t;
      cin >> t;
      vB.push_back(t);
    }

    cout << solve(vA, vB) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

int solve(const vector<int> &a, const vector<int> &b) {
  int ret = 0;
  int n1 = a.size();
  int n2 = b.size();
  int dp[1 + n1][1 + n2];

  for (int i = 0; i <= n1; ++i) {
    for (int j = 0; j <= n2; ++j) {
      dp[i][j] = 0;
    }
  }

  for (int i = 0; i < n1; ++i) {
    for (int j = 0; j < n2; ++j) {
      if (a[i] == b[j]) {
        dp[i + 1][j + 1] = dp[i][j] + 1;
      } else {
        // if( dp[i][j+1] > dp[i+1][j] )
        dp[i + 1][j + 1] = dp[i][j + 1];
        // max( dp[i+1][j], dp[i][j+1] );
      }
    }
  }

  for (int i = 0; i <= n2; ++i) {
    ret = max(ret, dp[n1][i]);
  }

  return ret;
}

int main() {
  int A, B;
  while (cin >> A >> B) {
    if (A == 0 || B == 0)
      break;
    vector<int> vA, vB;
    for (int i = 0; i < A; ++i) {
      int t;
      cin >> t;
      vA.push_back(t);
    }
    for (int i = 0; i < B; ++i) {
      int t;
      cin >> t;
      vB.push_back(t);
    }

    cout << solve(vA, vB) << endl;
  }
  return 0;
}
Problem p00495 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int dp[2][5010];

int main() {
  int n, m, a[5010], b[5010];

  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  for (int i = 1; i <= m; ++i) {
    cin >> b[i];
  }

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      dp[i % 2][j] =
          max(dp[!(i % 2)][j], a[i] == b[j] ? dp[!(i % 2)][j - 1] + 1 : 0);
    }
  }

  cout << *max_element(dp[0], dp[2]) << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int dp[5010][5010];

int main() {
  int n, m, a[5010], b[5010];

  cin >> n >> m;
  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
  }
  for (int i = 1; i <= m; ++i) {
    cin >> b[i];
  }

  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) {
      dp[i][j] = max(dp[i - 1][j], a[i] == b[j] ? dp[i - 1][j - 1] + 1 : 0);
    }
  }

  cout << *max_element(dp[n], dp[n] + m + 1) << endl;

  return 0;
}
Problem p00495 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

int aa[5001], ba[5001];
short dp[5001][5001];

int main() {
  int a, b;
  scanf("%d %d", &a, &b);
  for (int i = 1; i <= a; i++) {
    scanf("%d", &aa[i]);
  }
  for (int i = 1; i <= b; i++) {
    scanf("%d", &ba[i]);
  }

  for (int i = 1; i <= a; i++) {
    for (int j = 1; j <= b; j++) {
      if (aa[i] == ba[j]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      }
      // dp[i][j] = max(dp[i][j], dp[i - 1][j]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }

  short maxi = 0;
  for (int i = 0; i <= a; i++) {
    for (int j = 0; j <= b; j++) {
      // printf("%2d", dp[i][j]);

      maxi = max(maxi, dp[i][j]);
    }
    // puts("");
  }

  printf("%d\\n", maxi);

  return 0;
}

Error: MLE

Fixed submission:

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

int aa[5001], ba[5001];
int dp[5001][5001];

int main() {
  int a, b;
  scanf("%d %d", &a, &b);
  for (int i = 1; i <= a; i++) {
    scanf("%d", &aa[i]);
  }
  for (int i = 1; i <= b; i++) {
    scanf("%d", &ba[i]);
  }

  for (int i = 1; i <= a; i++) {
    for (int j = 1; j <= b; j++) {
      if (aa[i] == ba[j]) {
        dp[i][j] = dp[i - 1][j - 1] + 1;
      }
      // dp[i][j] = max(dp[i][j], dp[i - 1][j]);
      else
        dp[i][j] = dp[i - 1][j];
    }
  }

  int maxi = 0;
  for (int i = 0; i <= a; i++) {
    for (int j = 0; j <= b; j++) {
      // printf("%2d", dp[i][j]);

      maxi = max(maxi, dp[i][j]);
    }
    // puts("");
  }

  printf("%d\\n", maxi);

  return 0;
}
Problem p00495 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

#define chmax(x, y) (x = max(x, y))

int n, m;
int a[5010], b[5010];
int dp[2][5010];

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++)
    cin >> b[i];
  a[n] = -1;
  b[m] = -2;

  int res = 0;
  int *src = dp[0], *dst = dp[1];
  for (int i = 0; i <= n; i++) {
    fill_n(dst, 5010, 0);
    for (int j = 0; j <= m; j++) {
      chmax(res, chmax(dst[j], src[j]));
      if (a[i] == b[j]) {
        chmax(res, chmax(dst[j + 1], src[j] + 1));
      }
    }
    swap(src, dst);
  }

  cout << res << endl;
}

Error: MLE

Fixed submission:

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

#define chmax(x, y) (x = max(x, y))

int n, m;
int a[5010], b[5010];
int dp[5010][5010];

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < m; i++)
    cin >> b[i];
  a[n] = -1;
  b[m] = -2;

  for (int i = 0; i <= n; i++) {
    for (int j = 0; j <= m; j++) {
      chmax(dp[i + 1][j], dp[i][j]);
      if (a[i] == b[j]) {
        chmax(dp[i + 1][j + 1], dp[i][j] + 1);
      }
    }
  }

  int res = *max_element(*dp, *dp + 5010 * 5010);
  cout << res << endl;
}