Daft Trunk II
Daft Trunk are ready to move onto the next phase of producing their album: sampling!
They are going through hours of random music recordings. The recording is represented as an array of integers denoting pitch values. To create peak, they want to slice out a contiguous subarray (a sequence of consecutive elements) from the recording to use as a loop. However, a loop is only usable if it represents a valid sawtooth wave.
Formally, an array is a valid sawtooth wave if there is no index
such that
(
-indexed).
Given the recording of pitches, what is the total number of valid sawtooth wave subarrays?
Input
The first line contains a single integer
, the size of the studio recording.
The second line contains integers
, the recorded pitch values.
Output
Output a single integer, the total number of contiguous subarrays that are valid sawtooth waves.
Example
Input 1
3
3 2 1
Output 1
5
Explanation: The entire array of [3,2,1] is invalid because >
>
. However, all the other subarrays are valid:
[3], [2], [1], [3, 2], and [2, 1].
Therefore, the answer is .
Input 2
3
1 1 1
Output 2
6
Comments