AtCoder Beginner Contest 048 - Boxes and Candies

There are N boxes arranged in a row.
Initially, the i-th box from the left contains a_i candies.
Snuke can perform the following operation any number of times:
 - Choose a box containing at least one candy, and eat one of the candies in the chosen box.
His objective is as follows:
 - Any two neighboring boxes contain at most x candies in total.
Find the minimum number of operations required to achieve the objective.

-----Constraints-----
 - 2 ≤ N ≤ 10^5
 - 0 ≤ a_i ≤ 10^9
 - 0 ≤ x ≤ 10^9

-----Input-----
The input is given from Standard Input in the following format:
N x
a_1 a_2 ... a_N

-----Output-----
Print the minimum number of operations required to achieve the objective.

-----Sample Input-----
3 3
2 2 2

-----Sample Output-----
1

Eat one candy in the second box.
Then, the number of candies in each box becomes (2, 1, 2).

Source: atcoder | Difficulty: EASY


Solutions:
(N, x, *A) = map(int, open(0).read().split())
s = sum(A)
A = [min(x, a) for a in A]
for i in range(N - 1):
	A[i + 1] -= max(0, A[i] + A[i + 1] - x)
print(s - sum(A))


(N, X) = map(int, input().split())
A = list(map(int, input().split()))
c = 0
if A[0] > X:
	c += A[0] - X
	A[0] = X
for i in range(1, N):
	if A[i - 1] + A[i] > X:
		c += A[i - 1] + A[i] - X
		A[i] = X - A[i - 1]
print(c)


(N, x) = map(int, input().split())
a = list(map(int, input().split()))
result = 0
if a[0] > x:
	result += a[0] - x
	a[0] = x
for i in range(N - 1):
	if a[i] + a[i + 1] > x:
		result += a[i] + a[i + 1] - x
		a[i + 1] = x - a[i]
print(result)


(N, x) = map(int, input().split())
A = [0] + list(map(int, input().split()))
ans = 0
for i in range(N):
	temp = A[i + 1] + A[i]
	ans += max(temp - x, 0)
	A[i + 1] = A[i + 1] - max(temp - x, 0)
print(ans)


(n, x) = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
for i in range(1, n):
	if a[i] + a[i - 1] > x:
		ans += a[i] + a[i - 1] - x
		a[i] = max(0, x - a[i - 1])
print(ans)


(N, x) = list(map(int, input().split()))
a = list(map(int, input().split()))
op = 0
for i in range(N - 1):
	s = a[i] + a[i + 1]
	opc = max(0, s - x)
	a[i + 1] = max(0, a[i + 1] - opc)
	op += opc
print(op)


(N, X) = map(int, input().strip().split(' '))
A = [0] + list(map(int, input().strip().split(' ')))
cost = 0
for i in range(N):
	if A[i] + A[i + 1] > X:
		cost += A[i + 1] - (X - A[i])
		A[i + 1] = X - A[i]
print(cost)


(N, x) = map(int, input().split())
A = [int(i) for i in input().split()]
A = [0] + A
ans = 0
for i in range(N):
	t = A[i] + A[i + 1]
	if t > x:
		ans += t - x
		A[i + 1] -= t - x
print(ans)


(N, X, *A) = map(int, open(0).read().split())
A += [0]
s = 0
for n in range(N):
	d = max(0, A[n] + A[n - 1] - X)
	s += d
	A[n] -= d
print(s)


(N, x) = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
for i in range(1, N):
	tmp = a[i - 1] + a[i]
	if tmp > x:
		ans += tmp - x
		if a[i] >= tmp - x:
			a[i] -= tmp - x
		else:
			a[i] = 0
print(ans)


(N, x) = map(int, input().split())
S = [int(i) for i in input().split()]
(pre, ans) = (0, 0)
for i in range(N):
	tmp = pre + S[i]
	if tmp > x:
		ans += tmp - x
		S[i] -= tmp - x
	pre = S[i]
print(ans)


(n, x) = map(int, input().split())
l = list(map(int, input().split()))
ans = 0
if l[0] > x:
	ans += l[0] - x
	l[0] -= ans
for i in range(1, n):
	if l[i] + l[i - 1] > x:
		b = l[i] + l[i - 1] - x
		ans += b
		l[i] -= b
print(ans)


(n, x) = list(map(int, input().split()))
narr = list(map(int, input().split()))
c = 0
for i in range(1, n):
	total = narr[i] + narr[i - 1]
	if total <= x:
		continue
	else:
		c += total - x
		narr[i] = max(0, narr[i] - (total - x))
print(c)


(N, x) = map(int, input().split())
a = list(map(int, input().split()))
count = 0
count += max(0, a[0] - x)
a[0] -= max(0, a[0] - x)
for i in range(N - 1):
	count += max(0, a[i] + a[i + 1] - x)
	a[i + 1] -= max(0, a[i] + a[i + 1] - x)
print(count)


(N, x) = map(int, input().split())
A = [int(x) for x in input().split()]
ans = 0
p = 0
for a in A:
	s = a + p
	p = a
	if s > x:
		e = s - x
		ans += e
		p = a - e
print(ans)


(N, x) = map(int, input().split())
A = list(map(int, input().split()))
count = 0
for i in range(N - 1):
	if A[i] + A[i + 1] > x:
		count += A[i] + A[i + 1] - x
		A[i + 1] = max(x - A[i], 0)
print(count)


import sys
import numpy as np

def input():
	return sys.stdin.readline().rstrip()

def ii():
	return int(input())

def mi():
	return map(int, input().split())

def li():
	return list(mi())

def main():
	(n, x) = mi()
	a = li()
	cnt = 0
	for i in range(1, n):
		if a[i - 1] + a[i] > x:
			cnt += a[i - 1] + a[i] - x
			a[i] = max(0, x - a[i - 1])
	print(cnt)

def __starting_point():
	main()
__starting_point()


(n, x) = map(int, input().split())
A = list(map(int, input().split()))
S = sum(A)
if A[0] > x:
	A[0] = x
for i in range(n - 1):
	s = A[i + 1] + A[i]
	if s > x:
		A[i + 1] -= s - x
print(S - sum(A))


(n, x) = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
b = 0
for i in range(n):
	c = b + a[i]
	if c > x:
		ans += c - x
		b = a[i] + x - c
	else:
		b = a[i]
print(ans)


(N, x) = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
i = 0
while i < N - 1:
	if A[i] + A[i + 1] > x:
		d = A[i] + A[i + 1] - x
		ans += d
		A[i + 1] = 0 if A[i + 1] < d else A[i + 1] - d
	i += 1
print(ans)


(n, x, *a) = map(int, open(0).read().split())
a = [0] + a
c = 0
for i in range(n):
	c += max(0, a[i + 1] + a[i] - x)
	a[i + 1] = min(a[i + 1], x - a[i])
print(c)


(n, x, *A) = map(int, open(0).read().split())
A += (0,)
s = 0
for i in range(n):
	d = max(0, A[i] + A[i - 1] - x)
	s += d
	A[i] -= d
print(s)


(N, x) = [int(n) for n in input().split(' ')]
A = [int(a) for a in input().split(' ')]
cnt = 0
if A[0] > x:
	cnt += A[0] - x
	A[0] = x
for i in range(1, len(A)):
	if A[i] + A[i - 1] > x:
		cnt += A[i] + A[i - 1] - x
		A[i] = x - A[i - 1]
print(cnt)


(N, x) = map(int, input().split())
A = list(map(int, input().split()))
pre = 0
ans = 0
for a in A:
	eat = max(0, pre + a - x)
	pre = a - eat
	ans += eat
print(ans)


(n, x) = list(map(int, input().split()))
a = [int(y) for y in input().split()]
ans = 0
for i in range(1, n):
	d = a[i - 1] + a[i]
	if d <= x:
		continue
	a[i] -= min(a[i], d - x)
	a[i - 1] -= max(d - x - a[i], 0)
	ans += d - x
print(ans)


(N, x) = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
for i in range(1, N):
	y = max(A[i] + A[i - 1] - x, 0)
	if A[i] >= y:
		A[i] -= y
	else:
		A[i] = 0
	ans += y
print(ans)


(n, x) = list(map(int, input().split()))
a = list(map(int, input().split()))
cnt = max(0, a[0] - x)
a[0] -= cnt
for i in range(1, n):
	cnt_now = max(0, a[i - 1] + a[i] - x)
	a[i] -= cnt_now
	cnt += cnt_now
print(cnt)


(N, x) = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
for (i, a) in enumerate(A):
	if i == 0:
		ans += min(a, x)
		now_x = x - min(a, x)
		continue
	ans += min(a, now_x)
	now_x = x - min(a, now_x)
ans2 = 0
for (i, a) in enumerate(A[::-1]):
	if i == 0:
		ans2 += min(a, x)
		now_x = x - min(a, x)
		continue
	ans2 += min(a, now_x)
	now_x = x - min(a, now_x)
print(sum(A) - max(ans, ans2))


def resolve():
	(N, X) = map(int, input().split())
	A = list(map(int, input().split()))
	ans = 0
	for i in range(1, N):
		diff = max(A[i - 1] + A[i] - X, 0)
		ans += diff
		A[i] = max(A[i] - diff, 0)
	print(ans)

def __starting_point():
	resolve()
__starting_point()


(n, x) = map(int, input().split())
l = list(map(int, input().split()))
count = 0
for i in range(n - 1):
	if l[i] > x:
		count += l[i] - x
		l[i] -= l[i] - x
	if l[i] + l[i + 1] > x:
		count += l[i] + l[i + 1] - x
		l[i + 1] -= l[i] + l[i + 1] - x
print(count)


import functools
(n, x) = map(int, input().split())
a = [(0, 0)] + list(map(int, input().split()))

def f(a, b):
	d = max(0, b + a[0] - x)
	return (max(0, b - d), a[1] + d)
print(functools.reduce(f, a)[1])


(n, x) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
cnt = 0
for i in range(n - 1):
	y = a[i] + a[i + 1] - x
	if y > 0:
		a[i + 1] = max(0, a[i + 1] - y)
		cnt += y
print(cnt)


(n, x) = map(int, input().split())
a = list(map(int, input().split()))
c = 0
if a[0] > x:
	c += a[0] - x
	a[0] = x
for i in range(n - 1):
	if a[i] + a[i + 1] > x:
		c += a[i] + a[i + 1] - x
		a[i + 1] = x - a[i]
print(c)


(N, x) = map(int, input().split())
A = list(map(int, input().split()))
A_org = sum(A)
if A[0] > x:
	A[0] = x
for i in range(N - 1):
	if A[i] + A[i + 1] > x:
		A[i + 1] = x - A[i]
print(A_org - sum(A))


(n, x) = map(int, input().split())
A = [0] + list(map(int, input().split()))
c = 0
for i in range(n):
	m = A[i] + A[i + 1]
	if m > x:
		A[i + 1] = x - A[i]
		c += m - x
print(c)


def boxes():
	(N, x) = input().split()
	(N, x) = (int(N), int(x))
	arr = [int(a) for a in input().split()]
	total = 0
	for i in range(1, N):
		(a, b) = (arr[i - 1], arr[i])
		extra = a + b - x
		if extra > 0:
			total += extra
			if b >= extra:
				arr[i] -= extra
			else:
				arr[i] = 0
	print(total)
boxes()


(N, x) = map(int, input().split())
a = list(map(int, input().split()))
left = 0
total = 0
for i in range(N):
	cnt = max(a[i] - (x - left), 0)
	total += cnt
	left = a[i] - cnt
print(total)


(N, x) = map(int, input().split())
a = [int(i) for i in input().split()]
ans = 0
if a[0] > x:
	ans += a[0] - x
	a[0] = x
for i in range(1, N):
	if a[i - 1] + a[i] > x:
		ans += a[i] + a[i - 1] - x
		a[i] = x - a[i - 1]
print(ans)


(N, X) = map(int, input().split())
A = list(map(int, input().split()))
ans = max(A[0] - X, 0)
A[0] = min(X, A[0])
for i in range(1, N):
	a = A[i - 1]
	b = A[i]
	if a + b > X:
		ans += b - (X - a)
		A[i] = X - a
print(ans)


(N, X) = (int(x) for x in input().split())
A = [int(x) for x in input().split()]
diffs = []
for i in range(N - 1):
	diffs.append(A[i] + A[i + 1] - X)
ret = 0
for i in range(N - 1):
	tmp = min(diffs[i], A[i + 1])
	ret += tmp
	diffs[i] -= tmp
	if i + 1 < len(diffs):
		diffs[i + 1] -= min(tmp, diffs[i + 1])
	ret += diffs[i]
print(max(0, ret))


(n, x) = map(int, input().split())
(*l,) = map(int, input().split())
ans = 0
for i in range(n - 1):
	m = max([0, l[i] + l[i + 1] - x])
	ans += m
	l[i + 1] -= min([l[i + 1], m])
print(ans)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
v = sum(a)
if k < a[0]:
	a[0] = k
for i in range(n - 1):
	if k < a[i] + a[i + 1]:
		a[i + 1] = k - a[i]
print(v - sum(a))


def solve():
	ans = 0
	(N, x) = map(int, input().split())
	A = list(map(int, input().split()))
	if A[0] > x:
		ans += A[0] - x
		A[0] = x
	for i in range(1, N):
		if A[i] + A[i - 1] > x:
			ans += A[i] + A[i - 1] - x
			A[i] = x - A[i - 1]
	return ans
print(solve())


import sys
input = sys.stdin.readline

