Codeforces Round #723 C2

C2. Potions (Hard Version)

我们正着做的同时
维护一个堆 让后要是寄了
我们从堆里拿出一个最大的数拿出去即可
这样既合法并且我们剩下的也是最小的 所以肯定是最优解

#include <bits/stdc++.h>
using namespace std;
const int N = 2e5+10;
const int M = 998244353;
const int mod = 1e9+7;
#define int long long
int up(int a,int b){return a<0?a/b:(a+b-1)/b;}
#define endl '\n'
#define all(x) (x).begin(),(x).end()
#define YES cout<<"YES"<<endl;
#define NO cout<<"NO"<<endl;
#define _ 0
#define pi acos(-1)
#define INF 0x3f3f3f3f3f3f3f3f
#define fast ios::sync_with_stdio(false);cin.tie(nullptr);

void solve() {
    int n;cin>>n;
    int now=0,ans=0;
    vector<int>a(n+10);
    priority_queue<int,vector<int>,greater<int>>pq;
    for(int i=1;i<=n;i++){
        cin>>a[i];
        if(a[i]>=0)ans++,now+=a[i];
        else{
            now+=a[i];
            pq.push(a[i]);
            ans++;
            while(now<0){
                auto t=pq.top();pq.pop();
                now-=t;
                ans--;
            }
        }
    }
    cout<<ans<<endl;
}
signed main(){
    fast
    int t;t=1;//cin>>t;
    while(t--) {
        solve();
    }
    return ~~(0^_^0);
}
posted @ 2022-10-18 15:29  ycllz  阅读(19)  评论(0)    收藏  举报