#d1606. 最大权值划分
最大权值划分
Maximum Weight Partition
Problem Description
For a segment (contiguous subsequence) of a sequence, define its weight as the range of that segment, i.e. the difference between its maximum and minimum.
Given a sequence , you may partition it into any number of consecutive segments. Find the maximum possible value of the sum of the weights of all segments.
Input Format
The first line contains an integer , the length of the sequence ().
The second line contains integers , the given sequence ().
Output Format
Output one line containing one integer: the maximum possible sum of the segment weights.
Sample Input
5
9 6 1 8 8
Sample Output
10
Sample Explanation
Partitioning as gives the maximum weight.