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 solve(n, x, a_list):
    
    ans = 0
    for i in range(len(a_list)-1):
        tmp = a_list[i] + a_list[i+1]
        if tmp > x:
            d = tmp - x
            ans += d
            if a_list[i+1] - d >= 0:
                a_list[i+1] = a_list[i+1] -d
            else:
                a_list[i] = a_list[i] - (d-a_list[i+1])
                a_list[i+1] = 0
    return ans

if __name__ == "__main__":
    n, x = [int(i) for i in input().split()]
    a_list = [int(i) for i in input().split()]
    print(solve(n, x, a_list))

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

Solution (Python):
n,x = list(map(int, input().split()))
lst = [0]+list(map(int, input().split()))
ans = 0

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

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

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

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

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

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


if '__main__' == __name__:
    resolve()

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

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

print(A_org-sum(A))

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

print(A_org-sum(A))

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

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

boxes = [int(x) for x in input().split(' ')]

count = 0
for  i in range(n):
    if(boxes[i] > border):
        count += boxes[i] - border
        boxes[i] = border

for i in range(n-1):
    if(boxes[i] + boxes[i+1] > border):
        count += boxes[i] + boxes[i+1] - border
        boxes[i+1] -= boxes[i] + boxes[i+1] - border
        
print(count)

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

def main():
    N, x = map(int, readline().rstrip().split())
    A = list(map(int, readline().rstrip().split()))
    ans = 0
    for i, a in enumerate(A):
        if a > x:
            ans += a - x
            A[i] = x
    
    pre = 0
    for i, a in enumerate(A):
        if pre + a > x:
            A[i] = x - pre
            ans += a - A[i]
        pre = A[i]
    
    print(ans)

if __name__ == '__main__':
    main()

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

Solution (Python):


import sys
import math
from bisect import bisect_right as br
from bisect import bisect_left as bl
sys.setrecursionlimit(2147483647)
from heapq import heappush, heappop,heappushpop
from collections import defaultdict
from itertools import accumulate
from collections import Counter
from collections import deque
from operator import itemgetter
from itertools import permutations
mod = 10**9 + 7
inf = float('inf')
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))

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

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

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


if __name__ == '__main__':
    solve()

Solution (Python):
def main():
    n, x = map(int, input().split())
    A = list(map(int, input().split()))
    ans = 0
    if A[0] > x:
        ans += A[0] - x
        A[0] = x

    for i in range(n - 1):
        j = i + 1
        if j < n and A[i] + A[j] > x:
            diff = A[i] + A[j] - x
            ans += diff
            A[j] -= diff
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    from collections import deque

    N, L = map(int, input().split())
    *a, = map(int, input().split())

    def solve(a):
        dq = deque()
        s = 0
        ans = 0
        for x in a:
            if len(dq) > 1:
                t = dq.popleft()
                s -= t
            eat = max(0, s + x - L)
            if eat:
                x -= eat
                ans += eat
            s += x
            dq.append(x)
        return ans

    ans =solve(a) 

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    from collections import deque

    N, L = map(int, input().split())
    *a, = map(int, input().split())

    def solve(a):
        dq = deque()
        s = 0
        ans = 0
        for x in a:
            if len(dq) > 1:
                t = dq.popleft()
                s -= t
            eat = max(0, s + x - L)
            if eat:
                x -= eat
                ans += eat
            s += x
            dq.append(x)
        return ans

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

print(ans)

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

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

Solution (Python):
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from heapq import heapify, heappop, heappush
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

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

ans = 0
tmp = sum(A)
for i in range(N-1):
    
    a = max(A[i] + A[i+1] - X, 0)

    A[i+1] = max(0, A[i+1]-a)
    
    b = max(0, a - A[i+1])


c = 0
if A[0] + A[1] > X:
    c = A[0] + A[1] - X
A[0] = A[0] - c
print(tmp-sum(A))

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

ans = 0
for i in range(N - 1):
    j = i + 1
    if j < N and A[i] + A[j] > X:
        d = min(A[j], (A[i] + A[j]) - X)
        ans += d
        A[j] -= d
    if A[i] + A[j] > X:
        d = min(A[i], (A[i] + A[j]) - X)
        ans += d
        A[i] -= d

