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

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

for i in range(N):
    if wa[i] <= x:
        continue
    if wa[i] > x:
        dif = wa[i]-x
        ans += dif
        a[i] -= dif
        wa[i] -= dif
        wa[i+1] -= dif
        continue

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

print(ans)

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

for i in range(N):
    if wa[i] <= x:
        continue
    if wa[i] > x:
        dif = wa[i]-x
        ans += dif
        a[i] -= dif
        wa[i] -= dif
        wa[i+1] -= dif
        continue

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

print(ans)

Solution (Python):

import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1


def read_int():
    return int(input())


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, X, A):
    ans = 0
    for i, a in enumerate(A):
        ans += max(0, a-X)
        A[i] = min(a, X)

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


def main():
    N, X = read_int_n()
    A = read_int_n()
    print(slv(N, X, A))


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    n,x  = map(int,input().split())
    a = [int(x) for x in input().split()]
    ans = 0
    for i in range(1,n):
        if a[i]+a[i-1]<=x:
            pass
        else:
            if (a[i]+a[i-1])-x<=a[i]:
                ans += (a[i]+a[i-1])-x
                a[i] -= (a[i]+a[i-1])-x
            else:
                ans += (a[i]+a[i-1])-x 
                a[i] = 0
    print(ans)
if __name__ == "__main__":
    main()

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

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

Solution (Python):


n, x = map(int, input().split())
arr = list(map(int, input().split()))
c = 0
for i in range(1, n):
    if arr[i - 1] + arr[i] > x:
        dif = arr[i - 1] + arr[i] - x
        if arr[i] >= dif:
            
            arr[i] -= dif
        else:
            
            new_dif = dif - arr[i]
            arr[i] = 0
            arr[i - 1] -= new_dif
        c += dif
print(c)

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

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

print(cost)

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

for i in range(N):
    A[i] = B[i]
    if A[i] > x:
        A[i] = x
for i in range(N - 1):
    tmp = A[i] + A[i + 1]
    if tmp <= x: continue
    A[i + 1] -= tmp - x
print(sum([B[_] - A[_] for _ in range(N)]))

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

if(n==1):
    print(a[0]-x)
    exit()
ope=0
if(a[0]>x):
    ope+=a[0]-x
    a[0]=x

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

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

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

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

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

Solution (Python):

import sys
sys.setrecursionlimit(10000000)
INF = 1<<32


def solve(N: int, x: int, a: "List[int]"):
    ans = sum(a)

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

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

    print(ans-sum(a))
    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):
def solve():
    N, X = list(map(int, input().split()))
    AS = tuple(map(int, input().split()))
    
    

    cost = 0
    if AS[0] + AS[1] > X:
        should = AS[0] + AS[1] - X
        cost += should
        if AS[1] > should:
            b = AS[1] - should
        else:
            b = 0
    else:
        b = AS[1]

    for i in range(1, len(AS) - 1, 1):
        a = b
        b = AS[i + 1]
        if a + b > X:
            should = a + b - X
            cost += should
            if b > should:
                b = b - should
            else:
                b = 0

    return cost


if __name__ == '__main__':
    res = solve()
    print(res)

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

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

count=0
if(a[0]>x):
    count=a[0]-x
    a[0]=x
    
for ind,i in enumerate(a):
    if(ind==len(a)-1):
      break
    s=i+a[ind+1]
    if(s>x):
        count+=s-x
        a[ind+1]-=s-x
        
print(count)

Solution (Python):


n, x = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
c = 0
for i in range(1, len(a)):
	b = a[i - 1] + a[i]
	d = b - x
	if d > 0:
		c += d
		e = min(a[i], d)
		a[i] -= e
		d -= e
		if d > 0:
			a[i - 1] -= d
print(c)

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

Solution (Python):
n,x=map(int,input().split())
A=list(map(int,input().split()))
B=A[:]

s1=0

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

print(s1)

Solution (Python):

import sys
input = sys.stdin.readline


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:
        s = A[i]+A[i-1] - X
        ans += s
        A[i] -= s

print(ans)

Solution (Python):
def examC():
    N, x = LI()
    a = LI()
    cur = int(0)
    if a[0]>x:
        cur += a[0]-x
        a[0] = x
    for i in range(N-1):
        cur += max((a[i]+a[i+1])-x,0)
        a[i+1] = min(x-a[i],a[i+1])
    print(cur)


import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examC()

Solution (Python):
N,X = map(int,input().split())
Data = list(map(int,input().split()))
ans = 0
for i in range(N-1):
    a1 = Data[i]
    a2 = Data[i+1]

    diff = a1+a2-X
    if diff > 0:
        ans += diff
        if a2 >= diff:
            Data[i+1] -= diff
        else:
            Data[i+1] = 0
            Data[i] -= (a2-diff)
print(ans)

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

ans=0

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

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

n, x = map(int,put().split())
ls = list(map(int,put().split()))
ls.append(0)
ans = 0

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

Solution (Python):



n, x = map(int,input().split())
ls = list(map(int,input().split()))
ls.append(0)
ans = 0

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

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

n, x = map(int,input().split())
ls = list(map(int,input().split()))
ls.append(0)
ans = 0

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

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

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

if __name__ == "__main__":
    main()

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

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

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

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

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

print(ans)

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

out = 0
diff = 0
for i in range(n-1):
  if L[i] > x:
    diff = L[i] - x
    out += diff
    L[i] = x
  if L[i] + L[i+1] > x:
    diff = L[i] + L[i+1] - x
    out += diff
    L[i+1] = x - L[i]
print(out)

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

eat=0

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

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

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 7)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, K = MAP()
A = LIST()

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

