#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define vi vector<long long>
#define all(x) x.begin(), x.end()
#define endl "\n"
#define pr pair<ll, ll>
#define ff first
#define ss second
void solution();
int main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
solution();
return 0;
}
const int N = 5e5+10;
pr a[N];
void solution() {
ll n; cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i].ss;
for (int i = 1; i <= n; i++) cin >> a[i].ff;
sort(a+1, a+n+1, [&](pr p1, pr p2) {
return (p1.ff + p1.ss) < (p2.ff + p2.ss);
});
priority_queue<ll> q;
ll x = 0;
for (int i = 1; i <= n; i++) {
if (x <= a[i].ff) {
x += a[i].ss;
q.push(a[i].ss);
} else {
if (q.size() > 0 && q.top() > a[i].ss) {
x -= q.top();
x += a[i].ss;
q.pop();
q.push(a[i].ss);
}
}
}
cout << q.size() << endl;
}
| # | 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... |