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 computeMinimumNoOfOperations(candies, x):
    noOfOperations = 0
    for i in range(len(candies) - 1):
        if (candies[i] > x):
            candiesToEat = candies[i] - x
            candies[i] -= candiesToEat
            noOfOperations += candiesToEat
        if (candies[i] + candies[i + 1] > x):
            candiesToEat = candies[i] + candies[i + 1] - x
            candies[i + 1] -= candiesToEat
            noOfOperations += candiesToEat 
    return noOfOperations


def start():
    n, x = list(map(int, input().split()))
    candies = list(map(int, input().split()))
    return computeMinimumNoOfOperations(candies, x)

print(start())

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

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

    if(a[i+1] > 0 and sa <= a[i+1]):
      a[i+1]=a[i+1]-sa
    else:
      a[i+1]=0

print(count)

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] = candies[i] - x
    
print(count)

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

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

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

ans=0
if N>2:
  for i in range(1,N-1):
    sum0=a[i-1]+a[i]
    sum1=a[i]+a[i+1]
    if i==1 and sum0>x:
      ans+=sum0-x
      a[1]-=sum0-x
      if a[1]<0:
        a[0]+=a[1]
        a[1]=0
        
      sum0=a[i-1]+a[i]
      sum1=a[i]+a[i+1]
    if (sum0<=x and sum1>x):
      ans+=sum1-x
      a[i+1]-=sum1-x
      if a[i+1]<0:
        a[i]+=a[i+1]
        a[i+1]=0

else:
  if a[0]+a[1]>x:
    ans=a[0]+a[1]-x
print(ans)

Solution (Python):
def minimumNumOfCandies(candies,atmostValue):
  candiesToEat = 0
  for i in range( 0 , len ( candies ) - 1 ) :
    if( candies[i] > atmostValue ) :
      candiesToEat = candiesToEat + ( candies[i] - atmostValue)
      candies[i] = candies[i] - ( candies[i] - atmostValue )
    if( candies[i] + candies[i + 1] > atmostValue ):
      candiesToEat = candiesToEat + ( (candies[i + 1] + candies[i]) - atmostValue )
      candies[i + 1] = candies[i + 1] - ( (candies[i + 1] + candies[i])- atmostValue)
  return candiesToEat
n = list(int(i) for i in input().split())
candies = list( int (i) for i in input().split())
print(minimumNumOfCandies(candies,n[1]))

Solution (Python):
def minCandies(candies, n, max_candy):
    count = 0
    for i in range(n - 1):
        if candies[i] + candies[i + 1] > max_candy:
            remove = (candies[i] + candies[i + 1]) - max_candy
            candies[i + 1] -= remove if candies[i + 1] >= remove else candies[i + 1]
            count += remove 
    return count


n, max_candy = input().split()
candies = list(map(int, input().split()))
print(minCandies(candies, int(n), int(max_candy)))

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

print(count)

Solution (Python):
def minCandies(boxes,limit):
    out = 0
    for i in range(len(boxes) - 1):
        total = boxes[i] + boxes[i + 1]
        if total > limit:
            out += total - limit
            boxes[i + 1] = boxes[i + 1] - (total - limit)
            if boxes[i + 1] < 0:
                boxes[i + 1] = 0
    return out

num_of_boxes, limit = map(int,input().split())
boxes = list(map(int,input().split()))

if limit == 0:
    print(sum(boxes))
elif max([a + b for (a,b) in zip(boxes[:-1],boxes[1:])]) <= limit:
    print(0)
else:
    print(minCandies(boxes,limit))

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

Solution (Python):
def min_candies(li, x):
    result = 0
    for i in range(len(li) - 1):
        num = li[i] + li[i + 1]
        if num > x:
            result += num - x
            li[i + 1] = li[i + 1] - (num - x)
            if li[i + 1] < 0:
                li[i + 1] = 0

    return result

boxes, x = map(int, input().split())
li = list(map(int, input().split()))
print(min_candies(li, x))

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

