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

Buggy submission (status: Runtime Error):
/*   _ _ _
    | | | | ___  ___
    | | | || . ||  _|
    |_____||_  ||_|  _____
             |_|    |_____|  */
// Time : 19/10/20
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
const int nmax = 300 + 30;
int dis1[nmax][nmax], dis2[nmax][nmax];
int main() {
#ifdef Wqr_
  freopen("in.txt", "r", stdin);
#endif
  std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int n, m, l;
  cin >> n >> m >> l;
  int a, b, c;
  memset(dis1, inf, sizeof(dis1));
  memset(dis2, inf, sizeof(dis2));
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    --a, --b;
    dis1[a][b] = c;
    dis1[b][a] = c;
    dis1[i][i] = 0;
    dis2[i][i] = 0;
  }
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        dis1[i][j] = min(dis1[i][j], dis1[i][k] + dis1[k][j]);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (dis1[i][j] <= l) {
        dis2[i][j] = 1;
        dis2[j][i] = 1;
      }
    }
  }
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        dis2[i][j] = min(dis2[i][j], dis2[i][k] + dis2[k][j]);
      }
    }
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> a >> b;
    --a, --b;
    if (dis2[a][b] == inf)
      dis2[a][b] = 0;
    cout << (dis2[a][b] - 1) << endl;
  }
  return 0;
}

Fixed submission:
/*   _ _ _
    | | | | ___  ___
    | | | || . ||  _|
    |_____||_  ||_|  _____
             |_|    |_____|  */
// Time : 19/10/20
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef pair<int, int> pii;
const int nmax = 300 + 30;
int dis1[nmax][nmax], dis2[nmax][nmax];
int main() {
#ifdef Wqr_
  freopen("in.txt", "r", stdin);
#endif
  std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  int n, m, l;
  cin >> n >> m >> l;
  int a, b, c;
  memset(dis1, inf, sizeof(dis1));
  memset(dis2, inf, sizeof(dis2));
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    --a, --b;
    dis1[a][b] = c;
    dis1[b][a] = c;
  }
  for (int i = 0; i < n; i++) {
    dis1[i][i] = dis2[i][i] = 0;
  }
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        dis1[i][j] = min(dis1[i][j], dis1[i][k] + dis1[k][j]);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (dis1[i][j] <= l) {
        dis2[i][j] = 1;
        dis2[j][i] = 1;
      }
    }
  }
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        dis2[i][j] = min(dis2[i][j], dis2[i][k] + dis2[k][j]);
      }
    }
  }
  int q;
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> a >> b;
    --a, --b;
    if (dis2[a][b] == inf)
      dis2[a][b] = 0;
    cout << (dis2[a][b] - 1) << endl;
  }
  return 0;
}
Problem p02889 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
int main() {
  ll n, m, l, d[201][201], q, inf = 1e18;
  cin >> n >> m >> l;
  int a, b, c;
  rep(i, n) rep(j, n) d[i][j] = inf;
  rep(i, m) cin >> a >> b >> c, d[a - 1][b - 1] = c, d[b - 1][a - 1] = c;
  rep(i, n) d[i][i] = 0;
  rep(k, n) rep(i, n) rep(j, n) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
  rep(i, n) rep(j, n) {
    if (i == j)
      d[i][j] = 0;
    else if (d[i][j] <= l)
      d[i][j] = 1;
    else
      d[i][j] = inf;
  }
  rep(k, n) rep(i, n) rep(j, n) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
  cin >> q;
  rep(i, q) {
    int s, t;
    cin >> s >> t;
    if (d[s - 1][t - 1] == inf)
      cout << -1 << "\n";
    else
      cout << d[s - 1][t - 1] - 1 << "\n";
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<ll, ll> P;
typedef vector<ll> vec;
typedef vector<vec> mat;
int main() {
  ll n, m, l, d[301][301], q, inf = 1e18;
  cin >> n >> m >> l;
  int a, b, c;
  rep(i, n) rep(j, n) d[i][j] = inf;
  rep(i, m) cin >> a >> b >> c, d[a - 1][b - 1] = c, d[b - 1][a - 1] = c;
  rep(i, n) d[i][i] = 0;
  rep(k, n) rep(i, n) rep(j, n) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
  rep(i, n) rep(j, n) {
    if (i == j)
      d[i][j] = 0;
    else if (d[i][j] <= l)
      d[i][j] = 1;
    else
      d[i][j] = inf;
  }
  rep(k, n) rep(i, n) rep(j, n) d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
  cin >> q;
  rep(i, q) {
    int s, t;
    cin >> s >> t;
    if (d[s - 1][t - 1] == inf)
      cout << -1 << "\n";
    else
      cout << d[s - 1][t - 1] - 1 << "\n";
  }
}
Problem p02889 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>
using namespace std;

static const int MAX = 100;
static const long long INFTY = (1LL << 50);

int n;
long long d[MAX][MAX];
long long d_[MAX][MAX];

void floyd() {
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      if (d[i][k] == INFTY)
        continue;
      for (int j = 0; j < n; j++) {
        if (d[k][j] == INFTY)
          continue;
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }
}

void floyd_() {
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      if (d_[i][k] == INFTY)
        continue;
      for (int j = 0; j < n; j++) {
        if (d_[k][j] == INFTY)
          continue;
        d_[i][j] = min(d_[i][j], d_[i][k] + d_[k][j]);
      }
    }
  }
}

int main() {
  int e, u, v, c, l;
  cin >> n >> e >> l;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      d[i][j] = ((i == j) ? 0 : INFTY);
    }
  }

  for (int i = 0; i < e; i++) {
    cin >> u >> v >> c;
    d[u - 1][v - 1] = c;
    d[v - 1][u - 1] = c;
  }

  floyd();

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (d[i][j] <= l) {
        d_[i][j] = 1;
      } else {
        d_[i][j] = INFTY;
      }
    }
  }

  floyd_();

  int q, s, t;
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> s >> t;
    if (d_[s - 1][t - 1] == INFTY) {
      cout << -1 << endl;
    } else {
      cout << d_[s - 1][t - 1] - 1 << endl;
    }
  }
  return 0;
}

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

