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

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
const ll N = 2000009;
const ll p = 31;
const ll m = 1e9 + 7;
const ll inf = 1e14;
ll mod = 1e9 + 7;
ll powm(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
// ll fact[N];

// ll ncr(ll n,ll r){
//      return (((fact[n]*powm(fact[r],mod-2))%mod)*powm(fact[n-r],mod-2))%mod;
// }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  ll t = 1;
  // cin>>t;
  while (t--) {
    ll n, m;
    cin >> n >> m;
    vector<ll> a(n);
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    make_heap(a.begin(), a.end());
    for (int i = 0; i < m; i++) {
      ll k = a.front();
      pop_heap(a.begin(), a.end());
      a.pop_back();
      k = k / 2;
      a.push_back(k);
      push_heap(a.begin(), a.end());
    }
    ll ans = 0;
    for (auto i : a) {
      ans += i;
    }
    cout << ans;
  }
}


Fixed submission:
#include <bits/stdc++.h>
typedef long long int ll;
using namespace std;
const ll N = 2000009;
const ll p = 31;
const ll m = 1e9 + 7;
const ll inf = 1e14;
ll mod = 1e9 + 7;
ll powm(ll a, ll b) {
  ll res = 1;
  while (b) {
    if (b & 1)
      res = (res * a) % mod;
    a = (a * a) % mod;
    b >>= 1;
  }
  return res;
}
// ll fact[N];

// ll ncr(ll n,ll r){
//      return (((fact[n]*powm(fact[r],mod-2))%mod)*powm(fact[n-r],mod-2))%mod;
// }
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);

  // #ifndef ONLINE_JUDGE
  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);
  // #endif
  ll t = 1;
  // cin>>t;
  while (t--) {
    ll n, m;
    cin >> n >> m;
    vector<ll> a(n);
    for (int i = 0; i < n; i++) {
      cin >> a[i];
    }
    make_heap(a.begin(), a.end());
    for (int i = 0; i < m; i++) {
      ll k = a.front();
      pop_heap(a.begin(), a.end());
      a.pop_back();
      k = k / 2;
      a.push_back(k);
      push_heap(a.begin(), a.end());
    }
    ll ans = 0;
    for (auto i : a) {
      ans += i;
    }
    cout << ans;
  }
}
Problem p02912 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const long long INF = 1LL << 60;

int main() {
  int N, M;
  cin >> N >> M;
  priority_queue<long long> que;
  for (int i = 0; i < N; i++) {
    long long A;
    cin >> A;
    que.push(A);
  }
  for (int i = 0; i < M; i++) {
    long long t = que.top();
    que.pop();
    que.push(t / 2);
  }
  long long ans = 0;
  while (!que.empty())
    ans += que.top();
  que.pop();
  cout << ans << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
const long long INF = 1LL << 60;

int main() {
  int N, M;
  cin >> N >> M;
  priority_queue<long long> que;
  for (int i = 0; i < N; i++) {
    long long A;
    cin >> A;
    que.push(A);
  }
  for (int i = 0; i < M; i++) {
    long long t = que.top();
    que.pop();
    que.push(t / 2);
  }
  long long ans = 0;
  while (!que.empty())
    ans += que.top(), que.pop();
  cout << ans << endl;
}
Problem p02912 (C++)

Buggy submission (status: Runtime Error):
#include <bits //stdc++.h>
using namespace std;
int main(void) {
  unsigned long long a, b, c, d, i, j, ans;
  ans = 0;
  cin >> a >> b;
  vector<unsigned long long> v;
  v.push_back(0);
  for (i = 0; i < a; i++) {
    cin >> c;
    ans = ans + c;
    while (c) {
      d = c / 2;
      v.push_back(c - d);
      c = d;
    }
  }
  sort(v.begin(), v.end());
  j = v.size();
  for (i = 0; i < b; i++) {
    ans = ans - v[j - 1 - i];
  }

  cout << ans << endl;
}


Fixed submission:
#include <bits //stdc++.h>
using namespace std;
int main(void) {
  unsigned long long a, b, c, d, i, j, ans;
  ans = 0;
  cin >> a >> b;
  vector<unsigned long long> v;
  v.push_back(0);
  for (i = 0; i < a; i++) {
    cin >> c;
    ans = ans + c;
    while (c) {
      d = c / 2;
      v.push_back(c - d);
      c = d;
    }
  }
  sort(v.begin(), v.end());
  j = v.size();
  if (j < b)
    b = j;
  for (i = 0; i < b; i++) {
    ans = ans - v[j - 1 - i];
  }

  cout << ans << endl;
}
Problem p02912 (C++)

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

int main() {
  int n, m;
  cin >> n >> m;
  long long a[n];
  std::vector<long long> v;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    v.push_back(a[i]);
  }

  // sort(v.begin(), v.end());
  for (int i = 0; i < m; ++i) {
    sort(v.begin(), v.end());
    v[v.size() - 1] /= 2;
  }
  cout << accumulate(v.begin(), v.end(), 0LL) << endl;
  return 0;
}


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

int main() {
  int n, m;
  cin >> n >> m;
  long long a[n];
  std::vector<long long> v;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    v.push_back(a[i]);
  }
  /*
      for (int i = 0; i < m; ++i) {
          sort(v.begin(), v.end());
          v[v.size()-1] /= 2;
      }
  */
  make_heap(v.begin(), v.end());
  for (int i = 0; i < m; i++) {
    long long a = v.front();
    pop_heap(v.begin(), v.end());
    v.pop_back();
    a = a / 2;
    v.push_back(a);
    push_heap(v.begin(), v.end());
  }
  cout << accumulate(v.begin(), v.end(), 0LL) << endl;
  return 0;
}
Problem p02912 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define N 2000000
typedef long long LL;
int a[N], tot, n, m;
LL sum;

