#d1103. 子串的最大差
子串的最大差
Maximum Difference over All Substrings
Problem Description
The maximum difference of a sequence is defined as the difference between its maximum and minimum elements. For example, the maximum difference of is , and that of is .
A substring of a sequence is a contiguous segment of it.
Given an array of length , compute the sum of the maximum differences over all substrings of the sequence.
Input Format
The first line contains an integer .
The second line contains integers .
Output Format
Output one line containing a single integer: the answer.
Sample Input
3
1 2 3
Sample Output
4
Constraints
, .