#P2866. Cow Sight Counting

    ID: 16124 Type: Default 1000ms 256MiB

Cow Sight Counting

Cow Sight Counting

Farmer John has (N) cows participating in the Crazy Hair Festival. All cows are standing in a single row facing right. They are numbered (1, 2, \ldots, N) such that the (N)th cow is at the front and the (1)st cow is at the back. The height of the (i)th cow is given by (h_i). For the (i)th cow, you need to consider the cows in front of it (i.e. cows with indices (i+1, i+2, \ldots, N)). The (i)th cow can see a contiguous block of cows from (i+1) up to (j) if [ h_i > h_{i+1},; h_i > h_{i+2},; \ldots,; h_i > h_j, ] that is, its view is blocked once a cow with a height greater than or equal to (h_i) appears. Define (C_i) as the number of cows visible to the (i)th cow. Your task is to compute [ C_1 + C_2 + \cdots + C_N. ]

inputFormat

The first line contains an integer (N) denoting the number of cows. The second line contains (N) integers (h_1, h_2, \ldots, h_N) representing the heights of the cows from the back to the front.

outputFormat

Output a single integer representing the total number of visible cows, i.e. (C_1 + C_2 + \cdots + C_N).

sample

4
4 3 1 2
5