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):
import sys
def LI():
    return list(map(int,sys.stdin.readline().rstrip().split()))  

N,x = LI()
a = LI()

ans = 0



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

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

print(ans)

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

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

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

print(ans)

Solution (Python):
import sys
 
def eat(i,j,candies,counter):
    s=candies[i]+candies[j]
    if s>x:
        if candies[i]>=s-x:
            candies[i]-=s-x
        else:
            candies[j]-=s-x-candies[i]
            candies[i]=0
        counter+=s-x
    return counter
 
def main(n,x,candies):
    counter=0
    for i in range(1,n):
        counter=eat(i,i-1,candies,counter)
    return counter
 
n,x=map(int,sys.stdin.readline().strip().split())
candies=list(map(int,sys.stdin.readline().strip().split()))
print(main(n,x,candies))

Solution (Python):
import sys

def eat(i,candies,counter):
    s=candies[i]+candies[i-1]
    nc=max(0,s-x)
    counter+=nc
    candies[i]-=nc
    return counter

def main(n,x,candies):
    counter=0
    if candies[0]>x:
        counter=candies[0]-x
        candies[0]=x
    for i in range(1,n):
        counter=eat(i,candies,counter)
    return counter

n,x=map(int,sys.stdin.readline().strip().split())
candies=list(map(int,sys.stdin.readline().strip().split()))
print(main(n,x,candies))

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

ans = 0
p = 0

for ai in a:
  if p + ai > x:
    d = p + ai - x
    ans += d
    p = ai - d
  else:
    p=ai
    
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()))
ans = 0

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

print(ans)

Solution (Python):
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():
    N, X = in_nn()
    A = in_nl()

    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] - (X - A[i - 1])
            A[i] = X - A[i - 1]

    
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys
import itertools

import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines



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

total = 0
goal = x * (N - 1)
for i in range(N):
    if i == 0 or i == N - 1:
        total += A[i]
    else:
        total += 2 * A[i]

if total <= goal:
    print(0)
    exit()

ans = 0
if A[0] > x:
    diff = A[0] - x
    ans += diff
    A[0] -= diff





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

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

print(ans)

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

print(ans)

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

Solution (Python):
n,x=map(int,input().split())
*a,=map(int,input().split())
b=[]
b.append(min(a[0],x))
for i in range(1,n):
    b.append(max(min(x-b[i-1], a[i]),0))

print(sum(a)-sum(b))

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

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

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

    
    
    

print(ans)

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

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

Solution (Python):
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n,x = map(int, input().split())
a = list(map(int, input().split()))
def sub(p,q):
    if p+q<=x:
        ans = (0, (p,q))
    elif (p+q-x)<=q:
        ans = (p+q-x, (p, x-p))
    else:
        ans = (p+q-x, (x, 0))
    return ans
ans = 0
a1,a2 = a[:2]
num, (a1,a2) = sub(a1,a2)
ans += num
for i in range(n-2):
    a1,a2 = a2, a[i+2]
    num, (a1,a2) = sub(a1,a2)
    ans += num
print(ans)

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

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

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

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

Solution (Python):
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


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

    ans = 0

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

    print(ans)


if __name__ == '__main__':
    main()

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

Solution (Python):
import sys
import numpy as np
import random
from decimal import Decimal
import itertools
import re
import bisect
from collections import deque, Counter
from functools import lru_cache

sys.setrecursionlimit(10**9)
INF = 10**13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
def SERIES(n): return np.fromstring(sys.stdin.buffer.read(), dtype=np.int32, sep=' ')
def GRID(h,w): return np.fromstring(sys.stdin.buffer.read(), dtype=np.int32, sep=' ').reshape(h,-1)[:,:w]
def GRIDfromString(h,w): return np.frombuffer(sys.stdin.buffer.read(), 'S1').reshape(h,-1)[:,:w]
MOD = 1000000007

def main():
	n, x = LI()
	a_list = LI()
	count = 0

	for i in range(n-1):
		a1, a2 = a_list[i], a_list[i+1]
		if a1 + a2 <= x:
			continue
		else:
			if a1 <= x:
				eat_candy = (a1+a2) - x
				a_list[i+1] -= eat_candy
				count += eat_candy
			else:
				a_list[i] = x
				a_list[i+1] = 0
				count += a1 + a2 - x

	print(count)


