cswork / OAOpen practice workspace

The D. E. Shaw GroupOA #8

Maximum Size Subarray Sum

Estimated MediumJudge-ready

Topics (AI-labelled)Stacks & Heaps

For each position i in the array, find the length of the longest contiguous subarray that contains i and has a[i] as its maximum, forming an array b. Output the sum of all elements of b. The subarray may contain other maxima equal to a[i].

The standard input/output, the ranges added by this site, and the examples follow this problem's description.

Input

The first line contains n (this site's addition: 1≤n≤100000); the second line contains n integers (this site's addition: −10^9≤a[i]≤10^9). The original snapshot only gives n≤10^5 and no element range; the answer is output as a 64-bit integer.

Output

Output the sum of the longest subarray lengths over all positions, stored as a 64-bit integer.

Example 1

Input:

6
10 20 10 9 12 14

Output:

17

Example 2

Input:

2
5 5

Output:

4

Example 3

Input:

3
3 2 1

Output:

6

Editorial

Loading solution…