CodeEff/ECCO huggingface dataset:
# Original solution
# coding: utf-8
N, x = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 0
for i in range(N - 1): # 貪欲法で解く
    while a[i] + a[i + 1] > x and a[i + 1] >= 1: # 左の組から順に、組の右側から食べていく
        ans += 1
        a[i + 1] -= 1
    while a[i] + a[i + 1] > x: # それでも足りないなら組の左側も食べる
        ans += 1
        a[i] -= 1
print(ans)

# Optimized solution
# coding: utf-8
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 and a[i + 1] >= 1: # 左の組から順に、組の右側から食べていく
        ans += min(a[i] + a[i + 1] - x, a[i + 1])
        a[i + 1] -= min(a[i] + a[i + 1] - x, a[i + 1])
    if a[i] + a[i + 1] > x: # それでも足りないなら組の左側も食べる
        ans += a[i] + a[i + 1] - x
        a[i] -= a[i] + a[i + 1] - x
print(ans)
# Original solution
N, x = list(map(int, input().split()))
a = list(map(int, input().split()))

diff = [a[i] + a[i+1] for i in range(N-1)]

counter = 0

for i in range(N-2):
    while diff[i] > x:
        counter += 1
        diff[i] -= 1
        diff[i+1] -= 1
if diff[N-2] > x:
    counter += diff[N-2] - x

print(counter)

# Optimized solution
N, x = list(map(int, input().split()))
a = list(map(int, input().split()))

a += [0]
counter = 0

for i in range(N-1):
    flow = a[i] + a[i+1] - x
    if flow > 0:
        counter += flow
        a[i+1] = max(a[i+1]-flow, 0)

print(counter)
# Original solution
# -*- coding: utf-8 -*-

# AtCoder Beginner Contest
# Problem C

if __name__ == '__main__':
    n, x = list(map(int, input().split()))
    a = list(map(int, input().split()))
    count = 0

    if a[0] > x:
        count += a[0] - x
        a[0] -= count

    for i in range(n - 1):
        summed = a[i] + a[i + 1]

        if summed > x:
            diff = summed - x
            count += diff
            a[i + 1] -= diff

    print(count)


# Optimized solution
# -*- coding: utf-8 -*-

# AtCoder Beginner Contest
# Problem C

if __name__ == '__main__':
    n, x = list(map(int, input().split()))
    a = list(map(int, input().split()))
    count = 0

    if a[0] > x:
        count += a[0] - x
        a[0] -= count

    for i in range(n - 1):
        diff = a[i] + a[i + 1] - x

        if diff > 0:
            count += diff
            a[i + 1] -= diff

    print(count)
# Original solution
n,x=list(map(int,input().split()));a=list(map(int,input().split()));ans=0
for i in range(n-1):
  while sum(a[i:i+1+1])>x:
    if a[i+1]==0:a[i]-=1
    else:a[i+1]-=1
    ans+=1
print(ans)

# Optimized solution
n,x=list(map(int,input().split()));a=list(map(int,input().split()));ans=0
for i in range(n-1):
  m=a[i]+a[i+1]
  if a[i]>=x:a[i+1]=0;a[i]=x
  else:a[i+1]=min(x-a[i],a[i+1])
  ans+=m-a[i]-a[i+1]
print(ans)
# Original solution
n, x = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
count = 0
for i in a:
    if i > x:
        i = x
        count += i - x
b = a.copy()

count1 = count
for i in range(n-1):
    while a[i] + a[i+1] > x:
        if a[i] > 0:
            a[i] -= 1
        else:
            a[i+1] -= 1
        count1 += 1

count2 = count
for i in range(n-1):
    while b[i] + b[i+1] > x:
        
        if b[i+1] > 0:
            b[i+1] -= 1
        else:
            b[i] -= 1
        count2 += 1

count = min(count1, count2)
print(count)








# Optimized solution
n, x = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]

count = 0
previous = 0

for i in a:
    if i + previous > x: 
        excess = i + previous - x
        count += excess
        previous = x - previous
    else: # if not exceeding x.
        previous = i

