E - Potions (Hard Version)

这是一道关于算法的题目,求解在一条线上有正负效果的药水,如何选择饮用使得健康值始终保持非负的最大药水数量。采用反悔贪心策略,优先考虑加血药水,负值药水也先喝,并存入优先队列。当健康值小于0时,从队列中取出最减生命的药水。最终得出的最大药水饮用数为答案。
摘要由CSDN通过智能技术生成

Day1—E

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

There are nn potions in a line, with potion 11 on the far left and potion nn on the far right. Each potion will increase your health by aiai when drunk. aiai 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 nn (1≤n≤2000001≤n≤200000) — the number of potions.

The next line contains nn integers a1a1, a2a2, ... ,anan (−109≤ai≤109−109≤ai≤109) which represent the change in health after drinking that potion.

Output

Output a

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值