cost = 0



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

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

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

if __name__ == '__main__':
    main()

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

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

if __name__ == '__main__':

    print(main())

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

pre = sum(a)

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

print(pre - sum(a))

Solution (Python):


from collections import Counter,deque


from sys import stdin,setrecursionlimit

setrecursionlimit(10**6)
input=stdin.readline

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

Solution (Python):

import sys
sys.setrecursionlimit(12345678)
import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop


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

    
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def _pf(s): return print(s, flush=True)

N, x = LI()
A = LI()
original_sum = sum(A)
for aidx in range(N-1):
    if A[aidx] + A[aidx+1] <= x:
        continue
    else:
        diff = A[aidx] + A[aidx+1] - x
        sub = min(diff, A[aidx+1])
        A[aidx+1] -= sub
        diff -= sub
        if diff > 0:
            A[aidx] -= min(A[aidx], diff)


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

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

ba = sum(a)
if a[0] > x:
    a[0] = x
for i in range(n-1):
    if a[i] + a[i+1] <= x:
        continue
    a[i+1] = x - a[i]
print(ba - sum(a))

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

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

print(res)

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

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

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

answer = 0
for i in range(n):
    tmp = max(0, an[i] + an[i-1] - x)
    answer += tmp
    an[i] -= tmp
print(answer)

Solution (Python):
N, x = map(int, input().split())
As = list(map(int, input().split()))
r = 0
a = As[0]
if a > x:
  r += a-x
  p = x
else:
  p = a

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

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

ct = 0
for i in range(len(alist)-1):
    extra = alist[i]+alist[i+1]-x
    if extra <= 0:
        continue
    ct += extra
    if extra <= alist[i+1]-1:
        alist[i+1] -= extra
    else:
        alist[i] -= (extra-alist[i+1])
        alist[i+1] = 0

print(ct)

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

left = a[0]
for right in a[1:]:
    s = left + right
    if s > x:
        if left > x:
            right = 0
            left = x
        else:
            right -= s - x
        ans += s - x
    left = right

print(ans)

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

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

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

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

ans = tmp
print(tmp)

Solution (Python):


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


def solve():
    cnt=0
    for i in range(n-1):
        tmp=a[i]+a[i+1]-x
        if tmp>0:
            cnt+=tmp
            if tmp>a[i+1]:
                buf=tmp-a[i+1]
                a[i]-=buf
                tmp-=buf
            a[i+1]-=tmp
    return cnt

ans=solve()
print(ans)

Solution (Python):
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
import math
import queue

Mod = 1000000007




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(n-1):
        if a[i+1]+a[i] > x:
            ans += a[i+1]+a[i]-x
            a[i+1] -= a[i+1]+a[i]-x
        else:
            pass
    print(ans) 
if __name__ == "__main__":
    main() 





def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n < 2 is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))):
        if prime[i] == 1:
            for j in range(2*i,n):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n):
        if prime[i] == 1:
            res.append(i)
    return res

class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py

    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)

Solution (Python):

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

D = [0]*N
answer = 0
for i in range(N-1):
    if A[i] + A[i+1] > x:
        d = A[i] + A[i+1] - x
        e = min(A[i+1], d)
        answer += e
        A[i+1] -= e
        d -= e
        if d > 0:
            A[i] -= d
            answer += d
print(answer)

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

D = [0]*N
answer = 0
for i in range(N-1):
    if A[i] + A[i+1] > x:
        d = A[i] + A[i+1] - x
        e = min(A[i+1], d)
        answer += e
        A[i+1] -= e
        d -= e
        if d > 0:
            A[i] -= d
            answer += d
print(answer)

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

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

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

Solution (Python):
from sys import exit, stdin
N, x = [int(_) for _ in stdin.readline().rstrip().split()]
A = [int(_) for _ in stdin.readline().rstrip().split()]

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

print(ans)

