Problem:
 Score : 300 points    Problem Statement There are N boxes arranged in a row.
Initially, the i -th box from the left contains a_i candies.  Snuke can perform the following operation any number of times:   Choose a box containing at least one candy, and eat one of the candies in the chosen box.   His objective is as follows:   Any two neighboring boxes contain at most x candies in total.   Find the minimum number of operations required to achieve the objective.      Constraints  2 ≤ N ≤ 10^5  0 ≤ a_i ≤ 10^9  0 ≤ x ≤ 10^9         Input The input is given from Standard Input in the following format:  N  x  a_1  a_2  ...  a_N       Output Print the minimum number of operations required to achieve the objective.        Sample Input 1 3 3
2 2 2      Sample Output 1 1  Eat one candy in the second box.
Then, the number of candies in each box becomes (2, 1, 2) .       Sample Input 2 6 1
1 6 1 2 0 4      Sample Output 2 11  For example, eat six candies in the second box, two in the fourth box, and three in the sixth box.
Then, the number of candies in each box becomes (1, 0, 1, 0, 0, 1) .       Sample Input 3 5 9
3 1 4 1 5      Sample Output 3 0  The objective is already achieved without performing operations.       Sample Input 4 2 0
5 5      Sample Output 4 10  All the candies need to be eaten.

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

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

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

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

print(c)

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

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

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

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

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

print(count)

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

Solution (Python):


import sys

tmp = input().split()
hoge = list(map(lambda a: int(a), tmp))
n=hoge[0]
x=hoge[1]

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

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

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

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

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

cnt = 0
for i in range(len(a[:-1])):
    sumv = a[i] + a[i+1]
    if x < sumv:
        delta = sumv - x
        if a[i+1] > delta:
            a[i+1] -= (sumv - x)
            cnt += (sumv - x)
        else:
            ddelta = delta - a[i+1]
            a[i+1] = 0
            cnt += delta
print(cnt)

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

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

print(ans)

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

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

    if adj[N-2] > x:
        ans += adj[N-2]-x
else:
    if A[0] + A[1] <= x:
        ans = 0
    else:
        ans = A[0] + A[1] - x
print(str(ans))

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

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

cnt = 0
ans = 0

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

Solution (Python):
N,x=map(int,input().split())
A=[int(i) for i in input().split()]
ans=0
if A[0]>x:
    ans+=A[0]-x
    A[0]=x
for i in range(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()))
ans = 0
for i in range(N - 1):
    if a[i] + a[i + 1] > x:
        t = a[i] + a[i + 1] - 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())
a=[int(i) for i in input().split()]
ans=sum([max(a[i]-x,0) for i in range(N)])
a=[min(a[i],x) for i in range(N)]
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()))
b = [x if a[0]>x else a[0]]
for i in range(N-1):
  if b[i]+a[i+1]>x:
    b.append(x-b[i])
  else:
    b.append(a[i+1])
ans = 0
for i in range(N):
  ans += a[i]-b[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):
    diff = max(0,(a[i+1] + a[i]) - x)
    ans += diff
    if a[i+1] >= diff:
        a[i+1] = a[i+1] - diff
    else:
        a[i] -= diff - a[i+1]
        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 abs(a[i]+a[i+1]) <= x:
        continue
    elif abs(a[i]+a[i+1]) > x and i < n-2:
        y = abs(x-a[i+1]-a[i])
        if a[i+1] <= y:
            ans += a[i+1]
            a[i+1] = 0
            ans += max(0,abs(a[i]-x))
        else:
            ans += y
            a[i+1] -= y
    else:
        ans += a[i+1]+a[i]-x
print(ans)

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

total = 0
for i in range(1, len(A)):
    diff = A[i-1] + A[i] -x
    if diff <= 0:
        continue
    if diff <= A[i]:
        A[i] -= diff
    else:
        A[i-1] -= diff - A[i]
        A[i] = 0
    total += diff

print(total)

Solution (Python):
import sys
input=sys.stdin.readline
n,x=map(int,input().split())
A=list(map(int,input().split()))
cnt=0

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

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

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

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

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

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

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

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

Solution (Python):
import sys
sys.setrecursionlimit(10**7)
INF = 10 ** 18
MOD = 10 ** 9 + 7
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 II(): return int(sys.stdin.readline())
def SI(): return input()

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

    return ans

print(main())

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

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

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

Solution (Python):
def inpl():
    return list(map(int, input().split()))


N, x = inpl()
A = inpl()

ans = 0

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

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

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

Solution (Python):


