Problem:
 Score : 300 points    Problem Statement 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 1 3 3
2 2 2      Sample Output 1 1  Eat one candy in the second box.
Then, the number of candies in each box becomes (2, 1, 2) .       Sample Input 2 6 1
1 6 1 2 0 4      Sample Output 2 11  For example, eat six candies in the second box, two in the fourth box, and three in the sixth box.
Then, the number of candies in each box becomes (1, 0, 1, 0, 0, 1) .       Sample Input 3 5 9
3 1 4 1 5      Sample Output 3 0  The objective is already achieved without performing operations.       Sample Input 4 2 0
5 5      Sample Output 4 10  All the candies need to be eaten.

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

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

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

Solution (Python):
N, x = map(int, input().split())
a = list(map(int, input().split()))
ans = [0] * N

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

print(sum(ans))

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

Solution (Python):
N, x = map(int, input().split())
a = list(map(int, input().split()))
candy = min(a[0], x)
ans = a[0] - candy
a[0] = candy

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

print(ans)

Solution (Python):
N, x = map(int, input().split())
a = list(map(int, input().split()))
candy = min(a[0], x)
ans = a[0] - candy
a[0] = candy

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

print(ans)

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

ans = 0
for i in range(1, n):
    num_eat_candy = max(0, (a[i - 1] + a[i]) - x)
    ans += num_eat_candy
    a[i] = max(0, a[i] - num_eat_candy)
    
print(ans)

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

Solution (Python):
N,K=map(int,input().split())
A=list(map(int,input().split()))

ans=0
for i in range(N-1):
  if A[i]+A[i+1]>K:
    tmp=min(A[i+1],A[i]+A[i+1]-K)
    A[i+1]-=tmp
    ans+=tmp
  if A[i]+A[i+1]>K:
    tmp=min(A[i],A[i]+A[i+1]-K)
    A[i]-=tmp
    ans+=tmp
    
if A[-1]>K:
  tmp=A[-1]-K
  A[-1]-=tmp
  ans+=tmp
print(ans)

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

Solution (Python):
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)

Solution (Python):
n, x = map(int, input().split())
a = list(map(int, input().split()))
res = 0
if a[0] > x:
    res += a[0] - x
    a[0] = x

zan = x - a[0]

for i in range(1, n):
    if a[i] > zan:
        res += a[i] - zan
        a[i] = zan
    zan = x - a[i]

print(res)

Solution (Python):
"""


 = sys.stdin.readline().rstrip()
 = list(sys.stdin.readline().rstrip())


 = int(sys.stdin.readline())
 = map(int, sys.stdin.readline().split())
 = list(map(int, sys.stdin.readline().split()))
 = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  


a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654


char_list = list("abcd") 
"""

import math
import sys
import itertools
import queue
from fractions import gcd

def lcm(a, b):
    return a * b // gcd(a, b)

mod = 1000000007

if __name__ == "__main__":
    N,x = map(int, sys.stdin.readline().split())
    a = list(map(int, sys.stdin.readline().split()))+[0]
    ans=0
    for i in range(N):
        eated = max(0,a[i]+a[i-1]-x)
        ans += eated
        a[i]-=eated
    print(ans)

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

Solution (Python):
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))

Solution (Python):
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))

Solution (Python):
N, x = map(int, input().split())
a = list(map(int, input().split()))
li = [0] * N

li[0] = min(x, a[0])
for i in range(N-1):
  if li[i] + a[i + 1] > x:
    li[i + 1] = x - li[i]
  else:
    li[i + 1] = a[i + 1]
    
cnt = 0
for ai, lii in zip(a, li):
  cnt += ai - lii

print(cnt)

Solution (Python):
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):
    b = a[i]+a[i+1]
    if b<=x:
        continue
    else:
        sub = b-x
        a[i+1] = a[i+1]-sub
        ans+=sub
print(ans)

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

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

cnt = 0
for i in range(N-1):
    s = a[i]
    t = a[i+1]
    if s + t <= x:
        continue
    cnt += s + t -x
    if t >= s + t - x:
        a[i+1] -= s + t - x
    else:
        a[i+1] = 0
print(cnt)

Solution (Python):
N,x = map(int,input().split())
A = list(map(int,input().split()))

tmp = A[0] + A[1]
red = max(0,tmp - x)
ans = red
if red > 0:
    if red <= A[1]:
        A[1] -= red
    else:
        A[0] -= red - A[1]
        A[1] = 0
tmp = A[0] + A[1]


for i in range(2,N):
    tmp += A[i]
    tmp -= A[i-2]
    
    red = max(0,tmp - x)
    ans += red
    if red > 0:
        if red <= A[i]:
            A[i] -= red
        else:
            A[i-1] -= red - A[i]
    tmp = A[i-1] + A[i]   
print(ans)

