alexjercan/bugnet huggingface dataset:
Problem p00486 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define FOR(i, n) for (int i = 0; i < (int)(n); i++)
#define FORR(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define pb(a) push_back(a)
#define mp(x, y) make_pair(x, y)
#define ALL(a) a.begin(), a.end()
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, -1, sizeof(a))
#define len(a) sizeof(a)
#define ll long long
#define pii pair<int, int>
#define INF 1 << 29
#define MAX 101010
using namespace std;

int h, w, n;
ll x[MAX], y[MAX], cx[MAX], cy[MAX];

ll calc(ll X, ll Y) {
  ll ma = 0LL;
  ll tot = 0LL;
  FOR(i, n) {
    ll d = labs(X - x[i]) + labs(Y - y[i]);
    ma = max(ma, d);
    tot += d;
  }
  return 2 * tot - ma;
}

void solve() {
  cin >> h >> w;
  cin >> n;
  FOR(i, n) cin >> x[i] >> y[i];
  FOR(i, n) cx[i] = x[i];
  FOR(i, n) cy[i] = y[i];
  sort(cx, cx + n);
  sort(cy, cy + n);
  ll mx[2], my[2];
  mx[0] = cx[(n - 1) >> 1];
  mx[1] = cx[n >> 1];
  my[0] = cy[(n - 1) >> 1];
  my[1] = cy[n >> 1];
  ll mi = LONG_MAX / 4, miX, miY;
  FOR(i, 2) {
    FOR(j, 2) {
      ll ret = calc(mx[i], my[j]);
      if (ret < mi) {
        mi = ret;
        miX = mx[i];
        miY = my[j];
      }
    }
  }
  cout << mi << endl;
  cout << miX << " " << miY << endl;
}

int main() {
  solve();
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, n) for (int i = 0; i < (int)(n); i++)
#define FORR(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define pb(a) push_back(a)
#define mp(x, y) make_pair(x, y)
#define ALL(a) a.begin(), a.end()
#define ZERO(a) memset(a, 0, sizeof(a))
#define MINUS(a) memset(a, -1, sizeof(a))
#define len(a) sizeof(a)
#define ll long long
#define pii pair<int, int>
#define INF 1 << 29
#define MAX 101010
using namespace std;

int h, w, n;
ll x[MAX], y[MAX], cx[MAX], cy[MAX];

ll calc(ll X, ll Y) {
  ll ma = 0LL;
  ll tot = 0LL;
  FOR(i, n) {
    ll d = labs(X - x[i]) + labs(Y - y[i]);
    ma = max(ma, d);
    tot += d;
  }
  return 2 * tot - ma;
}

void solve() {
  cin >> h >> w;
  cin >> n;
  FOR(i, n) cin >> x[i] >> y[i];
  FOR(i, n) cx[i] = x[i];
  FOR(i, n) cy[i] = y[i];
  sort(x, cx + n);
  sort(y, cy + n);
  ll mx[2], my[2];
  mx[0] = cx[(n - 1) >> 1];
  mx[1] = cx[n >> 1];
  my[0] = cy[(n - 1) >> 1];
  my[1] = cy[n >> 1];
  ll mi = LONG_MAX / 4, miX, miY;
  FOR(i, 2) {
    FOR(j, 2) {
      ll ret = calc(mx[i], my[j]);
      if (ret < mi) {
        mi = ret;
        miX = mx[i];
        miY = my[j];
      }
    }
  }
  cout << mi << endl;
  cout << miX << " " << miY << endl;
}

int main() {
  solve();
  return 0;
}
Problem p00486 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std;

long long int INF = (long long)10000000000 * 100000;
long long int ans = 0;
long long int w, h;
long long int x[100001], y[100001];
vector<long long int> vx;
vector<long long int> vy;
long long rx, ry;
int n;

