#P2866. [USACO06NOV] Bad Hair Day S

[USACO06NOV] Bad Hair Day S

Problem Description

Farmer John has NN cows on a Bad Hair Day.

All cows stand in a single line facing right, and they are numbered from left to right as 1,2,⋯ ,N1, 2, \cdots, N. Cow NN is at the front, and cow 11 is at the back. The height of cow ii is hih_i.

For the cows in front of cow ii, if hi>hi+1,hi>hi+2,⋯ ,hi>hjh_i > h_{i+1}, h_i > h_{i+2}, \cdots, h_i > h_j, then cow ii is considered able to see cows i+1i+1 through jj.

Define CiC_i as the number of cows visible to cow ii. Please help Farmer John compute C1+C2+⋯+CNC_1 + C_2 + \cdots + C_N.

Input Format

The input contains N+1N + 1 lines.

The first line contains an integer NN, the number of cows.
The next NN lines each contain an integer hih_i, representing the heights of cows 1,2,⋯ ,N1, 2, \cdots, N.

Output Format

Output a single line with one integer, the value of C1+C2+⋯+CNC_1 + C_2 + \cdots + C_N.

6
10
3
7
4
12
2
5

Hint

Constraints

For 100%100\% of the testdata, it is guaranteed that 1≤N≤8×1041 \leq N \leq 8 \times 10^4, 1≤hi≤1091 \leq h_i \leq 10^9.

Translated by ChatGPT 5