"""
Created on Sun Feb 11 09:54:00 2019

@author: shinjisu
"""



def getInt(): return int(input())


def getIntList(): return [int(x) for x in input().split()]


def dmp(x):
    global debug
    if debug:
        print(x)


def probC_try1():
    N, x = getIntList()
    a = getIntList()
    dmp((N, x, a))
    eatCnt = 0
    half = (x+1) // 2
    dmp((N,x,a,half))
    for i in range(N-1):
        if a[i]+a[i+1] > x:
            
            eat = a[i] - min(a[i],max(a[i+1]-half,half),half)
            eatCnt += eat
            a[i] = min(a[i],max(a[i+1]-half,half),half)
    eat = a[N-1] - min(a[N-1], half)
    eatCnt += eat
    a[N-1] = min(a[N-1],half)
    dmp((eatCnt,a))
    i=0
    while i<N:
        while i<N-1 and a[i]+a[i+1]<=x: i+=1
        cnt=1
        while i<N-1 and a[i]+a[i+1]>x: 
            i+=1; cnt+=1
        eatCnt += cnt//2
        dmp((cnt,eatCnt))
        i+=1
    return eatCnt


def probC():
    N, X = getIntList()
    a = getIntList()
    dmp((N, X, a))
    eatCnt = 0
    for i in range(N-1):
        dmp((eatCnt,a))
        if a[i] > X:
            eat = a[i] - X
            dmp(('eat',eat))
            eatCnt += eat
            a[i] = X
        if a[i]+a[i+1] > X:
            eat = a[i]+a[i+1] - X
            dmp(('eat',eat))
            eatCnt += eat
            if a[i+1] < eat:
                
                a[i+1] = 0
            else:
                a[i+1] -= eat
    dmp((eatCnt,a))
    return eatCnt


debug = False  
print(probC())

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

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

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

print(total)

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

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

before = sum(l)
l[0] = min(x, l[0])
for i in range(1, N):
    s = l[i-1]+l[i]
    if s > x:
        l[i] = x-l[i-1]

print(before-sum(l))

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

print(sum(copy)-sum(a))

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:
        pass
    else:
        need = (A[i]+A[i+1]) - X
        ans += need
        A[i+1] = max(0, A[i+1]-need)

print(ans)

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

answer=0

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

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

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

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

ans = 0

for i in range(N - 1):
	if a[i] + a[i + 1] <= x:
		pass
	else:
		if a[i] <= x:
			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)

Solution (Python):
def inpl(): return [int(i) for i in input().split()]
N, K = inpl()
A = inpl()
back = 0
ans = 0
for i in A:
    chan = max(0, back + i - K)
    back = i - chan
    ans += chan
print(ans)

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

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

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


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

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

Solution (Python):
import sys
import collections
import itertools
import math
from collections import Counter
from collections import deque
MOD = 10 ** 9 + 7


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

B = A.copy()


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





total = 0
for i in range(N):
    total += A[i] - B[i]

print(total)

sys.exit(0)

Solution (Python):
iipt=lambda:int(input())
miipt=lambda:list(map(int,input().split(" ")))
N,x=miipt()
l=miipt()
l+=[0,0]
ans=0
d=[0]*(N+2) 

d[-1]=1

d[N]=1
nxt=[0,N-1]
while True:
    f=False
    nnxt=[]
    for i in nxt:
        if d[i-1] and d[i+1]:
            df=max(l[i-1],l[i+1])+l[i]-x
            if df>0:
                l[i]-= df
                ans+=df
            continue
        j,k=(i-1,i+1) if d[i-1] else (i+1,i-1)
        d[i]=1

        f=True

        nnxt.append(k)
        df=l[i]+l[j]-x

        if df>0:
            l[i]-= df
            ans+=df           
    if not f:
        break
    nxt=nnxt
"""for i in range(0,N-1):
    d=l[i]+l[i+1]-x
    if d>0:
        ans+=d
        l[i+1]-=d"""
print(ans)

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

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

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

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

Solution (Python):

def main():
    buf = input()
    buflist = buf.split()
    N = int(buflist[0])
    x = int(buflist[1])
    buf = input()
    buflist = buf.split()
    a = list(map(int, buflist))
    eaten = 0
    for i in range(1,len(a)):
        need_to_eat = a[i-1] + a[i] - x
        if need_to_eat <= 0:
            continue
        else:
            eaten += need_to_eat
            if need_to_eat > a[i]:
                a[i-1] -= need_to_eat - a[i]
                need_to_eat = a[i]
            a[i] -= need_to_eat
    print(eaten)