def main():
	(N, x) = list(map(int, input().split()))
	A = list(map(int, input().split()))
	ans = 0
	for i in range(N - 1):
		if A[i] + A[i + 1] <= x:
			continue
		if A[i] >= x:
			ans += A[i] - x + A[i + 1]
			A[i] = x
			A[i + 1] = 0
		else:
			ans += A[i] + A[i + 1] - x
			A[i + 1] = x - A[i]
	print(ans)

def __starting_point():
	main()
__starting_point()


import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(10 ** 7)
INF = float('inf')

def I():
	return int(input())

def F():
	return float(input())

def SS():
	return input()

def LI():
	return [int(x) for x in input().split()]

def LI_():
	return [int(x) - 1 for x in input().split()]

def LF():
	return [float(x) for x in input().split()]

def LSS():
	return input().split()

def resolve():
	(N, x) = LI()
	a = LI()
	s = [a[i] + a[i + 1] for i in range(N - 1)]
	s_after = [min(i, x) for i in s]
	a_after = [0] * N
	a_after[0] = min(a[0], s_after[0])
	for i in range(N - 1):
		a_after[i + 1] = min(s_after[i] - a_after[i], a[i + 1])
	ans = sum(a) - sum(a_after)
	print(ans)

def __starting_point():
	resolve()
__starting_point()


(N, X) = map(int, input().split())
(*A,) = map(int, input().split())
ans = 0
if A[0] > X:
	ans += A[0] - X
	A[0] = X
i = 1
while i < N:
	if A[i] + A[i - 1] > X:
		ans += A[i] + A[i - 1] - X
		A[i] = X - A[i - 1]
	i += 1
print(ans)


(N, X) = (int(T) for T in input().split())
A = list(map(int, input().split()))
ans = 0
for i in range(0, N - 1):
	if A[i] + A[i + 1] > X:
		s = A[i] + A[i + 1] - X
		ans = s + ans
		A[i] = A[i] - max(0, A[i] - A[i + 1])
		A[i + 1] = max(0, A[i + 1] - s)
print(ans)


((N, x), a) = [list(map(int, input().split())) for _ in [0] * 2]
r = 0
for i in range(1, N):
	e = max(a[i] + a[i - 1] - x, 0)
	a[i] -= min(e, a[i])
	r += e
print(r)


(n, x) = map(int, input().split(' '))
list_a = list(map(int, input().split(' ')))
count = 0
last = 0
for a in list_a:
	act = max(0, last + a - x)
	last = a - act
	count += act
print(count)


(N, x) = map(int, input().split())
A = list(map(int, input().split()))
pre_a = 0
ans = 0
for a in A:
	if a > x - pre_a:
		ans += a - (x - pre_a)
		pre_a = x - pre_a
	else:
		pre_a = a
print(ans)


(N, x) = list(map(int, input().split()))
A = list(map(int, input().split()))
if x == 0:
	print(sum(A))
	return
count = 0
for i in range(N - 1):
	d = A[i] + A[i + 1] - x
	if d <= 0:
		continue
	if A[i + 1] >= d:
		A[i + 1] -= d
		count += d
	else:
		count += d
		A[i] = d - A[i + 1]
		A[i + 1] = 0
print(count)


(N, x) = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
pre_ai = A[0]
for i in range(1, N):
	ai = A[i]
	over = max(pre_ai + ai - x, 0)
	ans += over
	pre_ai = max(ai - over, 0)
print(ans)


(N, x) = map(int, input().split())
A = [int(i) for i in input().split()]
A.insert(0, 0)
ans = 0
for i in range(N):
	if A[i] + A[i + 1] > x:
		ans += abs(A[i + 1] - (x - A[i]))
		A[i + 1] = x - A[i]
print(ans)


(n, x) = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
for i in range(1, n):
	now = a[i] + a[i - 1]
	if x >= now:
		continue
	ans += now - x
	a[i] -= min(a[i], now - x)
print(ans)


(n, m) = map(int, input().split(' '))
a = list(map(int, input().split(' ')))
count = 0
for i in range(1, n - 1):
	suma = a[i - 1] + a[i]
	if suma > m:
		count += suma - m
		if a[i] > suma - m:
			a[i] -= suma - m
		else:
			a[i - 1] -= suma - m - a[i]
			a[i] = 0
else:
	if a[-1] + a[-2] > m:
		count += a[-2] + a[-1] - m
		a[-1] -= a[-2] + a[-1] - m
if n <= 2:
	if count + sum(a) - m >= 0:
		print(count + sum(a) - m)
	else:
		print(0)
else:
	print(count)


(N, x) = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
for i in range(1, N):
	num = max(a[i - 1] + a[i] - x, 0)
	ans += num
	a[i] = max(0, a[i] - num)
print(ans)


(n, x, *a) = map(int, open(0).read().split())
ans = 0
if a[0] > x:
	ans = a[0] - x
	a[0] = x
for i in range(n - 1):
	if a[i + 1] + a[i] > x:
		d = a[i + 1] + a[i] - x
		a[i + 1] -= d
		ans += d
print(ans)


(n, x) = map(int, input().split())
A = list(map(int, input().split()))
B = A[:]
s1 = 0
for i in range(n - 1):
	w = B[i] + B[i + 1]
	if w > x:
		s1 += w - x
		B[i + 1] = 0 if x <= B[i] else x - B[i]
print(s1)


(N, x) = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 0
for i in range(N - 1):
	if a[i] + a[i + 1] > x:
		diff = a[i] + a[i + 1] - x
		ans += diff
		a[i + 1] -= min(diff, a[i + 1])
print(ans)


(N, X) = (int(T) for T in input().split())
A = [int(T) for T in input().split()]
Eat = 0
for AN in range(0, N - 1):
	if A[AN] + A[AN + 1] > X:
		Need = A[AN] + A[AN + 1] - X
		if A[AN + 1] >= Need:
			A[AN + 1] -= Need
		else:
			A[AN] -= Need - A[AN + 1]
			A[AN + 1] = 0
		Eat += Need
print(Eat)


(n, x) = list(map(int, input().split()))
a = [int(i) for i in input().split()]
op = 0
for i in range(1, n):
	s = a[i - 1] + a[i]
	if s > x:
		op += s - x
		if a[i] < s - x:
			a[i] = 0
		else:
			a[i] -= s - x
print(op)


(N, x, *a) = map(int, open(0).read().split())
ans = 0
if a[0] > x:
	ans += a[0] - x
	a[0] = x
for i in range(N - 1):
	d = sum(a[i:i + 2]) - x
	if d > 0:
		ans += d
		a[i + 1] -= d
print(ans)


(N, x) = map(int, input().split())
a = list(map(int, input().split()))
cnt = 0
for i in range(1, N):
	if a[i - 1] > x:
		b = a[i - 1] - x
		cnt += b
		a[i - 1] -= b
	if a[i - 1] + a[i] > x:
		b = a[i - 1] + a[i] - x
		cnt += b
		a[i] -= b
print(cnt)


