RubrikOA #20
Mike and Gems
Estimated MediumJudge-ready
Topics (AI-labelled)Stacks & HeapsGreedy
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.
Input
The first line contains n; the second line contains n integers a[i]. 1≤n≤100000, −1000000000≤a[i]≤1000000000.
Output
Output the maximum number of gems that can be picked up while keeping stamina non-negative throughout.
Example 1
Input:
2
1 -1
Output:
2
Example 2
Input:
4
2 -3 1 0
Output:
3
Example 3
Input:
3
-5 -1 -3
Output:
0
Editorial
Loading solution…