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

def main():
    n, x = list(map(int, input().rstrip().split()))
    A = list(map(int, input().rstrip().split()))

    
    ans = 0 if A[0] < x else A[0] - x
    A[0] = min(A[0], x)
    
    prv = A[0]
    for i in range(1, n):
        dif = prv + A[i] - x
        if dif > 0:
            A[i] -= dif
            ans += dif
        prv = A[i]
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):

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


if __name__ == "__main__":
    main()

Solution (Python):

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


if __name__ == "__main__":
    main()

Solution (Python):
import itertools
N,x = list(map(int,input().split(" ")))
As = list(map(int,input().split(" ")))
ans = 0
for i in range(0,N-1):
    l = As[i]
    r = As[i+1]
    diff = max((r+l) - x,0)
    if diff >= r:
        ans += r

        diff -= r
        As[i + 1] = r = 0
        ans += diff

    else:
        r -= diff
        As[i + 1] = r
        ans += diff

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

        else:
            cnt += (a[i+1]+a[i])-x
            a[i+1] -= (a[i+1]+a[i])-x

print(cnt)

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

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

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(0, N - 1):
    total = a[i] + a[i + 1]
    if total > x:
        diff = total - x
        a[i + 1] -= diff
        cnt += diff
print(cnt)

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

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

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

ans=0
for i in range(1,len(A)):
    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()))
sum_a = sum(a)

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

print(sum_a - sum(a))

Solution (Python):
N, x = map(int, input().split())
*A, = map(int, input().split())
ans = max(A[0] - x, 0)
if A[0] > x: A[0] = x
for i in range(N-1):
    S = A[i] + A[i+1]
    if S > x:
        ans += S - x
        A[i+1] -= min(S - x, A[i+1])
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] = 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):
C=0;
p=input().rstrip().split(' ')
x=input().rstrip().split(' ')
for i in range(0,len(x)-1):
    t=int(x[i]) + int(x[i+1])
    if t>int(p[1]):
        g=t-int(p[1])
        C+=g;
        if g>int(x[i+1]):
            g=g-int(x[i+1])
            x[i+1]=0;
            x[i]=int(x[i])-g;
        else:
            x[i+1]=int(x[i+1])-g;
            
print(C)

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+1] >= a[i] + a[i+1] - x:
            count += a[i] + a[i+1] - x
            a[i+1] -= a[i] + a[i+1] - x
        else:
            count += a[i] + a[i+1] - x
            a[i+1] = 0
            a[i] = x

print (count)

Solution (Python):
import sys
N, x = map(int, sys.stdin.readline().split())
a = list(map(int, sys.stdin.readline().split()))
minus_count = 0
for i in range(N-1):
    temp_sum = a[i] + a[i+1]
    minus_target = temp_sum - x
    if minus_target <= 0:
        continue
    else:
        minus_count += minus_target
        a[i+1] = a[i+1] - minus_target
        if a[i+1] < 0:
            a[i+1] = 0

print(minus_count)

Solution (Python):


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

prev = 0
answer = 0
for a in A:
  eat = max(0, prev + a - x)
  prev = a - eat
  answer += eat

print(answer)

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

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

ans = max(A[0] - X, 0)
B = [min(A[0], X)]
for i in range(1, N):
  var = max(A[i] + B[-1] - X, 0)
  ans += var
  B.append(A[i] - var)
  
print(ans)

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

for i in range(1, n):
    total = a[i-1] + a[i]
    sa = total - x
    if sa > 0:
        result += sa
        a[i-1] = 0
        a[i] -= sa - a[i-1]
        if a[i] < 0:
            a[i] = 0

    else:
        a[i-1] = 0
print(result)

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

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

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

Solution (Python):

"""貪欲法
"""
def solve():
    N, x = list(map(int, input().split()))
    A = list(map(int, input().split()))

    ans = 0

    total = A[1] + A[0]
    if total > x:
        diff = min(total-x, A[1])
        A[1] -= diff
        ans += diff

    total = A[1] + A[0]
    if total > x:
        diff = total - x
        A[0] -= diff
        ans += diff

    for i in range(2, N):
        total = A[i] + A[i-1]

        if total > x:
            diff = total - x
            A[i] -= diff
            ans += diff
    
    print(ans)