import math
(N, x) = list(map(int, input().split()))
L = [int(x) for x in input().split()]
ans = 0
for i in range(1, N):
	if L[i - 1] + L[i] > x:
		if L[i - 1] > x:
			ans += L[i - 1] - x
			ans += L[i]
			L[i] = 0
		else:
			ans += L[i] + L[i - 1] - x
			L[i] = x - L[i - 1]
print(ans)


(N, K) = map(int, input().split())
a = list(map(int, input().split()))
s = sum(a)
for i in range(N - 1):
	if a[i] >= K:
		a[i] = K
		a[i + 1] = 0
	else:
		a[i + 1] = min(a[i + 1], K - a[i])
print(s - sum(a))


(n, x, *a) = map(int, open(0).read().split())
t = r = 0
for i in a:
	t = min(i, x - t)
	r += i - t
print(r)


(N, x) = map(int, input().split())
A = [int(x) for x in input().split()]
prev = 0
answer = 0
for a in A:
	eat = max(0, prev + a - x)
	prev = a - eat
	answer += eat
print(answer)


from math import ceil, floor, factorial, gcd, sqrt, log2, cos, sin, tan, acos, asin, atan, degrees, radians, pi, inf
from itertools import accumulate, groupby, permutations, combinations, product, combinations_with_replacement
from collections import deque, defaultdict, Counter
from bisect import bisect_left, bisect_right
from operator import itemgetter
from heapq import heapify, heappop, heappush
from queue import Queue, LifoQueue, PriorityQueue
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)

def input():
	return sys.stdin.readline().strip()

def INT():
	return int(input())

def MAP():
	return map(int, input().split())

def LIST():
	return list(MAP())
(n, x) = MAP()
a = LIST()
ans = 0
for i in range(n - 1):
	if a[i] + a[i + 1] > x:
		ans += a[i] + a[i + 1] - x
		a[i + 1] = max(0, x - a[i])
print(ans)


(n, x) = map(int, input().split())
a = list(map(int, input().split()))
ans = [0] * n
if a[0] > x:
	ans[0] = x
else:
	ans[0] = a[0]
for i in range(1, n):
	ans[i] = min(a[i], x - ans[i - 1])
print(sum(a) - sum(ans))


(n, x) = map(int, input().split())
(*a,) = map(int, input().split())
b = []
b.append(min(a[0], x))
for i in range(1, n):
	b.append(max(min(x - b[i - 1], a[i]), 0))
print(sum(a) - sum(b))


(n, x) = list(map(int, input().split()))
a = list(map(int, input().split()))
cnt = 0
for i in range(n):
	if a[i] > x:
		cnt += a[i] - x
		a[i] = x
for i in range(n - 1):
	dif = a[i] + a[i + 1] - x
	if dif > 0:
		a[i + 1] -= dif
		cnt += dif
print(cnt)


(N, x) = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]
eat = [0] * N
eat[0] = max(A[0] - x, 0)
for i in range(N - 1):
	eat[i + 1] = max(A[i + 1] + A[i] - eat[i] - x, 0)
print(sum(eat))


(N, x) = map(int, input().split())
a = list(map(int, input().split()))
(answer, prev) = (0, 0)
for i in range(N):
	diff = max(a[i] + prev - x, 0)
	answer += diff
	a[i] -= diff
	prev = a[i]
print(answer)


(N, K) = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
for (i, j) in zip(range(N - 1), range(1, N)):
	Asum = A[i] + A[j]
	if Asum <= K:
		continue
	eat = Asum - K
	d = min(eat, A[j])
	A[j] -= d
	eat -= d
	ans += d
	d = min(eat, A[i])
	A[i] -= d
	ans += d
print(ans)


(N, x) = map(int, input().split())
l = list(map(int, input().split()))
c = 0
if l[0] > x:
	c += l[0] - x
	l[0] = x
for i in range(len(l) - 1):
	if l[i] + l[i + 1] > x:
		c += l[i + 1] + l[i] - x
		l[i + 1] = x - l[i]
print(c)


(n, x) = map(int, input().split())
c = 0
l = list(map(int, input().split()))
for i in range(1, n):
	if l[i - 1] + l[i] > x:
		c += l[i - 1] + l[i] - x
		l[i] = max(0, l[i] - (l[i - 1] + l[i] - x))
print(c)


(N, x) = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
for i in range(len(A) - 1):
	t = max(0, sum(A[i:i + 2]) - x)
	ans += t
	if A[i + 1] < t:
		A[i + 1] = 0
	else:
		A[i + 1] -= t
print(ans)


import sys
from io import StringIO
import unittest

class TestClass(unittest.TestCase):

	def assertIO(self, input, output):
		(stdout, stdin) = (sys.stdout, sys.stdin)
		(sys.stdout, sys.stdin) = (StringIO(), StringIO(input))
		resolve()
		sys.stdout.seek(0)
		out = sys.stdout.read()[:-1]
		(sys.stdout, sys.stdin) = (stdout, stdin)
		self.assertEqual(out, output)

	def test_入力例_1(self):
		input = '3 3\n2 2 2'
		output = '1'
		self.assertIO(input, output)

	def test_入力例_2(self):
		input = '6 1\n1 6 1 2 0 4'
		output = '11'
		self.assertIO(input, output)

	def test_入力例_3(self):
		input = '5 9\n3 1 4 1 5'
		output = '0'
		self.assertIO(input, output)

	def test_入力例_4(self):
		input = '2 0\n5 5'
		output = '10'
		self.assertIO(input, output)

def resolve():
	(N, x) = list(map(int, input().split()))
	a = list(map(int, input().split()))
	ans = 0
	for i in range(N):
		if a[i] > x:
			ans += a[i] - x
			a[i] = x
	for i in range(1, N):
		if a[i - 1] + a[i] > x:
			ans += a[i] + a[i - 1] - x
			a[i] = x - a[i - 1]
	print(ans)

def __starting_point():
	resolve()
__starting_point()


(N, x) = list(map(int, input().split()))
a = list(map(int, input().split()))
Sum = 0
if a[0] - x > 0:
	Sum += a[0] - x
	a[0] = x
if a[N - 1] - x > 0:
	Sum += a[N - 1] - x
	a[N - 1] = x
for i in range(1, N):
	d = max(a[i] + a[i - 1] - x, 0)
	a[i] -= d
	Sum += d
print(Sum)


(N, x) = map(int, input().split())
A = [0] + list(map(int, input().split()))
t = 0
for i in range(N):
	eat = max(0, A[i] + A[i + 1] - x)
	A[i + 1] -= eat
	t += eat
print(t)


(N, X) = (int(x) for x in input().split())
A = [int(x) for x in input().split()]
ret = 0
for i in range(N - 1):
	if A[i] + A[i + 1] > X:
		tmp = A[i] + A[i + 1] - X
		ret += tmp
		A[i + 1] -= min(A[i + 1], tmp)
print(ret)


