要注意的是 我的优先队列一开始都开错了 应该是把负的最多的放到栈顶
#include<stdio.h>
#include<algorithm>
using namespace std;
#include<queue>
priority_queue<int,vector<int>,greater<int> >q;
int main()
{
long long sum=0,x;
int n,s=0;
scanf("%d",&n);
for(int i=1; i<=n; i++)
{
scanf("%lld",&x);
if(x>=0)
{
sum+=x;//如果当前这个药瓶是增加的 可以直接喝
s++;
}
else
{
if(sum+x>=0)//如果喝了就为0了 放到队列里
{
sum+=x;
q.push(x);
s++;
}
else
{
if(q.size()>0)//如果喝了就为0了 把之前喝的 减去生命值最多的那个 替换掉
{
int w=q.top(); //如果当前这个比最负的那个还要负 就不用换了
if(x>w)
{
sum-=w;
sum+=x;
q.pop();
q.push(x);
}
}
}
}
}
printf("%d\n",s);
return 0;
}