long long int dfs(long long int sx, long long int sy) {
  vector<long long int> v;
  for (int i = 0; i < n; i++) {
    v.push_back(abs(sx - x[i]) + abs(sy - y[i]));
  }
  sort(v.begin(), v.end());
  long long res = 0;
  for (int i = 0; i < n; i++) {
    if (i < n - 1)
      res += v[i] * 2;
    else
      res += v[i];
  }
  return res;
}

int main(void) {
  cin >> w >> h;
  ans = INF;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i];
    vx.push_back(x[i]);
    vy.push_back(y[i]);
  }
  if (n == 1) {
    cout << 0 << endl;
    cout << x[0] << " " << y[0] << endl;
  } else {
    sort(vx.begin(), vx.end());
    sort(vy.begin(), vy.end());
    for (int j = -1; j <= 1; j += 2) {
      for (int i = -1; i <= 1; i += 2) {
        long long int res = dfs(vx[(n + j) / 2], vy[(n + i) / 2]);
        if (res < ans) {
          rx = vx[(long long)((n + j) / 2)];
          ry = vy[(long long)((n + i) / 2)];
          ans = res;
        }
      }
    }
    cout << ans << endl;
    cout << rx << " " << ry << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;

long long int INF = (long long)10000000000 * 100000;
long long int ans = 0;
long long int w, h;
long long int x[100001], y[100001];
vector<long long int> vx;
vector<long long int> vy;
long long rx, ry;
int n;

long long int dfs(long long int sx, long long int sy) {
  vector<long long int> v;
  for (int i = 0; i < n; i++) {
    v.push_back(abs(sx - x[i]) + abs(sy - y[i]));
  }
  sort(v.begin(), v.end());
  long long res = 0;
  for (int i = 0; i < n; i++) {
    if (i < n - 1)
      res += v[i] * 2;
    else
      res += v[i];
  }
  return res;
}

int main(void) {
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    memset(x, 0, sizeof(x));
    memset(y, 0, sizeof(y));
    vx.clear();
    vy.clear();
    ans = INF;
    cin >> n;
    for (int i = 0; i < n; i++) {
      cin >> x[i] >> y[i];
      vx.push_back(x[i]);
      vy.push_back(y[i]);
    }
    if (n == 1) {
      cout << 0 << endl;
      cout << x[0] << " " << y[0] << endl;
    } else {
      sort(vx.begin(), vx.end());
      sort(vy.begin(), vy.end());
      for (int j = -1; j <= 1; j += 2) {
        for (int i = -1; i <= 1; i += 2) {
          long long int res = dfs(vx[(n + i) / 2], vy[(n + j) / 2]);
          if (res < ans) {
            rx = vx[(long long)((n + i) / 2)];
            ry = vy[(long long)((n + j) / 2)];
            ans = res;
          }
        }
      }
      cout << ans << endl;
      cout << rx << " " << ry << endl;
    }
  }
  return 0;
}
Problem p00487 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <set>
using namespace std;

typedef pair<int, int> P;
int N;
P A[300000];
multiset<int> os;

bool f(long long X) {
  multiset<int> s(os);
  multiset<int> g;
  long long t = 0;
  for (int i = 0; i < X - 1; i++) {
    int x = A[i].second;
    s.erase(s.find(x));
    g.insert(x);
    t += x;
  }

  for (int i = 0; i < N - X + 1; i++) {
    int x = A[i].second;
    long long b = (long long)A[i].first * X - x;
    auto it = g.find(x);
    if (it != g.end()) {
      g.erase(g.find(x));
      long long d = *s.begin();
      s.erase(s.begin());
      g.insert(d);
      t += d - x;
    } else
      s.erase(s.find(x));
    if (t <= b)
      return true;
  }
  return false;
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    A[i] = P(b, a);
  }
  sort(A, A + N);
  for (int i = 0; i < N; i++)
    os.insert(A[i].second);

  int lo = 0, hi = N + 1;
  while (hi - lo > 1) {
    int mid = (lo + hi) / 2;
    if (f(mid))
      lo = mid;
    else
      hi = mid;
  }
  cout << lo << "\\n";
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <iterator>
#include <numeric>
#include <set>
using namespace std;

