C2. Potions (Hard Version) Codeforces Round 723 (Div. 2)

文章讨论了如何在有限的药水中最大化保持非负健康状态,通过使用优先队列策略来确定在给定条件下的最大可喝药水数量。

This is the hard version of the problem. The only difference is that in this version n≤200000�≤200000. You can make hacks only if both versions of the problem are solved.

There are n� potions in a line, with potion 11 on the far left and potion n� on the far right. Each potion will increase your health by ai�� when drunk. ai�� can be negative, meaning that potion will decrease will health.

You start with 00 health and you will walk from left to right, from first potion to the last one. At each potion, you may choose to drink it or ignore it. You must ensure that your health is always non-negative.

What is the largest number of potions you can drink?

Input

The first line contains a single integer n� (1≤n≤2000001≤�≤200000) — the number of potions.

The next line contains n� integers a1�1, a2�2, ... ,an�� (−109≤ai≤109−109≤��≤109) which represent the change in health after drinking that potion.

Output

Output a single integer, the maximum number of potions you can drink without your health becoming negative.

Example

input

Copy

6
4 -4 1 -3 1 -3

output

Copy

5

Note

For the sample, you can drink 55 potions by taking potions 11, 33, 44, 55 and 66. It is not possible to drink all 66 potions because your health will go negative at some point

题目大致意思:给n瓶药水,照顺序喝,不能喝到负数,问最多喝几瓶?

这题考点是优先队列的使用。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl "\n"

ll n,m;

void solve(){
	cin >> n;
	ll sum=0,c=0,x;
	priority_queue<ll,vector<ll>,greater<ll> >q;
	while(n--){
		cin >> x;
		if(sum+x >= 0){
			c++;
			sum+=x;
			q.push(x);
		}else{
			if(q.empty() || x <= q.top())continue;
			else{
				sum-=q.top();
				q.pop();
				sum+=x;
				q.push(x);
			}
		}
	}
	cout << c << endl;
	return;
}

int main(){
	ll t=1;//cin >> t;
	while(t--)solve();
	return 0;
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值