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).

Difficulty: introductory

Starter code:

Solutions:
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,=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)

#coding: utf-8

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)



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()

    # 隣り合う2つの和
    s = [a[i] + a[i+1] for i in range(N - 1)]
    # 隣り合う2つの和をx以下にしたもの
    s_after = [min(i, x) for i in s]

    # 隣り合う2つの和をx以下にしたものから元の数列を復元
    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=(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)


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 = (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)

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 = 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:
        y = a[i] + a[i+1] - x
        count += y
        a[i+1] = max(0, a[i+1]-y)
print(count)

N, x = 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:
        temp = a[i] + a[i+1] - x 
        if a[i+1] - temp >= 0:
            ans += temp
            a[i+1] -= temp
        else:
            ans += temp
            a[i+1] = 0
            a[i] -= temp - a[i+1]
     
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):
    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())
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 = 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))

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=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
    else:
        continue
print(ans)

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

ans = 0
for i in range(N-1):
  n = a[i]+a[i+1]
  if n > x:
    ans += n-x
    if n-x > a[i+1]:
      a[i], a[i+1] = a[i]-(n-x-a[i+1]), 0
    else:
      a[i+1] -= (n-x)
print(ans)

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)

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)

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, 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()))

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
    else:
        continue
print(ans)

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 = 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
# print(A)
# print(ans)

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(A)
print(ans)

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 = 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 in range(n-1):
    if a[i] <= x:
        if a[i]+a[i+1] <= x: continue
        else:
            ans += (a[i]+a[i+1])-x
            a[i+1] -= (a[i]+a[i+1])-x
    else:
        ans += a[i]-x + a[i+1]
        a[i], a[i+1] = x, 0
if a[-1] > x: ans += a[-1]-x
print(ans)

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

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 = list(map(int, input().split()))
a = list(map(int, input().split()))

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


print(ans)


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=[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)

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()))

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 = 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] + A[i + 1] - x
        A[i + 1] = max(x - A[i], 0)
print(ans)

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

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())

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

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

count = 0

if L[0] > M:
	count+= (L[0]-M)
	L[0]-= (L[0]-M)

for i in range (1, N):
	if L[i]+L[i-1] > M:
		count+= (L[i]+L[i-1])-M
		L[i]-= (L[i]+L[i-1])-M
    
print(count)

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
if a[0]>x:
  ans+=a[0]-x
  a[0]=x
for i in range(n-1):
  if a[i]+a[i+1]>x:
    ans+=(a[i+1]-(x-a[i]))
    a[i+1]=(x-a[i])
print(ans)

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[::]
# print(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)
    # print(ans1)
ans1 += temp_cnt[-1]


print(ans1)


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,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
  #print(i, count, a)
else:
  if a[-1]+a[-2] > m:
    count += a[-2] + a[-1]-m
    a[-1] -= a[-2] + a[-1]-m
#print(a, count)
if n <= 2:
  if count + sum(a)-m >= 0:
    print(count + sum(a)-m)
  else:
    print(0)
else:
  print(count)

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

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

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 = 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)
    #print(a)


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)


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=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)

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

#!/usr/bin/env python
# coding: utf-8

# In[8]:


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


# In[9]:


ans = 0
if a[0] > x:
    ans += a[0]-x
    a[0] = x
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
print(ans)


# In[ ]:






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 = 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)


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])
        """
        if int(alist[i]) > x:
            s = s + int(alist[i+1])
            alist[i+1] = 0

        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])
        """
    print(s)
candy()

#
# abc048 c
#

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
2 2 2"""
        output = """1"""
        self.assertIO(input, output)

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

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

    def test_入力例_4(self):
        input = """2 0
5 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():
     # unittest.main()
     resolve()

__starting_point()

f=lambda:map(int,input().split())
N,X=f()
*A,=f()

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 ,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] + a[i+1] -x = k
        a[i+1] = -a[i] + k

print(result)

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=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 = list(map(int, input().split()))
nums = list(map(int, input().split()))
# print(nums)
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(nums)
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=list(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(n-1):
  if a[i]+a[i+1]>x:
    if a[i+1]>(x-a[i]):
      ans+=(a[i+1]-(x-a[i]))
      a[i+1]=(x-a[i])
if a[-2]+a[i-1]>x:
  if a[-1]>(x-a[-2]):
    ans+=(a[-1]-(x-a[-2]))
    a[-1]=(x-a[-2])
print(ans)
    


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 = 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)

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

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


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()))
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, *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 = map(int, input().split())
a = list(map(int, input().split()))
ans = sum(a)
a = [i if i <= x else x for i in a]
for i in range(n-1):
    if a[i]+a[i+1] > x:
        a[i+1] = x-a[i]
print(ans-sum(a))

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 = list(map(int, input().split()))
A = [0] + [int(x) for x in input().split()]
ans = 0
for i in range(1,N+1):
    if A[i-1] + A[i] > x:
        ans += A[i-1] + A[i] - x
        A[i] = x - A[i-1]
print(ans)


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


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
for i in range(1, n):
    if a[i] + a[i-1] > x:
        ans += min(a[i], a[i] + a[i-1] - x)
        a[i] = max(0, x - a[i-1])
    if a[i] + a[i-1] > x:
        ans += a[i-1] - x
        a[i-1] = x
print(ans)

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


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

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()))
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=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, K = map(int,input().split())
a = list(map(int,input().split()))
if a[0] > K:
    ans = a[0]-K
    a[0] = K
else:
    ans = 0
for i in range(1, N):
    if a[i-1]+a[i] > K:
        ans += a[i] - (K-a[i-1])
        a[i] = K-a[i-1]
print(ans)

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) # 一つ前の値の差分を含めて、xとの差分を取得し、比較
  t = i - s
  ans += s
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)
# print(al)


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):
    if A[i]+A[i-1] > x:
        ans += A[i]+A[i-1]-x
        A[i] -= A[i]+A[i-1]-x
 
print(ans)

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()

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=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)


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()

#coding: utf-8

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=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)

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,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)

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

N,x = 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:
        y = a[i] + a[i+1] - x
        ans += y
        a[i+1] = max(0,a[i+1]-y)
print(ans)

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