CF1526C2.Potions (Hard Version)
普及/提高-
通过率:0%
AC君温馨提醒
该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。
题目描述
This is the hard version of the problem. The only difference is that in this version n≤200000 . You can make hacks only if both versions of the problem are solved.
There are n potions in a line, with potion 1 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 0 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?
输入格式
The first line contains a single integer n ( 1≤n≤200000 ) — the number of potions.
The next line contains n integers a1 , a2 , ... , an ( −109≤ai≤109 ) which represent the change in health after drinking that potion.
输出格式
Output a single integer, the maximum number of potions you can drink without your health becoming negative.
输入输出样例
输入#1
6 4 -4 1 -3 1 -3
输出#1
5
说明/提示
For the sample, you can drink 5 potions by taking potions 1 , 3 , 4 , 5 and 6 . It is not possible to drink all 6 potions because your health will go negative at some point