(N, x) = map(int, input().split())
a = list(map(int, input().split()))
cnt = 0
for i in range(1, N):
	s = a[i - 1] + a[i]
	ov = s - x
	if ov > 0:
		cnt += ov
		if a[i] < ov:
			a[i] = 0
		else:
			a[i] -= ov
print(cnt)


(n, x) = list(map(int, input().split()))
a = list(map(int, input().split()))
if x == 0:
	print(sum(a))
else:
	eat = 0
	for i in range(n - 1):
		tmp = 0
		if a[i] + a[i + 1] > x:
			tmp = a[i] + a[i + 1] - x
			eat += tmp
			if tmp > a[i + 1]:
				tmp -= a[i + 1]
				a[i + 1] = 0
				a[i] -= tmp
			else:
				a[i + 1] -= tmp
	print(eat)


import sys
(N, X) = map(int, input().split())
A = list(map(int, sys.stdin.readline().rsplit()))
B = A[:]
for i in range(N - 1):
	a = A[i] + A[i + 1]
	if a > X:
		if A[i + 1] >= a - X:
			A[i + 1] -= a - X
		else:
			A[i] -= a - X - A[i + 1]
			A[i + 1] = 0
res = 0
for i in range(N):
	res += B[i] - A[i]
print(res)


(n, x) = map(int, input().split())
a = list(map(int, input().split()))
s = sum(a)
for i in range(n - 1):
	if a[i] + a[i + 1] < x:
		continue
	if a[i] > x:
		a[i] = x
	a[i + 1] = x - a[i]
print(s - sum(a))


(n, x) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
b = [i for i in a]
b[0] = min(b[0], x)
for i in range(1, n):
	if b[i - 1] + b[i] > x:
		b[i] = x - b[i - 1]
print(sum(a) - sum(b))


(N, X) = map(int, input().split())
(*A,) = map(int, input().split())
B = [0] * N
for i in range(N):
	B[i] = A[i] if A[i] <= X else X
cnt = sum(A) - sum(B)
for i in range(N - 1):
	if X < B[i] + B[i + 1]:
		d = B[i] + B[i + 1] - X
		B[i + 1] -= d
		cnt += d
print(cnt)


(n, x) = map(int, input().split())
A = list(map(int, input().split()))
C = A[:]
s2 = 0
for i in range(n - 1):
	v = C[n - 1 - i] + C[n - 2 - i]
	if v > x:
		s2 += v - x
		C[n - 2 - i] = 0 if x <= C[n - 1 - i] else x - C[n - 1 - i]
print(s2)


(n, x) = map(int, input().split())
(*a,) = map(int, input().split())
v = [i if i < x else x for i in a]
for i in range(n - 1):
	if v[i] + v[i + 1] > x:
		v[i + 1] = x - v[i]
print(sum(a) - sum(v))


(N, x) = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
sum_val = A[0] + A[1]
if A[0] + A[1] > x:
	if A[1] >= sum_val - x:
		ans += sum_val - x
		A[1] -= sum_val - x
	else:
		ans += A[1] + (A[0] - x)
		A[0] -= x - A[1]
		A[1] = 0
for i in range(1, N - 1):
	sum_val = A[i] + A[i + 1]
	if sum_val <= x:
		continue
	ans += sum_val - x
	A[i + 1] -= sum_val - x
print(ans)


(N, x, *A) = list(map(int, open(0).read().split()))
ans = 0
for i in range(1, N):
	required = A[i] + A[i - 1] - x
	if required <= 0:
		continue
	elif required <= A[i]:
		A[i] -= required
	else:
		A[i] = 0
		rem = required - A[i]
		A[i - 1] -= rem
	ans += required
print(ans)


(n, x) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
y = 0
a1 = 0
a2 = 0
for a3 in a:
	s = max(a1 + a2 - x, a2 + a3 - x, 0)
	y += s
	a1 = a2
	a2 = a3 - s
print(y)


(n, x) = map(int, input().split())
a = list(map(int, input().split()))
asum = sum(a)
if a[0] > x:
	a[0] = x
for i in range(n - 1):
	if a[i] + a[i + 1] > x:
		a[i + 1] = x - a[i]
asum = asum - sum(a)
print(asum)


(N, X) = map(int, input().split())
L = list(map(int, input().split()))
count = 0
for i in range(N - 1):
	if i == 0:
		if L[i] > X:
			count += L[i] - X
			L[i] = X
	if L[i + 1] + L[i] > X:
		count += L[i + 1] - (X - L[i])
		L[i + 1] = X - L[i]
print(count)


(n, x) = map(int, input().split())
A = list(map(int, input().split())) + [0]
ans = 0
for i in range(n):
	diff = max(0, A[i] + A[i - 1] - x)
	ans += diff
	A[i] -= diff
print(ans)


import sys
input = sys.stdin.readline
(n, x) = map(int, input().split())
a_list = list(map(int, input().split()))
count = 0
if a_list[0] > x:
	count += a_list[0] - x
	a_list[0] -= a_list[0] - x
for i in range(len(a_list)):
	if i + 1 == len(a_list):
		break
	if a_list[i] + a_list[i + 1] > x:
		count += a_list[i] + a_list[i + 1] - x
		a_list[i + 1] -= a_list[i] + a_list[i + 1] - x
print(count)


(N, x) = [int(x) for x in input().split()]
a = list([int(x) for x in input().split()])
count = 0
for i in range(N):
	if a[i] > x:
		count += a[i] - x
		a[i] -= a[i] - x
	if i - 1 >= 0:
		if a[i] + a[i - 1] > x:
			count += a[i] + a[i - 1] - x
			a[i] -= a[i] + a[i - 1] - x
print(count)


(N, x) = map(int, input().split())
ls = list(map(int, input().split()))
ans = 0
for i in range(N - 1):
	if ls[i] + ls[i + 1] <= x:
		continue
	else:
		eat = -x + ls[i] + ls[i + 1]
		ls[i + 1] -= eat
		if ls[i + 1] < 0:
			ls[i + 1] = 0
		ans += eat
print(ans)


f = lambda : map(int, input().split())
(N, X) = f()
(*A,) = f()
A = [0] + A
c = 0
for i in range(N):
	c += max(0, A[i] + A[i + 1] - X)
	A[i + 1] = min(A[i + 1], X - A[i])
print(c)


(n, x) = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
p = 0
for ai in a:
	if p + ai > x:
		d = p + ai - x
		ans += d
		p = ai - d
	else:
		p = ai
print(ans)


import io
(nim, mike) = map(int, input().split())
kite = list(map(int, input().split()))
counter1 = 0
result = 0
for i in kite:
	highest = max(0, counter1 + i - mike)
	counter1 = i - highest
	result += highest
print(result)


(N, X) = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
if A[0] > X:
	ans += A[0] - X
	A[0] = X
for i in range(1, N):
	S = A[i - 1] + A[i]
	if S > X:
		ans += S - X
		A[i] -= S - X
