C:浮桥配重
补题链接🔗:https://cpc.csgrandeur.cn/csgoj/problemset/problem?pid=1585
题目要求最少次数,那就在不得不用的时候再用,感觉跟之前的一道打怪兽题好像
可以用大根堆存b,当目前的余额小于0时,开始使用b,尽量用大的,因为是求最小的
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
#define fi first
#define se second
const ll N=5005,mod=92084931,INF=1e18;
void solve(){
ll n;
cin>>n;
vector<ll>a(n),b(n);
priority_queue<ll>q;
for(ll i=0;i<n;i++)cin>>a[i];
for(ll i=0;i<n;i++)cin>>b[i];
ll res=0;
for(ll i=0;i<n;i++){
res+=a[i];
q.push(b[i]);
if(res<0){
while(!q.empty()&&res<0){
res+=q.top();
q.pop();
}
if(res<0){
cout<<-1;
return;
}
}
}
cout<<n-q.size();
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
ll T = 1;
//cin >> T;
while (T--)
solve();
return 0;
}

浙公网安备 33010602011771号