← 返回 capitalone 的题目列表Implement a Given Array Algorithm (Iterative Reduction by First Non-zero)
类型:online_judge
Given a non-negative integer array nums, run the following procedure and return the final value res:
Initialize res = 0.
Repeat until there is no positive number left:
Scan from left to right to find the first positive element; let its value be x. If none exists, stop.
Starting from that index, scan rightward:
If nums[j] < x, stop this scan (go to step 3).
Otherwise set nums[j] -= x and continue.
Add x to res and go back to step 1.
Output res.
Input
Line 1: integer n
Line 2: n non-negative integers nums
Output
Integer res.
(Constraints depend on the OA; assume up to 2e5 elements.)
Example
Input
5
0 3 3 1 5
Output
4