Candies by Rating
n children stand in a row, numbered 1 to n from left to right. Child i has a rating r_i.
You hand out candies so that both rules hold:
- every child gets at least one candy;
- if two children stand next to each other and one has a strictly higher rating than the other, the one with the higher rating gets strictly more candies.
Neighbours with equal ratings are not constrained in any way relative to each other.
Find the minimum total number of candies you need.
Input
The first line contains an integer n (1 ≤ n ≤ 2·10^5).
The second line contains n integers r_1, r_2, …, r_n (1 ≤ r_i ≤ 10^9).
Output
Print one integer: the minimum total number of candies.
Notes
In the first example the children can get 2, 1, 2 candies: the middle child has the lowest rating, and each of the outer children beats it, so each needs at least 2. The total is 5.
In the second example an optimal distribution is 1, 2, 3, 1, 3, 2, 1. The two children with rating 3 stand next to each other, so the second of them can get a single candy even though the first gets 3. The total is 13.
The answer can exceed 2^31, so use a 64-bit integer type.