Small Weird Measurements

Count the subarrays in which the signs of consecutive differences strictly alternate (length 1 counts, equal neighbours break it).

Easy3ArrayImplementationBrute forceInterviewNo attempts yetTime limit2sMemory limit512 MB

Problem

Dudu is getting in shape for a trip to Thailand, so he weighs himself every morning.

Some stretches of days are good, with the weight going down day after day, and some are bad, with it going up day after day. Others are just weird.

An interval of measurements is weird if it alternates between increasing and decreasing. For example, measurements on consecutive days of [1, 3, 2, 5, 1] form a weird interval. [1, 3, 5, 4] is not weird, because the value increases from 1 to 3 and then increases again from 3 to 5. Every interval of length 1 is weird by definition.

If two neighbouring measurements are equal, that step is neither an increase nor a decrease, so an interval that holds two equal neighbours is not weird.

An interval is identified by its start position and end position, not by its values. If the same list of values appears at two different positions, both intervals are counted.

Given a sequence of integers, count the weird intervals.

Input

The first line contains the length of the sequence, NN.

The second line contains the NN integers a1,a2,,aNa_1, a_2, \dots, a_N of the sequence, separated by spaces.

  • 1N1001 \le N \le 100
  • 0ai1090 \le a_i \le 10^9

Output

Print the number of non-empty weird intervals of the sequence on one line.