AmazonOA #231
Get the Fewest Moves (~Operations~)~
Estimated MediumJudge-ready
Topics (AI-labelled)Prefix & RangesGreedy
One operation chooses a non-empty prefix and applies +1 or −1 to all of its elements simultaneously. Find the minimum number of operations to make every element zero. These are not suffix operations.
Input
The first line contains n; the second line contains the integers. 1≤n≤100000, −10^9≤element≤10^9.
Output
Output a single integer, the answer.
Example 1
Input:
3
3 2 1
Output:
3
Example 2
Input:
5
3 2 0 0 -1
Output:
5
Example 3
Input:
2
-1 -1
Output:
1
Example notes
The answers are 3, 5 and 1. In the second example the absolute value of the first element cannot be used in place of the last.
Editorial
Loading solution…