Codeforces Round #548 (Div. 2) B. Chocolates

You went to the store, selling 𝑛n types of chocolates. There are 𝑎𝑖ai 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 𝑥𝑖xi chocolates of type 𝑖i (clearly, 0𝑥𝑖𝑎𝑖0≤xi≤ai), then for all 1𝑗<𝑖1≤j<i at least one of the following must hold:

  • 𝑥𝑗=0xj=0 (you bought zero chocolates of type 𝑗j)
  • 𝑥𝑗<𝑥𝑖xj<xi (you bought less chocolates of type 𝑗j than of type 𝑖i)

For example, the array 𝑥=[0,0,1,2,10]x=[0,0,1,2,10] satisfies the requirement above (assuming that all 𝑎𝑖𝑥𝑖ai≥xi), while arrays 𝑥=[0,1,0]x=[0,1,0], 𝑥=[5,5]x=[5,5] and 𝑥=[3,2]x=[3,2] don't.

Calculate the maximum number of chocolates you can buy.

Input

The first line contains an integer 𝑛n (1𝑛21051≤n≤2⋅105), denoting the number of types of chocolate.

The next line contains 𝑛n integers 𝑎𝑖ai (1𝑎𝑖1091≤ai≤109), denoting the number of chocolates of each type.

Output

Print the maximum number of chocolates you can buy.

Examples
input
Copy
5
1 2 1 3 6
output
Copy
10
input
Copy
5
3 2 5 4 10
output
Copy
20
input
Copy
4
1 1 1 1
output
Copy
1
Note

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

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

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

 

 题意:给定一个串,在每一位取数字,要求取的数不能多余给定的数,并且在取完整个串后,取出的数字构成的串满足严格上升(可以有前缀零,前面的零有多个同样满足严格上升)

 思路:易知最后一个数字一定是取到最大值(给定的数),所有从后往前遍历,每次取之前最大的数减一和当前数字中较小的那个,全都加进总答案中就好了。

 代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <set>
#include <map>
#define INF 0x3f3f3f3f
#define ll long long
#define ull unsigned long long
#define lowbit(x) (x&(-x))
#define eps 0.00000001
#define PI acos(-1)
#define ms(x,y) memset(x, y, sizeof(x))
using namespace std;

const int maxn = 2e5+10;
int arr[maxn];

int main()
{
    int n;
    scanf("%d", &n);
    for(int i=0;i<n;i++)
        scanf("%d" , arr + i);
    int maxx = arr[n-1];
    ll tot = maxx;
    for(int i=n-2;i>=0;i--)
    {
        int tp = min(maxx-1, arr[i]);
        if(tp < 0) tp = 0;
        tot += tp;
        maxx = tp;
    }
    
    printf("%lld\n", tot);
}

  

posted @ 2019-03-23 13:02  HazelNuto  阅读(296)  评论(0编辑  收藏  举报