#562
Unrated
Cow Frisbee
시간 제한
2s
메모리 제한
1024MB
제출
0
정답
0
맞힌 사람
0
정답 비율
0.0%

문제

Farmer John's NN cows (N3×105)N \leq 3 \times 10^5) have heights 1,2,,N1, 2, \ldots, N. One day, the cows are standing in a line in some order playing frisbee; let h1hNh_1 \ldots h_N denote the heights of the cows in this order (so the hh's are a permutation of 1N1 \ldots N).

Two cows at positions ii and jj in the line can successfully throw the frisbee back and forth if and only if every cow between them has height lower than min(hi,hj)\min(h_i, h_j).

Please compute the sum of distances between all pairs of locations i<ji<j at which there resides a pair of cows that can successfully throw the frisbee back and forth. The distance between locations ii and jj is ji+1j-i+1.

입력

The first line of input contains a single integer NN. The next line of input contains h1hNh_1 \ldots h_N, separated by spaces.

출력

Output the sum of distances of all pairs of locations at which there are cows that can throw the frisbee back and forth. Note that the large size of integers involved in this problem may require the use of 64-bit integer data types (e.g., a "long long" in C/C++).

예제 입력 1

7
4 3 1 2 5 6 7

예제 출력 1

24

점수

Test cases 1-3 satisfy N5000N\le 5000.Test cases 4-11 satisfy no additional constraints.

코드 제출

코드를 제출하려면 로그인이 필요합니다.

로그인
내 제출
제출 내역이 없습니다.
맞은 사람
아직 맞은 사람이 없습니다.
난이도 투표
Unrated0명 투표
로그인 후 AC 받으면 투표할 수 있습니다.
전체 제출
제출 내역이 없습니다.