static const int MAX = 300;
static const long long INFTY = 9223372036854775807;

int n;
long long d[MAX][MAX];
long long d_[MAX][MAX];

void floyd() {
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      if (d[i][k] == INFTY)
        continue;
      for (int j = 0; j < n; j++) {
        if (d[k][j] == INFTY)
          continue;
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }
}

void floyd_() {
  for (int k = 0; k < n; k++) {
    for (int i = 0; i < n; i++) {
      if (d_[i][k] == INFTY)
        continue;
      for (int j = 0; j < n; j++) {
        if (d_[k][j] == INFTY)
          continue;
        d_[i][j] = min(d_[i][j], d_[i][k] + d_[k][j]);
      }
    }
  }
}

int main() {
  int e, u, v, c, l;
  cin >> n >> e >> l;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      d[i][j] = ((i == j) ? 0 : INFTY);
    }
  }

  for (int i = 0; i < e; i++) {
    cin >> u >> v >> c;
    d[u - 1][v - 1] = c;
    d[v - 1][u - 1] = c;
  }

  floyd();

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (d[i][j] <= l) {
        d_[i][j] = 1;
      } else {
        d_[i][j] = INFTY;
      }
    }
  }

  floyd_();

  int q, s, t;
  cin >> q;
  for (int i = 0; i < q; i++) {
    cin >> s >> t;
    if (d_[s - 1][t - 1] == INFTY) {
      cout << -1 << endl;
    } else {
      cout << d_[s - 1][t - 1] - 1 << endl;
    }
  }
  return 0;
}
Problem p02889 (C++)

Buggy submission (status: Time Limit Exceeded):
#if 1

#include <bits/stdc++.h>
using namespace std;
int n, m, l;
int d[305][305];
vector<pair<int, int>> v[305];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m >> l;
  int x, y, z;
  for (int i = 1; i <= m; i++) {
    cin >> x >> y >> z;
    v[x].push_back({y, z});
    v[y].push_back({x, z});
  }
  priority_queue<pair<pair<int, int>, int>> q;
  int c[305];
  for (int i = 1; i <= n; i++) {
    q.push({{0, 0}, i});
    memset(c, 0, sizeof(c));
    while (!q.empty()) {
      x = -q.top().first.first;
      y = -q.top().first.second;
      z = q.top().second;
      q.pop();
      if (c[z])
        continue;
      c[z] = 1;
      d[i][z] = x;
      for (auto k : v[z]) {
        if (k.second > l)
          continue;
        if (y + k.second > l) {
          q.push({{-x - 1, -k.second}, k.first});
        } else
          q.push({{-x, -y - k.second}, k.first});
      }
    }
    for (int j = 1; j <= n; j++) {
      if (c[j] == 0)
        d[i][j] = -1;
    }
  }
  int qq;
  cin >> qq;
  while (qq--) {
    cin >> x >> y;
    cout << d[x][y] << '\n';
  }
}

#endif


Fixed submission:
#if 1

#include <bits/stdc++.h>
using namespace std;
int n, m, l;
int d[305][305];
vector<pair<int, int>> v[305];

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m >> l;
  int x, y, z;
  for (int i = 1; i <= m; i++) {
    cin >> x >> y >> z;
    v[x].push_back({y, z});
    v[y].push_back({x, z});
  }
  priority_queue<pair<pair<int, int>, int>> q;
  int c[305];
  for (int i = 1; i <= n; i++) {
    q.push({{0, 0}, i});
    memset(c, 0, sizeof(c));
    while (!q.empty()) {
      x = -q.top().first.first;
      y = -q.top().first.second;
      z = q.top().second;
      q.pop();
      if (c[z])
        continue;
      c[z] = 1;
      d[i][z] = x;
      for (auto k : v[z]) {
        if (c[k.first])
          continue;
        if (k.second > l)
          continue;
        if (y + k.second > l) {
          q.push({{-x - 1, -k.second}, k.first});
        } else
          q.push({{-x, -y - k.second}, k.first});
      }
    }
    for (int j = 1; j <= n; j++) {
      if (c[j] == 0)
        d[i][j] = -1;
    }
  }
  int qq;
  cin >> qq;
  while (qq--) {
    cin >> x >> y;
    cout << d[x][y] << '\n';
  }
}

#endif
Problem p02889 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;

const long MAXV = 101;
const long MAXD = (long)1e12;

long V;