print(count)
# Original solution
import sys

n, x, *a = list(map(int, sys.stdin.read().split()))
a = [0] + a

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

if __name__ ==  '__main__':
  main()

# Optimized solution
import sys

n, x, *a = list(map(int, sys.stdin.read().split()))

def main():
  cnt = 0
  lim = x
  for c in a:
    if c > lim:
      cnt += c - lim
      lim = x - lim
    else:
      lim = x - c
  print(cnt)

if __name__ ==  '__main__':
  main()
# Original solution
N, x = list(map(int, input().split()))
A = list(map(int, input().split()))
cnt = 0
for i in range(N-1):
    while (A[i]+A[i+1])>x:
        if A[i+1]>0:
            A[i+1] -= 1
        else:
            A[i] -= 1
        cnt += 1
print(cnt)

# Optimized solution
N, x = list(map(int, input().split()))
A = list(map(int, input().split()))
cnt = 0
for i in range(N-1):
    if A[i] + A[i+1] > x:
        diff = A[i] + A[i+1] - x
        if A[i+1]>diff:
            A[i+1] -= diff
        else:
            A[i+1] = 0
        cnt += diff
print(cnt)
# Original solution
n, x = list(map(int, input().split()))
A = list(map(int, input().split()))
ans = 0
for i in range(1, n):
    if A[i-1]+A[i] <= x:
        continue
    eat = A[i-1]+A[i] - x
    ans += eat
    A[i] = max(0, A[i]-eat)

print(ans)

# Optimized solution
n, x = list(map(int, input().split()))
A = list(map(int, input().split()))
ans = 0
for i in range(n):
    if i == 0:
        if A[i] <= x:
            continue
        eat = A[i] - x
        ans += eat
        A[i] = A[i] - eat
        continue
    if A[i-1]+A[i] <= x:
        continue
    eat = A[i-1] + A[i] - x
    ans += eat
    A[i] = A[i] - eat

print(ans)
# Original solution
n, x = list(map(int, input().split()))
a = list(map(int, input().split()))

num = len(a)
ans = 0

for i in range(num - 1):
    tar = a[i] + a[i + 1]
    if (tar <= x):
        continue
    else:
        while (tar > x):
            a[i + 1] -= 1
            ans += 1
            tar = a[i] + a[i + 1]


print(ans)


# Optimized solution
n, x = list(map(int, input().split()))
a = list(map(int, input().split()))
num = len(a)


ans = 0

for i in range(num - 1):
    tar = a[i] + a[i + 1]
    if (tar <= x):
        continue
    else:
        dif = tar - x
        if (a[i + 1] >= dif):
            ans += dif
            a[i + 1] -= dif

        else:
            ans += a[i + 1]
            dif -= a[i + 1]
            a[i + 1] = 0

            a[i] -= dif
            ans += dif


print(ans)
# Original solution
(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)

# Optimized solution
n,x,*a=list(map(int,open(0).read().split()));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)
# Original solution
n, x = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 0
for i in range(n - 1):
    while a[i] + a[i + 1] > x:
        if a[i + 1] == 0:
            ans += 1
            a[i] -= 1
        else:
            ans += 1
            a[i + 1] -= 1
print(ans)

# Optimized solution
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:
        ans += a[i + 1] + a[i] - x
        a[i + 1] = x - a[i]
        if a[i + 1] < 0:
            a[i + 1] = 0
print(ans)
# Original solution
n, x = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 0
for i in range(n - 1):
    while a[i] + a[i + 1] > x:
        if a[i + 1] != 0:
            ans += 1
            a[i + 1] -= 1
        else:
            ans += 1
            a[i] -= 1
print(ans)


# Optimized solution
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:
        ans += a[i + 1] + a[i] - x
        a[i + 1] = x - a[i]
        if a[i + 1] < 0:
            a[i + 1] = 0
