B. Chocolates

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

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.

题意:假如从后往前看,就是找最后一颗巧克力,然后每次前面的那颗巧克力至少比当前这颗少一,求至少可以取多少颗巧克力。就是从后面往前面贪心过去就行。这里有一个要注意的地方就是数据类型要用long long不用数据会超范围。我就是 这样WA了一发...

#include<iostream>
#include<cstdio> using namespace std; long long a[]; int main()
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++)
scanf("%d",a+i);
int i=n-;
long long x=a[n-]-,sum=a[n-]; //直接把最后一颗巧克力加入总和中,记录下一颗巧克力最多能拿多少
while(i>=)
{
if(x<=) //当所取的巧克力小于等于0时,就可以跳出循环了
break;
else
{
x=min(a[i],x); //找到当前至少能去多少颗巧克力
sum+=x; //加入总和
x--; //记录下一次索取的最大值
i--;
}
}
cout<<sum<<endl;
return ;
}

B. Chocolates的更多相关文章

  1. CF1139B Chocolates

    题目地址:CF1139B Chocolates 前一个必须要少于后一个,那么模拟+贪心即可 倒序模拟,每次在上次取的个数减一和这次可以取的最多数量之间取min 直到为0(注意不要减到负数) 再就是注意 ...

  2. UVA10590 Boxes of Chocolates Again

    题意 将正整数N拆分成若干个正整数之和,问有多少种不重复的拆分方案. \(n \leq 5000\) 分析 用f(i,j)表示将i拆成若干个数字,最大的那个数字(即最后一个数)不超过j的方案数. 转移 ...

  3. Dull Chocolates Gym - 101991D 离散化 前缀和

    题目链接:https://vjudge.net/problem/Gym-101991D 具体思路:首先看数据范围,暴力肯定不可以,可以下离散化,然后先求出离散化后每一个点到(1,1)的符合题目的要求的 ...

  4. Uva 10590 Boxes of Chocolates Again

    题面戳这里 dp的姿势有两种(都保证了拆分的有序): \(f_{i,j}\)表示拆分中最大数为\(j\),和为\(i\)的方案数.转移\[f_{i,j} = \sum_{k = 1}^j f_{i-j ...

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

    You went to the store, selling 

  6. Codeforces Round #361 (Div. 2) C

    C - Mike and Chocolate Thieves Description Bad news came to Mike's village, some thieves stole a bun ...

  7. cheap gucci bags for women finish fashion jewellery has to move

    Is certainly his dresser seem or dress creation process into video clip. Bus dropped???? Especially ...

  8. 【英语魔法俱乐部——读书笔记】 1 初级句型-简单句(Simple Sentences)

    第一部分 1 初级句型-简单句(Simple Sentences):(1.1)基本句型&补语.(1.2)名词短语&冠词.(1.3)动词时态.(1.4)不定式短语.(1.5)动名词.(1 ...

  9. 在 Windows 上遇到非常多 TIME_WAIT 連線時應如何處理

        我們公司所代管的網站裡,有幾個流量是非常大的,在尖峰的時刻同時上線人數可能高達數千到數萬人,而在這個時候如果使用 netstat 或 TCPView 查看所有 TCP 連線時就會看到非常多處於 ...

随机推荐

  1. c++ split(getline实现)

    众所周知 c++中string没有自带的split函数(亏你还是老大哥) 网上关于split函数的优秀写法很多 本人不再赘述 今几日翻C++API时发现了getline一个有趣的方法 istream& ...

  2. java web 二维码生成

    pom支持: <!-- 二维码支持包 start--> <dependency> <groupId>com.google.zxing</groupId> ...

  3. 勒索病毒[recoverydata54@cock.li].harma,这样恢复文件。

    还没有从搬新家的喜悦中恢复回来,突然有一天发现,自己的1T的硬盘的历史遗迹里面的文件都不能打开了.尤其是孩子们的珍贵照片. 这可让我着急了好几天.过了几天我才知道,原来是有天晚上,4周岁的儿子自己不知 ...

  4. 链式栈的C++实现

    这是去年的内容,之前放在github的一个被遗忘的reporsity里面,今天看到了就拿出来 #include<iostream> #include<string> using ...

  5. MySQL 的索引是什么?怎么优化?

    索引类似大学图书馆建书目索引,可以提高数据检索的效率,降低数据库的IO成本.MySQL在300万条记录左右性能开始逐渐下降,虽然官方文档说500~800w记录,所以大数据量建立索引是非常有必要的.My ...

  6. 用python编写一个合格的ftp程序,思路是怎样的?

      经验1.一般在比较正规的类中的构造函数.都会有一个verify_args函数,用于验证传入参数.尤其是对于系统传参.2.并且系统传参,其实后面大概都是一个函数名 例如:python server. ...

  7. 00:Java简单了解

    浅谈Java之概述 Java是SUN(Stanford University Network),斯坦福大学网络公司)1995年推出的一门高级编程语言.Java是一种面向Internet的编程语言.随着 ...

  8. macOS安装Python MySQLdb

    macOS安装Python MySQLdb 0. 参考 Mac OS X - EnvironmentError: mysql_config not found 1. 背景 import MySQLdb ...

  9. win10 安装ubuntu16.04双系统

    安装了两天的ubuntu系统,很是头疼,发现网上的内容,比较繁杂,因此,写此博客,进行综合整理,总结了安装方法.方便大家安装,减少搜索. 电脑是老师的电脑,配置为: 主板:微星 CPU:英特尔i5 7 ...

  10. Zookeeper的客户端使用

    1.1 Zookeeper API(原生) 1)连接的创建是异步的,需要开发人员自行编码实现等待 2)连接没有超时自动的重连机制 3)Zookeeper本身没提供序列化机制,需要开发人员自行指定,从而 ...