void warshall_floyd(vector<vector<long>> &d) {
  for (long k = 1; k <= V; k++) {
    for (long i = 1; i <= V; i++) {
      for (long j = 1; j <= V; j++) {
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }
  return;
}

int main() {
  long N, M, L, Q;
  cin >> N >> M >> L;

  vector<vector<long>> d(MAXV, vector<long>(MAXV, MAXD));
  vector<vector<long>> f(MAXV, vector<long>(MAXV, MAXD));

  V = N;

  vector<long> ans;

  for (long i = 0; i < M; i++) {
    long a, b, c;
    cin >> a >> b >> c;

    d[a][b] = c;
    d[b][a] = c;
  }

  warshall_floyd(d);

  for (long i = 1; i <= V; i++) {
    for (long j = 1; j <= V; j++) {
      if (d[i][j] <= L) {
        f[i][j] = 1;
        f[j][i] = 1;
      }
    }
  }

  warshall_floyd(f);

  cin >> Q;

  vector<long> s(Q), t(Q), r(Q);
  for (long i = 0; i < Q; i++) {
    long result;
    cin >> s[i] >> t[i];
    /*
    if( d[s[i]][t[i]]<MAXD ){
        r[i] = d[s[i]][t[i]];
    }else{
        r[i] = -1;
    }
    */
    if (f[s[i]][t[i]] >= MAXD) {
      result = -1;
      ans.push_back(result);
    } else {
      result = f[s[i]][t[i]] - 1;
      ans.push_back(result);
    }
  }

  for (long i = 0; i < Q; i++) {
    // cout << "i s t d[s][t] r[i]" << i << " " << s[i] << " " << t[i] << " " <<
    // r[i] << endl;
    cout << ans[i] << endl;
  }

  return 0;
}

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

const long MAXV = 301;
const long MAXD = (long)1e12;

long V;

void warshall_floyd(vector<vector<long>> &d) {
  for (long k = 1; k <= V; k++) {
    for (long i = 1; i <= V; i++) {
      for (long j = 1; j <= V; j++) {
        d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
      }
    }
  }
  return;
}

int main() {
  long N, M, L, Q;
  cin >> N >> M >> L;

  vector<vector<long>> d(MAXV, vector<long>(MAXV, MAXD));
  vector<vector<long>> f(MAXV, vector<long>(MAXV, MAXD));

  V = N;

  vector<long> ans;

  for (long i = 0; i < M; i++) {
    long a, b, c;
    cin >> a >> b >> c;

    d[a][b] = c;
    d[b][a] = c;
  }

  warshall_floyd(d);

  for (long i = 1; i <= V; i++) {
    for (long j = 1; j <= V; j++) {
      if (d[i][j] <= L) {
        f[i][j] = 1;
        f[j][i] = 1;
      }
    }
  }

  warshall_floyd(f);

  cin >> Q;

  vector<long> s(Q), t(Q), r(Q);
  for (long i = 0; i < Q; i++) {
    long result;
    cin >> s[i] >> t[i];
    /*
    if( d[s[i]][t[i]]<MAXD ){
        r[i] = d[s[i]][t[i]];
    }else{
        r[i] = -1;
    }
    */
    if (f[s[i]][t[i]] >= MAXD) {
      result = -1;
      ans.push_back(result);
    } else {
      result = f[s[i]][t[i]] - 1;
      ans.push_back(result);
    }
  }

  for (long i = 0; i < Q; i++) {
    // cout << "i s t d[s][t] r[i]" << i << " " << s[i] << " " << t[i] << " " <<
    // r[i] << endl;
    cout << ans[i] << endl;
  }

  return 0;
}
Problem p02890 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef long double ld;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
#define REP(i, m, n) for (ll i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define F first
#define S second
constexpr char ln = '\n';

const int mx = 100010;
const ll mod = 1e9 + 7;

int main() {
  ll n;
  cin >> n;
  vector<int> a(n);
  map<int, int> mp;
  rep(i, n) {
    cin >> a[i];
    mp[a[i]]++;
  }

  vector<ll> b;
  for (auto it : mp) {
    b.emplace_back(it.S);
  }
  sort(b.begin(), b.end());
  ll m = b.size();
  vector<ll> r(n + 1, 0);
  rep(i, n) { r[i + 1] = r[i] + b[i]; }

  REP(k, 1, n + 1) {
    ll ok = -1, ng = n + 1, mid = (ok + ng) / 2;
    while (ng - ok > 1) {
      mid = (ok + ng) / 2;
      ll pos = lower_bound(b.begin(), b.end(), mid) - b.begin();
      ll cnt = r[pos] + mid * (m - pos);
      if (cnt >= mid * k) {
        ok = mid;
      } else {
        ng = mid;
      }
    }
    ll ans = ok;
    cout << ans << ln;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
typedef long long ll;
typedef long double ld;
const int inf = 1e9 + 7;
const ll longinf = 1LL << 60;
#define REP(i, m, n) for (ll i = (int)(m); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
#define F first
#define S second
constexpr char ln = '\n';

const int mx = 100010;
const ll mod = 1e9 + 7;

int main() {
  ll n;
  cin >> n;
  vector<int> a(n);
  map<int, int> mp;
  rep(i, n) {
    cin >> a[i];
    mp[a[i]]++;
  }

  vector<ll> b;
  for (auto it : mp) {
    b.emplace_back(it.S);
  }
  sort(b.begin(), b.end());
  ll m = b.size();
  vector<ll> r(n + 1, 0);
  rep(i, n) { r[i + 1] = r[i] + (i < m ? b[i] : 0); }

  REP(k, 1, n + 1) {
    ll ok = -1, ng = n + 1, mid = (ok + ng) / 2;
    while (ng - ok > 1) {
      mid = (ok + ng) / 2;
      ll pos = lower_bound(b.begin(), b.end(), mid) - b.begin();
      ll cnt = r[pos] + mid * (m - pos);
      if (cnt >= mid * k) {
        ok = mid;
      } else {
        ng = mid;
      }
    }
    ll ans = ok;
    cout << ans << ln;
  }
  return 0;
}
Problem p02890 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define int long long
int MOD = 1000000007;
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<int> A(N);
  map<int, int> mp;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    mp[A[i]]++;
  }
  A.clear();
  for (auto m : mp) {
    A.push_back(m.second);
  }
  while (A.size() < N)
    A.push_back(0);
  sort(A.begin(), A.end());
  int cur = 0;
  int sum = 0;
  vector<int> res(N + 1, 0);
  for (int i = 1; i <= N; i++) {
    while (A[cur] < i) {
      sum += A[cur];
      cur++;
    }
    int t = i * (N - cur) + sum;
    // cerr << i << " " << t / i << endl;
    res[t / i] = i;
  }
  for (int i = N - 1; i > 0; i--) {
    res[i] = max(res[i], res[i + 1]);
  }
  for (int i = 0; i < N; i++) {
    cout << res[i + 1] << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
#define int long long
int MOD = 1000000007;
signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<int> A(N);
  map<int, int> mp;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    mp[A[i]]++;
  }
  A.clear();
  for (auto m : mp) {
    A.push_back(m.second);
  }
  while (A.size() < N)
    A.push_back(0);
  sort(A.begin(), A.end());
  int cur = 0;
  int sum = 0;
  vector<int> res(N + 1, 0);
  for (int i = 1; i <= N; i++) {
    while (cur < N && A[cur] < i) {
      sum += A[cur];
      cur++;
    }
    int t = i * (N - cur) + sum;
    // cerr << i << " " << t / i << endl;
    res[t / i] = i;
  }
  for (int i = N - 1; i > 0; i--) {
    res[i] = max(res[i], res[i + 1]);
  }
  for (int i = 0; i < N; i++) {
    cout << res[i + 1] << endl;
  }
}
Problem p02890 (C++)

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

using namespace std;

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

  vector<int> c(n);

  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;

    c[a - 1]++;
  }

  sort(c.rbegin(), c.rend());

  vector<int> d(n);

  int j = n - 1;
  int tot = 0;
  for (int i = 1; i <= n; i++) {
    while (j >= 0 && c[j] <= i) {
      tot += c[j];
      j--;
    }

    d[i] = ((j + 1) * i + tot) / i;
  }

  for (int i = 1; i <= n; i++) {
    int ok = 0, ng = n + 1;

    while (ng - ok > 1) {
      int m = (ok + ng) / 2;

      if (d[m] >= i) {
        ok = m;
      } else {
        ng = m;
      }
    }

    cout << ok << endl;
  }
}


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