print(ans)
# Original solution
n, x = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 0
for i in range(n-1):
    delta = max(0, a[i] + a[i+1] - x)
    a[i+1] -= delta
    if a[i+1] < 0:
        a[i] += a[i+1]
        a[i+1] = 0
    ans += delta
print(ans)

# Optimized solution
n, x = list(map(int, input().split()))
a = list(map(int, input().split()))

c = 0
for i in range(n-1):
	t = max(0, a[i] + a[i+1] - x)
	a[i+1] -= t
	if a[i+1] < 0:
		a[i] += a[i+1]
		a[i+1] = 0
	c += t
print(c)
# Original solution
N,X=list(map(int,input().split()))
*A,=list(map(int,input().split()))

ans=0

if A[0]>X:
    ans+=A[0]-X
    A[0]=X 

i=0
while i+1<=N-1:
    if A[i]+A[i+1]>X:
        ans+=A[i]+A[i+1]-X
        A[i+1]=X-A[i]
    i+=1

print(ans)

# Optimized solution
N,X=list(map(int,input().split()))
*A,=list(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)
# Original solution
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)


# Optimized solution
N,x=list(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)
# Original solution
import copy
[n,x]=list(map(int,input().split()))
a=list(map(int,input().split()))
a0=copy.copy(a)
ans=0
for i in range(n-1):
	while a[i]+a[i+1]>x:
		a[i+1]-=1
		ans+=1
print(ans)

# Optimized solution
import copy
[n,x]=list(map(int,input().split()))
a=list(map(int,input().split()))
a0=copy.copy(a)
ans=0
if a[0]+a[1]>x and a[0]>x:
	ans+=a[0]-x
	a[0]-=a[0]-x
	if a[0]+a[1]>x:
		ans+=a[1]
		a[1]=0	
for i in range(n-1):
	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)
# Original solution
n,x,*a=list(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):
  while a[i+1]+a[i]>x:
    a[i+1]-=1
    ans+=1
print(ans)

# Optimized solution
n,x,*a=list(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)
# Original solution
N,x =list(map(int,input().split()))
a=list(map(int,input().split()))
cnt=0
for i in range(N-1):
    if a[i]+a[i+1]>x:
        while a[i+1]>0 and a[i]+a[i+1]>x:
            a[i+1]-=1
            cnt+=1
#端はこの操作では実現できない可能性があるので別に
if a[0]+a[1]>x:
    while a[0]+a[1]>x:
        a[0]-=1
        cnt+=1
if a[-1]+a[-2]>x:
    while a[-1]+a[-2]>x:
        a[-1]-=1
        cnt+=1
print(cnt)

# Optimized solution
N,x =list(map(int,input().split()))
a=list(map(int,input().split()))
cnt=0
for i in range(N-1):
    if a[i]+a[i+1]>x:
        #マイナスになるのを防ぐ
        if a[i+1]<(a[i]+a[i+1]-x):
            cnt+=a[i+1]
            a[i+1]=0
        else:
            cnt+=a[i]+a[i+1]-x
            a[i+1]-=a[i]+a[i+1]-x
#端はこの操作では実現できない可能性があるので別に
if a[0]+a[1]>x:
    cnt+=a[0]+a[1]-x
    a[0]-=a[0]+a[1]-x
if a[-1]+a[-2]>x:
    cnt+=a[-1]+a[-2]-x
    a[-1]-=a[-1]+a[-2]-x
print(cnt)
# Original solution
N,x = list(map(int, input().split()))
alist = list(map(int,input().split()))
ans = 0

for al in range(1,N):
    while (alist[al-1]+alist[al]) > x:
        alist[al] -= 1
        ans += 1

print(ans)

# Optimized solution
N,x = list(map(int, input().split()))
alist = list(map(int,input().split()))
ans = 0

if alist[0] > x:
    ans += (alist[0] - x)
    alist[0] -= (alist[0] - x)

for al in range(1,N):
    if (alist[al-1]+alist[al]) > x:
        ans += (alist[al-1]+alist[al]) - x
        alist[al] -= (alist[al-1]+alist[al]) - x