Solution (Python):
n, x = map(int, input().split())
a = list(map(int, input().split()))
cnt = 0
for i in range(1, n):
    while True:
        if a[i] + a[i-1] > x:
            if a[i] != 0:
                t = min((a[i] + a[i-1] - x), a[i])
                a[i] -= t
                cnt += t
            else:
                t = min((a[i] + a[i-1] - x), a[i-1])
                a[i-1] -= t
                cnt += t
        else:
            break
print(cnt)

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

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

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

ans=0
for i in range(n-1):
    left=a[i]
    right=a[i+1]
    if left+right<=x:
        continue
    eat=(left+right)-x
    ans+=eat
    a[i+1]=max(0, right-eat)
print(ans)

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

Solution (Python):

import collections
import itertools
import sys
import time

def getint(): return int(input())
def getints(): return list(map(int, input().split()))

n,x=getints()
vs=getints()

res = 0
if vs[0] > x:
    eat = vs[0] - x
    res += eat
    vs[0] -= eat

for i in range(1,n):
    exceed = max(0, vs[i-1] + vs[i] - x)
    eat = min(vs[i], exceed)
    vs[i] -= eat
    res += eat
    exceed = max(0, vs[i-1] + vs[i] - x)
    eat = min(vs[i-1], exceed)
    res += eat

print (res)

Solution (Python):
n,x=map(int,input().split())
a=list(map(int,input().split()))
a.insert(0,0)
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()))
def solve(arr,sup):
    ans=0
    a=arr[:]
    for i in range(len(a)-1):
        tmp = a[i]+a[i+1]-sup
        if tmp>0:
            a[i+1]-=min(tmp,a[i+1])
            ans+=tmp
    return ans
print(min(solve(A,x), solve(A[::-1],x)))

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

cnt = 0

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

for i in range(n-1):
    dis = 0
    if lst[i]+lst[i+1] > x:
        dis = lst[i] + lst[i+1] - x
        lst[i+1] -= dis
        cnt += dis

print(cnt)

Solution (Python):
N, total = map(int, input().split())
li = list(map(int, input().split()))

ans = 0
for i in range(N-1):
    diff = li[i] + li[i+1] - total
    if diff > 0:
        if li[i+1] > diff:
            li[i+1] -= diff
        else:
            li[i] -= (diff - li[i+1])
            li[i+1] = 0
        ans += diff
print(ans)

Solution (Python):
N, total = map(int, input().split())
li = list(map(int, input().split()))

ans = 0
for i in range(N-1):
    wa = li[i] + li[i+1]
    if wa > total:
        if li[i+1] > wa - total:
            li[i+1] -= (wa - total)
        else:
            li[i] -= ((wa - total) - li[i+1])
            li[i+1] = 0
        ans += wa - total
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] - X
            A[i] = X
        if A[i + 1] > X - A[i] :
            dis = A[i + 1] - (X - A[i])
            ans += dis
            A[i + 1] -= dis

print(ans)

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

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

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

ate_n = 0

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

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

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

ans = 0
d = 0
if A[0] > X:
    d = A[0] - X
    ans += d
for i in range(N-1):
    s = A[i]+A[i+1]-d
    if s > X:
        d = s - X
        ans += d
    else:
        d = 0
 
print(ans)

Solution (Python):
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)

from math import factorial



def main():
    num, max_num = map(int, input().split())
    data = list(map(int, input().split())) + [0]

    ans = 0
    for i in range(num):
        if data[i] + data[i + 1] > max_num:
            ans += min(data[i + 1], data[i] + data[i + 1] - max_num)
            data[i + 1] -= min(data[i + 1], data[i] + data[i + 1] - max_num)

            if data[i] + data[i + 1] > max_num:
                ans += data[i] - max_num

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):

import sys
sys.setrecursionlimit(300000)


def solve(N: int, x: int, a: "List[int]"):
    ret = 0
    for i in range(1, N):
        if a[i] + a[i - 1] > x:
            hoge = (a[i] + a[i - 1]) - x
            ret += hoge
            tmp = min(hoge, a[i])
            a[i] -= tmp
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  
    x = int(next(tokens))  
    a = [ int(next(tokens)) for _ in range(N) ]  
    solve(N, x, a)