if __name__ == "__main__":
    solve()

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

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

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

sumA=[A[i]+A[i+1] for i in range(N-1)]

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

Solution (Python):
def smaller(x, y):
	if x < y: return x
	return y

def larger(x, y):
	if x > y: return x
	return y

N, x = map(int, input().split())
A = list(map(int, input().split()))
sum1 = 0
for i in range(0, N):
	sum1 += A[i]
for i in range(1, N-1):
	leftadd = A[i-1] + A[i]
	rightadd = A[i]+ A [i+1]
	left = leftadd - x
	if left < 0: left = 0
	if A[i] < left: left = A[i]
	right = rightadd - x
	if right < 0: right = 0
	if A[i] < right: right = A[i]
	A[i] -= smaller(left, right)
for i in range(1, N-1):
	leftadd = A[i-1] + A[i]
	rightadd = A[i]+ A [i+1]
	left = leftadd - x
	if left < 0: left = 0
	if A[i] < left: left = A[i]
	right = rightadd - x
	if right < 0: right = 0
	if A[i] < right: right = A[i]
	A[i] -= larger(left, right)
if A[0]+A[1] > x:
	A[0] -= smaller(A[0], A[0]+A[1]-x)
	if A[0]+A[1] > x:
		A[1] -= A[0]+A[1]-x
if A[N-2]+A[N-1] > x:
	A[N-1] -= smaller(A[N-1], A[N-2]+A[N-1]-x)
	if A[N-2]+A[N-1] > x:
		A[N-2] -= A[N-2]+A[N-1]-x
sum2 = 0
for i in range(0, N):
	sum2 += A[i]
print(sum1-sum2)

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

cost = 0
num = 0

for i in range(N-1):
    cost = max(0, a[i] + a[i+1] - x)
    num += cost
    a[i+1] = max(0, a[i+1] - cost)

print(num)

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n,m=LI()

  l=LI()

  cnt=0

  for i in range(n-2):
    if l[i]+l[i+1]<=m:
      continue
    else:
      if l[i]>m:
        a=l[i+1]
        b=max(0,l[i]+l[i+1]-m)
        cnt+=b
        l[i+1]=0
      else:
        b=max(0,l[i]+l[i+1]-m)
        cnt+=b
        l[i+1]-=b

  cnt+=max(0,l[-1]+l[-2]-m)

  return cnt

print(main())

Solution (Python):

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

Solution (Python):

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

cost = 0
for i in range(1, n):
    if arr[i] + arr[i - 1] > x:
        abure = arr[i] + arr[i-1] - x
        if arr[i] >= abure:
            arr[i] -= abure
        else:
          
            arr[i - 1] -= abure - arr[i]
            arr[i] = 0
        cost += abure
print(cost)

Solution (Python):

n, x = [ int(v) for v in input().split() ]
candy_list =  [ int(v) for v in input().split() ]


def eating(c):
	global x
	a, b = candy_list[i], candy_list[i+1]
	y = a + b - x
	if y >= 0:
		b -= y
		a += min(0,b)
		b = max(0,b)
		candy_list[i], candy_list[i+1] = a, b
	else:
		y = 0
	return y

ans = 0
for i in range(n-1):
	ans += eating(i)

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

if A[-1] > x:
    ans += A[-1] - x
    A[-1] = x

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

print(ans)

Solution (Python):




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

print(ans)

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

while i<len(a)-1:
    d=a[i]+a[i+1]-x
    if d>0:
        a[i+1]-=d
        ans+=d
        
        
    i+=1
    
print(ans)

Solution (Python):

n, x = map(int, input().split())
a = list(map(int, input().split()))
new_a = []
new_a += a
if a[0] > x:
    new_a[0] = x
for i in range(n-1):
    while True:
        if new_a[i] + new_a[i+1] > x:
            new_a[i+1] = x-new_a[i]
        if new_a[i] + new_a[i+1] <= x:
            break

print(sum(a) - sum(new_a))

Solution (Python):

import copy
import random
import bisect 
import fractions 
import math
import sys

mod = 10**9+7
sys.setrecursionlimit(mod) 

