Codeforces Round 548 (Div. 2)
B
Chocolates
Codeforces
1000
原题
← AC →
翻译中…

You went to the store, selling $n$ types of chocolates. There are $a_i$ chocolates of type $i$ in stock.

翻译中…

You have unlimited amount of cash (so you are not restricted by any prices) and want to buy as many chocolates as possible. However if you buy $x_i$ chocolates of type $i$ (clearly, $0 \le x_i \le a_i$), then for all $1 \le j \lt i$ at least one of the following must hold:

翻译中…
  • $x_j = 0$ (you bought zero chocolates of type $j$)
  • $x_j \lt x_i$ (you bought less chocolates of type $j$ than of type $i$)
翻译中…

For example, the array $x = [0, 0, 1, 2, 10]$ satisfies the requirement above (assuming that all $a_i \ge x_i$), while arrays $x = [0, 1, 0]$, $x = [5, 5]$ and $x = [3, 2]$ don't.

翻译中…

Calculate the maximum number of chocolates you can buy.

翻译中…

Input

翻译中…

The first line contains an integer $n$ ($1 \le n \le 2 \cdot 10^5$), denoting the number of types of chocolate.

翻译中…

The next line contains $n$ integers $a_i$ ($1 \le a_i \le 10^9$), denoting the number of chocolates of each type.

翻译中…

Output

翻译中…

Print the maximum number of chocolates you can buy.

翻译中…

Note

翻译中…

In the first example, it is optimal to buy: $0 + 0 + 1 + 3 + 6$ chocolates.

翻译中…

In the second example, it is optimal to buy: $1 + 2 + 3 + 4 + 10$ chocolates.

翻译中…

In the third example, it is optimal to buy: $0 + 0 + 0 + 1$ chocolates.

翻译中…

样例

Input #1
5
1 2 1 3 6
Output #1
10
InputCopy OutputCopy
Input #2
5
3 2 5 4 10
Output #2
20
InputCopy OutputCopy
Input #3
4
1 1 1 1
Output #3
1
InputCopy OutputCopy
题目信息
题号
1139B
时间限制
2 s
内存限制
256 MB
标签
贪心
模拟

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。