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

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

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

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

count = 0
for i in range(N-1):
	diff = (A[i] + A[i + 1]) - X
	if 0 < diff:
		count += diff
		if diff <= A[i + 1]:
			A[i + 1] -= diff
		else:
			diff -= A[i + 1]
			A[i + 1] = 0
			A[i] -= diff
print(count)

Solution (Python):
def boxes_and_candies(N: int, x: int, A: list) -> int:
    count = 0
    for i in range(1, N):
        a1, a2 = A[i - 1], A[i]
        

        if a1 + a2 <= x:
            
            
            continue

        diff = (a1 + a2) - x
        count += diff
        

        if a2 > diff:
            A[i] -= diff
        else:
            A[i-1] -= diff - A[i]
            A[i] = 0

    return count


if __name__ == "__main__":
    N, x = map(int, input().split())
    A = [int(s) for s in input().split()]
    ans = boxes_and_candies(N, x, A)
    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(1, n):
    if a[i] + a[i-1] > x:
        cnt += a[i] - (x - a[i-1])
        a[i] = x - a[i-1]
print(cnt)

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] + a[i-1] > x:
        b = a[i] + a[i-1] - x
        minab = min(a[i], b)
        a[i] -= minab
        cnt += minab
        
        b -= minab
        cnt += b
        a[i-1] -= b
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] > 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] = x - a[i]
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:
    tmp = a[i]
    a[i] = max(x-a[i-1], 0)
    ans += tmp - a[i]
  if a[i] == 0 and a[i-1] > x:
    tmp = a[i-1]
    a[i-1] = x
    ans += tmp - a[i-1]
print(ans)

Solution (Python):
from functools import reduce

def main():
    
    
    
    
    
    
    

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

    
    
    
    
    
    N,x = (int(_) for _ in input().split())
    a = list(int(_) for _ in input().split())
    cost = 0
    for i in range(1,N):
        tmp_sum = a[i-1] + a[i]
        if(tmp_sum > x):
            cost += tmp_sum - x
            a[i] = a[i] - (tmp_sum - x)
            if(a[i]<0):
                a[i-1] = a[i-1] + a[i]
                a[i] = 0
                
    print(cost)
    
if __name__ == '__main__':
    main()

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

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

Solution (Python):
N , X = map(int,input().split())
alist = list(map(int,input().split()))
first = sum(alist)

for i in range(N):
    if alist[i] > X:
        alist[i] = X

for i in range(N-1):
    if alist[i]+alist[i+1] > X:
        alist[i+1] = (X - alist[i]) 

print(first - sum(alist))

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

Solution (Python):


















import math
import copy
from collections import defaultdict, Counter

from itertools import product

from itertools import permutations

from itertools import combinations
from bisect import bisect_left, bisect_right


def s_inpl(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
INF = float("inf")

N, x = s_inpl()
a = inpl()

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

print(ans)

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

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

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

print(count)

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

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

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

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

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

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

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

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


print(cnt)

Solution (Python):
def main():
    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] = x - A[i-1]
    print(ans)


if __name__ == "__main__":
    main()

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

Solution (Python):

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

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

res = 0
for i in range(N-1):
    if A[i] + A[i+1] > x:
        eat = A[i]+A[i+1]-x
        A[i+1] = max(0,A[i+1]-eat)
        res += eat
        if A[i+1]-eat < 0:
            A[i] -= abs(A[i+1]-eat)


print(res)

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

print(cnt)

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

print(cnt)

Solution (Python):
N, x = map(int, raw_input().split())
a = map(int, raw_input().split())
ans = 0
for i in range(N-1):
  t = a[i]+a[i+1]-x
  if t>0:
    a[i] -= max(0, t-a[i+1])
    a[i+1] -= min(a[i+1], t)
    ans += t
print ans

Solution (Python):

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)

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

Solution (Python):
def main():
    _, x = map(int, input().split())
    a = [int(s) for s in input().split()]

    print(solve(a, x))

def solve(a, x):
    b = 0
    s = 0

    for t in a:
        b = min(x - b, t)
        s += t - b
    
    return s

main()

Solution (Python):

import sys