typedef pair<int, int> P;
int N;
P A[300000];
multiset<long long> os;

bool f(long long X) {
  multiset<long long> s(os);

  for (int i = 0; i < N - X + 1; i++) {
    long long b = (long long)A[i].first * X - A[i].second;
    s.erase(s.find(A[i].second));
    auto it = s.begin();
    long long t = accumulate(s.begin(), next(it, X - 1), 0);
    if (t <= b)
      return true;
  }
  return false;
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++) {
    int a, b;
    cin >> a >> b;
    A[i] = P(b, a);
  }
  sort(A, A + N);
  for (int i = 0; i < N; i++)
    os.insert(A[i].second);

  int lo = 0, hi = N + 1;
  while (hi - lo > 1) {
    int mid = (lo + hi) / 2;
    if (f(mid))
      lo = mid;
    else
      hi = mid;
  }
  cout << lo << "\\n";
  return 0;
}
Problem p00487 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define INF (2 << 28)
#define fr first
#define sc second
using namespace std;

typedef long long ll;
typedef pair<int, int> iP;

int n;
iP bug[311111]; // first: release, second: capacity
priority_queue<iP, vector<iP>, greater<iP>> que;

int main() {

  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> bug[i].fr >> bug[i].sc;

  sort(bug, bug + n);
  int maxi = 0, cap = INF;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cap = min(bug[i].sc, cap);
    sum += bug[i].fr;
    que.push(iP(bug[i].sc, bug[i].fr));
    while (sum > (ll)cap * (int)que.size() && !que.empty()) {
      iP p = que.top();
      que.pop();
      sum -= p.sc;
      cap = (que.empty() ? INF : que.top().fr);
    }
    maxi = max(maxi, (int)que.size());
  }
  cout << maxi << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define INF (2 << 28)
#define fr first
#define sc second
using namespace std;

typedef long long ll;
typedef pair<int, int> iP;

int main() {

  int n;
  iP bug[111111]; // first: release, second: capacity
  priority_queue<iP, vector<iP>, greater<iP>> que;

  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> bug[i].fr >> bug[i].sc;

  sort(bug, bug + n);
  int maxi = 0, cap = INF;
  ll sum = 0;
  for (int i = 0; i < n; i++) {
    cap = min(bug[i].sc, cap);
    sum += bug[i].fr;
    que.push(iP(bug[i].sc, bug[i].fr));
    while (sum > (ll)cap * (int)que.size() && !que.empty()) {
      iP p = que.top();
      que.pop();
      sum -= p.sc;
      cap = (que.empty() ? INF : que.top().fr);
    }
    maxi = max(maxi, (int)que.size());
  }
  cout << maxi << endl;
}
Problem p00487 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define int long long

struct Bug {
  int out, in;
  bool operator<(const Bug &a) const { return in > a.in; }
};

int N;
Bug b[300000];

bool isTokyo(int k) {
  multiset<int> latte;
  int sum = 0;
  for (int i = 0; i < N; i++) {
    if (latte.size() < k - 1) {
      sum += b[i].out;
      latte.insert(b[i].out);
      continue;
    }
    if ((double)(sum + b[i].out) / (double)k <= (double)b[i].in)
      return true;
    if (latte.size() == 0)
      continue;
    multiset<int>::iterator max_ = latte.end();
    max_--;
    if (*max_ > b[i].out) {
      sum -= *max_;
      latte.erase(max_);
      sum += b[i].out;
      latte.insert(b[i].out);
    }
  }
  return false;
}

signed main() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> b[i].out >> b[i].in;
  sort(b, b + N);
  int l = 0, r = N + 1;
  while (l + 1 != r) {
    int piv = (l + r) / 2;
    if (isTokyo(piv))
      l = piv;
    else
      r = piv;
  }
  printf("%d\\n", l);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

struct Bug {
  int out, in;
  bool operator<(const Bug &a) const { return in > a.in; }
};

int N;
Bug b[300000];