if __name__ == '__main__':
    main()

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

ret = 0
for i in range(1, N):
    cnt = max(0, A[i] + A[i - 1] - x)

    ret += cnt
    A[i] = max(0, A[i] - cnt)

print(ret)

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

ans = 0
for i in range(N - 1):
    num = As[i] + As[i + 1]
    if num <= x: continue
    
    ans += num - x
    if As[i + 1] >= num - x:
        As[i + 1] -= num - x
    else:
        As[i + 1] = 0
        As[i] = x

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

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

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

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

Solution (Python):

line = input().split(" ")
n = int(line[0])
x = int(line[1])
a = [int(n) for n in input().split(" ")]

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

print(ret)

Solution (Python):
n,m = map(int,raw_input().split())

arr = map(int,raw_input().split())

ans = 0
for i in range(1,n):
	if arr[i] + arr[i-1] > m:
		t = min(arr[i],arr[i] + arr[i-1]-m)
		arr[i] -= t
		ans += t

	if arr[i] + arr[i-1] > m:
		t = min(arr[i-1],arr[i-1]-m)
		arr[i-1] -= t
		ans += t

print ans

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

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

ans = 0

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

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

Solution (Python):
def getinputdata():

    
    array_result = []
    
    data = input()
    
    array_result.append(data.split(" "))

    flg = 1

    try:

        while flg:

            data = input()

            if(data != ""):
                
                array_result.append(data.split(" "))

            else:

                flg = 0
    finally:

        return array_result

arr_data = getinputdata()

n = int(arr_data[0][0])
x = int(arr_data[0][1])

arr=[int(arr_data[1][x]) for x in range(0,n)]

cnt=0

for i in range(n-1):
    
    zan=arr[i]+arr[i+1]-x
     
    if 0<arr[i+1]<=zan:

        cnt+=arr[i+1]
        zan-=arr[i+1]
        arr[i+1]=0

    elif arr[i+1]>zan>0:

        cnt+=zan
        arr[i+1]-=zan
        zan=0

    if 0<arr[i]<=zan:

        cnt+=arr[i]
        zan-=arr[i]
        arr[i]=0

    elif arr[i]>zan>0:

        cnt+=zan
        arr[i]-=zan
        zan=0

    
print(cnt)

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

ans = A[0]-x if A[0]>x else 0
back = min(A[0],x)
for a in A[1:]:
    if back+a > x:
        ans += back+a-x
        back = x-back
    else:
        back = 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):
    sub = max(0, A[i] + A[i+1] - X)
    ans += sub
    A[i+1] = max(0, A[i+1] - sub)

print(ans)

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

nc = 0

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

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

print(nc)

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

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

for i in range(len(nlist)):
    if nlist[i]>x:
        d=nlist[i]-x
        ans+=nlist[i]-x
        nlist[i]=x
        if i+1<len(nlist):
            nlist[i+1]-=d
print(ans)

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

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

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

Solution (Python):
ri=(raw_input()).split(" ")
n=int(ri[0])
x=int(ri[1])
a=(raw_input()).split(" ")
for i in range(n):
    a[i]=int(a[i])



eat=0

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


print(eat)

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

res = 0

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

print(res)

Solution (Python):
from sys import stdin, stdout
import numpy as np
def readLine_int_list():return list(map(int, stdin.readline().split()))



def main():
    n, x = readLine_int_list()

    _a = readLine_int_list()
    a = _a.copy()
    
    for i in range(n-1):
        if a[i] + a[i+1] > x:
            
            if a[i] > x:
                a[i] = x
            
            if x - a[i] >= 0:
                if a[i] == 0:
                    a[i+1] = x
                elif a[i] > 0:
                    a[i+1] = x-a[i]
    print(np.sum(np.array(_a) - np.array(a)))
    
if __name__ == '__main__':
    main()

Solution (Python):


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

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

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

Solution (Python):

import math
import sys
import itertools
import numpy as np
import functools
import collections
mo = 1000000007
r = range

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

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

ret = 0
for i in r(1, n):
    for j in r(2):
        if a[i] + a[i-1] > x:
            tmp = min(a[i-j], a[i] + a[i-1] - x)
            ret += tmp
            a[i-j] -= tmp

print(ret)

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

    if a[i] + a[i+1] > x:
        prev = a[i+1]
        a[i+1] -= a[i] + a[i+1] - x
        ans += prev - a[i+1]

print(ans)

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

ans = 0

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

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

print(ans)