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

You went to the store, selling ? types of chocolates. There are ?? chocolates of type ? 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 ?? chocolates of type ? (clearly, 0≤??≤??), then for all 1≤?<? at least one of the following must hold:

??=0 (you bought zero chocolates of type ?)
??<?? (you bought less chocolates of type ? than of type ?)
For example, the array ?=[0,0,1,2,10] satisfies the requirement above (assuming that all ??≥??), while arrays ?=[0,1,0], ?=[5,5] and ?=[3,2] don’t.

Calculate the maximum number of chocolates you can buy.

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

The next line contains ? integers ?? (1≤??≤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.

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

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

代码:

#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);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值