if __name__ == '__main__':
    main()

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

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

    ans2 = 0
    for i in range(N - 1):
        index = N - 1 - i
        s = B[index] + B[index - 1]
        if s <= x:
            continue
        diff = s - x
        if diff > B[index - 1]:
            B[index] -= diff - B[index - 1]
            B[index - 1] = 0
        else:
            B[index - 1] -= diff
        ans2 += diff
    ans += min(ans1, ans2)

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import math

import queue
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit


ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    n,x = map(int,ipt().split())
    a = [int(i) for i in ipt().split()]
    ans = 0
    if a[0]+a[1] > x:
        ans += a[0]+a[1]-x
        a[1] = max(0,x-a[0])
    for i in range(1,n-1):
        ai = a[i]
        ai1 = a[i+1]
        if ai+ai1 > x:
            ans += ai+ai1-x
            a[i+1] = x-ai
    print(ans)



    return

if __name__ == '__main__':
    main()

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

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

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

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

ans = 0

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

Solution (Python):
from copy import deepcopy
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:
        c += a[i] + a[i + 1] - x
        a[i + 1] -= a[i] + a[i + 1] - x
        if a[i + 1] < 0:
            a[i] -= -a[i + 1]
            a[i + 1] = 0
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] = x
for i in range(1,n):
    temp = a[i-1] + a[i]
    if temp > x:
        a[i] -= temp-x
        ans += temp-x
print(ans)

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

Solution (Python):





import math
import fractions
import sys
import bisect
import heapq  
import collections
from collections import Counter
from collections import deque
import pprint

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""