print(ans)
# Original solution
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 and x < a[i] :
        ans += a[i+1]+a[i]-x
        
        a[i] = a[i]-(x-a[i+1])
        a[i+1] = 0
        
    elif (a[i]+a[i+1]) > x:
        ans += a[i+1]+a[i]-x
        
        a[i+1] = x-a[i]
        
print(ans)

# Optimized solution
N, x = list(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)
# Original solution
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:
        
        if X - a[i] >= 0:
            ans += a[i+1] - (X - a[i])
            a[i+1] = (X - a[i])
        else:
            ans += a[i+1]
            a[i+1] = 0
            ans += a[i] - X
            a[i] = X
            
print(ans)

# Optimized solution
N, x = list(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] + a[i - 1] > x:
        ans += a[i] - (x - a[i - 1])
        a[i] = x - a[i - 1]

print(ans)
# Original solution
def main():
    from collections import deque

    N, L = list(map(int, input().split()))
    *a, = list(map(int, input().split()))

    def solve(a):
        dq = deque()
        s = 0
        ans = 0
        for x in a:
            if len(dq) > 1:
                t = dq.popleft()
                s -= t
            eat = max(0, s + x - L)
            if eat:
                x -= eat
                ans += eat
            s += x
            dq.append(x)
        return ans

    ans = min(solve(a), solve(reversed(a)))

    print(ans)


if __name__ == '__main__':
    main()


# Optimized solution
def main():
    from collections import deque

    N, L = list(map(int, input().split()))
    *a, = list(map(int, input().split()))

    def solve(a):
        dq = deque()
        s = 0
        ans = 0
        for x in a:
            if len(dq) > 1:
                t = dq.popleft()
                s -= t
            eat = max(0, s + x - L)
            if eat:
                x -= eat
                ans += eat
            s += x
            dq.append(x)
        return ans

    ans =solve(a) # reversed(a)確認しないとどうなるか

    print(ans)


if __name__ == '__main__':
    main()
# Original solution
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:
    cnt = (A[i]+A[i+1]) - x
    ans += cnt
    A[i+1] -= cnt
    if A[i+1] < 0:
      A[i+1] = 0
      
print(ans)      

# Optimized solution
N, x = list(map(int, input().split()))
A = list(map(int, input().split()))
ans = 0
limit = x
for a in A:
  if limit < a:
    ans += a - limit
    limit = x - limit
  else:
    limit = x - a
    
print(ans)
# Original solution
n, x = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 0

for i in range(1, n):
    while a[i-1] + a[i] > x:
        a[i] -= 1
        ans += 1

print(ans)




# Optimized solution
n, x = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 0

if a[0] > x:
    ans += a[0] - x
    a[0] = x

# print(a, ans)

for i in range(1, n):
    total = a[i-1] + a[i]
    if total > x:
        # print('yay,', total-x)
        a[i] -= total - x
        ans += total - x

    # while a[i-1] + a[i] > x:
    #     a[i] -= 1
    #     ans += 1

print(ans)
# Original solution
N, X = list(map(int, input().split(' ')))
A = list(map(int, input().split(' ')))

cost = 0
diff = max(0, sum(A[0:2]) - X)
A[0] -= max(0, diff - A[1])
A[1] -= min(A[1], diff)
cost += diff

for i in range(2, N):
    diff = max(0, A[i - 1] + A[i] - X)
    A[i - 1] -= max(0, diff - A[i])
    A[i] -= min(A[i], diff)
    cost += diff

print(cost)


# Optimized solution
N, X = list(map(int, input().split(' ')))
A = list(map(int, input().split(' ')))

cost = max(0, A[0] + A[1] - X)
A[1] = max(0, A[1] - cost)

for i in range(2, N):
    cost += max(0, A[i - 1] + A[i] - X)
    A[i] = max(0, A[i] - max(0, A[i - 1] + A[i] - X))

print(cost)
# Original solution
N,x = list(map(int,input().split()))
a=list(map(int,input().split()))

eat=0