int main() {
  // freopen("sample.in", "r", stdin);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    int tmp;
    scanf("%d", &tmp);
    sum += tmp;
    while (tmp) {
      a[++tot] = tmp - tmp / 2, tmp /= 2;
    }
  }
  if (m >= tot)
    return cout << 0 << endl, 0;
  nth_element(a + 1, a + tot - m + 1, a + tot + 1);
  // for(int i = 1; i <= tot; i++) cout << a[i] << endl;
  int kth = a[tot - m + 1], num = 0;
  for (int i = 1; i <= tot; i++)
    if (a[i] > kth)
      sum -= a[i], num++;
  sum = sum - (LL)kth * (m - num);
  cout << sum << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define N 3000010
typedef long long LL;
int a[N], tot, n, m;
LL sum;

int main() {
  // freopen("sample.in", "r", stdin);
  cin >> n >> m;
  for (int i = 1; i <= n; i++) {
    int tmp;
    scanf("%d", &tmp);
    sum += tmp;
    while (tmp) {
      a[++tot] = tmp - tmp / 2, tmp /= 2;
    }
  }
  if (m >= tot)
    return cout << 0 << endl, 0;
  nth_element(a + 1, a + tot - m + 1, a + tot + 1);
  // for(int i = 1; i <= tot; i++) cout << a[i] << endl;
  int kth = a[tot - m + 1], num = 0;
  for (int i = 1; i <= tot; i++)
    if (a[i] > kth)
      sum -= a[i], num++;
  sum = sum - (LL)kth * (m - num);
  cout << sum << endl;
}
Problem p02912 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
#include <ctype.h>
#include <float.h>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <string.h>
#include <string>
using namespace std;