def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    for i in range(2, int(-(-n ** 0.5 // 1)) + 1):
        if temp % i == 0:
            cnt = 0
            while temp % i == 0:
                cnt += 1
                temp //= i
            arr.append([i, cnt])

    if temp != 1:
        arr.append([temp, 1])

    if arr == []:
        arr.append([n, 1])

    return arr



def power(a, n, mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod



def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n - 1
    while (tmp >= l):
        a = a * tmp % mod
        tmp -= 1
    return a


inf = 10 ** 18
mod = 10 ** 9 + 7

n,x = lr()
a = lr()
w = []
ans=0
for i in range(n-1):
    w.append((a[i]+a[i+1])-x)
for i in range(n-1):
    s,t = a[i],a[i+1]
    ret = 0
    if w[i] > 0:
        if t >= w[i]:
            ans+=w[i]
            ret=w[i]
            a[i+1]-=w[i]
        else:
            ans+=w[i]
            ret=t
            a[i+1]=0
    if i < n-2:
        w[i+1]-=ret
print(ans)

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

f = sum(As)
  
for i in range(0,N):
  if As[i]+As[i+1] > x:
    if As[i] <= x:
      As[i+1] = x - As[i]
    else:
      As[i+1] = 0
      As[i] = x

s = sum(As)

print(f-s)

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

Solution (Python):


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


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    N, x = read_ints()
    A = read_ints()
    answer = 0
    for i in range(1, N):
        if A[i-1]+A[i] > x:
            eat = A[i]+A[i-1]-x
            if eat <= A[i]:
                A[i] -= eat
                answer += eat
            else:
                eat -= A[i]
                answer += A[i]
                A[i] = 0
                A[i-1] -= eat
                answer += eat
    return answer


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

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

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

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

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




'''
a=[]
b=[]
for i in range():
    A, B = list(map(int, input().split()))map(int, input().split())
    a.append(A)   
    b.append(B)'''

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

print(ans)

Solution (Python):
num = [0]
N, X = map(int, input().split())
[num.append(i) for i in list(map(int, input().split()))]

cost = 0
for i in range(1, N+1):
    if num[i-1] + num[i] > X:
        newcost = num[i-1] + num[i] - X
        num[i] -= newcost
        cost += newcost

print(cost)

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

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

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

Solution (Python):


LI = lambda: list(map(int, input().split()))


N, x = LI()
A = LI()


def main():
    ans = 0
    for i in range(1, N):
        s = A[i - 1] + A[i]
        d = max(0, s - x)
        ans += d
        A[i] -= min(d, A[i])
    print(ans)


if __name__ == "__main__":
    main()

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

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

print(sum_arr - sum(arr))

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

Solution (Python):
n, x = map(int,input().split())
origdat = list(map(int, input().split()))
from copy import deepcopy

res = 0

cnt = 0
dat = deepcopy(origdat)
for i in range(n):
    if dat[i] == 0:
        continue
    if dat[i] > x: 
        res += dat[i] - x
        dat[i] = x
    if i == n-1:
        break
    if dat[i] + dat[i+1] > x:
        res += (dat[i] + dat[i+1]) - x
        dat[i+1] -= (dat[i] + dat[i+1]) - x

print(res)

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

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

print(ans)

Solution (Python):
candi_num, can_exist_num = list(map(int, input().split()))
A = list(map(int, input().split()))

ans = 0


ofs = max(0, A[0] + A[1] - can_exist_num)
if ofs > A[1]:
  A[0] -= ofs - A[1]
  ans += ofs - A[1]

for i in range(1, candi_num):
  two_sum = A[i - 1] + A[i]
  ofs = max(0, two_sum - can_exist_num)
  A[i] -= ofs
  ans += ofs

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

Solution (Python):
import numpy as np
import math
import sys
def sinput(): return sys.stdin.readline()
def iinput(): return int(sinput())
def imap(): return map(int, sinput().split())
def fmap(): return map(float, sinput().split())
def iarr(): return list(imap())
def farr(): return list(fmap())
def sarr(): return sinput().split()

n, x = imap()
A = iarr()
ans = 0
for i in range(1, len(A)):
    tmp = A[i] + A[i-1]
    if tmp > x:
        A[i] -= tmp - x
        ans += tmp - x
    A[i] = max(0, A[i])
print(ans)

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

print(c)

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

cost = max(0, A[0] + A[1] - X)
A[1] = max(0, A[1] - cost)

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

print(cost)

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

cost = 0
diff = max(0, sum(A[0:2]) - X)
A[0] -= max(0, diff - A[1])
A[1] -= min(A[1], diff)
cost += diff

for i in range(2, N):
    diff = max(0, A[i - 1] + A[i] - X)
    A[i - 1] -= max(0, diff - A[i])
    A[i] -= min(A[i], diff)
    cost += diff

print(cost)

Solution (Python):
n, x, *A = map(int, open(0).read().split())
s = 0
if A[0] > x:
    s += 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
        A[i] = x - A[i-1]
print(s)

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

count = 0

for i, c in enumerate(a):
	try:
		j = a[i + 1]
	except IndexError:
		break
	while (a[i + 1] + c) > x:
		if (a[i + 1] + c) == x:
			break
		count += abs(x - (a[i + 1] + c))
		a[i + 1] -= abs(x - (a[i + 1] + c))
		if a[i + 1] <= 0:
			a[i + 1] = 0
			break
	else:
		continue

print(count)

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

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

print(result)

Solution (Python):
def main():
    n, x = list(map(int, input().split()))
    A = list(map(int, input().split()))
    B = [min(A[0], x), ]
    for i in range(n - 1):
        if B[i] + A[i + 1] > x:
            B.append(x - B[i])
        else:
            B.append(A[i + 1])
    ans = sum([a - b for a, b in zip(A, B)])
    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):
    if a[i] > x:
        ans += a[i] - x
        a[i] = x
    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()))
pre = 0
ans = 0

for a in A:
  eat = max(0,pre+a-x)
  pre = a - eat
  ans+= eat

print(ans)

Solution (Python):
N,X,*A = map(int,open(0).read().split())
A+=[0]
s = 0

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

print(s)

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

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

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

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] > x:
        ans +=  a[i]-x
        a[i] = x
    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,=map(int,input().split())

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

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

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

count = 0

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

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

Solution (Python):
import sys


import math
import collections

import bisect
import itertools
import fractions

import copy
import heapq
import decimal

import queue
import numpy as np

sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))





def main():
    n, x = ns()
    a = na()

    ans = 0

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

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from sys import stdin
import collections
import math


n,x   = [int(x) for x in stdin.readline().rstrip().split()]
arr = [int(x) for x in stdin.readline().rstrip().split()]

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

print(ans)

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

moves = 0
if aa[0] > x:
    moves += aa[0] - x
    aa[0] = x

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

print(moves)

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

Solution (Python):
import sys
def main():
    N,x=tuple(map(int,sys.stdin.readline().split()))
    A=list(map(int,sys.stdin.readline().split()))
    pa,r=0,0
    for a in A:
        c=a+pa-x
        if c>0:
            r+=c
            pa=a-c
        else:pa=a
    print(r)
if __name__=='__main__':main()

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

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

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

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

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

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

Solution (Python):
n, x = map(int, input().split())
a = list(map(int, input().split())) + [0]
ans = 0
for i in range(1, n + 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):
from sys import stdin
def main():
    
    readline=stdin.readline
    n,x=map(int,readline().split())
    a=list(map(int,readline().split()))

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

    print(ans)
    
if __name__=="__main__":
    main()

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