PatreonOA #2
Sum of Subarray Regions
Estimated MediumJudge-ready
Topics (AI-labelled)Stacks & Heaps
The region of each position is the longest contiguous interval that contains the position and in which its height is still the maximum; equal heights still count as the maximum.
The standard input/output conventions and constraints added by this site are described under "Input".
Input
n≤100000, heights 1..10^9.
Output
Output the sum of the lengths of all regions.
Example 1
Input:
3
3 5 6
Output:
6
Example 2
Input:
2
5 5
Output:
4
Example 3
Input:
3
2 1 2
Output:
7
Example 4
Input:
4
1 3 2 4
Output:
9
Editorial
Loading solution…