print(ans)

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

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

Solution (Python):
import sys
input = sys.stdin.readline
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:
        s = a[i-1] + a[i] - x
        if a[i] - s >= 0:
            a[i] -= s
            ans += s
        else:
            a[i] = 0
            ans += s
print(ans)

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

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

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

print(ans)

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

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

Solution (Python):

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

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

sum_e = sum(A)
print(sum_s - sum_e)

Solution (Python):

si = lambda: input().strip()
ii = lambda: int(input())
mii = lambda: map(int, input().split())
lmii = lambda: list(map(int, input().split()))
lmsi = lambda: list(map(str, input().split()))
smii = lambda: sorted(map(int, input().split()))



N, x = mii()
A = lmii()

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

print(ans)

Solution (Python):


import sys
input= lambda: sys.stdin.readline().rstrip()
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))

def resolve():
    N,x=pin()
    A=list(pin())

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

resolve()

Solution (Python):
N,X=map(int,input().split())
a=list(map(int,input().split()))
total=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]=X-a[i]
print(total-sum(a))

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

    ans = []
    if a[0] <= x:
        ans.append(a[0])
    else:
        ans.append(x)
    for i in range(1,n):
        if ans[i-1] + a[i] <= x:
            ans.append(a[i])
        else:
            ans.append(x-ans[i-1])
    print(sum(a)-sum(ans))

resolve()

Solution (Python):
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) 
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    


MOD = 10**9 + 7
INF = 2**31  

from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  


N, x = read_ints()
A = read_ints()






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


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

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

ans = 0
for i in range(n):
    ans += a[i] - tmp[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):
  t_sum=a[i]+a[i-1]
  tt=0
  if t_sum>x:
    tt=t_sum-x
    tmp1=min(a[i],tt)
    ans+=tmp1
    a[i]-=tmp1
    tt-=tmp1
    if tt>0:
    
      tmp2=min(a[i-1],tt)
      ans+=tmp2
      a[i-1]-=tmp2
print(ans)

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

ans = 0
for i in range(n):
    ans += a[i] - tmp[i]
print(ans)

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

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

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

    diff = (a[i] + a[i + 1]) - x
    ans += diff

    tmp = min(diff, a[i + 1])
    a[i + 1] -= tmp
    diff -= tmp
    a[i] -= diff
      
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()))

B = []
for i in range(N):
    if i == 0:
        B.append(A[i])
    else:
        B.append(A[i]+A[i-1])
B.append(A[-1])

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

print(ans)

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

ans = 0
for i in range(1, N):
    a = A[i - 1]
    b = A[i]
    if a + b <= x:
        continue
    over = (a + b) - x
    ans += over
    A[i] = max(0, b - over)

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

        if a[i] - p >= 0:
            a[i] -= p
            ans += p
        elif a[i] - p < 0:
            a[i] = 0
            a[i-1] -= p - a[i]
            ans += p
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(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):
def main():
    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:
            num_to_eat = min(a[i+1], a[i] + a[i+1] - x)
            ans += num_to_eat
            a[i+1] -= num_to_eat
            if a[i] + a[i+1] > x:
                num_to_eat = min(a[i], a[i] + a[i+1] - x)
                ans += num_to_eat
                a[i] -= num_to_eat

    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):
    L = A[i]
    R = A[i + 1]
    eat = max(0, L + R - x)
    ans += eat
    A[i + 1] = max(0, R - eat)

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):
    if a[i]>x:
        ans+=a[i]-x
        a[i]=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):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

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

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

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

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

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

abc048_c()

Solution (Python):
import sys
import math  
import bisect  
import queue  


def input():
    return sys.stdin.readline().rstrip()


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

    A_origin = A.copy()

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

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

    res = 0
    for i in range(N):
        res += abs(A_origin[i] - A[i])

    print(res)


if __name__ == '__main__':
    main()

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

    total = sum(a)

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

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

    print(total-sum(a))

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

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for _ in range(j)] for _ in range(k)]




