A coastal survey team maintains a straight-line chain of n signal beacons, numbered 1 to n from the shore outward, each holding a stored charge level. The team powers on beacons strictly in order from beacon 1 to beacon n. When a beacon is powered on, it activates (and is counted) only if its current charge is strictly greater than 0. Every time a beacon activates, the shared feeder cable immediately draws exactly 1 unit of charge from every beacon that comes after it in the chain, never dropping any beacon's charge below 0. Beacons that fail to activate draw no charge from anyone. Determine how many beacons end up activated once the whole chain has been powered on in order.
The first line contains a single integer n, the number of beacons. The second line contains n space-separated integers p_1, p_2, ..., p_n, the initial charge level of each beacon in chain order.
Print a single integer: the number of beacons that activate.
Example 1
Input
5 1 1 2 1 3
Expected
3
Explanation
Beacon 1 has charge 1>0, activates (count=1) and drains 1 from beacons 2-5: [1,0,1,0,2]. Beacon 2 now has charge 0, fails to activate. Beacon 3 has charge 1>0, activates (count=2) and drains 1 from beacons 4-5: beacon 4 stays at 0 (already 0, clamped) and beacon 5 becomes 1. Beacon 4 has charge 0, fails. Beacon 5 has charge 1>0, activates (count=3). Total activated = 3.
Example 2
Input
3 0 1 2
Expected
2
Explanation
Beacon 1 has charge 0, fails to activate. Beacon 2 has charge 1>0, activates (count=1) and drains 1 from beacon 3, making it 1. Beacon 3 now has charge 1>0, activates (count=2). Total activated = 2.
Ready to solve this?
Sign in to open the editor, run your code against the sample tests, and submit against the full test suite.
Sign in to solve →