def main():
	N, x = map(int,input().split(" "))
	tmp = input().split(" ")
	candy = [int(s) for s in tmp]
	
	ans = 0
	for i, val in enumerate(candy):
		if i+1 == len(candy): break
		
		tmp = candy[i] + candy[i+1]
		if x < tmp and candy[i+1] >= tmp - x:
			candy[i+1] = candy[i+1] - (tmp - x)
			ans = ans + (tmp - x)
		elif x < tmp:
			candy[i] = candy[i] - ((tmp - x) - candy[i+1])
			candy[i+1] = 0
			ans = ans + (tmp - x)
	
	print(ans)

if __name__ == "__main__":
	main()

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

def calc(A, X):
    a = A[:]
    r = 0
    for i in range(len(A) - 1):
        t = a[i] + a[i + 1]
        if t > X:
            d = t - X
            if d <= a[i + 1]:
                r += d
                a[i + 1] -= d
            else:
                r += d - a[i + 1] + a[i + 1]
                a[i] -= d - a[i + 1]
                a[i + 1] = 0
    
    return r

result = calc(A, X)

print(result)

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

Solution (Python):

from collections import defaultdict
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007






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

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

ans = 0

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

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

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

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

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

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

Solution (Python):
def main():
	n, x = map(int, input().split())
	a = list(map(int, input().split()))
	ans = 0
	for i in range(1, n):
		temp = x - a[i] - a[i-1]
		if temp < 0:
			ans += min(-1*temp, a[i])
			a[i] -= min(-1*temp, a[i])
		temp = x - a[i] - a[i-1]
		if temp < 0:
			ans += min(-1*temp, a[i-1])
			a[i-1] -= min(-1*temp, a[i-1])
	
	print(ans)

main()

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

ans = 0
left_minus = 0
right_minus = 0
for i in range(N - 1):
    left = A[i]
    right = A[i + 1]
    over = left + right - left_minus - x
    if over > 0:
        if over <= right:
            right_minus = over
        else:
            right_minus = right
            left_minus += over - right
    else:
        right_minus = 0

    ans += left_minus
    left_minus = right_minus

    if i == N - 2:
        ans+= left_minus

print(ans)

Solution (Python):
def solve(N, x, As):
    ans = max(0, As[0] - x)
    As[0] -= ans
    for i in range(0, N - 1):
        d = As[i] + As[i + 1]
        ans += max(0, d - x)
        As[i + 1] -= max(0, d - x)
    return ans


if __name__ == "__main__":
    N, x = map(int, input().split(" "))
    As = list(map(int, input().split(" ")))
    print(solve(N, x, As))

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

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

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

    ans = 0
    for i in range(1, N):
        d = a[i] + a[i-1] - X
        if d > 0:  
            a[i] -= d
            ans += d
            if a[i] < 0:
                a[i-1] += a[i]
                a[i] = 0
    return ans


def main():
    print(solve())


if __name__ == '__main__':
    main()

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

for i in range(N-2):
    if b[i] <= 0:
        continue
    c = min(b[i],a[i+1])
    ans += b[i]
    b[i] = 0
    b[i+1] -= c
    a[i+1] -= c

ans += max(b[-1],0)

print(ans)

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

print(cnt)

Solution (Python):
from collections import deque
n, x = map(int, input().split())
*a, = map(int, input().split())
a = deque(a)
a.appendleft(0)
a.append(0)
ans = 0

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

print(ans)

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

for i in range(n-1):
    v=a[i]+a[i+1]
    if i and v-x>a[i+1]:
        a[i+1]=x-a[i]
        c+=(v-x)
    else:
        if 0<v-x<=a[i+1]:
            a[i+1]-=(v-x)
            c+=(v-x)
        elif v-x>a[i+1]:
            a[i]-=(v-x-a[i+1])
            a[i+1]=0
            c+=(v-x)

print(c)

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

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

if A[0] > x:
        need = A[0] - x
        sub = min(A[0], need)
        A[0] -= sub
        ans += sub

print(ans)

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

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

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

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

print(ct)

Solution (Python):
N,x=map(int,input().split())
A=list(map(int,input().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 = map(int, input().split())
a = [int(x) for x in input().split()]
ans = 0
for i in range(N-1):
    eat = max(0, a[i] + a[i+1] - x)
    ans += eat
    eat1 = min(a[i+1], eat)
    eat2 = eat - eat1
    a[i+1] -= eat1
    a[i] -= eat2
print(ans)

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] -= ans

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

print(ans)

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

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

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

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]+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)

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

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

