#include <bits/stdc++.h>
using namespace std;
using pii = pair<int,int>;
#define fs first
#define sc second
int n;
pii a[510000];
int main() {
ios_base::sync_with_stdio(false); cin.tie(0);
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i].sc;
for (int i = 1; i <= n; i++) cin >> a[i].fs;
sort(a+1, a+1+n, [](pii a, pii b){return a.fs+a.sc < b.fs+b.sc;});
priority_queue<int,vector<int>, less<int>> pq;
int now = 0, ans = 0;
for (int i = 1; i <= n; i++) {
// cout << a[i].fs << ' ' << a[i].sc << ' ' << now << '\n';
if (now <= a[i].fs) {
now += a[i].sc;
ans++;
pq.push(a[i].sc);
}
else if (pq.size() and now-pq.top()<=a[i].fs and pq.top()>a[i].sc) {
now -= pq.top();
now += a[i].sc;
pq.pop();
pq.push(a[i].sc);
}
}
cout << ans << '\n';
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |