#include<bits/stdc++.h>
// no of bits to store no 2e5 is 19
// 2e5 ka 5 times square root lene se 2 or 3 ke pass pahunch jate hai
using namespace std;

#define int               long long
#define mod               1000000007
#define MAX               1000000000
#define ff                first
#define ss                second
#define mp                make_pair
#define pb                push_back
#define pii               pair<int,int>
#define sp(x,y)           fixed<<setprecision(y)<<x
#define setbits(x)        __builtin_popcount(x)
#define fast_in_out       ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define rep(i,a,b)        for(int i=a;i<b;i++)
#define ll                long long

//   powermod(a,b,m) ==  powermod(a,b%(m-1),m)  when m is prime fermat's theorem a^(p-1) %p=1 p is prime
int powermod(int a,int b,int c) //  gives (a^b)%c
{
    int ans = 1,tempans=a%c;
    while(b>0)
    {
        if(b&1==1)
        {
            ans=(ans*tempans)%c;
        }
             tempans=(tempans*tempans)%c;
             b=b>>1;
    }
    return ans;
}

int modinverse(int a,int m)   // gives a inverse wrt m and m is prime
{
    return powermod(a,m-2,m);
}

ll add(ll x, ll y) {
   x += y;
   if (x >= mod) return x - mod;
   return x;
}
ll sub(ll x, ll y) {
   x -= y;
   if (x < 0) return x + mod;
   return x;
}
ll mult(ll x, ll y) {
   return (x * y) % mod;
}
// my template                                              m
// prime factorization                                      pf
// ncr and factorials                                       ncr
// matrix exponentiation of fibonacci series in log(n)      matexpofib
// cycles in a graph                                        cycle

bool compare(pii p1,pii p2)
{
   int x1=p1.ff,y1=p1.ss;
   int x2=p2.ff,y2=p2.ss;
   int on= 2*x1 + y1;
   int se=2*x2 + y2;
   return on>se;
}


void solve()
{
   int n;
   cin>>n;
   pii a[n];int sum=0;
   rep(i,0,n)
   {
      cin>>a[i].ff>>a[i].ss;
      sum+=a[i].ff;
   }
   sort(a,a+n,compare);
   int temp=0;int ans=0;
   for(int i=0;i<n;i++)
   {
      int x=a[i].ff;
      int y=a[i].ss;
      sum-=x;
      temp+=(x+y);ans++;
      if(temp>sum)
      {
         break;
      }
   }
   cout<<ans<<endl;



}


int32_t main()
{
  fast_in_out;

  int t=1;//cin>>t;
  int count=0;


   while(t--)
   {
       count++;
       solve();
   }
}

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

int main() {
    int n;
    cin >> n;
    int a[n],b[n],c[n];
    for(int i=0; i<n; i++) {
        cin >> a[i];
        a[i]--;
    }
    for(int i=0; i<n; i++) {
        cin >> b[i];
        b[i]--;
    }
    for(int i=0; i<n; i++) {
        cin >> c[i];
        c[i]--;
    }
    vector<int>cnt(n);
    for(int j=0; j<n; j++) {
        cnt[b[c[j]]]++;
    }
    long long ans=0;
    for(int i=0; i<n; i++) ans+=cnt[a[i]];
    cout << ans << endl;
}