Solution (Python):


n, x = 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):
    eat = max(0, a[i-1] + a[i] - x)
    a[i] -= eat
    cnt += eat

print(cnt)

Solution (Python):
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)

Solution (Python):

import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7



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

count = 0


if A[0] > x:
    diff = A[0]-x
    count += diff
    A[0] = x

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

print(count)

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

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

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

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

def calc(A,x):
  res = 0
  for i in range(len(A)-1):
    if A[i] + A[i+1] > x:
      if A[i] < x:
        res += A[i+1] - (x - A[i])
        A[i+1] = x - A[i]
      else:
        res += A[i] - x + A[i+1]
        A[i] = x
        A[i+1] = 0
  return res

print(calc(a,x))

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

def calc(A,x):
  res = 0
  for i in range(len(A)-1):
    if A[i] + A[i+1] > x:
      if A[i] < x:
        res += A[i+1] - (x - A[i])
        A[i+1] = x - A[i]
      else:
        res += A[i] - x + A[i+1]
        A[i] = x
        A[i+1] = 0
  return res

ar = a[::-1]
print(min(calc(a,x),calc(ar,x)))

Solution (Python):
N, x = map(int, input().split())
A = list(map(int, input().split()))

answer = 0
if A[0] > x:
    answer += A[0] - x
    A[0] = x
    
for i in range(N-1):
    diff = A[i] + A[i+1] - x
    if diff > 0:
        answer += diff
        A[i+1] -= diff

print(answer)

Solution (Python):
N, X = map(int, input().split())
A = list(map(int, input().split()))

answer = 0
if A[0] > X:
    answer += A[0] - X
    A[0] = X
    
for i in range(N-1):
    diff = A[i] + A[i+1] - X
    if diff > 0:
        answer += diff
        A[i+1] -= diff

print(answer)

Solution (Python):
n, x = map(int, input().split())
a = [0] + list(map(int, input().split()))

ans = 0

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

print(ans)

Solution (Python):
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)

Solution (Python):
from collections import Counter,defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

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

Solution (Python):
n,x,*a=map(int,open(0).read().split())
c=max(0,a[0]-x)
a[0]=min(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)

Solution (Python):
n, x = map(int, input().split())
a = list(map(int, input().split()))
a.append(0)
ans = 0

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

print(ans)

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

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

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

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

Solution (Python):
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,x=map(int,input().split())
    A=list(map(int,input().split()))
    res=0
    for i in range(1,n):
        c=A[i]+A[i-1]-x
        if c>0:
            res+=c
            A[i]=max(A[i]-c,0)
    print(res)
resolve()

Solution (Python):
n,x = 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 j in range(n-1):
  if a[j]+a[j+1]>=x:
    d = a[j]+a[j+1]-x
    cnt += d   
    a[j+1] -= d 
        
print(cnt)

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

print(ans)

Solution (Python):
n,x = 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:
    dif = min(a[i+1],a[i]+a[i+1]-x)
    a[i+1] -= dif
    cnt += dif
if a[0] > x:
  cnt += a[0]-x
print(cnt)

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

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

m = min(a)
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:
        tmp = x - a[i - 1]
        ans += a[i] - tmp
        a[i] = tmp

print(ans)

Solution (Python):
N, x = map(int, input().split())
l = list(map(int, input().split()))
l.append(0)
hiku_sum = 0

for i in range(N):
    l_sum = l[i] + l[i + 1]
    if l_sum > x:
        hiku_min = l_sum - x
        hiku_sum += hiku_min
        if l[i + 1] >= hiku_min:
            l[i + 1] -= hiku_min
        else:
            l[i] -= hiku_min - l[i + 1]
            l[i + 1] = 0
print(hiku_sum)

Solution (Python):
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] = x

for i in range(N-1):
  add = max(a_list[i] + a_list[i+1] - x, 0)
  a_list[i+1] -= add
  count += add

print(count)

Solution (Python):
import sys
input = sys.stdin.readline

def main():
    N, x = map(int, input().split())
    a = list(map(int, input().split())) + [0]

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


if __name__ == "__main__":
    main()

Solution (Python):
N, x = map(int, input().split())
A = [int(x) for x in 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
        if A[i + 1] >= diff:
            A[i + 1] -= diff

        else:
            A[i+1] = 0

print(ans)

Solution (Python):
N,x= map(int,input().split())
array = list(map(int,input().split()))
array2=array.copy()
result=0











for i in range(0,N-1):
    if 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]-x)
            array[i+1]=0



print(sum(array2)-sum(array))

Solution (Python):
def solve(N, LIM):
    ax = list(map(int, input().split()))
    ans = 0
    left = ax[0]

    for i in range(N-2):
        
        right = ax[i+1]
        total = left + right
        should_eat = total - LIM
        if should_eat > 0:
            ans += should_eat
            if should_eat < right:
                left = right - should_eat
            else:
                left = 0
        else:
            left = right

    ans += (left + ax[-1]) - LIM if (left+ax[-1]) > LIM else 0
    return ans


