Rar the Cat has N cards, with unique labels 1 through N. He arranges them in some order in a row, where the ith card from the left is Ai.
Peanut comes in, and picks a random non-empty consecutive segment of cards. For each segment Peanut could pick, there exists a card with the minimum value. Find the sum of the minimum values among all possible segments Peanut could choose. Refer to the sample for more details.
The first line of input will contain one integer, N.
The next line of input will contain N integers, with the ith integer containing Ai.
The output should contain one integer, the sum of the minimum values among all possible segments.
Subtask 1 (16%): 1 ≤ N ≤ 300.
Subtask 2 (30%): 1 ≤ N ≤ 2 000.
Subtask 3 (20%): 1 ≤ N ≤ 500 000, A will be in ascending order.
Subtask 4 (34%): 1 ≤ N ≤ 500 000.
Subtask 5 (0%): Sample Testcases
3 2 1 3
9
The possible selections are [2], [1], [3], [2, 1], [1, 3], [2, 1, 3] with minimums 2, 1, 3, 1, 1, 1 respectively.
Therefore the answer is (2 + 1 + 3 + 1 + 1 + 1) = 9.
8 5 4 8 1 2 6 7 3
85
Subtask | Score |
---|---|
1 | 16 |
2 | 30 |
3 | 20 |
4 | 34 |
5 | 0 |