308472: CF1526C2. Potions (Hard Version)

Memory Limit:256 MB Time Limit:1 S
Judge Style:Text Compare Creator:
Submit:0 Solved:0

Description

Potions (Hard Version)

题意翻译

给你一个长度为 $n$ 的序列 $A$,要求你找出最长的一个子序列使得这个子序列任意前缀和都非负。 其中 $n\le 2\times 10^5$,$-10^9\le a_i\le 10^9$。

题目描述

This is the hard version of the problem. The only difference is that in this version $ n \leq 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 $ a_i $ when drunk. $ a_i $ 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 \leq n \leq 200000 $ ) — the number of potions. The next line contains $ n $ integers $ a_1 $ , $ a_2 $ , ... , $ a_n $ ( $ -10^9 \leq a_i \leq 10^9 $ ) 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

Input

题意翻译

给你一个长度为 $n$ 的序列 $A$,要求你找出最长的一个子序列使得这个子序列任意前缀和都非负。 其中 $n\le 2\times 10^5$,$-10^9\le a_i\le 10^9$。

加入题单

上一题 下一题 算法标签: