其他分享
首页 > 其他分享> > 1139 B. Chocolates bymrhao61 vectora可以直接传入long long int ,long long 很重要

1139 B. Chocolates bymrhao61 vectora可以直接传入long long int ,long long 很重要

作者:互联网

这道题告诉我们long long 真的很重要

#include<iostream>
#include<vector>
using namespace std;
int main()
{
	long long int ans=0,max=0;
	long long int n;cin>>n;
	vector<int>a;
	for(long long int i=0;i<n;i++)
	{
		long long int x;cin>>x;
		a.push_back(x);
	}	
	for(long long int i=n-1;i>=0;i--)
	{
		long long int temp;
		if(i==n-1)
		{
			ans=ans+a[i];
			temp=a[i]-1;
		}
		else
		{
			if(a[i]>=temp)
			{
				ans=ans+temp;
				temp--;
			}
			else
			{
				ans=ans+a[i];
				temp=a[i]-1;
			}
		}
		if(temp<0)temp=0;
	}
	cout<<ans;
}

B. Chocolates
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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≤xi≤ai), then for all 1≤j<i at least one of the following must hold:

xj=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 x=[0,0,1,2,10] satisfies the requirement above (assuming that all ai≥xi), 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≤n≤2⋅105), denoting the number of types of chocolate.

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

Output
Print the maximum number of chocolates you can buy.

Examples
inputCopy
5
1 2 1 3 6
outputCopy
10
inputCopy
5
3 2 5 4 10
outputCopy
20
inputCopy
4
1 1 1 1
outputCopy
1
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.

标签:1139,buy,temp,int,chocolates,long,ans
来源: https://blog.csdn.net/weixin_43870649/article/details/88844464