if a[0]>x:
  eat+=a[0]-x+1
  a[0]=x-1
  
for i in range(N-1):
  while a[i]+a[i+1]>x:
    a[i+1]-=1
    eat+=1
    
print(eat)

# Optimized solution
N,x = list(map(int,input().split()))
a=list(map(int,input().split()))

eat=0

if a[0]>x:
  eat+=a[0]-x
  a[0]=x

if a[N-1]>x:
  eat+=a[N-1]-x
  a[N-1]=x
  
for i in range(N-1):
  if a[i]+a[i+1]>x:
    eat+=a[i+1]-x+a[i]
    a[i+1]=x-a[i]
    
print(eat)
# Original solution
N,X=list(map(int,input().split()))
*A,=list(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)

# Optimized solution
N,X=list(map(int,input().split()))
*A,=list(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)
# Original solution
N,x = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
count = 0

while a[0] > x:
    a[0] -= 1
    count += 1

for i in range(N-1):
    while a[i] + a[i+1] > x:
        a[i+1] -= 1
        count += 1
        
print(count)


# Optimized solution
N,x = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
count = 0

while a[0] > x:
    count += a[0] - x
    a[0] = x

for i in range(N-1):
    if a[i] + a[i+1] > x:
        count += a[i+1] - (x - a[i])
        a[i+1] = x - a[i]

print(count)
# Original solution
N, x = list(map(int, input().split()))
a = list(map(int, input().split()))
cnt = 0
for i in range(N-1):
    while a[i] + a[i+1] > x:
        a[i+1]-=1
        cnt+=1
print(cnt)


# Optimized solution
N, x = list(map(int, input().split()))
a = [0] + list(map(int, input().split()))
cnt = 0
for i in range(N):
    if a[i]+a[i+1]>x:
        tmp = a[i]+a[i+1]-x
        a[i+1] -= tmp
        cnt+=tmp
print(cnt)
# Original solution
N,x = list(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)        

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

def solve(N,x,A):
    ans = max(A[0]-x,0)
    A[0] -= ans
    for i in range(1,N):
        p = max(A[i]-(x-A[i-1]),0)
        A[i] -= p
        ans += p
    return ans
print((solve(N,x,A)))

# Optimized solution
def solve():
  ans = 0
  N, x = list(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()))
# Original solution
# coding: utf-8
n, x = list(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)))

# Optimized solution
# coding: utf-8
n, x = list(map(int,input().split()))
A = list(map(int,input().split()))
S = sum(A)
for i in range(n-1):
    s = A[i+1] + A[i]
    if s > x:
        if s-x > A[i+1]:
            t = s-x-A[i+1]
            A[i+1] = 0
            A[i] -= t
        else:
            A[i+1] -= s - x
print((S - sum(A)))
# Original solution
# coding: utf-8
n, x = list(map(int,input().split()))
A = list(map(int,input().split()))
S = sum(A)
for i in range(n-1):
    s = A[i+1] + A[i]
    if s > x:
        if s-x > A[i+1]:#後ろから全部引ける時
            t = s-x-A[i+1]
            A[i+1] = 0
            A[i] -= t
        else:#後ろから全部引くと負になるとき(前からも引く)
            A[i+1] -= s - x
print((S - sum(A)))

# Optimized solution
# coding: utf-8
n, x = list(map(int,input().split()))
A = list(map(int,input().split()))
S = sum(A)
for i in range(n-1):
    s = A[i+1] + A[i]
    if s > x:
        if A[i+1] >= s-x:
            A[i+1] -= (s-x)
        else:
            t = s-x-A[i+1]
            A[i+1] = 0
            A[i] -= t
print((S - sum(A)))
# Original solution
#6問目
N, x = list(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):
        while(a[i] + a[i+1] > x):
            count += 1
            a[i+1] -= 1
print(count)