using namespace std;

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

  vector<int> c(n);

  for (int i = 0; i < n; i++) {
    int a;
    cin >> a;

    c[a - 1]++;
  }

  sort(c.rbegin(), c.rend());

  vector<int> d(n + 1);

  int j = n - 1;
  int tot = 0;
  for (int i = 1; i <= n; i++) {
    while (j >= 0 && c[j] <= i) {
      tot += c[j];
      j--;
    }

    d[i] = ((j + 1) * i + tot) / i;
  }

  for (int i = 1; i <= n; i++) {
    int ok = 0, ng = n + 1;

    while (ng - ok > 1) {
      int m = (ok + ng) / 2;

      if (d[m] >= i) {
        ok = m;
      } else {
        ng = m;
      }
    }

    cout << ok << endl;
  }
}
Problem p02890 (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 rep(i, n) for (ll i = 0; i < n; ++i)
#define rep2(i, a, b) for (ll i = a; i < b; ++i)
const ll MOD = 1000000007;
const ll INF = 1e9;
const ll IINF = 1e18;
const double EPS = 1e-8;
const double pi = acos(-1);

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int N;
  cin >> N;
  map<int, int> m;
  rep(i, N) {
    int a;
    cin >> a;
    m[a]++;
  }
  vector<int> cnt;
  for (auto x : m) {
    cnt.push_back(x.second);
  }
  sort(cnt.begin(), cnt.end());
  int s = cnt.size();
  vector<int> cnt2(s);
  cnt2[0] = 0;
  rep(i, s) { cnt2[i + 1] = cnt2[i] + cnt[i]; }
  rep(i, N) {
    int lb = -1, ub = N + 1;
    while (ub - lb > 1) {
      int mid = (ub + lb) / 2;
      int x = lower_bound(cnt.begin(), cnt.end(), mid) - cnt.begin();
      int sum = cnt2[x] + mid * (s - x);
      if (sum >= (i + 1) * mid) {
        lb = mid;
      } else {
        ub = mid;
      }
    }
    cout << lb << 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 rep(i, n) for (ll i = 0; i < n; ++i)
#define rep2(i, a, b) for (ll i = a; i < b; ++i)
const ll MOD = 1000000007;
const ll INF = 1e9;
const ll IINF = 1e18;
const double EPS = 1e-8;
const double pi = acos(-1);

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

int main() {
  int N;
  cin >> N;
  map<int, int> m;
  rep(i, N) {
    int a;
    cin >> a;
    m[a]++;
  }
  vector<int> cnt;
  for (auto x : m) {
    cnt.push_back(x.second);
  }
  sort(cnt.begin(), cnt.end());
  int s = cnt.size();
  vector<int> cnt2(s + 1);
  cnt2[0] = 0;
  rep(i, s) { cnt2[i + 1] = cnt2[i] + cnt[i]; }
  rep(i, N) {
    int lb = -1, ub = N + 1;
    while (ub - lb > 1) {
      int mid = (ub + lb) / 2;
      int x = lower_bound(cnt.begin(), cnt.end(), mid) - cnt.begin();
      int sum = cnt2[x] + mid * (s - x);
      if (sum >= (i + 1) * mid) {
        lb = mid;
      } else {
        ub = mid;
      }
    }
    cout << lb << endl;
  }
}
Problem p02890 (C++)

Buggy submission (status: Runtime Error):
#pragma GCC optimize("-O3")
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
void solve();
signed main() {
#ifdef Sprinkle
  clock_t clock_start = clock();
  assert(freopen("test.txt", "r", stdin));
#else
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#endif
  solve();
#ifdef Sprinkle
  cerr << "Running Time: " << ((clock() - clock_start) * 1.0 / CLOCKS_PER_SEC)
       << "s\n";
#endif
}
#ifdef Sprinkle
void err() { cerr << "\n"; }
template <typename T, typename... U> void err(const T &hd, const U &...tl) {
  cerr << hd << (sizeof...(tl) ? ", " : "");
  err(tl...);
}
#define debug(x...)                                                            \
  cerr << "" #x " = ";                                                         \
  err(x)
#else
#define debug(...) 0
#endif
int n;
int a[300005], qian[300005];
int ans[300005];
void solve() {
  int n, k;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> k;
    a[k]++;
  }
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; ++i) {
    qian[i] = qian[i - 1] + a[i];
  }
  a[n + 1] = n + 1;
  int t = 1;
  for (int i = 1; i <= n; ++i) {
    while (a[t] * (t - i) <= qian[t - 1])
      t++;
    ans[n - i + 1] = qian[t - 1] / (t - i);
  }
  for (int i = 1; i <= n; ++i) {
    cout << ans[i] << endl;
  }
}

