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 解():
    iN,ix = [int(_) for _ in input().split()]
    aA =[int(_) for _ in input().split()]

    
    if ix == 0 :
        print(sum(aA))
    else:
        iCount = 0
        for i in range(1,iN):
            i差 = aA[i-1] + aA[i] -ix
            if 0 < i差  :
                if aA[i] <= i差:
                    aA[i] = 0
                    iCount += i差
                else:
                    iCount += i差
                    aA[i] -= i差

        print(iCount)
解()

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

AA = [0 for i in range(N-1)]
for i in range(N-1):
    AA[i] = A[i] + A[i+1]

cnt = 0

for i in range(N-1):
    aa = AA[i]
    diff = aa - x

    if aa > x:
        cnt += diff
        if A[i+1] >= diff:
            A[i+1] -= diff
        else:
            A[i]   -= diff - A[i+1]
            A[i+1]  = 0

        if i < N -2:
            AA[i] = A[i] + A[i+1]
            AA[i+1] = A[i+1] + A[i+2]
        else:
            AA[i] = A[i] + A[i+1]

print(cnt)

Solution (Python):
N, x = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
if a[0] > x:
    ans = a[0] - x
    a[0] = x

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

print(ans)

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

Solution (Python):


from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)

INF = 10 ** 18

N, x = map(int, raw_input().split())
a = map(int, raw_input().split())

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

print ans

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

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

print(eat)

Solution (Python):
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


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

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

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


if __name__ == '__main__':
    main()

Solution (Python):
import sys


Nx = input().split()
an = input().split()

anx = list()

for item in an:
    anx.append(int(item))

x = int(Nx[1])
N = int(Nx[0])
i = 0
count = 0
while i < N - 1:
    sum = anx[i] + anx[i + 1]
    
    if sum > x:
        count += sum - x
        num = sum - x
        anxi = anx[i+1]
        anxi -= num
        if anxi < 0:
            anxi2 = anx[i] + anxi
            anx[i] = anxi2
            anxi = 0
        anx[i+1] = anxi
    i += 1
print(count)

Solution (Python):



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

print(ans)

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

res = float('inf')
for b in (a,a[::-1]):
    tmp = 0
    for i in range(n-1):
        if b[i]+b[i+1]>x:
            tmp += b[i] + b[i+1] - x
            if b[i]>x:
                b[i],b[i+1] = x,0
            else:
                b[i+1] = x - b[i]
    res = min(res,tmp)

print(res)

Solution (Python):
il = lambda: list(map(int, input().split()))
ils = lambda n, s="int(input())": [eval(s) for _ in range(n)]

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

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

ans = 0
prev = A[0]
if prev > X:
  ans = prev - X
  prev = X
  
for i in range(1,N):
  now = A[i]
  eat = max(0, prev + now - X)
  ans += eat
  prev = now - eat
  
print(ans)

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

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

result = 0

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

print(result)

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

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

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

print(count)

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

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

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

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

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

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

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

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

ans = 0

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

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

print(ans)

Solution (Python):
f,g = list(map(int,input().split()))

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

c = 0
p = 1
if a[0] > g:
    c += a[0] - g
    a[0] = g
while p <= f-1:
    if a[p-1] + a[p] > g:
        eat = a[p-1] + a[p] - g
        c += eat
        a[p] -= eat
    p += 1
print(c)

Solution (Python):
n, x = map(int, input().split())
a = list(map(int, input().split()))
if a[0] > x:
    pre = x
    num = a[0] - x
else:
    pre = a[0]
    num = 0

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

print(num)

Solution (Python):
N,x = (int(i) for i in input().split())
a = [int(i) for i in input().split()]
count = 0
if a[1] > x:
    difference = x-a[1]
    count += difference
    a[1] -= difference
     
for i in range(N-1):
    difference = x - (a[i] + a[i+1])
    if difference < 0:
        count += abs(difference)
        a[i+1] += difference
        if a[i+1] < 0:
            a[i] += a[i]
            a[i+1] = 0 


print(count)

Solution (Python):

"""
Created on Mon Aug 13 14:21:46 2018
ABC048C
@author: maezawa
"""

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

cnt = 0
pre = 0
for i in a:
    if pre + i <= x:
        pre = i
        continue
    elif x-pre >= 0:
        cnt += i - (x-pre)
        pre = x-pre
    else:
        cnt += pre + i - x
        pre = 0

print(cnt)

Solution (Python):


def main():
    n, x = map(int, input().split())
    a_list = list(map(int, input().split()))
    a_list_cp = a_list[:]
    ans = 0

    for i in range(1, n):
        temp = a_list[i-1] + a_list[i]
        if temp > x:
            if x - a_list[i-1] >= 0:
                a_list[i] = x - a_list[i-1]
            else:
                a_list[i] = 0
                a_list[i-1] = a_list[i-1] + x - a_list[i-1]
    for a, b in zip(a_list, a_list_cp):
        ans += b - a
    print(ans)

