AmazonOA #86
Find Minimum Sum (Non-Decreasing Servers)
Estimated MediumJudge-ready
Topics (AI-labelled)GreedyIn-place Arrays
One operation chooses any contiguous range and increases each element in it by x≥0, at a cost of x rather than the range length times x. Find the minimum total cost to make the array non-decreasing.
Input
The first line contains n (1..100000), and the second line contains n power values (1..10⁹).
Output
Output a single integer answer.
Example 1
Input:
5
3 4 1 6 2
Output:
7
Example 2
Input:
3
1 2 3
Output:
0
Example 3
Input:
3
5 1 1
Output:
4
Example notes
Example 1: 4→1 drops by 3 and 6→2 drops by 4, so the cost is 7. Example 2: already non-decreasing, cost 0. Example 3: add 4 to the last two elements together, giving 5, 5, 5, cost 4.
Editorial
Loading solution…