Files
python/TangDou/LuoGuBook/P3817.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

25 lines
735 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#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;
}