B. Chocolates
B. Chocolates
2 seconds
256 megabytes
standard input
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的更多相关文章
- CF1139B Chocolates
题目地址:CF1139B Chocolates 前一个必须要少于后一个,那么模拟+贪心即可 倒序模拟,每次在上次取的个数减一和这次可以取的最多数量之间取min 直到为0(注意不要减到负数) 再就是注意 ...
- UVA10590 Boxes of Chocolates Again
题意 将正整数N拆分成若干个正整数之和,问有多少种不重复的拆分方案. \(n \leq 5000\) 分析 用f(i,j)表示将i拆成若干个数字,最大的那个数字(即最后一个数)不超过j的方案数. 转移 ...
- Dull Chocolates Gym - 101991D 离散化 前缀和
题目链接:https://vjudge.net/problem/Gym-101991D 具体思路:首先看数据范围,暴力肯定不可以,可以下离散化,然后先求出离散化后每一个点到(1,1)的符合题目的要求的 ...
- Uva 10590 Boxes of Chocolates Again
题面戳这里 dp的姿势有两种(都保证了拆分的有序): \(f_{i,j}\)表示拆分中最大数为\(j\),和为\(i\)的方案数.转移\[f_{i,j} = \sum_{k = 1}^j f_{i-j ...
- Codeforces Round #548 (Div. 2) B. Chocolates
You went to the store, selling
- Codeforces Round #361 (Div. 2) C
C - Mike and Chocolate Thieves Description Bad news came to Mike's village, some thieves stole a bun ...
- 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 ...
- 【英语魔法俱乐部——读书笔记】 1 初级句型-简单句(Simple Sentences)
第一部分 1 初级句型-简单句(Simple Sentences):(1.1)基本句型&补语.(1.2)名词短语&冠词.(1.3)动词时态.(1.4)不定式短语.(1.5)动名词.(1 ...
- 在 Windows 上遇到非常多 TIME_WAIT 連線時應如何處理
我們公司所代管的網站裡,有幾個流量是非常大的,在尖峰的時刻同時上線人數可能高達數千到數萬人,而在這個時候如果使用 netstat 或 TCPView 查看所有 TCP 連線時就會看到非常多處於 ...
随机推荐
- Skills CodeForces - 613B (双指针)
大意: $n$门课, 第$i$门分数$a_i$, 可以增加共$m$分, 求$cnt_{mx}*cf+mi*cm$的最大值 $cnt_{mx}$为满分的科目数, $mi$为最低分, $cf$, $cm$ ...
- redis 学习(16)-- redis 持久化
redis 持久化 什么是持久化 redis 将所有数据保持在内存中,对数据的更新将异步地保存在磁盘中 持久化的方式 1. 快照 快照是某时某刻对数据的完整备份. 在: MySQL Dump Redi ...
- 删库?半个DBA的跑路经验总结
0. 国内呆不下了,赶紧出国 首先,不要选动车,要选最近的一班飞机,尽快出国,能走高速走高速,不然选人少的路线. 没错,我们 DBA 都是常备护照的. 切记,注意看高德地图实时路况. 我们有个前辈就是 ...
- 分布式的几件小事(四)dubbo负载均衡策略和集群容错策略
1.dubbo负载均衡策略 ①random loadbalance 策略 默认情况下,dubbo是random loadbalance 随机调用实现负载均衡,可以对provider不同实例设置不同的权 ...
- 帝国cms 常用标签汇总
1.列表内容标签 [!--empirenews.listtemp--]<!--list.var1-->[!--empirenews.listtemp--] 2.分页标签 [!--show. ...
- 关于获取某月某日最后一天时Calendar的cal.getActualMaximum(Calendar.DAY_OF_MONTH)的吐槽
例如: 在2017.03.29-31号 新建一个Calendar的単例 设置年:2017 设置月:2 int day = cal.getActualMaximum(Calendar.DAY_OF_MO ...
- Alpha阶段评审
组别 项目名称 学生互评 助教评分 点评建议 第1组 校园物资共享平台 7.97 6.17 界面较为简洁美观,实现物品信息发布,登录注册和个人信息等功能,交易功能待完善: 博客内容比较完善,有alp ...
- docker 入门(1)
1,docker 的安装卸载 https://docs.docker.com/install/linux/docker-ce/ubuntu/ 2,docker中的基本概念 镜像(Image) 容器(C ...
- v-bind:value="diy" 添加到 <input type="button" /> 中可以,添加到<button />中不可以,diy是data中的数据
v-bind:value="diy" 添加到 <input type="button" /> 中可以, 添加到<button />中不可 ...
- golang 结构体嵌入和匿名成员
考虑一个二维的绘图程序,提供了一个各种图形的库,例如矩形.椭圆形.星形和轮形等几 何形状.这里是其中两个的定义 type Circle struct { X, Y, Radius int } type ...