P3817 小A的糖果

题目描述

小A有N个糖果盒,第i个盒中有a[i]颗糖果。

小A每次可以从其中一盒糖果中吃掉一颗,他想知道,要让任意两个相邻的盒子中加起来都只有x颗或以下的糖果,至少得吃掉几颗糖。

输入输出格式

输入格式:

 

第一行输入N和x。

第二行N个整数,为a[i]。

 

输出格式:

 

至少要吃掉的糖果数量。

 

输入输出样例

输入样例#1:
3 3
2 2 2
输出样例#1:
1

输入样例#2:
6 1
1 6 1 2 0 4
输出样例#2:
11
输入样例#3:
5 9
3 1 4 1 5
输出样例#3:
0

说明

样例解释1

吃掉第二盒中的糖果。

样例解释2

第二盒吃掉6颗,第四盒吃掉2颗,第六盒吃掉3颗。

30%的测试数据,2<=N<=20,0<=a[i], x<=100

70%的测试数据,2<=N<=1000,0<=a[i], x<=10^5

100%的测试数据,2<=N<=10^5,0<=a[i], x<=10^9

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<math.h>
#include<cstdio>
using namespace std;
#define LL long long 
int n,x,a[100009],t;
LL ans;
int main()
{
    scanf("%d%d",&n,&x);
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&a[i]);
        if(a[i]>x)    ans+=a[i]-x,a[i]=x;        
    }
    for(int i=1;i<=n;i++)
    {
        if( a[i]+a[i+1]>x )
        {
            t=a[i]+a[i+1]-x;
            ans+=t;
            a[i+1]-=t;
        }
    }
    cout<<ans;
    return 0;
}

 

posted @ 2017-08-15 10:57  浪矢-CL  阅读(142)  评论(0编辑  收藏  举报