Problem:
微生物実験(Bug Party)
あなたはJust Odd Inventions 社を知っているだろうか? この会社の業務は「ただ奇妙な発明(just odd inventions)」をすることである．ここでは略してJOI 社と呼ぶ．
JOI 社では多くの微生物を1 つのシャーレに生きたまま閉じ込める研究をしている．調査対象の微生物は
N
匹存在し，1, 2, ... ,
N
という番号がついている．各微生物はシャーレに閉じ込められると，foo (fatally odd object) と呼ばれる有害物質を一瞬のうちにシャーレ内に放出する．各微生物が放出するfoo の量は知られている．シャーレに閉じ込められた全微生物が放出したfoo はシャーレ内の各微生物が均等に摂取する．各微生物のfoo 許容量は知られており，この量を超えてfoo を摂取すると微生物は死んでしまう．
微生物
i
のfoo 放出量は
a
i
ミリグラム，foo 許容量は
b
i
ミリグラムである．すなわち，シャーレに微生物
i
1
,
i
2
, ... ,
i
k
を閉じ込めたとき，シャーレ内の各微生物はそれぞれ(
a
i
1
+
a
i
2
+ ... +
a
i
k
)/
k
ミリグラムのfoo を摂取し，シャーレ内の微生物
i
は，この摂取量がbi より大きいと死んでしまう．
JOI 社からの委託により，あなたは出来るだけ多くの微生物をシャーレに生きたまま閉じ込めなければならない．ただし，微生物の死骸はシャーレ内の環境に悪影響を及ぼすため，シャーレ内のどの微生物もfoo の摂取で死んではならない．
なお，JOI 社が「ただ奇妙な発明」をすることでどうやって利益を得ているかは，今もって謎であり，JOI社内でも社長以外の誰も知らない．
課題
調査対象の微生物数と，各微生物のfoo 放出量とfoo 許容量が与えられたとき，1 つのシャーレに閉じ込めることができる微生物数の最大値を求めるプログラムを作成せよ．
制限
1 ≤
N
≤ 300000 = 3 × 10
5
調査対象の微生物の数
1 ≤
a
i
≤ 100000 = 10
5
微生物
i
のfoo 放出量(ミリグラム)
1 ≤
b
i
≤ 100000 = 10
5
微生物
i
のfoo 許容量(ミリグラム)
入力
標準入力から以下の入力を読み込め．
1 行目には整数
N
が書かれており，調査対象の微生物が
N
匹存在することを表す．
続くN 行には各微生物の情報が書かれている．
i
+ 1 行目(1 ≤
i
≤
N
) には，正整数
a
i
,
b
i
が空白を区切りとして書かれており，微生物i のfoo 放出量が
a
i
ミリグラムでfoo 許容量が
b
i
ミリグラムであることを表す.
出力
標準出力に，1 つのシャーレに閉じ込めることができる微生物数の最大値を1 行で出力せよ．
注意
この問題では，扱う整数の範囲が32 ビットに収まらない可能性があることに注意せよ．
採点基準
採点用データのうち，配点の30% については，
N
≤ 1000 を満たす．
入出力の例
入力例
6
12 8
5 9
2 4
10 12
6 7
13 9
出力例
3
この例では，微生物2, 4, 5 をシャーレに入れれば，放出されるfoo の合計量は5 + 10 + 6 = 21 ミリグラムとなり，それぞれの微生物が摂取するfoo の量は21/3 = 7 ミリグラムとなる．微生物2, 4, 5 のfoo 許容量はそれぞれ9, 12, 7 ミリグラムなので，シャーレ内のどの微生物も死なない．また，4 匹以上の微生物をどの微生物も死なないようにシャーレに入れることはできない．
問題文と自動審判に使われるデータは、
情報オリンピック日本委員会
が作成し公開している問題文と採点用テストデータです。

Submitted solutions:

