#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
const int N=2e5+10;
typedef long long ll;
int n;ll c[N],w[N];
typedef pair<ll,ll> pr;
pr p[N];
bool cmp(const pr &x,const pr &y){
return x.first+x.second<y.first+y.second;
}
priority_queue<ll> Q;
int main()
{
freopen("pendant.in","r",stdin);
freopen("pendant.out","w",stdout);
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%lld%lld",&p[i].first,&p[i].second);
sort(p+1,p+n+1,cmp);
for (int i=1;i<=n;i++)
c[i]=p[i].first,w[i]=p[i].second;
long long W=0;int ans=0;
for (int i=1;i<=n;i++)
if (c[i]>=W) ans++,W+=w[i],Q.push(w[i]);else
if (w[i]<Q.top()) W+=w[i]-Q.top(),Q.pop(),Q.push(w[i]);
printf("%d\n%lld\n",ans,W);
return 0;
}