2488: 【普及-】【P3817】小A的糖果

内存限制:128 MB 时间限制:1.000 S
评测方式:文本比较 命题人:
提交:2 解决:2

题目描述

小 A 有 lns="http://www.w3.org/1998/Math/MathML"> 个糖果盒,第 lns="http://www.w3.org/1998/Math/MathML"> 个盒中有 lns="http://www.w3.org/1998/Math/MathML"> 颗糖果。

小 A 每次可以从其中一盒糖果中吃掉一颗,他想知道,要让任意两个相邻的盒子中糖的个数之和都不大于 lns="http://www.w3.org/1998/Math/MathML">,至少得吃掉几颗糖。

输入

输入的第一行是两个用空格隔开的整数,代表糖果盒的个数 lns="http://www.w3.org/1998/Math/MathML"> 和给定的参数 lns="http://www.w3.org/1998/Math/MathML">

第二行有 lns="http://www.w3.org/1998/Math/MathML"> 个用空格隔开的整数,第 lns="http://www.w3.org/1998/Math/MathML"> 个整数代表第 lns="http://www.w3.org/1998/Math/MathML"> 盒糖的糖果个数 lns="http://www.w3.org/1998/Math/MathML">

输出

输出一行一个整数,代表最少要吃掉的糖果的数量。

样例输入 复制

3 3
2 2 2

样例输出 复制

1

提示

样例输入输出 1 解释

吃掉第 2 盒中的一个糖果即可。


样例输入输出 2 解释

第 2 盒糖吃掉 lns="http://www.w3.org/1998/Math/MathML">6 颗,第 4 盒吃掉 lns="http://www.w3.org/1998/Math/MathML">2 颗,第 6 盒吃掉 lns="http://www.w3.org/1998/Math/MathML">3 颗。


数据规模与约定

  • 对于 lns="http://www.w3.org/1998/Math/MathML">30% 的数据,保证 lns="http://www.w3.org/1998/Math/MathML">20lns="http://www.w3.org/1998/Math/MathML">,100
  • 对于 lns="http://www.w3.org/1998/Math/MathML">70% 的数据,保证 lns="http://www.w3.org/1998/Math/MathML">103lns="http://www.w3.org/1998/Math/MathML">,105
  • 对于 lns="http://www.w3.org/1998/Math/MathML">100% 的数据,保证 lns="http://www.w3.org/1998/Math/MathML">2105lns="http://www.w3.org/1998/Math/MathML">0,109

#include<bits/stdc++.h>
using namespace std;
int a[100010];
int main(){
	int n,x;
	long long ans=0;
	memset(a,0,sizeof(a));
	cin>>n>>x;
	for (int i=1;i<=n;i++) {
		cin>>a[i];
	}
	for (int i=0;i<n;i++){
		if (a[i] +a[i+1]>x){
			int t=a[i+1];
			a[i+1]=x-a[i];
			ans+=t-a[i+1];
		}
	} 	
	cout<<ans;
	return 0;
}