#include #include #include using namespace std; using ll = long long; using ull = unsigned long long; #define rep(i,n) for(int i=0; i<(n); i++) const ull MOD = 1000000007; const ll INF = MOD * MOD; int N; vector> SW; vector dp; int main(){ cin >> N; SW.resize(N); rep(i,N){ int w,s; cin >> w >> s; SW[i] = {s,w}; } sort(SW.begin(),SW.end(), [](pair l, pair r)->bool{ ll lx = min(l.first, r.first - l.second); ll rx = min(r.first, l.first - r.second); return lx < rx; } ); dp.assign(N+1,-INF); dp[0] = INF; rep(i,N){ int w,s; cin >> w >> s; for(int k=i; k>=0; k--){ dp[k+1] = max(dp[k+1],min(dp[k]-SW[i].second,SW[i].first)); } } int ans = 0; rep(i,dp.size()) if(dp[i] >= 0) ans = i; cout << ans << endl; return 0; } struct ios_do_not_sync{ ios_do_not_sync(){ ios::sync_with_stdio(false); cin.tie(nullptr); } } ios_do_not_sync_inst;