if __name__ == '__main__':
    n, x = map(int, input().split())
    print(solve(n, x))

Solution (Python):
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 k in range(1,N):
    if A[k-1] + A[k] > x:
        ans += (A[k-1] + A[k]) - x
        A[k] = x - A[k-1]

print(ans)

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

Solution (Python):
n, x = map(int, input().split())
a = [int(x) for x in input().split()]
original_sum = sum(a)
for i in range(n-1):
    if x < a[i] + a[i+1]:
        new = x - a[i]
        if 0 <= new:
            a[i+1] = new
        else:
            a[i] += new
            a[i+1] = 0
else:
    print(original_sum - sum(a))

Solution (Python):
N, x = 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(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)

Solution (Python):
N,A=map(int,input().split())
a=list(map(int,input().split()))
cnt=0
for i in range(1,N):
    tmp=max(0,(a[i-1]+a[i])-A)
    if i==1:
        if a[1]<tmp:
            a[0]-=tmp-a[1]
            a[1]=0
            cnt+=tmp
            continue
    
    cnt+=tmp
    a[i]-=tmp
    
print(cnt)

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

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

cnt = 0
for i in range(len(a)):
    if a[i] > x:
        cnt += a[i]-x
        a[i] = x

for i in range(1, len(a)):
    if a[i-1] + a[i] > x:
        cnt += a[i-1]+a[i] - x
        a[i] = a[i] - (a[i-1]+a[i] - x)

print(cnt)

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

Solution (Python):
n,x = map(int,input().split())

a = [int(i) for i in input().split()]

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

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

Solution (Python):
N, x = list(map(int, input().split(' ')))
a = list(map(int, input().split(' ')))

count = 0
for i in range(len(a) - 1):
    if a[i] + a[i + 1] > x:
        eat = a[i] + a[i + 1] - x
        if eat <= a[i + 1]:
            a[i + 1] -= eat
        else:
            a[i] = eat - a[i + 1]
            a[i + 1] = 0
        count += eat
print(count)

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

Solution (Python):
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)

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

print(ans)

Solution (Python):
n,x=map(int,input().split())
a=[int(i) for i in input().split()]

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

Solution (Python):
N, x = (int(x) for x in input().split())
a = [int(x) for x in input().split()]

original_amount = sum(a)

for i in range(N - 1):
    pair_amount = a[i] + a[i + 1]
    if x >= pair_amount:
        continue
    diff = pair_amount-x
    a[i] = min(a[i], a[i]-(diff-a[i + 1]))
    a[i + 1] = max(a[i + 1] - diff, 0)

result_amount = sum(a)

print(original_amount-result_amount)

Solution (Python):
def resolve():
    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):
        s = a[i] + a[i + 1]
        if s > x:
            c += s - x
            a[i + 1] -= s - x
    print(c)


if __name__ == "__main__":
    resolve()

Solution (Python):
import copy

def sol1(N, X, A):
  cnt = 0
  for i in range(N-1):
    diff = A[i] + A[i+1] - X
    if diff <= 0: continue
    
    if A[i+1] >= diff:
      A[i+1] -= diff
    else:
      A[i] = A[i] - (diff - A[i+1])
      A[i+1] = 0
    cnt += diff
  return cnt

def sol2(N, X, A):
  cnt = 0
  for i in range(N-2, -1, -1):
    diff = A[i] + A[i+1] - X
    if diff <= 0: continue
    
    if A[i] >= diff:
      A[i] -= diff
    else:
      A[i] = 1
      A[i+1] -= diff - (A[i] - 1)

    cnt += diff
  return cnt

if __name__ == '__main__':
  N,X = map(int,input().split())
  A = list(map(int, input().split()))

  print(min(sol1(N,X,copy.deepcopy(A)), sol2(N,X,copy.deepcopy(A))))