Fixed submission:
#pragma GCC optimize("-O3")
#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
void solve();
signed main() {
#ifdef Sprinkle
  clock_t clock_start = clock();
  assert(freopen("test.txt", "r", stdin));
#else
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#endif
  solve();
#ifdef Sprinkle
  cerr << "Running Time: " << ((clock() - clock_start) * 1.0 / CLOCKS_PER_SEC)
       << "s\n";
#endif
}
#ifdef Sprinkle
void err() { cerr << "\n"; }
template <typename T, typename... U> void err(const T &hd, const U &...tl) {
  cerr << hd << (sizeof...(tl) ? ", " : "");
  err(tl...);
}
#define debug(x...)                                                            \
  cerr << "" #x " = ";                                                         \
  err(x)
#else
#define debug(...) 0
#endif
int n;
int a[300005], qian[300005];
int ans[300005];
void solve() {
  int n, k;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> k;
    a[k]++;
  }
  sort(a + 1, a + n + 1);
  for (int i = 1; i <= n; ++i) {
    qian[i] = qian[i - 1] + a[i];
  }
  a[n + 1] = n + 1;
  int t = 1;
  for (int i = 1; i <= n; ++i) {
    while (t < n + 1 && 1ll * a[t] * (t - i) <= qian[t - 1])
      t++;
    ans[n - i + 1] = qian[t - 1] / (t - i);
  }
  for (int i = 1; i <= n; ++i) {
    cout << ans[i] << endl;
  }
}
Problem p02890 (C++)

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

int N;
int A[30010];
vector<int> V;
int ans[300010];

int main() {

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

  for (int i = 1; i <= N; i++) {
    V.push_back(A[i]);
  }
  sort(V.begin(), V.end());

  int tmp = N;
  int sum = 0;
  int idx = 0;
  for (int i = 0; i < N; i++) {
    while (V.size() != idx && V[idx] == i) {
      idx++;
      tmp--;
    }

    sum += tmp;

    ans[sum / (i + 1)] = i + 1;
  }

  int MAX = 0;
  for (int i = N; i > 0; i--) {
    MAX = max(MAX, ans[i]);
    ans[i] = MAX;
  }

  for (int i = 1; i <= N; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}


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

int N;
int A[300010];
vector<int> V;
int ans[300010];

int main() {

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

  for (int i = 1; i <= N; i++) {
    V.push_back(A[i]);
  }
  sort(V.begin(), V.end());

  int tmp = N;
  int sum = 0;
  int idx = 0;
  for (int i = 0; i < N; i++) {
    while (V.size() != idx && V[idx] == i) {
      idx++;
      tmp--;
    }

    sum += tmp;

    ans[sum / (i + 1)] = i + 1;
  }

  int MAX = 0;
  for (int i = N; i > 0; i--) {
    MAX = max(MAX, ans[i]);
    ans[i] = MAX;
  }

  for (int i = 1; i <= N; i++) {
    cout << ans[i] << endl;
  }

  return 0;
}
Problem p02890 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int read() {
  int xx = 0, ff = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      ff = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    xx = xx * 10 + ch - '0';
    ch = getchar();
  }
  return xx * ff;
}
long long READ() {
  long long xx = 0, ff = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      ff = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    xx = xx * 10 + ch - '0';
    ch = getchar();
  }
  return xx * ff;
}
char one() {
  char ch = getchar();
  while (ch == ' ' || ch == '\n')
    ch = getchar();
  return ch;
}
const int maxn = 100010;
int N, a[maxn], b[maxn], c[maxn], d[maxn], e[maxn];
int main() {
  // freopen("in","r",stdin);
  N = read();
  for (int i = 1; i <= N; i++)
    a[read()]++;
  for (int i = 1; i < maxn; i++)
    b[a[i]]++; // b:occur times
  for (int i = 1; i <= N; i++) {
    c[i] = c[i - 1] + b[i] * i; // c:sum of occur times <= i
    d[i] = d[i - 1] + b[i];     // d:prefix sum
  }
  for (int i = 1; i <= N; i++)
    e[i] = c[i] / i + d[N] - d[i];
  for (int i = 1, j = N; i <= N; i++) {
    for (; j; j--)
      if (e[j] >= i) {
        printf("%d\n", j);
        break;
      }
    if (!j)
      printf("%d\n", j);
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>
using namespace std;
int read() {
  int xx = 0, ff = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      ff = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    xx = xx * 10 + ch - '0';
    ch = getchar();
  }
  return xx * ff;
}
long long READ() {
  long long xx = 0, ff = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      ff = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    xx = xx * 10 + ch - '0';
    ch = getchar();
  }
  return xx * ff;
}
char one() {
  char ch = getchar();
  while (ch == ' ' || ch == '\n')
    ch = getchar();
  return ch;
}
const int maxn = 300010;
int N, a[maxn], b[maxn], c[maxn], d[maxn], e[maxn];
int main() {
  // freopen("in","r",stdin);
  N = read();
  for (int i = 1; i <= N; i++)
    a[read()]++;
  for (int i = 1; i < maxn; i++)
    b[a[i]]++; // b:occur times
  for (int i = 1; i <= N; i++) {
    c[i] = c[i - 1] + b[i] * i; // c:sum of occur times <= i
    d[i] = d[i - 1] + b[i];     // d:prefix sum
  }
  for (int i = 1; i <= N; i++)
    e[i] = c[i] / i + d[N] - d[i];
  for (int i = 1, j = N; i <= N; i++) {
    for (; j; j--)
      if (e[j] >= i) {
        printf("%d\n", j);
        break;
      }
    if (!j)
      printf("%d\n", j);
  }
  return 0;
}
Problem p02890 (C++)

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

const int N = (3e5) + 5;
int n, a[N];
int freq[N];
int ac[N];

int f(int x, int pos) {
  while (pos >= 0) {
    if (freq[pos] < x)
      break;
    pos--;
  }
  return (pos >= 0 ? ac[pos] : 0) + x * (n - pos - 1);
}

