CF1526C2.Potions (Hard Version)

传统题 时间 1000 ms 内存 256 MiB 7 尝试 32 已通过 10 标签

Potions (Hard Version)

CF1526C2 · Potions (Hard Version)

英文原题面

Statement

This is the hard version of the problem. The only difference is that in this version n200000n \leq 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 increases your health by aia_i when drunk. aia_i can be negative, meaning that the potion decreases your health. You start with 00 health, and you will walk from left to right, from the 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 (1n2000001 \leq n \leq 200000) — the number of potions. The next line contains nn integers a1a_1, a2a_2, ... ,ana_n (109ai109-10^9 \leq a_i \leq 10^9) 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.

样例

样例 1

输入:

6
4 -4 1 -3 1 -3

输出:

5

样例解释(英文原文)

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

在线编程 IDE

建议全屏模式获得最佳体验