code_snippet: #include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
long long n, a[400000], b[400000], x[531441], x2[533141];
vector<pair<long long, long long>>vec, vec2;
void add(long long p, long long r) { while (p <= 524287) { x[p] += r; p += p&-p; } }
long long sum(long long t) { long long cnt = 0; while (t >= 1) { cnt += x[t]; t -= t&-t; }return cnt; }
void add2(long long p, long long r) { while (p <= 524287) { x2[p] += r; p += p&-p; } }
long long sum2(long long t) { long long cnt = 0; while (t >= 1) { cnt += x2[t]; t -= t&-t; } return cnt; }
int main() {
	cin >> n; for (int i = 0; i < n; i++) { cin >> a[i] >> b[i]; vec.push_back(make_pair(b[i], i)); vec2.push_back(make_pair(a[i], i)); }
	sort(vec.begin(), vec.end()); sort(vec2.begin(), vec2.end());
	long long maxn = 0;
	for (int i = vec.size() - 1; i >= 0; i--) {
		long long num = vec[i].second;
		long long pos1 = lower_bound(vec2.begin(), vec2.end(), make_pair(a[num], num)) - vec2.begin();
		add(pos1 + 1, a[num]); add2(pos1 + 1, 1);
		long long L = 0, R = 320000, M;
		while (true) {
			M = (L + R) / 2;
			long long T1 = sum(M), T2 = sum2(M);
			long long U1 = sum(M + 1), U2 = sum2(M + 1);
			long long V1 = 0; if (T2 > 0)V1 = (T1 + T2 - 1) / T2;
			long long V2 = 0; if (U2 > 0)V2 = (U1 + U2 - 1) / U2;
			if (V1 <= vec[i].first && V2 > vec[i].first) { maxn = max(maxn, T2); break; }
			if (V1 > vec[i].first)R = M;
			if (V2 <= vec[i].first)L = M;
			if (M == 319999) { maxn = max(maxn, T2); break; }
		}
	}
	cout << maxn << endl;
	return 0;
}
accuracy: 1
time_ms: 340
memory_kb: 29840
final_rank: 13

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

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

    int N;
    cin >> N;
    vector<pair<int,int>> v;
    v.reserve(N);
    while (N--) {
        int a, b;
        cin >> a >> b;
        // store as (deadline, cost)
        v.emplace_back(b, a);
    }
    // sort by deadline ascending
    sort(v.begin(), v.end());
    // "in" holds selected costs (max‐heap), "out" holds candidates (min‐heap)
    priority_queue<int> in;
    priority_queue<int, vector<int>, greater<>> out;

    long long s = 0;
    // process deadlines from large down to 0
    for (int t = 100000; t >= 0; --t) {
        // move all items with deadline == t into the candidate queue
        while (!v.empty() && v.back().first == t) {
            out.push(v.back().second);
            v.pop_back();
        }
        // balance: if largest selected > smallest candidate, swap them
        while (!in.empty() && !out.empty() && in.top() > out.top()) {
            s += -in.top() + out.top();
            in.push(out.top());
            out.push(in.top());
            in.pop();
            out.pop();
        }
        // try to take new items while budget allows
        while (!out.empty()) {
            // if adding next candidate would exceed t*(|in|+1), stop
            if (static_cast<long long>(t) * (in.size() + 1) < s + out.top()) {
                break;
            }
            s += out.top();
            in.push(out.top());
            out.pop();
        }
    }
    // answer is number taken
    cout << in.size() << "\n";
    return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 8028
final_rank: 1

code_snippet: #include <algorithm>
#include <bitset>
#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];
bitset<300010> use;
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++;
    }
    ll product = num * b[i].fs;
    while (id < n && (p[id].fs <= b[i].fs || (sum + p[id].fs <= (num + 1) * b[i].fs))) {
      if (!use[p[id].sc]) {
        sum += p[id].fs;
        use[p[id].sc] = true;
        num++;
        product = num * b[i].fs;
      }
      id++;
    }
    if (sum <= product)
      ans = max(ans, num);
    sum -= a[b[i].sc];
    num--;
    product = num * b[i].fs;
  }
  cout << ans << endl;
}
accuracy: 1
time_ms: 70
memory_kb: 14916
final_rank: 2

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
#define all(A) A.begin(),A.end()
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)
template<class T>
bool chmax(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p < q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}
template<class T>
bool chmin(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p > q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}

int main() {

    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    ll N;
    cin>>N;
    vector<pair<ll,ll>> P(N);
    rep(i,N)cin>>P[i].second>>P[i].first;
    sort(all(P));
    ll L=0,R=N+1;
    while(R-L>1){
        ll M=(R+L)/2;
        ll S=0;
        priority_queue<ll> Q;
        bool OK=0;
        for(ll i=N-1;i>=0;i--){
            if(Q.size()==M-1){
                if(P[i].first*M-P[i].second>=S)OK=1;
            }
            Q.push(P[i].second);
            S+=P[i].second;
            if(Q.size()==M){
                S-=Q.top();
                Q.pop();
            }
        }
        (OK?L:R)=M;
    }
    cout<<L<<endl;

}
accuracy: 1
time_ms: 350
memory_kb: 15428
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = int;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
using vb = vector<bool>;
using vvb = vector<vb>;
using vvvb = vector<vvb>;
#define all(A) A.begin(),A.end()
#define rep(i, n) for (ll i = 0; i < (ll) (n); i++)
template<class T>
bool chmax(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p < q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}
template<class T>
bool chmin(T& p, T q, bool C = 1) {
    if (C == 0 && p == q) {
        return 1;
    }
    if (p > q) {
        p = q;
        return 1;
    }
    else {
        return 0;
    }
}

