DatabricksOA #9
Leftmost Non-Zero Subtract
Estimated EasyJudge-ready
Topics (AI-labelled)Simulation
You are given an array of non-negative integers. Repeatedly find the leftmost nonzero element; with its value x, process the elements from that position to the right one by one, subtracting x from an element only if its current value is at least x; in each round, add x to the total. Once the array is all zeros, output the total.
Standard input/output and this site's supplementary ranges follow this problem's description.
Input
The first line contains n, and the second line contains n integers. The source gives the value range 0≤a[i]≤10000; since direct simulation is O(n²), this site narrows n to 1≤n≤1000 so that it finishes within the time limit.
Output
Output the total accumulated during the process, as one integer.
Example 1
Input:
5
0 2 2 0 3
Output:
3
Example 2
Input:
3
4 4 0
Output:
4
Example 3
Input:
3
1 2 3
Output:
3
Editorial
Loading solution…