Solution (Python):
def main():
    n, x = map(int, input().split())
    aa = list(map(int, input().split()))
    res = 0
    for i in range(1, n):
        sub = aa[i - 1] + aa[i] - x
        if sub > 0:
            aa[i] -= sub
            if aa[i] < 0:
                aa[i - 1] += aa[i]
                aa[i] = 0
            res += sub
    print(res)


if __name__ == '__main__':
    main()

Solution (Python):
n, x = map(int, input().split())
a = input().split()
a = [int(m) for m in a]
ans = 0
for i in range(1, n):
    t = a[i] + a[i - 1]
    if t <= x:
        pass
    else:
        ans += a[i] - (x - a[i - 1])
        if i == 1:
            if x - a[i - 1] < 0:
                a[i] = 0
                a[i - 1] = x
            else:
                a[i] = x - a[i - 1]
        else:
            a[i] = x - 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(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)

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

Solution (Python):


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

a += [0]

count = 0
for i in range(N):
    extra = a[i]+a[i+1] - X
    if extra <= 0:
        continue
    count += extra
    if a[i+1] >= extra:
        a[i+1] -= extra
    else:
        a[i] -= extra-a[i+1]
        a[i+1] = 0
    
print(count)

Solution (Python):

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

sums = [0] * (N - 1)
for i, ai in enumerate(a):
    if i == 0: prev = ai
    else:
        sums[i - 1] = ai + prev
        prev = ai

ans = 0

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

for i in range(N - 1):
    
    if sums[i] <= x: continue
    
    ans += (sums[i] - x)
    if i < N - 2: sums[i + 1] -= (sums[i] - x)
    sums[i] = x

print(ans)

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

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

print(count)

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

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

b = a[:]

if b[0] > x:
    b[0] = x

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

print(ans)

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

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


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

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

Solution (Python):
n,x = map(int,raw_input().split())
arr = map(int,raw_input().split())
su = arr[0]
ans = 0
for i in range(1,n):
    su += arr[i]
    if su > x:
        diff = min(arr[i],su-x)
        arr[i] -= diff
        su = arr[i-1]+arr[i]
        if su > x:
            diff1 = min(arr[i-1],su-x)
            arr[i-1] -= diff1
            su = arr[i-1]+arr[i]
            ans += diff1
        ans += diff
    su = arr[i]
print ans

Solution (Python):
N, x = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
s = sum(A)
cnt = 0
for i in range(1, len(A)):
    if A[i-1] + A[i] >= x:
        if x >= A[i-1]:
            A[i] -= A[i-1] + A[i] - x
        else:
            A[i] = 0
            A[i-1] -= A[i-1] - x

print(s - sum(A))

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

l_A = len(A)
cnt = 0
for i in range(l_A):
    j = l_A - i - 1
    if i >= j:
        break
    
    if A[i] + A[i+1] > x:
        diff = (A[i] + A[i+1]) - x
        if A[i+1] >= diff:
            cnt += diff
            A[i+1] = A[i+1] - diff
        else:
            remain_diff = diff - A[i+1]
            A[i+1] = 0
            A[i] = A[i] - remain_diff
            cnt += diff

    
    if A[j-1] + A[j] > x:
        diff = (A[j-1] + A[j]) - x
        if A[j-1] >= diff:
            cnt += diff
            A[j-1] = A[j-1] - diff
        else:
            remain_diff = diff - A[j-1]
            A[j-1] = 0
            A[j] = A[j] - remain_diff
            cnt += diff

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

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

Solution (Python):


n, x = 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)

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

print(count)

Solution (Python):

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

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

ans = 0
for org,aft in zip(an,an_s):
    ans += abs(org - aft)


print(ans)

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

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

Solution (Python):
N,x = map(int,input().split())
a = list(map(int,input().split()))
ans = 0
ans += max(a[0]-x,0)
a[0] = min(a[0],x)
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)

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

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



    if(a[i]+a[i+1]>x):
        sum += a[i]+a[i+1]-x
        a[i+1] = a[i+1] - (a[i]+a[i+1]-x)
print(sum)

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

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




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

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