if __name__ == '__main__':
    main()

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

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

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

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

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

Solution (Python):
N,X=map(int,input().split(" "))
A=input()
A=A.rstrip().split(" ")
listA=[]
cnt=0
for i in range(N):
    listA.append(int(A[i]))
    if i==0:
        if listA[0]>X:
            cnt+=listA[0]-X
            listA[0]=X
    else:
        if listA[i-1]+listA[i]>X:
            cnt+=(listA[i-1]+listA[i]-X)
            listA[i]=X-listA[i-1]
print(cnt)

Solution (Python):
N,x = map(int,input().split())
a = list(map(int,input().split()))
a_sum = 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] -= max(0,(a[i]+a[i+1]-x))
    else:
        pass
print(a_sum-sum(a))

Solution (Python):
def inpl(): return input().split()
n, x = map(int, inpl())
a = list(map(int, inpl()))
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())
aN = list(map(int, input().split()))

cnt = 0

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


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

print(cnt)

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

ans = 0
y = x

for a in A:
  if a > y:
    ans += a - y
  y = x - min(a, y)
  
print(ans)

Solution (Python):
a,b=map(int,input().split())
k=list(map(int,input().split()))

ans=0

for i in range(0,a-1):
    now=k[i]+k[i+1]
    if now > b:
        c=now-b
        if k[i+1]<c:
            k[i+1]=0
        else:
            k[i+1]-=c
        ans+=c

print(ans)

Solution (Python):
N,x = map(int, input().split())
A = [int(x) for x in input().split()]
 
ans = 0
p = 0
for a in A:
    e = max(0, a + p - x)
    ans += e
    p = a - e
print(ans)

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

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

Solution (Python):

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

    ans=0

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

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

    print(ans)

if __name__ == "__main__":
        main()

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

ans = 0
p = 0
for a in A:
    s = a + p
    p = a
    if s > x:
        e = s - x
        ans += e
        p = a - e
print(ans)

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

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

print(cnt)

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

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

print(count)

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

print(ans)

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

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

eat_count = 0

for i, a in enumerate(candy_list[:-1]):
  gap = candy_list[i] + candy_list[i+1] - x
  if gap <= 0:
    continue
  eat_count += gap
  if gap > 0:
    if candy_list[i+1] - gap >= 0:
      candy_list[i+1] -= gap
    else:
      candy_list[i+1] = 0
      gap -= candy_list[i+1]
      candy_list[i] -= gap

print(eat_count)

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

result = 0
mode = 0
if a[0] > x:
    result += a[1]
    a[1] = 0
    result += a[0] - x
    a[0] = x
for i in range(1, n):
    s = a[i] + a[i - 1]
    if s > x:
        t = s - x
        result += t
        a[i] -= t

print(result)

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

Ans = 0

d = max(0,A[0]-X)
A[0] -= d
Ans += d

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

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

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

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

v=[i if i<x else x for i in a]

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

print(sum(a)-sum(v))

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  
else:
  pass
j = 1
while j <= n-1:
  if a[j-1]+a[j]<=x:
    j += 1
  else:
    ans += a[j]-x+a[j-1]
    a[j]=x-a[j-1]
    j += 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(N-1):
    t = a[i] + a[i+1] - x
    if t <= 0: continue
    ans += t
    if a[i+1] >= t:
        a[i+1] -= t
    else:
        a[i+1] = 0
        a[i] -= (t - a[i+1])
print(ans)

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

print(ans)

Solution (Python):
def m():
    n, x = map(int, input().split())
    a = list(map(int, input().split()))
    b = a.copy()
    ans = 0
    for i in range(n):
        if b[i] > x:
            b[i] = x

    for j in range(1, n):
        if b[j-1] + b[j] > x:
            b[j] -= (b[j-1] + b[j]) - x
        else:
            pass
    for k in range(n):
        ans += a[k] - b[k]
    return ans

print(m())

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

prev = ans1 = 0
for a in src:
    if prev+a > X:
        eat = prev+a - X
        ans1 += eat
        prev = max(0, a-eat)
    else:
        prev = a

prev = ans2 = 0
for a in src[::-1]:
    if prev+a > X:
        eat = prev+a - X
        ans2 += eat
        prev = max(0, a-eat)
    else:
        prev = a

print(min(ans1,ans2))

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

ans1 = 0
tmp = 0
for a in src:
    eat = max(0, tmp + a - X)
    ans1 += eat
    tmp = a - eat
ans2 = 0
tmp = 0
for a in reversed(src):
    eat = max(0, tmp + a - X)
    ans2 += eat
    tmp = a - eat
print(min(ans1,ans2))

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

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

Solution (Python):