print(ans)


def main():
	(N, x) = list(map(int, input().split()))
	A = list(map(int, input().split()))
	Acount = sum(A)
	if A[0] > x:
		A[0] = x
	for i in range(N - 1):
		tmp = A[i] + A[i + 1]
		if tmp > x:
			A[i + 1] -= tmp - x
	ans = Acount - sum(A)
	print(ans)

def __starting_point():
	main()
__starting_point()


(N, x) = map(int, input().split())
A = [int(i) for i in input().split()]
ans = max(A[0] - x, 0)
A[0] = min(x, A[0])
for i in range(N - 1):
	sur = max(A[i] + A[i + 1] - x, 0)
	ans += sur
	A[i + 1] -= sur
print(ans)


(n, x) = map(int, input().split())
a = list(map(int, input().split()))
count = 0
for i in range(1, n):
	if a[i - 1] < 0:
		a[i - 1] = 0
	b = a[i - 1] + a[i]
	if b > x:
		a[i] -= b - x
		count += b - x
print(count)


(N, x) = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
prev = 0
for i in range(N):
	c = a[i] + prev - x
	if c > 0:
		ans += c
		a[i] -= c
	prev = a[i]
print(ans)


(n, x) = map(int, input().split())
a = list(map(int, input().split()))
eat = 0
for i in range(1, n):
	s = a[i - 1] + a[i] - x
	if s > 0:
		eat += min(s, a[i])
		a[i] -= min(s, a[i])
s = a[1] + a[0] - x
eat += max(s, 0)
print(eat)


(n, x) = [int(i) for i in input().split()]
a = [0] + [int(i) for i in input().split()]
ans = 0
for i in range(n):
	if a[i] + a[i + 1] > x:
		ans += a[i] + a[i + 1] - x
		a[i + 1] -= a[i] + a[i + 1] - x
print(ans)


(n, x) = map(int, input().split())
l = list(map(int, input().split()))
c = 0
t = l[0]
for a in l[1:]:
	d = t + a - x
	if d > 0:
		if a < d:
			a = 0
		else:
			a -= d
		c += d
	t = a
print(c)


(n, x) = list(map(int, input().split()))
A = list(map(int, input().split()))
cnt = [0] * (n - 1)
ans1 = 0
for i in range(n - 1):
	cnt[i] = A[i] + A[i + 1]
	cnt[i] -= x
	cnt[i] = max(cnt[i], 0)
temp_cnt = cnt[:]
for i in range(n - 1):
	if temp_cnt[i] == 0:
		continue
	if i < n - 2:
		ans1 += temp_cnt[i]
		temp_cnt[i + 1] -= min(A[i + 1], temp_cnt[i])
		temp_cnt[i] = 0
		temp_cnt[i + 1] = max(temp_cnt[i + 1], 0)
ans1 += temp_cnt[-1]
print(ans1)


import collections
(n, x) = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
q = collections.deque()
if a[0] <= x:
	q.append(a[0])
else:
	ans += a[0] - x
	q.append(x)
for i in range(1, n):
	tmp = q[-1] + a[i]
	if tmp <= x:
		q.append(a[i])
	else:
		ans += tmp - x
		q.append(x - q[-1])
	q.popleft()
print(ans)


(N, x) = map(int, input().split())
A = list(map(int, input().split()))
if A[0] > x:
	cnt = A[0] - x
	A[0] = A[0] - cnt
else:
	cnt = 0
for i in range(1, N):
	gap = A[i] + A[i - 1] - x
	if gap > 0:
		cnt += gap
		A[i] -= gap
print(cnt)


(n, x) = [int(_) for _ in input().split(' ')]
a = [int(_) for _ in input().split(' ')]
y = 0
for i in range(len(a) - 1):
	c = a[i] + a[i + 1]
	if c > x:
		a[i + 1] = max(0, a[i + 1] - (c - x))
		y += c - x
print(y)


(n, x) = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
add_a = []
for i in range(n - 1):
	add_a.append(max(a[i] + a[i + 1] - x, 0))
ans = 0
for i in range(n - 1):
	ans += min(add_a[i], a[i + 1])
	if i != n - 2:
		add_a[i + 1] = max(0, add_a[i + 1] - min(add_a[i], a[i + 1]))
	add_a[i] -= min(add_a[i], a[i + 1])
ans += add_a[0]
print(ans)


(n, x) = list(map(int, input().split()))
al = list(map(int, input().split()))
cnt = 0
for i in range(n - 1):
	if al[i] + al[i + 1] > x:
		if al[i + 1] > al[i] + al[i + 1] - x:
			cnt += al[i] + al[i + 1] - x
			al[i + 1] -= al[i] + al[i + 1] - x
		else:
			cnt += al[i + 1] + al[i] - x
			al[i + 1] = 0
			al[i] = x
print(cnt)


f = lambda : map(int, input().split())
(n, x) = f()
l = f()
t = a = 0
for i in l:
	t = min(i, x - t)
	a += i - t
print(a)


(n, x, *a) = map(int, open(0).read().split())
b = [0] * n
b[0] = min(x, a[0])
for i in range(n - 1):
	b[i + 1] = max(min(x - b[i], a[i + 1]), 0)
	b[i] = min(b[i], x - b[i + 1])
ans = 0
for (i, j) in zip(a, b):
	ans += i - j
print(ans)


(n, x) = map(int, input().split())
a = [int(i) for i in input().split()]
cnt = 0
for i in range(n - 1):
	if a[i] + a[i + 1] > x:
		n = a[i] + a[i + 1] - x
		a[i + 1] -= min(n, a[i + 1])
		cnt += n
print(cnt)


(N, x) = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
for i in range(N - 1):
	L = A[i]
	R = A[i + 1]
	eat = max(0, L + R - x)
	ans += eat
	A[i + 1] = max(0, R - eat)
print(ans)


s = input().split()
n = int(s[0])
x = int(s[1])
s = input().split()
ans = max(0, int(s[0]) + int(s[1]) - x)
prev = max(0, int(s[1]) - ans)
for i in range(2, len(s)):
	now = max(0, int(s[i]) + prev - x)
	ans += now
	prev = int(s[i]) - now
print(ans)


(N, x) = list(map(int, input().split()))
A = list(map(int, input().split()))
cnt = 0
pre = 0
for i in range(N):
	dif = max(A[i] + pre - x, 0)
	cnt += dif
	A[i] -= dif
	pre = A[i]
print(cnt)


(n, x) = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
ans = 0
y = x
for a in A:
	if a > y:
		ans += a - y
	y = x - min(a, y)
print(ans)


(n, x) = map(int, input().split())
a = list(map(int, input().split()))
eat = 0
for i in range(n - 1):
	wa = a[i] + a[i + 1]
	if wa > x:
		sa = wa - x
		eat += sa
		if sa <= a[i + 1]:
			a[i + 1] -= sa
		else:
			a[i + 1] = 0
print(eat)