# Optimized solution
#6問目
N, x = list(map(int, input().split()))
a = list(map(int, input().split()))
count = 0
for i in range(N-1):
    #まず一番前の要素がxより小さくなければならない
    if(i == 0 and a[i] > x):
        count += a[i] - x
        a[i] = x
        
    if(a[i] + a[i+1] > x):
        count += a[i] + a[i+1] - x
        a[i+1] = x - a[i]
print(count)
# Original solution
N,x=list(map(int,input().split()))
A=list(map(int,input().split()))
A2=A[::-1]

ANS=0
for i in range(1,N):
    if A[i]+A[i-1]>x:
        ANS+=(A[i]+A[i-1]-x)
        if A[i]>=A[i]+A[i-1]-x:
            A[i]-=(A[i]+A[i-1]-x)
        else:
            A[i]=0



ANS2=0
for i in range(1,N):
    if A2[i]+A2[i-1]>x:
        ANS2+=(A2[i]+A2[i-1]-x)
        if A2[i]>=A2[i]+A2[i-1]-x:
            A2[i]-=(A2[i]+A2[i-1]-x)
        else:
            A2[i]=0

print((min(ANS,ANS2)))

# Optimized solution
N,x=list(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)
        if A[i]>=A[i]+A[i-1]-x:
            A[i]-=(A[i]+A[i-1]-x)
        else:
            A[i]=0

print(ANS)
# Original solution
N,x= list(map(int,input().split()))
array = list(map(int,input().split()))
result=0
for i in range(0,N-1):
    #i と i+1 が閾値以下じゃなかったら 
    while array[i] + array[i+1] > x:
        if array[i+1]!=0:
            array[i+1]-=1
        else:
            array[i]-=1
        result+=1
print(result)

# Optimized solution
N,x= list(map(int,input().split()))
array = list(map(int,input().split()))
array2=array.copy()
result=0
#TLE pattern
# for i in range(0,N-1):
#     #i と i+1 が閾値以下じゃなかったら 
#     while array[i] + array[i+1] > x:
#         if array[i+1]!=0:
#             array[i+1]-=1
#         else:
#             array[i]-=1
#         result+=1
# print(result)

for i in range(0,N-1):
    if array[i] + array[i+1] > x:
        # if array[i+1] - (array[i] + array[i+1] - x) >= 0:
        #     array[i+1]=array[i+1] - (array[i] + array[i+1] - x)
        if (array[i]+array[i+1])-x <= array[i+1]:
            array[i+1]= array[i+1] - (array[i]+array[i+1]-x)
        else:
            # array[i]=array[i] - (array[i]+array[i+1]) - x - array[i+1]
            array[i]= array[i]-(array[i]-x)
            array[i+1]=0

# print(array2)
# print(array)
print((sum(array2)-sum(array)))
# Original solution
n,x=list(map(int,input().split()))
a=list(map(int,input().split()))
b=a[0]
ans=0
for i in range(1,n):
  if b>x:
    ans+=b-x
    b=x
  if a[i]+b>x:
    ans+=b-x+a[i]
    a[i]=x-b
  b=a[i]
print(ans)

# Optimized solution
n,x=list(map(int,input().split()))
a=list(map(int,input().split()))
ans=max(a[0]-x,0)
a[0]-=ans
for i in range(n-1):
  if a[i]+a[i+1]>x:
    ans+=a[i+1]+a[i]-x
    a[i+1]=x-a[i]
print(ans)
# Original solution
# -*- coding: utf-8

n, x = list(map(int, input().split()))
a = list(map(int, input().split()))
count = 0

if a[0] > x:
    count += a[0] - x
    a[0] = x


for i in range(len(a) - 1):

    while a[i] + a[i + 1] > x:
        a[i + 1] -= 1
        count += 1
print(count)


# Optimized solution
# -*- coding: utf-8

n, x = list(map(int, input().split()))
a = list(map(int, input().split()))

count = max(0, a[0] - x)
a[0] = min(a[0], x)

for i in range(len(a) - 1):
    if a[i] + a[i + 1] > x:
        tmp_num = max(0, a[i] + a[i + 1] - x)
        count += tmp_num
        a[i + 1] -= tmp_num
print(count)