N,x = map(int,input().split())
a = [int(i) for i in input().split()]
part_sum = [0 for i in range(N)]

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


print(ans)

"""
1 6 1 2 0 4
1 0 1 0 0 1
0 1 0 1 0 1


"""

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()))
cnt = 0
for i in range(N-1):
    if A[i] + A[i+1] > x:
        diff = A[i] + A[i+1] - x
        if A[i+1]>diff:
            A[i+1] -= diff
        else:
            A[i+1] = 0
        cnt += diff
print(cnt)

Solution (Python):
N, x, *a = map(int, open(0).read().split())
ans = 0

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

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

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

c = A[0] - x
if c > 0:
    cost += c
    A[0] -= c

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

print(cost)

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

for i in range(N-1):
  if A[i] +  A[i+1] > x:
    tmp = A[i] +  A[i+1] - x
    cnt += A[i] +  A[i+1] - x
    A[i+1] = A[i+1] - tmp
    
print(cnt)

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

print(count)

Solution (Python):
import sys, os

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

def solve():
    n,x = f()
    a = f()
    sum1 = sum(a)

    a = [ min(x, i) for i in a ]
    ans = sum1 - sum(a)

    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)

solve()

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

ans = 0
for num in range(N-1):
	if alis[num] + alis[num+1] <= x: continue;
	
	if alis[num] > x:
		ans += alis[num] - x
		alis[num] = x
	if alis[num] + alis[num+1] > x:
		ans += alis[num]+alis[num+1] - x
		alis[num+1] -= alis[num]+alis[num+1] - x
print(ans)

Solution (Python):

"""
C - Boxes and Candies
https://atcoder.jp/contests/abc048/tasks/arc064_a

"""
import sys


def solve(N, x, candies):
    i = 1
    ans = 0
    while i < N:
        gap = candies[i - 1] + candies[i] - x
        if gap > 0:
            if gap <= candies[i]:
                candies[i] -= gap
                ans += gap
            else:
                ans += gap
                candies[i] = 0
        i += 1
    return ans


def main(args):
    N, x = map(int, input().split())
    candies = [int(x) for x in input().split()]
    ans = solve(N, x, candies)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

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

c = 0
for i in range(n-1):
    sum2 = vs[i] + vs[i+1]
    if sum2 <= x:
        continue

    surplus = sum2 - x
    c += surplus
    vs[i+1] -= min(vs[i+1], surplus)

print(c)

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

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

Solution (Python):
n,k=map(int,input().split())
arr=list(map(int,input().split()))
ans=0
for i in range(1,n):
  if arr[i]+arr[i-1]<=k:
    continue
  elif arr[i-1]<=k<arr[i]+arr[i-1]:
    ans+=arr[i]+arr[i-1]-k
    arr[i]=k-arr[i-1]
  else:
    ans+=arr[i]
    arr[i]=0
    ans+=arr[i-1]-k
    arr[i-1]=k
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:
        cnt+=a[i+1]-max(x-a[i],0)
        a[i+1]=max(x-a[i],0)
        if a[i+1]==0 and a[i]>x:
            cnt+=a[i]-x
            a[i]=x
print(cnt)

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

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

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

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

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

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

print(cnt)

Solution (Python):
import numpy as np
N, x = map(int, input().split())
A = np.array(list(map(int, input().split())))

count = np.sum((A - x)[A - x > 0])
A[A > x] = x
for i in range(1, N):
    if A[i] + A[i - 1] > x:
        decrease = A[i] + A[i - 1] - x
        A[i] = A[i] - decrease
        count += decrease
print(count)

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

B = [min(a, x) for a in A]
for i in range(N - 1):
    su = B[i] + B[i + 1]
    if su > x:
        B[i + 1] -= su - x

print(sum(a - b for a, b in zip(A, B)))

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

res = 0
pre = 0
for i in a:
    if i+pre>x:
        res += (i+pre-x)
        pre = i-(i+pre-x) 
    else:
        pre = i
print(res)

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

print(eat)

Solution (Python):
import sys
N, x = map(int, sys.stdin.readline().split())
a = list(map(int, sys.stdin.readline().split()))
a.insert(0,0)
ans = 0
for i in range(1,len(a)):
    if x < a[i-1] + a[i]:
        ans += (a[i-1] + a[i] - x)
        a[i] -= (a[i-1] + 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(N-1):
    eat = 0
    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+1] = 0
            a[i] -= (eat - a[i+1])
    ans += eat

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

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

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

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

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

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

