题目:P3817 小A的糖果 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
首先我们要想保持相邻两个数不大于x
那么在修改两个相邻的数时,我们要减小的是最后面的数,只有这样后面的数相加才会更小(对比修改前面的数)
n,x =map(int,input().split())
lst = list(map(int,input().split()))
cur = 0
for i in range(1,n):
op = lst[i] + lst[i-1]
if op > x:
cur += op-x
if op - x <= lst[i]:
#优先修改最后面的数
lst[i] -= (op-x)
else:
lst[i-1] -= (op-x-lst[i])
lst[i] = 0
print(cur)