Solution (Python):
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations
import sys
import string
from bisect import bisect_left


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LIM(): return list(map(lambda x:int(x) - 1, sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def LIRM(n): return [LIM() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
mod = 1000000007


n, x = LI()
candy = LI()
S = sum(candy)
candy[0] = min(candy[0], x)

for i in range(1, n):
    candy[i] -= max(candy[i] + candy[i-1] - x, 0)



print(S - sum(candy))

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

Solution (Python):
n, x = map(int, input().split())
a = input().split()

original_number, min_number = 0, 0

for i in range(n):
  original_number += int(a[i])
  
if int(a[0]) > x:
  a[0] = x
for i in range(n - 1):
  if int(a[i]) + int(a[i + 1]) > x:
    a[i + 1] = x - int(a[i])

for i in range(n):
  min_number += int(a[i])
  
ans = original_number - min_number
print(ans)

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

Solution (Python):
import sys
from collections import Counter

stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

mod = 10 ** 9 + 7

n, x = na()
aaa = na()
ans = 0
s = 0
for i in range(n):
    nt = x - s
    if aaa[i] <= nt:
        s = aaa[i]
        continue
    else:
        ans += aaa[i] - nt
        s = nt
print(ans)

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

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

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

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

for i in range(n-1):
    if a[i]+a[i+1]>x:
        cnt+=a[i+1]-(x-a[i])
        a[i+1]=x-a[i]
print(cnt)

Solution (Python):
n, x = map(int, input().split())
l = list(map(int, input().split()))

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

Solution (Python):
n, x = map(int, input().split())
l = list(map(int, input().split()))

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

Solution (Python):
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:
        diff = a[i] + a[i + 1] - x
        a[i + 1] = max(a[i + 1] - diff, 0)
        ans += diff

print(ans)

Solution (Python):
n,x=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):
        if(a[i-1] > x):
            eat = (a[i-1] - x) + a[i]
            a[i] -= a[i]
            a[i-1] -= a[i-1] - x
            ans += eat
        else:
            eat = (a[i-1] + a[i]) - x
            a[i] -= eat
            ans += eat
print(ans)

Solution (Python):
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)

Solution (Python):
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(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)

Solution (Python):
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)

Solution (Python):
from sys import stdin
N,x = [int(x) for x in stdin.readline().rstrip().split()]
data = [int(x) for x in stdin.readline().rstrip().split()]
ans = 0
for i in range(N-1):
    v1 = data[i]
    v2 = data[i+1]
    if v1 + v2 >= x:
        ans += (v1+v2-x)
        data[i+1] = max(v2-(v1+v2-x),0)
print(ans)

Solution (Python):
N, x = map(int,input().split(' '))
all_jar = [int(x) for x in input().split()]
first_jar_candy = all_jar[0]
c = 0
min_opr = 0
for each_jar in all_jar[1:]:
  s = first_jar_candy + each_jar
  c = min(each_jar, max(0, s - x))
  c2 = max(0, s - c - x)
  first_jar_candy = each_jar - c
  min_opr += c + c2
else:
  c += max(0, each_jar - x)
print(min_opr)

Solution (Python):
n, x = 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)

Solution (Python):
n, x = 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)

Solution (Python):
N, x = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]

a = A[0]
c = 0
ret = 0
for a_post in A[1:]:
  s = a + a_post
  c = min(a_post, max(0, (a + a_post) - x))
  c2 = max(0, a + a_post - c - x)
  a = a_post - c
  ret += c + c2
else:
  c += max(0, a_post - x)
print(ret)

Solution (Python):



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

c = 0
for i in range(N-1):
    s = a_list[i] + a_list[i+1]
    if s < x:
        
        continue
    elif s > x:
        
        
        if s - x < a_list[i+1]:
            a_list[i+1] -= s - x
        else:
            nokori = x - a_list[i+1]
            a_list[i+1] = 0
            a_list[i] -= nokori
        c += s - x

print(c)

Solution (Python):
N, x = map(int, input().split())
nums = list(map(int, input().split()))
before = nums[0]
cnt = 0
for i in range(1, N):
  ope = before+nums[i]-x
  if ope > 0:
    before = max(nums[i]-ope, 0)
    cnt += ope
  else:
    before = nums[i]
print(cnt)

Solution (Python):
n,x = input().split()
n = int(n)
x = int(x)
candies = input().split()
candies = [int(i) for i in candies]
count = 0
for i in range(n-1):
  if candies[i]+candies[i+1] > x:
    k = candies[i+1]-(candies[i]+candies[i+1]-x)
    if k >= 0:
      count += candies[i]+candies[i+1]-x
      candies[i+1] = k
    else:
      count += candies[i+1]
      candies[i+1] = 0
      count += candies[i] - x
      candies[i] = x
    
print(count)

Solution (Python):
n, x = [int(x) for x in input().split()]

boxes = [int(x) for x in input().split()]

index = 0
count = 0

while index < len(boxes):
    if boxes[index] == x:
        if index + 1 < len(boxes):
            count += boxes[index + 1]
            boxes[index + 1] = 0
        index += 2
    elif boxes[index] > x:
        count += boxes[index] - x
        boxes[index] = x
        if index + 1 < len(boxes):
            count += boxes[index + 1]
            boxes[index + 1] = 0
        index += 2
    else:
        if index + 1 < len(boxes):
            if boxes[index] + boxes[index + 1] > x:
                diff = boxes[index] + boxes[index + 1] - x
                boxes[index + 1] = boxes[index + 1] - diff
                count += diff
        index += 1
print(count)