Boxes and Candies


Time limit : 2sec / Memory limit : 256MB

Score : 300 points

Problem Statement

There are N boxes arranged in a row. Initially, the i-th box from the left contains ai candies.

Snuke can perform the following operation any number of times:

  • Choose a box containing at least one candy, and eat one of the candies in the chosen box.

His objective is as follows:

  • Any two neighboring boxes contain at most x candies in total.

Find the minimum number of operations required to achieve the objective.

Constraints

  • 2≤N≤105
  • 0≤ai≤109
  • 0≤x≤109

Input

The input is given from Standard Input in the following format:

N x
a1 a2 aN

Output

Print the minimum number of operations required to achieve the objective.


Sample Input 1

Copy
3 3
2 2 2

Sample Output 1

Copy
1

Eat one candy in the second box. Then, the number of candies in each box becomes (2,1,2).


Sample Input 2

Copy
6 1
1 6 1 2 0 4

Sample Output 2

Copy
11

For example, eat six candies in the second box, two in the fourth box, and three in the sixth box. Then, the number of candies in each box becomes (1,0,1,0,0,1).


Sample Input 3

Copy
5 9
3 1 4 1 5

Sample Output 3

Copy
0

The objective is already achieved without performing operations.


Sample Input 4

Copy
2 0
5 5

Sample Output 4

Copy
10

All the candies need to be eaten.

//一道简单的贪心,我想得太复杂了。。。而且结果要用 long long 保存

 #include <stdio.h>

 int box[];
int main()
{
int n,x;
while (scanf("%d%d",&n,&x)!=EOF)
{
for (int i=;i<=n;i++)
scanf("%d",&box[i]);
long long all=;
if (box[]>x)
{
all+=box[]-x;
box[]=x;
}
for (int i=;i<=n;i++)
{
int k = box[i]+box[i-]-x;
if (k>)
{
all+=k;
box[i]-=k;
}
}
printf("%lld\n",all);
}
}

Boxes and Candies(贪心)的更多相关文章

  1. Boxes and Candies

    问题 G: Boxes and Candies 时间限制: 1 Sec  内存限制: 128 MB[提交] [状态] 题目描述 There are N boxes arranged in a row. ...

  2. 2018.09.23 atcoder Boxes and Candies(贪心)

    传送门 一道挺有意思的贪心. 从1到n依次满足条件. 注意要特判第一个数已经大于x的情况. 但是如何贪心吃呢? 如果靠左的数没有越界,我们吃靠右的数. 原因是下一次靠右的数就会成为靠左的数,相当于多贡 ...

  3. CF GukiZ hates Boxes 【二分+贪心】

    Professor GukiZ is concerned about making his way to school, because massive piles of boxes are bloc ...

  4. BZOJ 4368: [IOI2015]boxes纪念品盒 贪心

    题意:给定一个环,环上有一些点包裹,你要从 $0$ 号点出发,然后每次带上一个容量为 $k$ 的背包. 问:如果要把所有的包裹都带回 $0$ 好点最少要走多少距离. 每一次只有 $3$ 种走法:走整圆 ...

  5. atcoder题目合集(持续更新中)

    Choosing Points 数学 Integers on a Tree 构造 Leftmost Ball 计数dp+组合数学 Painting Graphs with AtCoDeer tarja ...

  6. Codeforces Round #229 (Div. 2) C. Inna and Candy Boxes 树状数组s

    C. Inna and Candy Boxes   Inna loves sweets very much. She has n closed present boxes lines up in a ...

  7. Codeforces 488B - Candy Boxes

    B. Candy Boxes 题目链接:http://codeforces.com/problemset/problem/488/B time limit per test 1 second memo ...

  8. Codeforces Round #278 (Div. 2) B. Candy Boxes [brute force+constructive algorithms]

    哎,最近弱爆了,,,不过这题还是不错滴~~ 要考虑完整各种情况 8795058                 2014-11-22 06:52:58     njczy2010     B - Ca ...

  9. Codeforces Round #229 (Div. 2) C

    C. Inna and Candy Boxes time limit per test 1 second memory limit per test 256 megabytes input stand ...

随机推荐

  1. N++ 道ASP.NET面试题

    InterviewQuestions-ASP.NET N++ 道ASP.NET面试题 1. 简述 private. protected. public. internal 修饰符的访问权限. 答 . ...

  2. 倍福TwinCAT(贝福Beckhoff)基础教程5.1 TwinCAT-2 运行可执行文件

    个人认为这条命令做的参数比较混乱,PATHSTR是指可执行文件路径+最终文件名,DIRNAME是指可执行文件路径,最后COMNDLINE可有可无,是指带参数运行启动的文件   测试可以正常运行     ...

  3. js - 正斜杆网址转换

    2018-8-7(更新) let http = 'http://www.baidu.com/com/img/1.jpg'; let https = 'http:\\www.baidu.com\\com ...

  4. SQL SERVER 使用订阅发布同步数据库(转)

    一.数据库复制涉及  1.发布服务器:  数据的来源服务器,维护源数据,决定哪些数据将被分发,检测哪些数据发生了修改,并将这些信息提交给分发服务器.  2.分发服务器: 分发服务器负责把从发布服务器拿 ...

  5. Laravel创建项目和安装PHPStorm IDE插件

    一.win10下安装composer1.下载composer.phar,放入php的安装目录https://getcomposer.org/download/1.4.2/composer.phar 2 ...

  6. Android中Activity的生命周期图

  7. 冰点文库下载器V3.1.4

    下载百度文库的利器 http://www.bingdian001.com/?softid=3&ver=3.1.4可以下载

  8. mybatis like写法

    name like concat(concat('%',#{name}),'%') name like concat('%',#{name},'%')

  9. React Native 项目实战 -- DoubanProject

    引言:本文是我研究react-native时写的一个简单的demo,代码里有详细的注释,好废话不多说,直接上代码. 1.项目目录 2.index.android.js /** * index.andr ...

  10. TabControl

    1. ItemsSource="{Binding GroupList}" SelectedItem="{Binding SelectedGroupItem,Mode=Tw ...