int main() {

  long long N, M, A[100000] = {}, ans = 0, max, l = 0;
  cin >> N >> M;

  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  sort(A, A + N, greater<int>());
  max = 0;

  for (int i = 0; i < M; i++) {
    for (int j = 0; j <= l + 1; j++) {
      if (A[max] < A[j]) {
        max = j;
      }
    }
    A[max] /= 2;
    if (l < max) {
      l = max;
    }
  }

  for (int i = 0; i < N; i++) {
    ans += A[i];
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <ctype.h>
#include <float.h>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <string.h>
#include <string>
using namespace std;

int main() {

  long long N, M, A[100000] = {}, ans = 0, max, l = 0;
  cin >> N >> M;

  for (int i = 0; i < N; i++) {
    cin >> A[i];
  }

  sort(A, A + N, greater<int>());
  max = 0;

  for (int i = 0; i < M; i++) {
    for (int j = 0; j <= l + 1; j++) {
      if (A[max] < A[j]) {
        max = j;
      }
    }
    A[max] /= 2;
    if (l < max) {
      l = max;
    }

    if (i == M / 2) {
      sort(A, A + N, greater<int>());
      l = 0;
    }
  }

  for (int i = 0; i < N; i++) {
    ans += A[i];
  }

  cout << ans << endl;

  return 0;
}
Problem p02912 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define watch(x) cout << (#x) << " is " << (x) << endl
#define debug cout << "hi" << endl

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

const int MOD = 1e9 + 7;
const int INF32 = 1 << 30;
const ll INF64 = 1LL << 60;

int main() {
#ifndef ONLINE_JUDGE
  freopen(".in.txt", "r", stdin);
  freopen(".out.txt", "w", stdout);
#endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  priority_queue<ll> pq;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ll a;
    cin >> a;
    ans += a;
    pq.push(a);
  }
  for (int i = 0; i < m; i++) {
    ll t = pq.top();
    pq.pop();
    ans -= t / 2 + (t & 1);
    pq.push(t / 2);
  }
  cout << ans;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
#pragma GCC target("sse,sse2,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define watch(x) cout << (#x) << " is " << (x) << endl
#define debug cout << "hi" << endl

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;

const int MOD = 1e9 + 7;
const int INF32 = 1 << 30;
const ll INF64 = 1LL << 60;

int main() {
  // #ifndef ONLINE_JUDGE
  // freopen(".in.txt", "r", stdin);
  // freopen(".out.txt", "w", stdout);
  // #endif
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  int n, m;
  cin >> n >> m;
  priority_queue<ll> pq;
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    ll a;
    cin >> a;
    ans += a;
    pq.push(a);
  }
  for (int i = 0; i < m; i++) {
    ll t = pq.top();
    pq.pop();
    ans -= t / 2 + (t & 1);
    pq.push(t / 2);
  }
  cout << ans;

  return 0;
}
Problem p02912 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for (ll i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

int main() {
  ll n, m;
  cin >> n >> m;
  vl a(n);
  rep(i, n) cin >> a[i];
  ll ng = -1, ok = 1e18;
  while (ng + 1 < ok) {
    ll x = (ng + ok) / 2;
    ll cnt = 0;
    vl b = a;
    rep(i, n) {
      while (b[i] > x) {
        b[i] /= 2;
        cnt++;
      }
    }
    if (cnt > m)
      ng = x;
    else
      ok = x;
  }
  rep(i, n) {
    while (a[i] > ok) {
      a[i] /= 2;
      m--;
    }
  }
  sort(all(a), greater<ll>());
  rep(i, m) a[i] /= 2;
  cout << accumulate(all(a), 0LL) << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (ll i = 0, i##_len = (n); i < i##_len; ++i)
#define rep2(i, x, n) for (ll i = x, i##_len = (n); i < i##_len; ++i)
#define all(n) begin(n), end(n)
using ll = long long;
using P = pair<ll, ll>;
using vi = vector<int>;
using vl = vector<ll>;
using vs = vector<string>;
using vc = vector<char>;
using vb = vector<bool>;
using vd = vector<double>;
vi dir = {-1, 0, 1, 0, -1, -1, 1, 1, -1};

int main() {
  ll n, m;
  cin >> n >> m;
  vl a(n);
  rep(i, n) cin >> a[i];
  ll ng = -1, ok = 1e18;
  while (ng + 1 < ok) {
    ll x = (ng + ok) / 2;
    ll cnt = 0;
    vl b = a;
    rep(i, n) {
      while (b[i] > x) {
        b[i] /= 2;
        cnt++;
      }
    }
    if (cnt > m)
      ng = x;
    else
      ok = x;
  }
  rep(i, n) {
    while (a[i] > ok) {
      a[i] /= 2;
      m--;
    }
  }
  sort(all(a), greater<ll>());
  rep(i, min(m, n)) a[i] /= 2;
  cout << accumulate(all(a), 0LL) << endl;
}
Problem p02912 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
typedef long long ll;
#define sz(x) int(x.size())
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define MOD 1000000007
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> a(N);
  ll ans = 0;
  rep(i, N) {
    scanf("%d", &a[i]);
    ans += a[i];
  }

  vector<int> x;
  for (int i = 0; i < N; i++) {
    while (a[i] >= 1) {
      int X = a[i] % 2;
      a[i] /= 2;
      x.push_back(a[i] + X);
    }
  }
  sort(x.begin(), x.end(), greater<int>());
  for (int i = 0; i < M; i++) {
    ans -= x[i];
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
typedef long long ll;
#define sz(x) int(x.size())
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define MOD 1000000007
using namespace std;

int main() {
  int N, M;
  cin >> N >> M;
  vector<int> a(N);
  ll ans = 0;
  rep(i, N) {
    scanf("%d", &a[i]);
    ans += a[i];
  }

  vector<int> x;
  for (int i = 0; i < N; i++) {
    while (a[i] >= 1) {
      int X = a[i] % 2;
      a[i] /= 2;
      x.push_back(a[i] + X);
    }
  }
  sort(x.begin(), x.end(), greater<int>());
  for (int i = 0; i < M; i++) {
    ans -= x[i];
    if (ans < 0) {
      ans = 0;
      break;
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02913 (C++)

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

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

  int N;
  cin >> N;
  string S;
  cin >> S;
  int maxv = 0;
  for (int i = 0; i < N; i++) {
    char ch = S[i];
    for (int j = i + 1; j < N; j++) {
      if (S[j] != ch)
        continue;
      if (i + maxv > j || j + maxv > N)
        continue;
      int k = 0;
      while (j + k < N && i + k < j && S[j + k] == S[i + k])
        k++;
      maxv = max(maxv, k);
    }
  }
  cout << maxv << endl;

  return 0;
}


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

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

  int N;
  cin >> N;
  string S;
  cin >> S;
  int maxv = 0;
  for (int i = 0; i < N; i++) {
    char ch = S[i];
    for (int j = i + 1; j < N; j++) {
      if (S[j] != ch)
        continue;
      if (i + maxv > j || j + maxv > N)
        continue;
      if (i > 0 && S[i - 1] == S[j - 1])
        continue;
      int k = 0;
      while (j + k < N && i + k < j && S[j + k] == S[i + k])
        k++;
      maxv = max(maxv, k);
    }
  }
  cout << maxv << endl;

  return 0;
}
Problem p02913 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vii vector<int>
#define vll vector<ll>
#define lb lower_bound
#define pb push_back
#define mp make_pair
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep2(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(x) x.begin(), x.end()
// #define int ll
using namespace std;
const int INF(1 << 30);
const ll LLINF(1LL << 55LL);
const int MOD = 1000000007;
const int MAX = 510000;
const double pi = acos(-1);
const double eps = 1e-9;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  ll n, ans = 0;
  string s;
  cin >> n >> s;
  ll dp[6010][6010];
  rep(i, 5010) rep(j, 5010) dp[i][j] = 0;
  for (ll i = n - 1; i >= 0; i--) {
    for (ll j = n - 1; j > i; j--) {
      if (s[i] == s[j]) {
        dp[i][j] = max(dp[i][j], dp[i + 1][j + 1] + 1);
      }
      ans = max(ans, min(dp[i][j], j - i));
    }
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <vector>
#define ll long long
#define pii pair<int, int>
#define pll pair<ll, ll>
#define vii vector<int>
#define vll vector<ll>
#define lb lower_bound
#define pb push_back
#define mp make_pair
#define rep(i, n) for (ll i = 0; i < n; i++)
#define rep2(i, a, b) for (ll i = a; i < b; i++)
#define repr(i, n) for (ll i = n - 1; i >= 0; i--)
#define all(x) x.begin(), x.end()
// #define int ll
using namespace std;
const int INF(1 << 30);
const ll LLINF(1LL << 55LL);
const int MOD = 1000000007;
const int MAX = 510000;
const double pi = acos(-1);
const double eps = 1e-9;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  ll n, ans = 0;
  string s;
  cin >> n >> s;
  ll dp[5010][5010];
  rep(i, 5010) rep(j, 5010) dp[i][j] = 0;
  for (ll i = n - 1; i >= 0; i--) {
    for (ll j = n - 1; j > i; j--) {
      if (s[i] == s[j]) {
        dp[i][j] = max(dp[i][j], dp[i + 1][j + 1] + 1);
      }
      ans = max(ans, min(dp[i][j], j - i));
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02913 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <set>
#include <stdio.h>
#include <vector>
using namespace std;
typedef long long ll;
typedef std::vector<ll> lls;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))

void cinv(lls &llVec, ll n) {
  ll val;
  llVec.reserve((size_t)(n));
  for (ll i = 0; i < n; i++) {
    cin >> val;
    llVec.push_back(val);
  }
}

int main() {
  ll N;
  std::string S;
  cin >> N >> S;

  ll maxLen = 0;
  for (ll i = 0; i < N - 1; i++) {

    for (ll j = i + 1; j < N; j++) {

      // if (i != 0) {
      //	if (S[i - 1] == S[j - 1]) break;
      // }

      ll k;
      for (k = 0; i + k < j; k++) {
        char cik = S[i + k];
        char cjk = S[j + k];
        if (cik != cjk)
          break;
      }
      maxLen = MAX(maxLen, k);
    }
  }

  cout << maxLen << '\n';

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <set>
#include <stdio.h>
#include <vector>
using namespace std;
typedef long long ll;
typedef std::vector<ll> lls;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))

void cinv(lls &llVec, ll n) {
  ll val;
  llVec.reserve((size_t)(n));
  for (ll i = 0; i < n; i++) {
    cin >> val;
    llVec.push_back(val);
  }
}

int main() {
  ll N;
  std::string S;
  cin >> N >> S;

  ll maxLen = 0;
  for (ll i = 0; i < N - 1; i++) {

    for (ll j = i + 1; j < N; j++) {

      if (i != 0) {
        if (S[i - 1] == S[j - 1])
          continue;
      }

      ll k;
      for (k = 0; i + k < j; k++) {
        char cik = S[i + k];
        char cjk = S[j + k];
        if (cik != cjk)
          break;
      }
      maxLen = MAX(maxLen, k);
    }
  }

  cout << maxLen << '\n';

  return 0;
}
Problem p02913 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr long double EPS = 1e-15;
const long double PI = acos(-1);
constexpr int inf = 1e9;
constexpr ll INF = 2e18;
constexpr ll MOD = 1e9 + 7;
constexpr ll MOD1 = 998244353;
typedef pair<ll, ll> P;
// #define all(v) (v).begin(), (v).end()
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) rep(i, 0, n)
#define sz(s) (s).size()
#define pb push_back
#define fi first
#define se second
// #define mp make_pair

int n;
string s;

void input() { cin >> n >> s; }

void solve() {
  int ans = 0;
  for (int i = 0; i < sz(s); i++) {
    for (int j = i + 1; j < sz(s); j++) {
      if (s[i] != s[j])
        continue;
      int cnt = 1;
      for (int k = 1; j + k < sz(s); k++) {
        if (s[i + k] == s[j + k] && i + k < j)
          cnt++;
        else
          break;
      }
      ans = max(ans, cnt);
    }
  }
  cout << ans << endl;
}

int main(int argc, char *argv[]) {
  input();
  solve();
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
constexpr long double EPS = 1e-15;
const long double PI = acos(-1);
constexpr int inf = 1e9;
constexpr ll INF = 2e18;
constexpr ll MOD = 1e9 + 7;
constexpr ll MOD1 = 998244353;
typedef pair<ll, ll> P;
// #define all(v) (v).begin(), (v).end()
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define REP(i, n) rep(i, 0, n)
#define sz(s) (s).size()
#define pb push_back
#define fi first
#define se second
// #define mp make_pair

int n;
string s;

void input() { cin >> n >> s; }

void solve() {
  int ans = 0;
  for (int i = 1; i < sz(s); i++) {
    int t = 0;
    for (int j = 0; j + i < sz(s); j++) {
      t = (t + 1) * (s[j] == s[j + i]);
      ans = max(ans, min(t, i));
    }
  }
  cout << ans << endl;
}

int main(int argc, char *argv[]) {
  input();
  solve();
  return 0;
}
Problem p02913 (C++)

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

using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define INF LONG_MAX
#define MOD 1000000007
#define rng(a) a.begin(), a.end()
#define rrng(a) a.end(), a.begin()

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

  int N;
  string S;
  cin >> N >> S;
  int ans = 0;
  for (int i = 1; i < N; i++) {
    for (int j = 0; j < N; j++) {
      int res = 0;
      int m = j;
      while (S[j] == S[j + i] && j < N && j + i < N && j < i + m) {
        res++;
        j++;
      }
      ans = max(res, ans);
    }
  }
  cout << ans << endl;
  return 0;
}


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

using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define INF LONG_MAX
#define MOD 1000000007
#define rng(a) a.begin(), a.end()
#define rrng(a) a.end(), a.begin()

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

  int N;
  string S;
  cin >> N >> S;
  int ans = 0;
  for (int i = 1; i < N; i++) {
    for (int j = 0; j < N; j++) {
      int res = 0;
      int m = j;
      while (j < N && j + i < N && S[j] == S[i + j] && j < i + m) {
        res++;
        j++;
      }
      ans = max(res, ans);
    }
  }
  cout << ans << endl;
  return 0;
}
Problem p02913 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll MOD = (ll)(1e9 + 7);
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
#ifdef LOCAL
#define debug(x) cerr << #x << ": " << x << endl
#else
#define debug(x)
#endif
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int N;
string S;

inline vector<int> Z_algorithm(string S) {
  vector<int> Z(S.size());
  Z[0] = S.size();
  int i = 1, j = 0;
  while (i < S.size()) {
    while (i + j < S.size() && S[j] == S[i + j])
      j++;
    Z[i] = j;

    if (j == 0) {
      i++;
      continue;
    }
    int k = 1;
    while (k + Z[k] != j) {
      Z[i + k] = Z[k];
      k++;
    }
    i += k;
    j -= k;
  }
  return Z;
}

signed main() {
  cin >> N >> S;
  ll ans = 0;
  rep(i, N) {
    string T = S.substr(i, N - i);
    vector<int> v = Z_algorithm(T);
    for (int j = 1; j < v.size(); j++) {
      if (v[j] <= j)
        ans = max(ans, (ll)v[j]);
    }
  }
  cout << ans << endl;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const ll MOD = (ll)(1e9 + 7);
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int(i) = 0; (i) < (int)(n); (i)++)
#ifdef LOCAL
#define debug(x) cerr << #x << ": " << x << endl
#else
#define debug(x)
#endif
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};

int N;
string S;

inline vector<int> Z_algorithm(string S) {
  vector<int> Z(S.size());
  Z[0] = S.size();
  int i = 1, j = 0;
  while (i < S.size()) {
    while (i + j < S.size() && S[j] == S[i + j])
      j++;
    Z[i] = j;

    if (j == 0) {
      i++;
      continue;
    }
    int k = 1;
    while (i + k < S.size() && k + Z[k] < j) {
      Z[i + k] = Z[k];
      k++;
    }
    i += k;
    j -= k;
  }
  return Z;
}

signed main() {
  cin >> N >> S;
  ll ans = 0;
  rep(i, N) {
    string T = S.substr(i, N - i);
    vector<int> v = Z_algorithm(T);
    for (int j = 1; j < v.size(); j++) {
      if (v[j] <= j)
        ans = max(ans, (ll)v[j]);
    }
  }
  cout << ans << endl;
}
Problem p02913 (C++)

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

int n;
string s;

struct RollingHash {
  vector<unsigned long long> hash[2], power[2];
  const unsigned int seed = 10007;
  const unsigned int mod[2] = {999999937, 1000000007};

  RollingHash(const string &s) {
    int n = (int)s.size();
    for (int m = 0; m < 2; ++m) {
      hash[m].assign(n + 1, 0);
      power[m].assign(n + 1, 1);

      for (int i = 0; i < n; ++i) {
        hash[m][i + 1] = (hash[m][i] * seed + s[i]) % mod[m];
        power[m][i + 1] = power[m][i] * seed % mod[m];
      }
    }
  }

  // [l, r)
  inline unsigned long long get_hash(int l, int r, int m = 0) {
    unsigned long long ret =
        (hash[m][r] - hash[m][l] * power[m][r - l] % mod[m] + mod[m]) % mod[m];
    return ret;
  }
};

inline bool C(int k, RollingHash &rh) {
  for (int i = 0; i < n / 2 + n / 3; ++i)
    for (int j = i + k; j < n - k + 1; ++j) {
      if (s[i] != s[j])
        continue;
      if (rh.get_hash(i, i + k) == rh.get_hash(j, j + k))
        return true;
    }
  return false;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cin >> n >> s;

  RollingHash rh(s);

  int ok = 0, ng = n / 2 + 1;
  while (ng - ok > 1) {
    int mid = (ok + ng) >> 1;
    if (C(mid, rh))
      ok = mid;
    else
      ng = mid;
  }
  cout << ok << '\n';
  return 0;
}

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

int n;
string s;

struct RollingHash {
  vector<unsigned long long> hash[2], power[2];
  const unsigned int seed = 10007;
  const unsigned int mod[2] = {999999937, 1000000007};

  RollingHash(const string &s) {
    int n = (int)s.size();
    for (int m = 0; m < 2; ++m) {
      hash[m].assign(n + 1, 0);
      power[m].assign(n + 1, 1);

      for (int i = 0; i < n; ++i) {
        hash[m][i + 1] = (hash[m][i] * seed + s[i]) % mod[m];
        power[m][i + 1] = power[m][i] * seed % mod[m];
      }
    }
  }

  // [l, r)
  inline unsigned long long get_hash(int l, int r, int m = 0) {
    unsigned long long ret =
        (hash[m][r] - hash[m][l] * power[m][r - l] % mod[m] + mod[m]) % mod[m];
    return ret;
  }
};

inline bool C(int k, RollingHash &rh) {
  for (int i = 0; i < n / 2 + n / 4; ++i)
    for (int j = i + k; j < n - k + 1; ++j) {
      if (s[i] != s[j])
        continue;
      if (rh.get_hash(i, i + k) == rh.get_hash(j, j + k))
        return true;
    }
  return false;
}

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);
  cin >> n >> s;

  RollingHash rh(s);

  int ok = 0, ng = n / 2 + 1;
  while (ng - ok > 1) {
    int mid = (ok + ng) >> 1;
    if (C(mid, rh))
      ok = mid;
    else
      ng = mid;
  }
  cout << ok << '\n';
  return 0;
}
Problem p02913 (C++)

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

#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll, ll> pll;
typedef double ld;

template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

int main() {
  char s[5050];
  int n;
  scanf("%d", &n);
  scanf("%s", s);
  int max = 0;
  forn(i, n) {
    int t = 0;
    forn(j, n) {
      t = (t + 1) * (s[j] == s[j + i]);
      uax(max, min(i, t));
    }
  }
  printf("%d", max);
}

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

#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define forn(i, n) for (int i = 0; i < (int)(n); ++i)
#define for1(i, n) for (int i = 1; i <= (int)(n); ++i)
#define ford(i, n) for (int i = (int)(n)-1; i >= 0; --i)
#define fore(i, a, b) for (int i = (int)(a); i <= (int)(b); ++i)

using namespace std;

typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pii> vpi;
typedef vector<vi> vvi;
typedef long long ll;
typedef vector<ll> vll;
typedef vector<vll> vvll;
typedef pair<ll, ll> pll;
typedef double ld;

template <class T> bool uin(T &a, T b) { return a > b ? (a = b, true) : false; }
template <class T> bool uax(T &a, T b) { return a < b ? (a = b, true) : false; }

int main() {
  char s[5050];
  int n;
  scanf("%d", &n);
  scanf("%s", s);
  int max = 0;
  forn(i, n) {
    int t = 0;
    forn(j, n - i) {
      t = (t + 1) * (s[j] == s[j + i]);
      uax(max, min(i, t));
    }
  }
  printf("%d", max);
}
Problem p02913 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define exrep(i, a, b) for (ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;
const ll MAX_N = 100010;

int main() {
  ll n;
  string s;
  cin >> n >> s;

  vvl dp(n, vl(n)); // dp[i][j] : s[i]とs[j]から始めたとき一致する長さの最大値
  for (ll i = n - 1; i >= 0; i--) {
    for (ll j = n - 1; j >= 0; j--) {
      if (s[i] == s[j]) {
        dp[i][j] = min(j - i, dp[i + 1][j + 1] + 1);
      }
    }
  }

  ll ans = 0;
  rep(i, n - 1) {
    exrep(j, i + 1, n - 1) { chmax(ans, dp[i][j]); }
  }

  out(ans);
  re0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
typedef long long ll;
typedef vector<ll> vl;
typedef vector<vector<ll>> vvl;
#define rep(i, n) for (ll i = 0; i < n; i++)
#define exrep(i, a, b) for (ll i = a; i <= b; i++)
#define out(x) cout << x << endl
#define exout(x) printf("%.10f\n", x)
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define pb push_back
#define re0 return 0
const ll mod = 1000000007;
const ll INF = 1e16;
const ll MAX_N = 100010;

int main() {
  ll n;
  string s;
  cin >> n >> s;

  vvl dp(n + 1,
         vl(n + 1)); // dp[i][j] : s[i]とs[j]から始めたとき一致する長さの最大値
  for (ll i = n - 1; i >= 0; i--) {
    for (ll j = n - 1; j >= 0; j--) {
      if (s[i] == s[j]) {
        dp[i][j] = min(j - i, dp[i + 1][j + 1] + 1);
      }
    }
  }

  ll ans = 0;
  rep(i, n - 1) {
    exrep(j, i + 1, n - 1) { chmax(ans, dp[i][j]); }
  }

  out(ans);
  re0;
}
Problem p02913 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
const ll MOD = 1e9 + 7;
const ll inf = (LLONG_MAX >> 2);
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll n;
  string s;
  cin >> n >> s;
  ll ans = 0;
  for (ll l = n / 2; l > 0; --l) {
    if (l <= ans)
      break;
    for (ll i = 0; i < n; ++i) {
      for (ll j = l; j < n; ++j) {
        for (ll k = i; k < j and j + k - i < n; ++k) {
          if (s[k] != s[j + k - i])
            break;
          ans = max(ans, k - i + 1);
        }
      }
    }
  }
  cout << ans;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
const ll MOD = 1e9 + 7;
const ll inf = (LLONG_MAX >> 2);
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  ll n;
  string s;
  cin >> n >> s;
  ll ans = 0;
  for (ll l = n - 1; l > 0; --l) {
    ll crt = 0;
    for (ll i = 0; i + l < n; ++i) {
      if (s[i] == s[i + l]) {
        ++crt;
        ans = max(ans, min(l, crt));
      } else
        crt = 0;
    }
  }
  cout << ans;
  return 0;
}
Problem p02913 (C++)

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

vector<int> z_algorithm(string s) {
  vector<int> a(s.size());

  int c = 0;
  for (int i = 1; i < s.size(); i++) {
    if (i + a[i - c] < c + a[c]) {
      a[i] = a[i - c];
    } else {
      int j = max(0, c + a[c] - i);
      while (i + j < s.size() && s[j] == s[i + j])
        ++j;
      a[i] = j;
    }
  }
  a[0] = a.size();

  return a;
}

int main() {
  int n;
  string s;

  cin >> n >> s;

  int ans = 0;

  for (int i = 0; i < n - 1; i++) {
    auto z = z_algorithm(s.substr(i));

    for (int j = 0; j < z.size(); j++) {
      if (z[j] - 1 < j) {
        ans = max(ans, z[j]);
      }
    }
  }

  cout << ans << endl;
}

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

vector<int> z_algorithm(string s) {
  vector<int> a(s.size());

  int c = 0;
  for (int i = 1; i < s.size(); i++) {
    if (i + a[i - c] < c + a[c]) {
      a[i] = a[i - c];
    } else {
      int j = max(0, c + a[c] - i);
      while (i + j < s.size() && s[j] == s[i + j])
        ++j;
      a[i] = j;
      c = i;
    }
  }
  a[0] = a.size();

  return a;
}

int main() {
  int n;
  string s;

  cin >> n >> s;

  int ans = 0;

  for (int i = 0; i < n - 1; i++) {
    auto z = z_algorithm(s.substr(i));

    for (int j = 0; j < z.size(); j++) {
      if (z[j] - 1 < j) {
        ans = max(ans, z[j]);
      }
    }
  }

  cout << ans << endl;
}
Problem p02913 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI = 3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

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

#define CEIL(x, y) (((x) + (y)-1) / (y))
#define MOD 1000000007ULL

#define IN(l, r, x) ((l) <= (x) && (x) < (r))

int n;
string s;

bool ok(int len) {
  bool ret = 0;
  for (int i = 0; i < n - len; ++i) {
    map<string, bool> m;
    m[s.substr(i, len)] = true;
    for (int j = i + len; j < n; ++j) {
      if (m[s.substr(j, len)])
        ret = true;
    }
  }
  return ret;
}

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

  cin >> n;
  cin >> s;

  int l = 0, r = n / 2 + 1;
  while (l + 1 < r) {
    int m = (l + r) / 2;
    if (ok(m)) {
      l = m;
    } else {
      r = m;
    }
  }

  cout << l << "\n";
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef uint64_t u64;
typedef int64_t s64;
typedef uint32_t u32;
typedef int32_t s32;
typedef vector<s32> vs32;
typedef vector<u32> vu32;
typedef vector<s64> vs64;
typedef vector<u64> vu64;

const double PI = 3.14159265358979323846;

#define MAX(x, y) ((x) < (y) ? (y) : (x))
#define MIN(x, y) ((x) > (y) ? (y) : (x))

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

#define CEIL(x, y) (((x) + (y)-1) / (y))
#define MOD 1000000007ULL

#define IN(l, r, x) ((l) <= (x) && (x) < (r))

int n;
string s;

bool ok(int len) {
  bool ret = false;
  map<string, int> m;
  for (int i = 0; i <= n - len; ++i) {
    string t = s.substr(i, len);
    if (m[t] == 0) {
      m[t] = i + 1 + len;
    } else if (m[t] <= i + 1) {
      ret = true;
    }
  }
  return ret;
}

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

  cin >> n;
  cin >> s;

  int l = 0, r = n / 2 + 1;
  while (l + 1 < r) {
    int m = (l + r) / 2;
    if (ok(m)) {
      l = m;
    } else {
      r = m;
    }
  }

  cout << l << "\n";
  return 0;
}
Problem p02913 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define vi vector<int>
#define br cout << "\n";
#define all(x) (x).begin(), (x).end()
#define tr(c, i) for (auto i : c)
#define pii pair<int, int>
#define fast_io()                                                              \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr)
#define pq                                                                     \
  priority_queue<pair<ll, pii>, vector<pair<ll, pii>>, greater<pair<ll, pii>>> \
      p; // container adapter makes ascending q
#define er(x) cout << x << " "
#define err(x, y) cout << x << " " << y
const int M = 200005;
using namespace std;
clock_t time_p = clock();
void dem() {
  time_p = clock() - time_p;
  cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n";
}

using namespace std;
const int N = 5 * 1000;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
int lcp[N][N];
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int res = 0;
  for (int i = n - 1; i >= 0; --i) {

    for (int j = n - 1; j >= i; --j) {
      lcp[i][j] = s[i] == s[j] ? 1 + lcp[i + 1][j + 1] : 0;
      res = max(min(j - i, lcp[i][j]), res);
    }
  }

  cout << res;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long int
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
#define vi vector<int>
#define br cout << "\n";
#define all(x) (x).begin(), (x).end()
#define tr(c, i) for (auto i : c)
#define pii pair<int, int>
#define fast_io()                                                              \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(nullptr)
#define pq                                                                     \
  priority_queue<pair<ll, pii>, vector<pair<ll, pii>>, greater<pair<ll, pii>>> \
      p; // container adapter makes ascending q
#define er(x) cout << x << " "
#define err(x, y) cout << x << " " << y
const int M = 200005;
using namespace std;
clock_t time_p = clock();
void dem() {
  time_p = clock() - time_p;
  cerr << "Time Taken : " << (float)(time_p) / CLOCKS_PER_SEC << "\n";
}

using namespace std;
const int N = 5 * 1000 + 10;
const int dx[] = {1, -1, 0, 0};
const int dy[] = {0, 0, 1, -1};
int lcp[N][N];
int main() {
  int n;
  cin >> n;
  string s;
  cin >> s;
  int res = 0;
  for (int i = n - 1; i >= 0; --i) {

    for (int j = n - 1; j >= i; --j) {
      lcp[i][j] = s[i] == s[j] ? 1 + lcp[i + 1][j + 1] : 0;
      res = max(min(j - i, lcp[i][j]), res);
    }
  }

  cout << res;
  return 0;
}
Problem p02913 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n, m, i, j, k, l, ma, res = 0;
  vector<int> v[26];
  string s;
  cin >> n;
  cin >> s;
  for (i = n - 1; i >= 0; i--)
    v[(int)(s[i] - 'a')].push_back(i);
  for (i = 0; i < n - 1; i++) {
    m = (int)(s[i] - 'a');
    if (v[m].size() < 2)
      continue;
    for (j = 0; j < v[m].size() - 1; j++) {
      k = v[m][j];
      if (min(k - i, n - k) <= res)
        continue;
      if (s.substr(i, res + 1) != s.substr(k, res + 1))
        continue;
      res++;
      ma = min(k - i, n - k);
      if (ma == res)
        continue;
      l = ma;
      while (ma > res) {
        if (s.substr(i, l) != s.substr(k, l)) {
          if (ma == l)
            ma--;
          l = (l + res) / 2;
        } else {
          res = l;
          l = (ma + res + 1) / 2;
        }
      }
    }
    v[m].pop_back();
  }
  cout << res << endl;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
int main() {
  int n, m, i, j, k, l, ma, res = 0;
  vector<int> v[26];
  string s;
  cin >> n;
  cin >> s;
  for (i = n - 1; i >= 0; i--)
    v[(int)(s[i] - 'a')].push_back(i);
  for (i = 0; i < n - 1; i++) {
    m = (int)(s[i] - 'a');
    if (v[m].size() < 2)
      continue;
    for (j = 0; j < v[m].size() - 1; j++) {
      k = v[m][j];
      if (min(k - i, n - k) <= res)
        continue;
      if (s.substr(i, res + 1) != s.substr(k, res + 1))
        continue;
      res++;
      ma = min(k - i, n - k);
      if (ma == res)
        continue;
      l = ma;
      while (l > res) {
        if (s.substr(i + res, l - res) != s.substr(k + res, l - res)) {
          ma = l - 1;
          l = (l + res) / 2;
        } else {
          res = l;
          l = (ma + res + 1) / 2;
        }
      }
    }
    v[m].pop_back();
  }
  cout << res << endl;
  return 0;
}
Problem p02913 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
S = input()

ans = 0

# 2文字以上
for i in range(0, N - 3):
    for j in range(i + 2, N):
        if S[i : i + 1] == S[j : j + 1]:
            length, max_length = 2, j
            i, j = i + 1, j + 1
            while True:
                if i + 1 == max_length or j + 1 == N:
                    break
                else:
                    print(i, j)
                    i, j = i + 1, j + 1
                    print(i, j)
                    if S[i] == S[j]:
                        length += 1
                    else:
                        break
            if length > ans:
                ans = length

# 1文字
if ans == 0:
    for i in range(N - 1):
        s = S[i]
        if s in S[i + 1 :]:
            ans = 1

print(ans)


Fixed submission:
N = int(input())
S = input()

ans = 0
left, right = 0, 1
while right < N:
    s = S[left:right]
    if s in S[right:]:
        ans = max(ans, len(s))
        right += 1
    else:
        left += 1

print(ans)
Problem p02913 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>

using namespace std;

int n;
string str;

bool check(int mid) {
  for (int i = 0; i < n - mid; i++) {
    for (int j = i + mid; j < n; j++) {
      if (str[i] != str[j])
        continue;
      if (str.substr(i, mid) == str.substr(j, mid)) {
        return true;
      }
    }
  }
  return false;
}

int main() {
  cin >> n;
  cin >> str;
  int s = 0, g = n / 2 + 1;
  while (g - s > 1) {
    int mid = (s + g) / 2;
    if (check(mid)) {
      s = mid;
    } else {
      g = mid;
    }
  }
  cout << s << "\n";
}

Fixed submission:
#include <iostream>
#include <string>

using namespace std;

int n;
string str;

bool check(int mid) {
  for (int i = 0; i < n - mid; i++) {
    if (string(str.begin() + i + mid, str.end()).find(str.substr(i, mid)) !=
        string::npos) {
      return true;
    }
  }
  return false;
}

int main() {
  cin >> n;
  cin >> str;
  int s = 0, g = n / 2 + 1;
  while (g - s > 1) {
    int mid = (s + g) / 2;
    if (check(mid)) {
      s = mid;
    } else {
      g = mid;
    }
  }
  cout << s << "\n";
}
Problem p02913 (C++)

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = (1 << 30) - 1;
const ll LINF = (1LL << 60) - 1;

int dp[3010][3010];

int main() {
  int n;
  string s;
  cin >> n >> s;

  for (int i = n - 1; i >= 0; --i) {
    for (int j = n - 1; j >= i; --j) {
      if (s[i] == s[j]) {
        dp[i][j] = dp[i + 1][j + 1] + 1;
      }
    }
  }

  int res = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = i; j < n; ++j) {
      if (dp[i][j] <= j - i)
        chmax(res, dp[i][j]);
    }
  }
  cout << res << endl;
}

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
const int INF = (1 << 30) - 1;
const ll LINF = (1LL << 60) - 1;

int dp[5010][5010];

int main() {
  int n;
  string s;
  cin >> n >> s;

  for (int i = n - 1; i >= 0; --i) {
    for (int j = n - 1; j >= i; --j) {
      if (s[i] == s[j]) {
        dp[i][j] = dp[i + 1][j + 1] + 1;
      }
    }
  }

  int res = 0;
  for (int i = 0; i < n; ++i) {
    for (int j = i; j < n; ++j) {
      if (dp[i][j] <= j - i)
        chmax(res, dp[i][j]);
    }
  }
  cout << res << endl;
}
Problem p02913 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define fi first
#define sc second
#define ALL(x) x.begin(), x.end()
#define RALL(X) x.begin(), x.end()
#define FOR(i, n, k) for (i = 0; i < n; i += k)
#define FO(i, n, k) for (i = 1; i <= n; i += k)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define N 100005
#define mid ((l + r) / 2)
#define dbg(x) (cerr << #x << " : " << x << endl)
#define endl "\n"

using namespace std;

#define int long long

const int NMAX = 1e3 + 5;

int dp[NMAX][NMAX];
int n, res;

string s;

signed main() {
  ios_base::sync_with_stdio(false);

  cin >> n;
  cin >> s;

  for (int i = n - 1; i >= 0; i--) {
    for (int j = n - 1; j > i; j--) {
      if (s[i] == s[j]) {
        dp[i][j] = max(dp[i][j], 1 + dp[i + 1][j + 1]);
      }
      res = max(res, min(dp[i][j], j - i));
    }
  }

  cout << res;

  return 0;
}

/*
ababab

2 7
2 3 4 5 6 7

1 8
1 2 3 4 5 6 7 8
*/

Fixed submission:
#include <bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define fi first
#define sc second
#define ALL(x) x.begin(), x.end()
#define RALL(X) x.begin(), x.end()
#define FOR(i, n, k) for (i = 0; i < n; i += k)
#define FO(i, n, k) for (i = 1; i <= n; i += k)
#define CLEAR(a, b) memset(a, b, sizeof(a))
#define N 100005
#define mid ((l + r) / 2)
#define dbg(x) (cerr << #x << " : " << x << endl)
#define endl "\n"

using namespace std;

#define int long long

const int NMAX = 5e3 + 5;

int dp[NMAX][NMAX];
int n, res;

string s;

signed main() {
  ios_base::sync_with_stdio(false);

  cin >> n;
  cin >> s;

  for (int i = n - 1; i >= 0; i--) {
    for (int j = n - 1; j > i; j--) {
      if (s[i] == s[j]) {
        dp[i][j] = max(dp[i][j], 1 + dp[i + 1][j + 1]);
      }
      res = max(res, min(dp[i][j], j - i));
    }
  }

  cout << res;

  return 0;
}

/*
ababab

2 7
2 3 4 5 6 7

1 8
1 2 3 4 5 6 7 8
*/