Make a Monotone Sequence
Time limit2sMemory limit128 MB
Given N nonnegative integers, build a monotone (non-decreasing or non-increasing) sequence minimizing the total absolute difference from the original.
- Level
Hard8 of 10
- Topics
- Dynamic programming, Greedy, Heap, Math
- Solved
- No attempts yet
Problem
You are given a sequence of nonnegative integers. Choose a sequence to minimize .
The sequence must be monotone. That means it must satisfy either or .
Input
The first line contains the length of the sequence. Each of the next lines contains one value of in order.
Output
Print the minimum possible sum of absolute differences.