Time limit
2s
Memory limit
128 MB
You are given a sequence A1,A2,…,AN of N nonnegative integers. Choose a sequence B1,B2,…,BN to minimize ∣A1−B1∣+∣A2−B2∣+⋯+∣AN−BN∣.
The sequence B must be monotone. That means it must satisfy either B1≤B2≤⋯≤BN or B1≥B2≥⋯≥BN.
The first line contains the length N of the sequence. Each of the next N lines contains one value of A1,A2,…,AN in order.
Print the minimum possible sum of absolute differences.