int main() { // claudio van
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    freq[a[i] - 1]++;
  }
  sort(freq, freq + n);
  partial_sum(freq, freq + n, ac);
  int ans = n, pos = n - 1;
  for (int k = 1; k <= n; ++k) {
    while (ans > 0) {
      if (k <= f(ans, pos) / ans)
        break;
      ans--;
    }
    printf("%d\n", ans);
  }
  return 0;
}

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

const int N = (3e5) + 5;
int n, a[N];
int freq[N];
int ac[N];

int f(int x, int &pos) {
  while (pos >= 0) {
    if (freq[pos] < x)
      break;
    pos--;
  }
  return (pos >= 0 ? ac[pos] : 0) + x * (n - pos - 1);
}

int main() { // claudio van
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
    freq[a[i] - 1]++;
  }
  sort(freq, freq + n);
  partial_sum(freq, freq + n, ac);
  int ans = n, pos = n - 1;
  for (int k = 1; k <= n; ++k) {
    while (ans > 0) {
      if (k <= f(ans, pos) / ans)
        break;
      ans--;
    }
    printf("%d\n", ans);
  }
  return 0;
}
Problem p02890 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 3e5 + 7;
int a[maxn];
vector<int> ans;
vector<int> vec;
vector<int> rem;
long double f(int u, int k) {
  long double right = (long double)rem[u - 1] / (long double)(k - u);
  return right;
}
int SanFen(int l, int r, int k) // 找凸点
{
  while (l < r - 1) {
    int mid = (l + r) / 2;
    int mmid = (mid + r) / 2;
    if (f(mid, k) > f(mmid, k))
      l = mid;
    else
      r = mmid;
  }
  return f(l, k) > f(r, k) ? l : r;
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    a[x]++;
  }
  for (int i = 1; i < maxn; i++)
    if (a[i])
      vec.push_back(a[i]);
  sort(vec.begin(), vec.end(), greater<int>());
  for (int i = vec.size(); i <= n; i++)
    vec.push_back(0);
  rem.push_back(n - vec[0]);
  for (int i = 1; i < n; i++)
    rem.push_back(rem[i - 1] - vec[i]);
  ans.push_back(n);
  for (int kpl = 2; kpl <= n; kpl++) {
    int k = kpl;
    int l = SanFen(1, k, k);
    if (l >= k)
      l = k - 1;
    if (l <= 0)
      l = 1;
    int cnt = rem[l] / (k - l - 1);
    cnt = min(cnt, rem[l - 1] / (k - l));
    cnt = min(cnt, rem[l + 1] / (k - l - 2));
    cnt = min(cnt, n / k);
    ans.push_back(cnt);
  }
  for (auto u : ans)
    cout << u << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 3e5 + 7;
int a[maxn];
vector<int> ans;
vector<int> vec;
vector<int> rem;
long double f(int u, int k) {
  long double right = (long double)rem[u - 1] / (long double)(k - u);
  return right;
}
int SanFen(int l, int r, int k) // 找凸点
{
  while (l < r - 1) {
    int mid = (l + r) / 2;
    int mmid = (mid + r) / 2;
    if (f(mid, k) > f(mmid, k))
      l = mid;
    else
      r = mmid;
  }
  return f(l, k) > f(r, k) ? l : r;
}

int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    a[x]++;
  }
  for (int i = 1; i < maxn; i++)
    if (a[i])
      vec.push_back(a[i]);
  sort(vec.begin(), vec.end(), greater<int>());
  for (int i = vec.size(); i <= n; i++)
    vec.push_back(0);
  rem.push_back(n - vec[0]);
  for (int i = 1; i < n; i++)
    rem.push_back(rem[i - 1] - vec[i]);
  ans.push_back(n);
  for (int kpl = 2; kpl <= n; kpl++) {
    int k = kpl;
    int l = SanFen(1, k, k);
    if (l >= k)
      l = k - 1;
    if (l <= 0)
      l = 1;
    int cnt = rem[l - 1] / (k - l);
    if (l >= 0 && k != l + 1)
      cnt = min(cnt, rem[l] / (k - l - 1));
    if (l + 1 < k - 1)
      cnt = min(cnt, rem[l + 1] / (k - l - 2));
    cnt = min(cnt, n / k);
    ans.push_back(cnt);
  }
  for (auto u : ans)
    cout << u << endl;
}
Problem p02890 (C++)

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

#define fr first
#define se second

using namespace std;

const long long N = 2e5 + 7;
const long long inf = 1e9 + 7;
const long long mod = 1e9 + 7;

int n;
int a[N];
int main() {
  ios_base::sync_with_stdio(false);

  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    a[x]++;
  }
  sort(a + 1, a + n + 1);
  vector<int> p(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    p[i] = p[i - 1] + a[i];
  }
  int res = n;
  for (int k = 1; k <= n; k++) {
    while (true) {
      int l = 0, r = n;
      while (l < r) {
        int m = (l + r) / 2;
        if (a[m + 1] < res) {
          l = m + 1;
        } else {
          r = m;
        }
      }
      int s = p[l] + (n - l) * res;
      if (s >= k * res) {
        break;
      }
      res--;
    }
    cout << res << "\n";
  }
}

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

#define fr first
#define se second

using namespace std;

const long long N = 3e5 + 7;
const long long inf = 1e9 + 7;
const long long mod = 1e9 + 7;

