题目:
Description:
You went to the store, selling nn types of chocolates. There are aiai chocolates of type ii 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 xixi chocolates of type ii (clearly, 0≤xi≤ai0≤xi≤ai), then for all 1≤j<i1≤j<i at least one of the following must hold:
- xj=0xj=0 (you bought zero chocolates of type jj)
- xj<xixj<xi (you bought less chocolates of type jj than of type ii)
For example, the array x=[0,0,1,2,10]x=[0,0,1,2,10] satisfies the requirement above (assuming that all ai≥xiai≥xi), while arrays x=[0,1,0]x=[0,1,0], x=[5,5]x=[5,5] and x=[3,2]x=[3,2] don't.
Calculate the maximum number of chocolates you can buy.
Input
The first line contains an integer nn (1≤n≤2⋅1051≤n≤2⋅105), denoting the number of types of chocolate.
The next line contains nn integers aiai (1≤ai≤1091≤ai≤109), denoting the number of chocolates of each type.
Output
Print the maximum number of chocolates you can buy.
Examples
input
5 1 2 1 3 6
output
10
input
5 3 2 5 4 10
output
20
input
4 1 1 1 1
output
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<algorithm>
#include<cstring>
using namespace std;
int n;
long long a[200005],ans,sum=0;
int main()
{
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
sum=a[n];
ans=a[n];
for(int i=n-1;i>=1;i--)
{
if(a[i]>=sum)
{
sum--;
ans+=sum;
}
else
{
sum=a[i];
ans+=sum;
}
if(sum==0)break;
}
printf("%lld",ans);
return 0;
}