(n, x) = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
syaku = 0
for i in range(n - 1):
	if a[i] + a[i + 1] >= x:
		sa = a[i] + a[i + 1] - x
		ans += sa
		if sa <= a[i + 1]:
			a[i + 1] -= sa
		else:
			a[i] = sa - a[i + 1]
			a[i + 1] = 0
if n != 2 and a[n - 2] + a[n - 1] >= x:
	sa = a[i] + a[i + 1] - x
	ans += sa
	if sa <= a[n - 1]:
		a[n - 1] -= sa
	else:
		a[n - 2] = sa - a[n - 1]
		a[n - 1] = 0
print(ans)


(N, X) = map(int, input().split())
a = list(map(int, input().split()))
total = sum(a)
if a[0] > X:
	a[0] = X
for i in range(N - 1):
	if a[i] + a[i + 1] > X:
		a[i + 1] = X - a[i]
print(total - sum(a))


(n, x) = map(int, input().split())
A = [int(i) for i in input().split()]
count = max(A[0] - x, 0)
t = min(A[0], x)
for i in range(1, n):
	count += max(A[i] - (x - t), 0)
	t = min(A[i], x - t)
print(count)


(N, X) = map(int, input().split())
A = list(map(int, input().split()))
cnt = 0
for i in range(N - 1):
	temp = 0
	if A[i] + A[i + 1] <= X:
		continue
	else:
		temp = A[i] + A[i + 1] - X
		if A[i + 1] < temp:
			A[i + 1] = 0
			cnt += A[i + 1]
			temp -= A[i + 1]
		else:
			A[i + 1] -= temp
			cnt += temp
			temp -= A[i + 1]
		if A[i] + A[i + 1] > X:
			A[i] -= temp
			cnt += temp
print(cnt)


(n, x) = map(int, input().split())
A = list(map(int, input().split()))
cnt = 0
for i in range(n - 1):
	point = A[i] + A[i + 1]
	cnt += max(0, point - x)
	if point > x:
		A[i + 1] = max(0, x - A[i])
print(cnt)


(N, X) = map(int, input().split())
(*A,) = map(int, input().split())
B = [a if a <= X else X for a in A]
c = sum(A) - sum(B)
for i in range(N - 1):
	t = B[i] + B[i + 1]
	if X < t:
		d = t - X
		B[i + 1] -= d
		c += d
print(c)


(N, x) = list(map(int, input().split()))
arr = list(map(int, input().split()))
(cnt, prv) = (0, 0)
for i in range(N):
	diff = max(arr[i] + prv - x, 0)
	cnt += diff
	arr[i] -= diff
	prv = arr[i]
print(cnt)


(a, b) = map(int, input().split())
L = list(map(int, input().split()))
s = 0
if L[0] > b:
	s += L[0] - b
	L[0] = b
for i in range(1, a):
	if L[i - 1] + L[i] > b:
		s += L[i] - b + L[i - 1]
		L[i] = b - L[i - 1]
print(s)


(n, x) = map(int, input().split())
a = list(map(int, input().split()))
k = 0
cnt = 0
if a[1] + a[0] > x and x >= a[0]:
	k = x - a[0]
	cnt += a[1] + a[0] - x
elif a[1] + a[0] > x and x < a[0]:
	k = 0
	cnt += a[1] + a[0] - x
else:
	k = a[1]
for i in range(1, n - 1):
	if k + a[i + 1] <= x:
		k = a[i + 1]
	elif x >= k:
		cnt += k + a[i + 1] - x
		k = x - k
	else:
		cnt += k + a[i + 1] - x
		k = 0
print(cnt)


(N, x) = map(int, input().split())
a = list(map(int, input().split()))
r1 = sum(a)
flow = 0
for i in range(1, N):
	t = a[i] + a[i - 1] - x
	if flow != 0:
		a[i - 1] -= flow
		t -= flow
		flow = 0
	if t > 0:
		if t > a[i]:
			t -= a[i]
			a[i - 1] -= t
			t = a[i]
		flow = t
a[-1] -= flow
r2 = sum(a)
print(r1 - r2)


(N, X) = (int(T) for T in input().split())
A = [int(T) for T in input().split()]
Eat = 0
for AT in range(0, N - 1):
	if A[AT] + A[AT + 1] > X:
		Need = A[AT] + A[AT + 1] - X
		Eat += Need
		A[AT] -= max(0, Need - A[AT + 1])
		A[AT + 1] = max(0, A[AT + 1] - Need)
print(Eat)


(N, x) = list(map(int, input().split()))
nums = list(map(int, input().split()))
ans = 0
for i in range(1, N):
	if nums[i - 1] + nums[i] > x:
		diff = nums[i - 1] + nums[i] - x
		if diff > nums[i]:
			nums[i - 1] -= diff - nums[i]
			nums[i] = 0
		else:
			nums[i] -= diff
		ans += diff
print(ans)


(n, x) = map(int, input().split())
l = [int(i) for i in input().split()]
ans = 0
for i in range(1, n):
	if l[i] + l[i - 1] > x:
		tmp = l[i] + l[i - 1] - x
		ans += tmp
		l[i] = max(0, l[i] - tmp)
print(ans)


(n, x) = [int(s) for s in input().split()]
a_list = [int(s) for s in input().split()]
ans = 0
temp = a_list[0] - x
if temp > 0:
	ans += temp
	a_list[0] -= temp
for i in range(1, n):
	temp = a_list[i] + a_list[i - 1] - x
	if temp > 0:
		ans += temp
		a_list[i] -= temp
print(ans)


(n, x) = list(map(int, input().split()))
A = tuple(map(int, input().split()))
pre = max(0, A[0] - x)
ans = pre
for i in range(n - 1):
	a = A[i]
	b = A[i + 1]
	a -= pre
	tmp = 0
	if b > x:
		tmp += b - x
		b = x
	if a + b > x:
		tmp += a + b - x
	ans += tmp
	pre = tmp
print(ans)


(n, k) = map(int, input().split())
a = list(map(int, input().split()))
result = 0
if a[0] > k:
	result = a[0] - k
	a[0] = k
for i in range(n - 1):
	if a[i] + a[i + 1] > k:
		result += a[i] + a[i + 1] - k
		a[i + 1] = -a[i] + k
print(result)


(N, x) = map(int, input().split())
A = list(map(int, input().split())) + [0]
cnt = 0
for i in range(N):
	tmp = max(0, A[i] + A[i - 1] - x)
	cnt += tmp
	A[i] -= tmp
print(cnt)


(n, x) = map(int, input().split())
a = list(map(int, input().split()))
t = 0
ans = 0
for i in a:
	s = max(0, t + i - x)
	t = i - s
	ans += s
print(ans)


(n, m) = map(int, input().split())
a = list(map(int, input().split()))
total = sum(a)
if m < a[0]:
	a[0] = m
for i in range(1, n):
	if m < a[i] + a[i - 1]:
		a[i] = m - a[i - 1]