bool isTokyo(int k) {
  multiset<int> latte;
  int sum = 0;
  for (int i = 0; i < N; i++) {
    if (latte.size() < k - 1) {
      sum += b[i].out;
      latte.insert(b[i].out);
      continue;
    }
    if ((double)(sum + b[i].out) / (double)k <= (double)b[i].in)
      return true;
    multiset<int>::iterator max_ = latte.end();
    max_--;
    if (*max_ > b[i].out) {
      sum -= *max_;
      latte.erase(max_);
      sum += b[i].out;
      latte.insert(b[i].out);
    }
  }
  return false;
}

int main() {
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> b[i].out >> b[i].in;
  sort(b, b + N);
  int l = 0, r = N;
  while (l + 1 != r) {
    int piv = (l + r) / 2;
    if (isTokyo(piv))
      l = piv;
    else
      r = piv;
  }
  printf("%d\\n", l);
  return 0;
}
Problem p00487 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
pair<long long, long long> x[1 << 19];
long long bit[2][1 << 19], n, g[1 << 19];
void add(long long q, long long a, long long x) {
  while (a <= 400000) {
    bit[q][a] += x;
    a += a & -a;
  }
}
long long sum(long long q, long long a) {
  long long s = 0;
  while (a >= 1) {
    s += bit[q][a];
    a -= a & -a;
  }
  return s;
}
bool solve(long long g, long long p) {
  if (sum(1, g) == 0)
    return true;
  long long K = (sum(0, g) - 1) / sum(1, g);
  if (K >= p)
    return false;
  return true;
}
int main() {
  cin >> n;
  vector<pair<long long, long long>> vec;
  for (long long i = 0; i < n; i++) {
    cin >> x[i].second >> x[i].first;
  }
  sort(x, x + n);
  long long maxn = 0;
  for (int i = 0; i < n; i++) {
    vec.push_back(make_pair(x[i].second, i));
  }
  sort(vec.begin(), vec.end());
  for (int i = 0; i < n; i++) {
    g[vec[i].second] = i + 1;
  }
  for (long long i = n - 1; i >= 0; i--) {
    add(0, g[i], x[i].second);
    add(1, g[i], 1);
    long long L = 1, R = 320000, M, C = 0;
    while (true) {
      M = (L + R) / 2;
      bool V1 = solve(M - 1, x[i].first);
      bool V2 = solve(M, x[i].first);
      if (V1 == true && V2 == false) {
        C = sum(1, M - 1);
        break;
      }
      if (V1 == true && V2 == true)
        L = M;
      if (V1 == false && V2 == false)
        R = M;
      if (M >= 310000 && V1 == true && V2 == true) {
        C = sum(1, 310000);
        break;
      }
    }
    maxn = max(maxn, C);
  }
  cout << maxn << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
pair<int, int> x[100000];
int n;
int main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> x[i].second >> x[i].first;
  }
  sort(x, x + n);
  long long maxn = 0;
  for (int i = n - 1; i >= 0; i--) {
    vector<long long> P;
    for (int j = i; j < n; j++)
      P.push_back(x[j].second);
    sort(P.begin(), P.end());
    long long S = 0, T = 0;
    for (int j = 0; j < P.size(); j++) {
      S += P[j];
      T += 1;
      long long G = (S - 1) / T;
      if (G >= x[i].first) {
        maxn = max(maxn, T - 1);
        break;
      }
      if (j == P.size() - 1) {
        maxn = max(maxn, T);
        break;
      }
    }
  }
  cout << maxn << endl;
  return 0;
}
Problem p00487 (C++) — buggy submission, verdict: Time Limit Exceeded

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

struct Bug {
  long long a, b;
};