N, x = mi()
A = li()
adj = []
'''
for i in range(N-1):
    adj.append(A[i]+A[i+1])

ans = 0
for i, num in enumerate(adj):
    if num > x:
        ans += num - x
        if i < N-2:
            adj[i+1] = max(adj[i+1] - (num-x), A[i+2])
print(ans)
'''
ans = 0
for i in range(N-1):
    if A[i] + A[i+1] > x:
        wa = A[i] + A[i+1]
        ans += wa - x
        if A[i+1] < wa - x:
            A[i+1] = 0
        else:
            A[i+1] -= wa - x

print(ans)

Solution (Python):
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for _ in range(j)] for _ in range(k)]




N, x = mi()
A = li()
adj = []

for i in range(N-1):
    adj.append(A[i]+A[i+1])

ans = 0
for i, num in enumerate(adj):
    if num > x:
        ans += num - x
        if i < N-2:
            adj[i+1] = max(adj[i+1] - (num-x), A[i+2])
print(ans)

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


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

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n, x, *a = map(int, read().split())
memo = [0] * n
ans = 0
if a[0] > x:
    ans += a[0] - x
    a[0] = x
for i in range(n - 1):
    memo[i] = a[i] + a[i + 1]
for i, check in enumerate(memo):
    if check > x:
        ans += check - x
        memo[i + 1] -= check - x
print(ans)

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

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

print(ans)

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

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

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

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

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

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

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

print(res)

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

for i in range(N - 1):
    taberu = ai[i] + ai[i+1] - x
    if taberu > 0:
        gokei += taberu
        ai[i+1] = ai[i+1] - taberu 
print(gokei)

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

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

ans=0

if A[0]>X:
    ans+=A[0]-X
    A[0]=X 

i=0
while i+1<=N-1:
    if A[i]+A[i+1]>X:
        ans+=A[i]+A[i+1]-X
        A[i+1]=X-A[i]
    i+=1

print(ans)

Solution (Python):
n,x,*a = map(int,open(0).read().split())
b = [0]*n
b[0] = min(x,a[0])
for i in range(n-1):
  b[i+1] = max(min(x-b[i],a[i+1]),0)
  b[i] = min(b[i],x-b[i+1])
ans = 0
for i,j in zip(a,b):
  ans += i-j
print(ans)

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

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

for i in range(n-1):
  if a_lst[i] + a_lst[i+1] > x:
    if a_lst[i+1] - (x - a_lst[i]) >= 0:
      cnt += a_lst[i+1] -(x - a_lst[i])
      a_lst[i+1] = x - a_lst[i]
  else:
    pass
    
print(cnt)

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

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

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

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

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

actions = 0

for a in range(1, n):
    diff = alist[a] + alist[a - 1] - x
    if diff > 0:
        actions += diff
        alist[a] = max(0, alist[a] - diff)

print(actions)

Solution (Python):
import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

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

s = sum(A)
for i in range(1, N):
    if A[i] + A[i - 1] > x:
        d = sum(A[i - 1:i + 1]) - x
        for j in range(2):
            if d <= 0:
                break
            A[i - j], d = A[i - j] - min(A[i - j], d), d - min(A[i - j], d)

print(s - sum(A))

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

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

print(cnt)

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

ans = 0

for i in range(n-1):
    value = list_A[i] + list_A[i+1]
    if value > x:
        list_A[i+1] = max(0, list_A[i+1] - (value - x))
        ans += value - x

print(ans)

Solution (Python):
n,x=map(int,input().split())
a=list(map(int,input().split()))
asum=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]=x-a[i]
        
asum=asum-sum(a)
print(asum)

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

Solution (Python):
n,x,*a=map(int,open(0).read().split());t=r=0
for i in a:t=min(i,x-t);r+=i-t
print(r)

Solution (Python):
n,x,*a=map(int,open(0).read().split());r=0
for i in range(1,n):e=max(a[i]+a[i-1]-x,0);a[i]-=min(e,a[i]);r+=e
print(r)

Solution (Python):
(N,x),a=[list(map(int,input().split()))for _ in[0]*2];r=0
for i in range(1,N):e=max(a[i]+a[i-1]-x,0);a[i]-=min(e,a[i]);r+=e
print(r)