You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

25 lines
735 B

2 years ago
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
//题解:都要吃最后的,这样才最少。
const int N = 100010;
int a[N];
LL ans = 0; //因为是累加和注意小心是long long
int n, x; //糖果盒的个数 n,给定的参数 x(不能超过x)
int main() {
cin >> n >> x;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i < n; i++) {
if (a[i] + a[i + 1] > x) { //两个长度的滑动窗口
ans += a[i + 1] + a[i] - x; //吃掉的糖数量
a[i + 1] = x - a[i]; //a[i+1]剩余的数量。这个差,要在后面那个盒子里出,这样才最有效。
}
}
cout << ans << endl;
return 0;
}