int n;
int a[N];
int main() {
  ios_base::sync_with_stdio(false);

  cin >> n;
  for (int i = 1; i <= n; i++) {
    int x;
    cin >> x;
    a[x]++;
  }
  sort(a + 1, a + n + 1);
  vector<int> p(n + 1, 0);
  for (int i = 1; i <= n; i++) {
    p[i] = p[i - 1] + a[i];
  }
  int res = n;
  for (int k = 1; k <= n; k++) {
    while (true) {
      int l = 0, r = n;
      while (l < r) {
        int m = (l + r) / 2;
        if (a[m + 1] < res) {
          l = m + 1;
        } else {
          r = m;
        }
      }
      int s = p[l] + (n - l) * res;
      if (s >= k * res) {
        break;
      }
      res--;
    }
    cout << res << "\n";
  }
}
Problem p02890 (C++)

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

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  map<int, int> cnt;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    cnt[A[i]] += 1;
  }
  vector<int> all;
  for (auto p : cnt) {
    all.emplace_back(p.second);
  }
  sort(all.begin(), all.end());
  int K = all.size();
  vector<int> sum(K + 1);
  for (int i = 0; i <= K; i++) {
    sum[i + 1] = sum[i] + all[i];
  }
  for (int i = 1; i <= N; i++) {
    int lb = 0, ub = N / i + 1;
    while (ub - lb > 1) {
      int m = (lb + ub) / 2;
      int p = lower_bound(all.begin(), all.end(), m) - all.begin();
      (sum[p] + (K - p) * m < m * i ? ub : lb) = m;
    }
    printf("%d\n", lb);
  }
  return 0;
}


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

int main() {
  int N;
  cin >> N;
  vector<int> A(N);
  map<int, int> cnt;
  for (int i = 0; i < N; i++) {
    cin >> A[i];
    cnt[A[i]] += 1;
  }
  vector<int> all;
  for (auto p : cnt) {
    all.emplace_back(p.second);
  }
  sort(all.begin(), all.end());
  int K = all.size();
  vector<int> sum(K + 1);
  for (int i = 0; i < K; i++) {
    sum[i + 1] = sum[i] + all[i];
  }
  for (int i = 1; i <= N; i++) {
    int lb = 0, ub = N / i + 1;
    while (ub - lb > 1) {
      int m = (lb + ub) / 2;
      int p = lower_bound(all.begin(), all.end(), m) - all.begin();
      (sum[p] + (K - p) * m < m * i ? ub : lb) = m;
    }
    printf("%d\n", lb);
  }
  return 0;
}
Problem p02890 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define N 100005
int sum[N], cc[N], number[N];

void update(int *pen, int ind, int v) {
  while (ind < N) {
    pen[ind] += v;
    ind += ind & (-ind);
  }
}
int query(int *pen, int ind) {
  int ret = 0;
  while (ind > 0) {
    ret += pen[ind];
    ind = ind & (ind - 1);
  }
  return ret;
}
void solve() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    int a;
    scanf("%d", &a);
    number[a]++;
  }
  int numType = 0;
  for (int i = 1; i <= n; ++i) {
    if (number[i] > 0) {
      update(sum, number[i], number[i]);
      update(cc, number[i], 1);
      ++numType;
    }
  }
  for (int k = 1; k <= n; ++k) {
    int l = 0, r = n / k, mid;
    while (l < r) {
      mid = (l + r + 1) / 2;
      int kCC = query(cc, mid);
      long long qsum = query(sum, mid);
      qsum += (long long)(numType - kCC) * mid;
      if (qsum >= mid * k) {
        l = mid;
      } else {
        r = mid - 1;
      }
    }
    printf("%d ", l);
  }
  putchar('\n');
}
int main() {
  solve();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define N 300005
int sum[N], cc[N], number[N];

void update(int *pen, int ind, int v) {
  while (ind < N) {
    pen[ind] += v;
    ind += ind & (-ind);
  }
}
int query(int *pen, int ind) {
  int ret = 0;
  while (ind > 0) {
    ret += pen[ind];
    ind = ind & (ind - 1);
  }
  return ret;
}
void solve() {
  int n;
  scanf("%d", &n);
  for (int i = 0; i < n; ++i) {
    int a;
    scanf("%d", &a);
    number[a]++;
  }
  int numType = 0;
  for (int i = 1; i <= n; ++i) {
    if (number[i] > 0) {
      update(sum, number[i], number[i]);
      update(cc, number[i], 1);
      ++numType;
    }
  }
  for (int k = 1; k <= n; ++k) {
    int l = 0, r = n / k, mid;
    while (l < r) {
      mid = (l + r + 1) / 2;
      int kCC = query(cc, mid);
      long long qsum = query(sum, mid);
      qsum += (long long)(numType - kCC) * mid;
      if (qsum >= mid * k) {
        l = mid;
      } else {
        r = mid - 1;
      }
    }
    printf("%d ", l);
  }
  putchar('\n');
}
int main() {
  solve();
  return 0;
}
Problem p02890 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#define LL long long
#define MAXN 100000
int n, cnt[MAXN + 5], ans[MAXN + 5];
LL sum[MAXN + 5];
LL read() {
  LL x = 0, F = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      F = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * F;
}
int main() {
  n = read();
  for (int i = 1; i <= n; i++) {
    int x = read();
    cnt[x]++, sum[cnt[x]]++;
  }
  for (int i = 1; i <= n; i++)
    sum[i] += sum[i - 1];
  int t = 0;
  for (int k = n; k >= 1; k--) {
    while (t < n && sum[t + 1] >= 1LL * k * (t + 1))
      t++;
    ans[k] = t;
  }
  for (int i = 1; i <= n; i++)
    printf("%d\n", ans[i]);
}


Fixed submission:
#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#define LL long long
#define MAXN 300000
int n, cnt[MAXN + 5], ans[MAXN + 5];
LL sum[MAXN + 5];
LL read() {
  LL x = 0, F = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      F = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    x = x * 10 + c - '0';
    c = getchar();
  }
  return x * F;
}
int main() {
  n = read();
  for (int i = 1; i <= n; i++) {
    int x = read();
    cnt[x]++, sum[cnt[x]]++;
  }
  for (int i = 1; i <= n; i++)
    sum[i] += sum[i - 1];
  int t = 0;
  for (int k = n; k >= 1; k--) {
    while (t < n && sum[t + 1] >= 1LL * k * (t + 1))
      t++;
    ans[k] = t;
  }
  for (int i = 1; i <= n; i++)
    printf("%d\n", ans[i]);
}
Problem p02891 (C++)

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

using namespace std;