if __name__ == '__main__':
    main()

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

count = 0
for i in range(0, n, 2):
    if i != 0:
        if a[i - 1] + a[i] > x:
            temp = min(a[i - 1] + a[i] - x, a[i])
            a[i] -= temp
            count += temp
            if a[i - 1] + a[i] > x:
                temp = a[i - 1] + a[i] - x
                a[i - 1] -= temp
                count += temp
    if i == n - 1:
        pass
    else:
        if a[i] + a[i + 1] > x:
            temp = min(a[i] + a[i + 1] - x, a[i + 1])
            a[i + 1] -= temp
            count += temp
            if a[i] + a[i + 1] > x:
                temp = a[i] + a[i + 1] - x
                a[i] -= temp
                count += temp
print(count)

Solution (Python):

from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')


def A():
    a, b, h = IR(3)
    print((a+b)*h//2)
    return


def B():
    d = defaultdict(int)
    d["a"] = deque(S())
    d["b"] = deque(S())
    d["c"] = deque(S())
    tern = "a"
    while d[tern]:
        tern =  d[tern].popleft()
    print(str.capitalize(tern))
    return


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


def D():
    return


if __name__ == '__main__':
    C()

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

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

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

Solution (Python):
import copy

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

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

ans = sum(abs(x - y) for x, y in zip(a, b))
print(ans)

Solution (Python):



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

b = a.copy()
if a[0] > x:
    b[0] = x
for i in range(n-1):
    if b[i] + b[i+1] > x:
        b[i+1] -= (b[i]+b[i+1] - x)
        if b[i+1] < 0:
            b[i] -= abs(b[i+1])
            b[i+1] += abs(b[i+1])
ans = 0
for i in range(n):
    ans += abs(b[i] - a[i])
print(ans)

Solution (Python):

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

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


ans = 0
las = 0

for a in A:

    if las + a <= x:
        las = a

    else:

        las = x - las

        ans += a - las

print (ans)

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

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

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

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

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

if __name__ == '__main__':
    main()

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

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

Solution (Python):
import sys
import numpy as np


def main():
    n, x = [int(e) for e in input().split()]
    boxes = [int(e) for e in input().split()]
    boxesrev = list(reversed(boxes))
    ans1 = 0
    ans1 += max(0, boxes[0] - x)
    boxes[0] = min(boxes[0], x)
    ans1 += max(0, boxes[n - 1] - x)
    boxes[n - 1] = min(boxes[n - 1], x)
    for i in range(1, n - 2):
        ans1 += max(0, (boxes[i] + boxes[i - 1]) - x)
        boxes[i] = min(boxes[i], x - boxes[i - 1])
    if n > 2:
        ans1 += max(0, (boxes[n - 2] + max(boxes[n - 3], boxes[n - 1])) - x)
        boxes[n - 2] = min(boxes[n - 2], x - max(boxes[n - 3], boxes[n - 1]))
    else:
        ans1 += max(0, boxes[n - 2] + boxes[n - 1] - x)
        boxes[n - 1] = min(boxes[n - 1], x - boxes[n - 2])
    print(ans1)


main()

Solution (Python):
N,x = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
cnt = 0
for ind in range(len(a)-1):
    if(ind==0):
        if(a[1]+a[0]>x):
            cnt += a[1]+a[0]-x
            a[1]-=min(a[1]+a[0]-x,a[1])
            a[0]-=max((a[1]+a[0])-x,0)
    if(a[ind+1]+a[ind]>x):
        cnt += a[ind+1]+a[ind]-x
        a[ind+1] -= a[ind+1]+a[ind]-x
print(cnt)

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

Solution (Python):

def main():
    n, x = map(int, input().split())
    A = list(map(int, input().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 = max(0, prv + A[i] - x)
        A[i] -= dif
        ans += dif
        prv = A[i]
    print(ans)


if __name__ == "__main__":
    main()