tot = sum(a)
print(total - tot)


(n, x) = map(int, input().split())
a = map(int, input().split())
res = 0
pre = 0
for i in a:
	if i + pre > x:
		res += i + pre - x
		pre = i - (i + pre - x)
	else:
		pre = i
print(res)


(N, x) = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
pre = 0
for i in range(N - 1):
	s = A[i] + A[i + 1] - pre
	ans += max(0, s - x)
	pre = min(max(0, s - x), A[i + 1])
print(ans)


(N, x) = map(int, input().split())
a = list(map(int, input().split()))
y = min(a[0], x)
ans = a[0] - y
for i in range(1, N):
	z = a[i]
	w = max(y + z - x, 0)
	y = z - w
	ans += w
print(ans)


(a, b) = map(int, input().split())
l = list(map(int, input().split()))
t = l[0]
ans = max(0, t - b)
t = min(t, b)
for i in l[1:]:
	ans += max(0, t + i - b)
	t = max(0, i - max(0, t + i - b))
print(ans)


(n, x) = map(int, input().split())
l = [int(i) for i in input().split()]
last = 0
z = 0
for n in l:
	if n + last > x:
		z += n + last - x
		last = x - last
	else:
		last = n
print(z)


def candy():
	(boxNum, x) = [int(i) for i in input().split()]
	ain = input()
	alist = ain.split(' ')
	s = 0
	for i in range(0, boxNum - 1):
		if int(alist[i + 1]) + int(alist[i]) > x:
			s = s + (int(alist[i + 1]) + int(alist[i]) - x)
			alist[i + 1] = x - int(alist[i])
			if int(alist[i + 1]) < 0:
				alist[i] = int(alist[i]) + int(alist[i + 1])
				alist[i + 1] = 0
	print(s)
candy()


(N, x, *a) = map(int, open(0).read().split())
temp = 0
ans = 0
for c in a:
	t1 = temp
	temp += c
	if temp > x:
		ans += temp - x
		temp = x - t1
	else:
		temp -= t1
print(ans)


(N, x) = map(int, input().split())
a = list(map(int, input().split()))
cnt = 0
for i in range(1, N):
	eat = max(a[i - 1] + a[i] - x, 0)
	cnt += eat
	a[i] -= min(eat, a[i])
print(cnt)


(N, x) = map(int, input().split())
values = map(int, input().split())
count = 0
v1 = next(values)
for v2 in values:
	surplus = max(v1 + v2 - x, 0)
	count += surplus
	v1 = max(0, v2 - surplus)
print(count)


(N, X) = map(int, input().split())
A = list(map(int, input().split()))
wa = []
ans = 0
if A[0] > X:
	ans += A[0] - X
	A[0] = X
for i in range(N - 1):
	wa.append(A[i] + A[i + 1])
for j in range(N - 2):
	if wa[j] > X:
		ans += wa[j] - X
		wa[j + 1] -= wa[j] - X
if wa[N - 2] > X:
	ans += wa[N - 2] - X
print(ans)


(n, x) = map(int, input().split())
a = [0] + list(map(int, input().split()))
count = 0
for i in range(n):
	diff = max(0, a[i] + a[i + 1] - x)
	count += diff
	a[i + 1] -= diff
print(count)


(n, x, *A) = map(int, open(0).read().split())
s = 0
if A[0] > x:
	s += A[0] - x
	A[0] = x
for i in range(1, n):
	if A[i] + A[i - 1] > x:
		s += A[i] + A[i - 1] - x
		A[i] = x - A[i - 1]
print(s)


(n, x) = [int(x) for x in input().split()]
an = list(map(int, input().split())) + [0]
answer = 0
for i in range(n):
	tmp = max(0, an[i] + an[i - 1] - x)
	answer += tmp
	an[i] -= tmp
print(answer)


(n, x) = list(map(int, input().split()))
a = list(map(int, input().split()))
b = []
for i in range(1, n):
	b.append(a[i] + a[i - 1])
b.append(0)
res = 0
for i in range(n - 1):
	dif = max(b[i] - x, 0)
	minus = min(dif, a[i + 1])
	b[i + 1] -= minus
	b[i] -= dif
	res += dif
print(res)


(n, x) = map(int, input().split())
a = list(map(int, input().split()))
s = [0] * (n - 1)
ans = 0
if a[0] > x:
	ans += a[0] - x
	a[0] = x
for i in range(n - 1):
	s[i] = a[i] + a[i + 1]
for i in range(n - 2):
	if s[i] > x:
		t = s[i] - x
		s[i + 1] -= t
		s[i] = x
		ans += t
if s[n - 2] > x:
	ans += s[n - 2] - x
print(ans)


(N, x) = map(int, input().split())
A = [int(x) for x in input().split()]
ans = 0
p = 0
for a in A:
	e = max(0, a + p - x)
	ans += e
	p = a - e
print(ans)


(N, X) = map(int, input().split())
A = list(map(int, input().split()))
A_sum = sum(A)
A[0] = min(X, A[0])
for i in range(N - 1):
	if A[i] + A[i + 1] > X:
		A[i + 1] = X - A[i]
print(A_sum - sum(A))


(n, x) = map(int, input().split())
a = list(map(int, input().split()))
sm = 0
for i in range(n - 1):
	b = max(a[i] + a[i + 1] - x, 0)
	a[i + 1] = max(a[i + 1] - b, 0)
	sm += b
print(sm)


(n, x) = map(int, input().split())
arr = list(map(int, input().split()))
sum_arr = sum(arr)
arr[0] = min(x, arr[0])
for i in range(n - 1):
	diff = arr[i] + arr[i + 1]
	if diff > x:
		arr[i + 1] -= diff - x
print(sum_arr - sum(arr))


(n, x) = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 0
for i in range(n - 1):
	pre = a[i]
	post = a[i + 1]
	many = pre + post - x
	if many > 0:
		ans += many
		if many <= post:
			a[i + 1] -= many
		else:
			a[i] -= many - post
			a[i + 1] = 0
print(ans)


def candy():
	n = []
	n = input().split()
	N = int(n[0])
	x = int(n[1])
	alist = [int(i) for i in input().split()]
	s = 0
	for i in range(0, N - 1):
		c = int(alist[i])
		d = int(alist[i + 1])
		if int(alist[i] + alist[i + 1]) > x:
			if int(alist[i]) >= x:
				alist[i] = x
				alist[i + 1] = 0
			else:
				alist[i + 1] = int(x - alist[i])
			s += c - int(alist[i]) + d - int(alist[i + 1])
	print(s)
candy()


(n, x) = list(map(int, input().split()))
lst = [0] + list(map(int, input().split()))
ans = 0
for i in range(n):
	if lst[i] + lst[i + 1] > x:
		ans += lst[i] + lst[i + 1] - x
		lst[i + 1] -= lst[i] + lst[i + 1] - x
print(ans)