시간 제한메모리 제한제출정답맞힌 사람정답 비율
1 초 (추가 시간 없음) 1024 MB25111150.000%

문제

All explored mountain terraces in Girotti hills in Charitum Montes on the southern Martian hemisphere have a peculiar feature — their sizes are approximately equal, and they all lie on a hypothetical straight line.

The flat surface of the terraces is ideal for future housing development. Unusual configuration of the terraces allows for a daring engineering project which will connect some terraces by bridges.

Due to relative geological instability in the surrounding region, the surface of any two terraces connected by a bridge has to be in the same height. Obviously, a bridge between two terraces can be built only when the height of all terraces between the given two is less than the height of the two terraces to be connected.

The engineers of the project want to know the maximum total length of all bridges that can be built. To simplify the introductory calculations, the following assumptions are made. The distance between two neighbour terraces is negligibly small, it is considered to be zero in all cases. The width of a terrace is considered to be one length unit.

입력

The first line contains an integer N (1 ≤ N ≤ 3 · 105) the number of the terraces. The second line contains N integers a1, a2, . . . , aN (1 ≤ ai ≤ 106) where ai is the height of i-th terrace. The heights are given in the order of terraces on the (hypothetical) line.

출력

Print one integer – the maximum possible total length of all bridges.

예제 입력 1

5
1 2 3 3 1

예제 출력 1

0

예제 입력 2

6
5 5 5 3 2 3

예제 출력 2

1

예제 입력 3

6
2 3 2 1 2 3

예제 출력 3

4