if __name__ == '__main__':
    n, x = list(map(int, input().split()))
    a = list(map(int, input().split()))
    count = 0

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

    for i in range(n - 1):
        diff = a[i] + a[i + 1] - x

        if diff > 0:
            count += diff
            a[i + 1] -= diff

    print(count)

Solution (Python):





if __name__ == '__main__':
    n, x = list(map(int, input().split()))
    a = list(map(int, input().split()))
    count = 0

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

    for i in range(n - 1):
        summed = a[i] + a[i + 1]

        if summed > x:
            diff = summed - x
            count += diff
            a[i + 1] -= diff

    print(count)

Solution (Python):
from sys import stdin

N, x = [int(x) for x in stdin.readline().rstrip().split()]
candys = [int(x) for x in stdin.readline().rstrip().split()]

count = 0


for i in range(N):
    if candys[i] > x:
        num = candys[i] - x
        candys[i] = x
        count += num
        





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

print(count)

Solution (Python):
N, x = map(int, input().split(' '))
Candy = list(map(int, input().split(' ')))
count = 0
if Candy[0] > x:
    count += Candy[0] - x
    Candy[0] = x

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

print(count)

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


def put_sum_candies(candies):
    base = 0
    for i, _ in enumerate(candies):
        if i == 0:
            continue
        if candies[i-1] + candies[i] <= x:
            continue
        if candies[i-1] > x:
            base += candies[i-1] - x
            candies[i-1] -= candies[i-1] - x
            base += candies[i] + candies[i-1] - x
            candies[i] -= candies[i] + candies[i-1] - x
        elif candies[i-1] <= x:
            base += candies[i] + candies[i-1] - x
            candies[i] -= candies[i] + candies[i-1] - x
    return base


base = put_sum_candies(candies)
print(base)

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

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

    print(bef-sum(a))


if __name__ == '__main__':
    main()

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

N,X = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
print(c_boxes_and_candies(N, X, A))

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

b = [0]
for i in range(n):
    b.append(a[i])
b.append(0)    
counter = 0

if b[1] > x:
    counter = b[1] - x
    b[1] = x
for i in range(n+1):
    if b[i] + b[i+1] > x:
        s = b[i] + b[i+1] - x
        if b[i+1] - s > -1:
            b[i+1] = b[i+1] -s
            counter = counter + s
        else:
            k = b[i+1]
            counter = counter + s
            b[i+1] = 0
            b[i] = x
        
print(counter)

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

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

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

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

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

ans = 0
for i in range(1,N):
    if src[i-1] + src[i] <= X: continue
    eat = src[i-1] + src[i] - X
    src[i] = max(0, src[i] - eat)
    ans += eat
print(ans)

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

for i in range(n):
    if candies[i] > x:
        margin = candies[i] - x
        candies[i] -= margin
        ans += margin

for i in range(1, n):
    if candies[i] + candies[i-1] > x:
        margin = (candies[i] + candies[i-1]) - x
        candies[i] -= margin
        ans += margin

print(ans)

Solution (Python):
n,x=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]
y=0
a1=0
a2=0
for a3 in a:
  s = max(a1+a2-x,a2+a3-x,0)
  y += s
  a1 = a2 
  a2 = a3 - s
print(y)

Solution (Python):
n,x = map(int,input().split())
a = list(map(int, input().split()))
ans = 0
if x<a[0]:
    ans = abs(x-a[0])
    a[0] -= abs(x-a[0])

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

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

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

print(min(ans))

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

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

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

Solution (Python):

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

N, x = inpl()
A = [0] + list(inpl())

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

Solution (Python):
inline = list(map(int,input().split()))
N = inline[0]
x = inline[1]
inline = list(map(int,input().split()))
two = []
cost = 0;
for i in range(N-1):
    two.append(inline[i]+inline[i+1])

for i in range(N-2):
    if two[i] > x:
        cost += (two[i]-x)
        two[i+1] -= min((two[i]-x),inline[i+1])

if two[-1]>x:
    cost += (two[-1]-x)

print(cost)

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

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

Solution (Python):
def eat_candies(candies, x):
    eaten = max(0, candies[0] - x)
    candies[0] -= eaten
    for i in range(1, len(candies)):
        extra = max(0, candies[i-1] + candies[i] - x)
        candies[i] -= extra
        eaten += extra
    return eaten
        
        
if __name__ == '__main__':
    n, x = list(map(int, input().split()))
    candies = list(map(int, input().split()))
    print(eat_candies(candies, x))

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

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

print(cnt)

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

share = 0
rm = [0] * (N-1)
for i in range(N-1):
	rm[i] = max(0,(a[i]+a[i+1])-x)


for i in range(N-2):
	s = min([rm[i],rm[i+1],a[i+1]])
	share += s
	rm[i] -= s
	rm[i+1] -= s

print(share + sum(rm))

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