Solution (Python):


def read_input():
    n, x = map(int, input().split())
    alist = list(map(int, input().split()))

    return n, x, alist

def submit():
    n, x, alist = read_input()

    edit = 0

    if alist[0] > x:
        edit += alist[0] - x
        alist[0] = x

    for i in range(n - 1):
        if alist[i] + alist[i + 1] > x:
            diff = alist[i] + alist[i + 1] - x
            edit += diff
            alist[i + 1] = alist[i + 1] - diff

    print(edit)


if __name__ == '__main__':
    submit()

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

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

print(count_a)

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


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

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

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

print(ans)

Solution (Python):
from itertools import accumulate
n,x = map(int,input().split())
a = list(map(int,input().split()))
b = list(accumulate(a))
b = [0]+b
tmp = 0
cnt = 0
for i in range(n-1):
    t = b[i+2]-b[i]-x-tmp
    tmp = 0
    if t>0:
        cnt += t
        tmp += min(t,a[i+1])    
print(cnt)

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

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

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

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

print(functools.reduce(f, a)[1])

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

result = 0

def f(a, b):
    global result
    d = max(0, (b + a) - x)
    result += d
    return max(0, b - d)

functools.reduce(f, a)
print(result)

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

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

Solution (Python):
import sys

def main():
    input = sys.stdin.readline
    N, x = map(int, input().split())
    A = list(map(int, input().split()))

    ans = 0
    a = 0
    for i in range(N):
        diff = (A[i] + a) - x
        diff = max(0, diff)
        ans += diff
        a = A[i] - diff

    return ans


if __name__ == '__main__':
    print(main())

Solution (Python):
N, K = map(int, input().split())
a = list(map(int, input().split()))
s = sum(a)

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

print(s - sum(a))

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

Solution (Python):


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

count_candy_eat = 0

for i in range(N):
    if(i == 0):
        if(candy_list[0] > x):
            count_candy_eat += candy_list[0] - x
            candy_list[0] = x
    else:
        if(candy_list[i-1] + candy_list[i] > x):
            if(x - candy_list[i-1] > 0):
                count_candy_eat += candy_list[i-1] + candy_list[i] - x
                candy_list[i] = x - candy_list[i-1]
            else:
                count_candy_eat += candy_list[i]
                candy_list[i] = 0


print(count_candy_eat)

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

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

count = 0
for i in range(N):
  count += A0[i]-A[i]
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]=a[0]-x,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):
import sys
import collections
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())

n, x = nm()
a = nl()
ans = 0
for i in range(n):
    if i == 0:
        if a[i] <= x:
            tmp = a[i]
        else:
            if a[i] > x:
                ans += a[i] - x
                tmp = x
            else:
                tmp = a[i]
    else:
        if tmp + a[i] <= x:
            tmp = a[i]
        else:
            ans += tmp + a[i] - x
            tmp = x - tmp
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] = 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 = [int(i) for i in input().split()]

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

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

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

ans = 0
for i in range(N+1):
    ans += abs(a[i] - tmpa[i])
print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, x = MAP()
a = LIST()
count = 0
def reduce_candy(i): 
	global count
	if sum_near[i] <= x: 
		pass 
	elif sum_near[i] - a[i] <= x: 
		if not i == 0:
			sum_near[i-1] -= sum_near[i] - x
		a[i] -= sum_near[i] - x
		count += sum_near[i] - x
		sum_near[i] = x
	else: 
		if not i == 0:
			sum_near[i-1] -= a[i]
		sum_near[i] -= a[i]
		count += a[i]
		a[i] = 0
		a[i+1] -= sum_near[i] - x
		count += sum_near[i] - x
		sum_near[i] = x
sum_near = []
for i in range(N-1):
	sum_near.append(a[i] + a[i+1])

for i in range(N-2, -1, -1):
	reduce_candy(i)
	
	
print(count)

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

B=[a for a in 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 a,b in zip(A,B):
    ans+=(a-b)

print(ans)