cswork / OA打开练习工作台

RubrikOA #20

Mike and Gems

估计中等可在线判题

考点(AI 标注)单调栈与堆贪心调度

You pass n gems in a fixed order and must either pick up or destroy each one. Picking up gem i increases your current stamina by a[i]; stamina can never drop below 0 at any moment. As the problem states, initial stamina is 0. Find the maximum number of gems you can pick up.

输入格式

The first line contains n; the second line contains n integers a[i]. 1≤n≤100000, −1000000000≤a[i]≤1000000000.

输出格式

Output the maximum number of gems that can be picked up while keeping stamina non-negative throughout.

样例 1

输入:

2
1 -1

输出:

2

样例 2

输入:

4
2 -3 1 0

输出:

3

样例 3

输入:

3
-5 -1 -3

输出:

0