bool ok(const vector<Bug> &bug, int n) {
  if (n == 0)
    return true;
  if (n == 1)
    return any_of(begin(bug), end(bug), [](Bug b) { return b.a <= b.b; });
  priority_queue<Bug, vector<Bug>, function<bool(Bug, Bug)>> a(
      begin(bug) + 1, end(bug), [](Bug lhs, Bug rhs) {
        return lhs.a != rhs.a ? lhs.a > rhs.a : lhs.b > rhs.b;
      });
  priority_queue<Bug, vector<Bug>, function<bool(Bug, Bug)>> b(
      [](Bug lhs, Bug rhs) {
        return lhs.b != rhs.b ? lhs.b > rhs.b : lhs.a > rhs.a;
      });
  b.push(bug.front());
  long long sum = bug.front().a;
  while (!a.empty()) {
    b.push(a.top());
    sum += a.top().a;
    a.pop();
    if (b.size() < n)
      continue;
    if (sum <= b.top().b * n)
      return true;
    sum -= b.top().a;
    b.pop();
  }
  return false;
}

int main() {
  int N;
  cin >> N;
  vector<Bug> bug(N);
  for (auto &i : bug)
    cin >> i.a >> i.b;

  sort(begin(bug), end(bug), [](Bug lhs, Bug rhs) {
    return lhs.b != rhs.b ? lhs.b < rhs.b : lhs.a < rhs.a;
  });

  int low = 0, high = N + 1;
  while (1 < high - low) {
    int middle = (low + high) >> 1;
    (ok(bug, middle) ? low : high) = middle;
  }
  cout << low << endl;
}

Error: TLE

Fixed submission:

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

struct Bug {
  long long a, b;
};

bool ok(const vector<Bug> &bug, int n) {
  if (n == 0)
    return true;
  multiset<Bug, function<bool(Bug, Bug)>> S(
      begin(bug), end(bug), [](Bug lhs, Bug rhs) {
        return lhs.a != rhs.a ? lhs.a < rhs.a : lhs.b < rhs.b;
      });
  for (int i = 0; i <= bug.size() - n; ++i) {
    int deleted = S.erase(bug[i]);
    while (--deleted)
      S.insert(bug[i]);
    long long sum = bug[i].a;
    auto it = begin(S);
    for (int j = 1; j < n; ++j)
      sum += (it++)->a;
    if (sum <= bug[i].b * n)
      return true;
  }
  return false;
}

int main() {
  int N;
  cin >> N;
  vector<Bug> bug(N);
  for (auto &i : bug)
    cin >> i.a >> i.b;

  sort(begin(bug), end(bug), [](Bug lhs, Bug rhs) {
    return lhs.b != rhs.b ? lhs.b < rhs.b : lhs.a < rhs.a;
  });

  int low = 0, high = N + 1;
  while (1 < high - low) {
    int middle = (low + high) >> 1;
    (ok(bug, middle) ? low : high) = middle;
  }
  cout << low << endl;
}
Problem p00487 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#define fs first
#define sc second
using namespace std;
typedef long long ll;

int n;
pair<ll, int> p[300010], b[300010];
ll a[300010];
bool use[300010];

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

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i].fs;
    p[i] = make_pair(a[i], i);
    b[i].sc = i;
    use[i] = false;
  }
  sort(p, p + n);
  sort(b, b + n);

  int ans = 0;
  ll sum = 0;

  int id = 0, num = 0;
  for (int i = 0; i < n; i++) {
    if (!use[b[i].sc]) {
      sum += a[b[i].sc];
      use[b[i].sc] = true;
      num++;
    }

    while (id < n && p[id].fs <= b[i].fs) {
      if (!use[p[id].sc]) {
        sum += p[id].fs;
        use[p[id].sc] = true;
        num++;
      }
      id++;
    }

    while (id < n) {
      if (!use[p[id].sc]) {
        if (sum + p[id].fs > (num + 1) * b[i].fs)
          break;
        sum += p[id].fs;
        use[p[id].sc] = true;
        num++;
      }
      id++;
    }
    if (sum <= num * b[i].fs)
      ans = max(ans, num);

    sum -= a[b[i].sc];
    num--;
  }
  cout << ans << endl;
}


Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#define fs first
#define sc second
using namespace std;
typedef long long ll;

int n;
pair<ll, int> p[3000100], b[3000100];
ll a[3000100];
bool use[3000100];

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

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i] >> b[i].fs;
    p[i] = make_pair(a[i], i);
    b[i].sc = i;
    use[i] = false;
  }
  sort(p, p + n);
  sort(b, b + n);

  int ans = 0;
  ll sum = 0;

  int id = 0, num = 0;
  for (int i = 0; i < n; i++) {
    if (!use[b[i].sc]) {
      sum += a[b[i].sc];
      use[b[i].sc] = true;
      num++;
    }

    while (id < n) {
      // cout << b[i].first << " " << num << " " << sum << endl;
      // cout << id << " " << p[id].second << endl;
      if (!use[p[id].sc]) {
        if (sum + p[id].fs > (num + 1) * b[i].fs)
          break;
        sum += p[id].fs;
        use[p[id].sc] = true;
        num++;
      }
      id++;
    }
    // cout << b[i].first << " " << num << " " << sum << endl;
    ans = max(ans, num);

    sum -= a[b[i].sc];
    num--;
  }
  cout << ans << endl;
}
Problem p00487 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define int long long int
#define F first
#define S second
#define MP make_pair
using namespace std;
typedef pair<int, int> PII;
typedef pair<int, PII> PIP;

int n;
PII ab[300010];
priority_queue<PIP> q;
int ans = 0;

struct segtree {
  int tree[1048576];
  int size;

  void reset() {
    size = 1;
    while (size < n + 1) {
      size <<= 1;
    }
  }

  void add(int where, int value) {
    int now = size - 1 + where;
    while (true) {
      tree[now] += value;
      if (now == 0) {
        break;
      }
      now = (now - 1) / 2;
    }
  }

  int sum(int end, int left, int right, int now) {
    if (end < left) {
      return 0;
    } else if (right <= end) {
      return tree[now];
    } else {
      return sum(end, left, (left + right) / 2, now * 2 + 1) +
             sum(end, (left + right) / 2 + 1, right, now * 2 + 2);
    }
  }
} tr_sum, tr_many;

bool judge(int mid, int nowb) {
  if (tr_sum.sum(mid, 0, tr_sum.size - 1, 0) <=
      nowb * tr_many.sum(mid, 0, tr_many.size - 1, 0)) {
    return true;
  }
  return false;
}

int bs(int nowb) {
  int bss = 0, bse = tr_many.size;
  while (bse - bss > 1) {
    int mid = (bss + bse) / 2;
    if (judge(mid, nowb)) {
      bss = mid;
    } else {
      bse = mid;
    }
  }
  return tr_many.sum(bss, 0, tr_many.size - 1, 0);
}

signed main() {
  scanf("%lld", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%lld%lld", &ab[i].first, &ab[i].second);
  }

  sort(ab + 1, ab + n + 1);

  for (int i = 1; i <= n; i++) {
    q.push(MP(ab[i].S, MP(ab[i].F, i)));
  }

  tr_sum.reset();
  tr_many.reset();

  for (int i = 1; i <= n; i++) {
    PIP now = q.top();
    q.pop();

    tr_sum.add(now.S.S, now.S.F);
    tr_many.add(now.S.S, 1);

    ans = max(ans, bs(now.F));
  }

  printf("%lld\\n", ans);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define PB push_back
#define MP make_pair
using namespace std;
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef vector<PII> VP;
typedef long long int LL;
const LL L_INF = 3e18;
const int I_INF = 2e9;
int n;
int a[300010], b[300010];

int solve(int i, int many, int sum, int mx) {
  if (i < n)
    return max(solve(i + 1, many, sum, mx),
               solve(i + 1, many + 1, sum + a[i], min(mx, b[i])));

  if (!many)
    return 0;
  if ((sum % many) && sum / many < mx || !(sum % many) && sum / many <= mx)
    return many;
  return 0;
}
int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    scanf("%d%d", &a[i], &b[i]);

  printf("%d\\n", solve(0, 0, 0, I_INF));
  return 0;
}