Mike is stationed on planet AFWP-700, located on the outskirts of the Milky Way. The planet contains a rare, magical gem whose potency can either increase or decrease the stamina of anyone carrying it. Mike must travel a fixed path from Base A to Base B. Along this path, he will encounter a sequence of gems. At each gem, he is forced to make a choice: he must either collect it or destroy it to nullify its effect. He can collect at most n gems in the order they appear before reaching Base B.
A numerical value represents each gem's potency. Mike's stamina must never drop below zero at any point during his journey. Given the sequence of gem potencies, determine the maximum number of gems Mike can collect while ensuring his stamina remains non-negative throughout the entire trip.
The first line contains a single integer n, the total number of gems on the path.
The next line contains n space-separated integers (a_i for all ), representing the potency of each gem in the order encountered.
A single integer denoting the maximum number of gems Mike can successfully collect without his stamina ever falling below zero.
Input: gemPotencies = [1, 2, 3, 4, -15]
Output: 4
Explanation: Mike can collect the first four gems. Their total potency is 1 + 2 + 3 + 4 = 10, which keeps his stamina positive. He must then destroy the fifth gem with potency -15 to prevent his stamina from going negative. Collecting all five gems would result in a stamina of 10 + (-15) = -5, which is illegal.