int main(void) {
  string S;
  cin >> S;
  long long N;
  cin >> N;

  vector<int> Chars;
  int num = 1;
  for (int i = 1; i < S.size(); i++) {
    if (S[i] == S[i - 1]) {
      num++;
      if (i == S.size() - 1) {
        Chars.push_back(num);
      }
    } else {
      Chars.push_back(num);
      num = 1;
    }
  }

  long long result = 0;
  for (int i = 1; i < Chars.size() - 1; i++) {
    result += Chars[i] / 2;
  }
  if (S.front() != S.back()) {
    result += Chars.front() / 2;
    result += Chars.back() / 2;
    result *= N;
  } else {
    if (Chars.size() == 1) {
      result += Chars.front() * N;
      result /= 2;
    } else {

      int add = (Chars.front() + Chars.back()) / 2;
      result *= N;
      result += (N - 1) * add;
      result += Chars.front() / 2;
      result += Chars.back() / 2;
    }
  }

  cout << result << endl;

  return 0;
}

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

using namespace std;

int main(void) {
  string S;
  cin >> S;
  long long N;
  cin >> N;

  if (S.size() == 1) {
    int result = N / 2;
    cout << result << endl;
    return 0;
  }

  vector<int> Chars;
  int num = 1;
  for (int i = 1; i < S.size(); i++) {
    if (S[i] == S[i - 1]) {
      num++;
      if (i == S.size() - 1) {
        Chars.push_back(num);
      }
    } else {
      Chars.push_back(num);
      num = 1;
    }
  }

  long long result = 0;
  for (int i = 1; i < Chars.size() - 1; i++) {
    result += Chars[i] / 2;
  }
  if (S.front() != S.back()) {
    result += Chars.front() / 2;
    result += Chars.back() / 2;
    result *= N;
  } else {
    if (Chars.size() == 1) {
      result += Chars.front() * N;
      result /= 2;
    } else {

      int add = (Chars.front() + Chars.back()) / 2;
      result *= N;
      result += (N - 1) * add;
      result += Chars.front() / 2;
      result += Chars.back() / 2;
    }
  }

  cout << result << endl;

  return 0;
}
Problem p02891 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n; i >= 0; i--)
#define REP(i, s, t) for (int i = s; i <= t; i++)
#define RREP(i, s, t) for (int i = s; i >= t; i--)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000
#define int long long

signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string S;
  int K;
  cin >> S >> K;
  int ans1 = 0, ans2 = 0, ans3 = 0;
  string SS = S + S;
  string SSS = S + S + S;
  rep(i, S.length() - 1) {
    if (S[i] == S[i + 1]) {
      ans1++;
      S[i + 1] = '0';
    }
  }
  rep(i, SS.length() - 1) {
    if (SS[i] == SS[i + 1]) {
      ans2++;
      SS[i + 1] = '0';
    }
  }
  rep(i, SSS.length() - 1) {
    if (SSS[i] == SSS[i + 1]) {
      ans3++;
      SSS[i + 1] = '0';
    }
  }
  int increase = ans2 - ans1;
  int increase2 = ans3 - ans1;
  // if (increase2 != increase * 2) {
  //     cout << increase << " " << increase2 << endl;
  //     cout << S << endl;
  //     cout << SS << endl;
  //     cout << SSS << endl;
  // }
  assert(increase2 == increase * 2);
  cout << ans1 + increase * (K - 1) << endl;

  return 0;
}


Fixed submission:
#include "bits/stdc++.h"
using namespace std;
#define ll long long int
#define rep(i, n) for (int i = 0; i < n; i++)
#define rrep(i, n) for (int i = n; i >= 0; i--)
#define REP(i, s, t) for (int i = s; i <= t; i++)
#define RREP(i, s, t) for (int i = s; i >= t; i--)
#define dump(x) cerr << #x << " = " << (x) << endl;
#define INF 2000000000
#define mod 1000000007
#define INF2 1000000000000000000
#define int long long

signed main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string S;
  int K;
  cin >> S >> K;
  int ans1 = 0, ans2 = 0, ans3 = 0;
  string SS = S + S;
  string SSS = S + S + S;
  rep(i, S.length() - 1) {
    if (S[i] == S[i + 1]) {
      ans1++;
      S[i + 1] = '0';
    }
  }
  rep(i, SS.length() - 1) {
    if (SS[i] == SS[i + 1]) {
      ans2++;
      SS[i + 1] = '0';
    }
  }
  rep(i, SSS.length() - 1) {
    if (SSS[i] == SSS[i + 1]) {
      ans3++;
      SSS[i + 1] = '0';
    }
  }
  int increase = ans2 - ans1;
  int increase2 = ans3 - ans1;
  if (increase2 != increase * 2) {
    // cout << ans1 << " " << increase << " " << increase2 << endl;

    cout << ans1 + increase * (K / 2) + (ans3 - ans2) * ((K - 1) / 2) << endl;
    return 0;
  }
  // assert(increase2 == increase * 2);
  cout << ans1 + increase * (K - 1) << endl;

  return 0;
}
Problem p02891 (Python)

Buggy submission (status: Time Limit Exceeded):
def count_changes(s, k, prev=None):
    count = 0
    for _ in range(k):
        for cur in s:
            if cur == prev:
                count += 1
                prev = None
            else:
                prev = cur
    return count, prev


if __name__ == "__main__":
    s = input()
    k = int(input())
    print(count_changes(s, k)[0])


Fixed submission:
def count_changes(s, k, prev=None):
    count = 0
    for _ in range(k):
        for cur in s:
            if cur == prev:
                count += 1
                prev = None
            else:
                prev = cur
    return count, prev


if __name__ == "__main__":
    s = input()
    k = int(input())
    if len(set(s)) == 1:
        print((len(s) * k) // 2)
        exit()
    if k <= 10**8:
        print(count_changes(s, k)[0])
    else:
        base_count, prev = count_changes(s, 3)
        d, rest = divmod(k, 3)
        res = base_count * d
        if rest:
            res += count_changes(s, rest, prev)[0]
        elif s[0] == prev:
            res += d - 1
        print(res)