Solution (Python):
def main():
    N, x, *a = map(int, open(0).read().split())
    ans = a[0] - x if x < a[0] else 0
    pre = min(a[0], x)
    for i in range(1, N):
        dif = pre + a[i] - x
        if 0 < dif:
            ans += dif
            a[i] -= dif
        pre = a[i]
    print(ans)
    return


main()

Solution (Python):
def main():
    N, x, *a = map(int, open(0).read().split())
    ans = 0
    pre = a[0]
    for i in range(1, N):
        if x < pre + a[i]:
            cur = pre + a[i] - x
            ans += cur
            a[i] = a[i] - cur if cur < a[i] else 0
        pre = a[i]
    print(ans)
    return


main()

Solution (Python):
def main():
    N, x, *an = map(int, open(0).read().split())
    ans = 0
    for i in range(N - 1):
        if x < an[i] + an[i + 1]:
            cur = an[i] + an[i + 1] - x
            ans += cur
            if an[i + 1] <= cur:
                an[i + 1] = 0
            else:
                an[i + 1] -= cur
    print(ans)
    return


main()

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

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

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

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

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

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

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

print(ans)

Solution (Python):

import sys


def solve(N: int, x: int, a: "List[int]"):
    answer =0
    for i in range(N-1):
        consum = a[i]+a[i+1]
        if consum > x:
            a[i+1]-= min(consum-x,a[i+1])
            answer+=consum-x
        
    print(answer)
    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 = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]

count = 0
previous = 0

for i in a:
    if i + previous > x: 
        excess = i + previous - x
        count += excess
        previous = x - previous
    else: 
        previous = i

print(count)

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

count = 0
for index, i in enumerate(a):
  if index+2 > len(a):
    continue
  total = i + a[index+1]
  if total <= x:
    continue
  m = total - x
  count += m
  if i >= a[index+1]:
    i -= m
    if i < 0:
      i = 0
      m = m - i
    a[index] = i  
    if m > 0:
      a[index+1] -= m
      
  if a[index+1] > i:
    a[index+1] -= m
    if a[index+1] < 0:
      a[index+1] = 0
      m = m - a[index+1]
      if m > 0:
        a[index] -= m

print(count)

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

result = 0

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

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

cnt=0
pre=0

for i in range(N):
  dif=max(A[i]+pre-x,0)
  cnt+=dif
  A[i]-=dif
  pre=A[i]
  
print(cnt)

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

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

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

if SA[N-2]>x:
  ans+=SA[N-2]-x
print(ans)

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

Solution (Python):
import sys
n, x = [int(i) for i in sys.stdin.readline().split()]
a_ls = [int(i) for i in sys.stdin.readline().split()]
_sum = max(0, a_ls[0] - x)
last = min(x, a_ls[0])
for a in a_ls[1:]:
    if last + a > x:
        _sum += last + a - x
        last = x - last
    else:
        last = a
print(_sum)

Solution (Python):
import sys
read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs= sys.stdin.buffer.readlines























def main():
    N,x,*a=map(int,read().split())
    t=0
    ans=0
    for i in range(N-1):
        now=max(0,a[i]-t)
        t=max(0,now+a[i+1]-x)
        ans+=t
    print(ans)


if __name__ == '__main__':
    main()

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

ans = 0
before = 0

for i in a:
    if i + before > x:
        ans += i + before - x
        before = x - before
    else:
        before = i

print(ans)

Solution (Python):
n,k=map(int,input().split())
l=[int(i) for i in input().split()]
sm=0 
for i in range(n-1):
    if l[i]+l[i+1]<=k:
        continue 
    else: 
        sm+=l[i]+l[i+1]-k 
        req=l[i]+l[i+1]-k 
        if l[i+1]>=req: 
            l[i+1]-=req 
            req=0 
        else: 
            
            req-=l[i+1]
            l[i]-=req
            l[i+1]=0
print(sm)

Solution (Python):


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

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

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

print(ans)

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

Solution (Python):
n,x=map(int,input().split())
lists=list(map(int,input().split()))
count=0
for i  in range(n-1):
    if lists[i]+lists[i+1]<=x:
        continue
    elif lists[i]+lists[i+1]>x and lists[i]<=x:
        count+=lists[i]+lists[i+1]-x
        lists[i+1]=x-lists[i]

    elif lists[i]+lists[i+1]>x and lists[i]>x:
        count+=(lists[i]+lists[i+1]-x)
        lists[i+1]=0
print(count)

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

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

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

while(True):
    if len(a)<2:
        break
    
    minus = a[0]+a[1]-x
    if minus<=0:
        a.remove(a[0])
    elif minus>=a[1]:
        count+=minus
        a[1]=0
        a.remove(a[0])
    else:
        count += minus
        a[1] -= minus

print(count)

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

Solution (Python):


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

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

print(count)

Solution (Python):
if __name__ == "__main__":
    n, x = map(int, input().split())
    boxes = list(map(int, input().split()))

    moves = 0
    if boxes[0] > x:
        moves = boxes[0] - x
        boxes[0] = x
    for i in range(n - 1):
        y = boxes[i] + boxes[i + 1] - x
        if y > 0:
            boxes[i + 1] -= y
            moves += y
    print(moves)

Solution (Python):

N,x = map(int,input().split())
A = list(map(int,input().split()))
A_ = A[:]
ans = 0

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

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

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

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

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

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

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

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

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

if a[0]+a[1]>x:
    cnt+=a[0]+a[1]-x
    a[0]-=a[0]+a[1]-x
if a[-1]+a[-2]>x:
    cnt+=a[-1]+a[-2]-x
    a[-1]-=a[-1]+a[-2]-x
print(cnt)

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

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

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

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

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

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

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

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

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

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

if __name__ == '__main__':
    main()