int main() {

    cin.tie(nullptr);
    ios::sync_with_stdio(false);

    ll N;
    cin>>N;
    vector<pair<ll,ll>> P(N);
    rep(i,N)cin>>P[i].second>>P[i].first;
    sort(all(P));
    ll L=0,R=N+1;
    while(R-L>1){
        ll M=(R+L)/2;
        ll S=0;
        priority_queue<ll> Q;
        bool OK=0;
        for(ll i=N-1;i>=0;i--){
            if(Q.size()==M-1){
                if(P[i].first*M-P[i].second>=S)OK=1;
            }
            Q.push(P[i].second);
            S+=P[i].second;
            if(Q.size()==M){
                S-=Q.top();
                Q.pop();
            }
        }
        (OK?L:R)=M;
    }
    cout<<L<<endl;

}
accuracy: 0.47368421052631576
time_ms: 30
memory_kb: 3836
final_rank: 14

code_snippet: #pragma GCC optimize("Ofast")
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
#include <queue>
#include <cassert>
#include <chrono>
#include <random>
#include <set>
#include <utility>
#include <array>
#include <bitset>
#include <unordered_set>
using namespace std;
typedef long long int ll;
typedef unsigned long long int ull;

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll myRand(ll B) {
    return (ull)rng() % B;
}
inline double time() {
    return static_cast<long double>(chrono::duration_cast<chrono::nanoseconds>(chrono::steady_clock::now().time_since_epoch()).count()) * 1e-9;
}

int main() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n; cin >> n;
    vector<pair<ll,ll>> v(n);
    for (int i = 0; i < n; ++i) {
        cin >> v[i].second >> v[i].first;
    }
    sort(v.begin(), v.end());
    ll res = 0;
    ll sum = 0;
    ll cnt = 0;
    set<pair<ll,int>> p;
    for (int i = 0; i < n; ++i) {
        p.insert({v[i].second, i});
    }
    for (int i = 0; i < n; ++i) {
        ll mi = v[i].first;
        while (p.size()) {
            auto it = p.begin();
            ll nsum = sum + it->first;
            ll ncnt = cnt + 1;
            if (nsum <= mi*ncnt) {
                sum = nsum;
                cnt = ncnt;
                p.erase(it);
            }
            else {
                break;
            }
        }
        res = max(res, cnt);
        {
            ll ss = v[i].second;
            if (p.find({ss, i}) != p.end()) {
                p.erase(p.find({ss, i}));
            }
            else {
                sum -= ss;
                cnt -= 1;
            }
        }
    }
    cout << res << endl;
}
accuracy: 1
time_ms: 150
memory_kb: 26528
final_rank: 11

code_snippet: #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) {
      if (!use[p[id].sc]) {
        if (p[id].fs > b[i].fs && 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;
}
accuracy: 1
time_ms: 70
memory_kb: 15136
final_rank: 3

code_snippet: #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;
}
accuracy: 1
time_ms: 70
memory_kb: 15208
final_rank: 7

code_snippet: #include <algorithm>
#include <iostream>
#include <map>
#define fs first
#define sc second
using namespace std;
typedef long long ll;
const int MAXN = 300010;
int n;
pair<ll, int> p[MAXN], b[MAXN];
ll a[MAXN];
bool use[MAXN];
int main() {
  cin.tie(0);
  std::ios::sync_with_stdio(false);
  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;
}
accuracy: 1
time_ms: 70
memory_kb: 15228
final_rank: 8

code_snippet: #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 <= (ll)num * b[i].fs)
      ans = max(ans, num);
    sum -= a[b[i].sc];
    num--;
  }
  cout << ans << endl;
}
accuracy: 1
time_ms: 70
memory_kb: 15172
final_rank: 4

code_snippet: #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;
}
accuracy: 1
time_ms: 70
memory_kb: 15228
final_rank: 8

code_snippet: #include <algorithm>
#include <iostream>
#include <map>
#define fs first
#define sc second
using namespace std;
typedef long long ll;
const int MAXN = 300010;
int n;
pair<ll, int> p[MAXN], b[MAXN];
ll a[MAXN];
bool use[MAXN];
int main() {
  cin.tie(0);
  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;
  return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 15176
final_rank: 6

code_snippet: #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 <= static_cast<ll>(num) * b[i].fs)
      ans = max(ans, num);
    sum -= a[b[i].sc];
    num--;
  }
  cout << ans << endl;
}
accuracy: 1
time_ms: 70
memory_kb: 15172
final_rank: 4

code_snippet: #include <algorithm>
#include <iostream>
#include <map>
#define fs first
#define sc second
using namespace std;
typedef long long ll;
const int MAXN = 300010;
int n;
pair<ll, int> p[MAXN], b[MAXN];
ll a[MAXN];
bool use[MAXN];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  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;
  return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 15400
final_rank: 10