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
input = sys.stdin.readline

import math

import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache






MOD = 10**9+7
INF = float('inf')






def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]


def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()


def DD(arg): return defaultdict(arg)



def inv(n): return pow(n, MOD-2, MOD)


kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret


def factorization(n):
  arr = []
  temp = n
  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 make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors


def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum


def gcd(a, b):
    while b:
        a, b = b, a % b
    return a


def lcm(a, b):
    return a * b // gcd (a, b)


def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count


def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))


def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count





N,x = IL()
A = IL()

ans = 0

prev = 0
for a in A:
  if prev+a>x:
    ans += (prev+a)-x
    prev = a-((prev+a)-x)
  else:
    prev = a
print(ans)

Solution (Python):
n, x = map(int, input().split())
l = [int(i)for i in input().split()]
last = 0
z = 0
for n in l:
    if n + last > x:
        z += n + last - x
        last = x - last
    else:
        last = n
print(z)

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

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

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

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

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


state = 0

ans = 0

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

print(ans)

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

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


state = 0

ans = 0

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

print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy, copy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7



N, x = MAP()
a = LIST()
ans = max(0, a[0]-x)
a[0] = min(a[0], x)

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

print(ans)

Solution (Python):
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy, copy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7



N, x = MAP()
a = LIST()
ans = max(0, a[0]-x)
a[0] = min(a[0], x)

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

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

print(ans)

Solution (Python):


import sys
readline = sys.stdin.readline

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

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

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

print(ans)

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

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

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

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

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

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

print(ans)

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

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

        if a_list[i+1] >= diff:
            a_list[i+1] -= diff
        else:
            a_list[i] = diff - a_list[i+1]
            a_list[i+1] = 0
print(cnt)

Solution (Python):
import sys




from collections import deque

from fractions  import gcd

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

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


ans = 0

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

        else:
            ans += a[i + 1]
            dif -= a[i + 1]
            a[i + 1] = 0

            a[i] -= dif
            ans += dif


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

Solution (Python):
import sys

sys.setrecursionlimit(10000000)
import os
import math
import bisect
import collections
import itertools
import heapq
import re
import queue



ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)


MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, X = il()
    A = il()

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

    for n in range(N - 1):
        a = A[n] + A[n + 1]
        if a > X:
            A[n + 1] -= a - X
            ret += a - X
    print(ret)


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

print(ans)

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

ans = 0

for i in range(N-1):
    s = a[i] + a[i+1]

    if s <= x:
        continue
    else:
        a[i+1] = max(0,a[i+1]-(s-x))
        ans += s-x

print(ans)

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

Solution (Python):
_n, x = map(int, input().split())
boxes = list(map(int, input().split()))
cnt = 0
for idx, _b  in enumerate(boxes):
  if idx == 0:
    continue
  else:
    if boxes[idx] + boxes[idx - 1] >= x:
      sum = boxes[idx] + boxes[idx - 1]
      if boxes[idx] - (sum - x) >= 0:
        boxes[idx] -= sum - x
      else:
        boxes[idx] = 0
      cnt += sum - x
    else:
      continue

print(cnt)

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

Solution (Python):

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

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

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

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

print(answer)

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

import sys
sys.setrecursionlimit(10**9)
INF=10**18
MOD=10**9+7
input=lambda: sys.stdin.readline().rstrip()
YesNo=lambda b: bool([print('Yes')] if b else print('No'))
YESNO=lambda b: bool([print('YES')] if b else print('NO'))
int1=lambda x:int(x)-1

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

if __name__ == '__main__':
    main()

Solution (Python):
import sys

stdin = sys.stdin
inf = 1 << 60
mod = 1000000007

sys.setrecursionlimit(10 ** 7)

ni      = lambda: int(ns())
nin     = lambda y: [ni() for _ in range(y)]
na      = lambda: list(map(int, stdin.readline().split()))
nan     = lambda y: [na() for _ in range(y)]
nf      = lambda: float(ns())
nfn     = lambda y: [nf() for _ in range(y)]
nfa     = lambda: list(map(float, stdin.readline().split()))
nfan    = lambda y: [nfa() for _ in range(y)]
ns      = lambda: stdin.readline().rstrip()
nsn     = lambda y: [ns() for _ in range(y)]
ncl     = lambda y: [list(ns()) for _ in range(y)]
nas     = lambda: stdin.readline().split()

n, x = na()
a = na()

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

print(ans)

Solution (Python):


import sys






























def main():
    mod = 1000000007                
    inf = float('inf')              
    
    sys.setrecursionlimit(10**6)    
    def input(): return sys.stdin.readline().rstrip()
    def ii():    return int(input())
    def mi():    return map(int, input().split())
    def mi_0():  return map(lambda x: int(x)-1, input().split())
    def lmi():   return list(map(int, input().split()))
    def lmi_0(): return list(map(lambda x: int(x)-1, input().split()))
    def li():    return list(input())
    
    
    n, x = mi()
    L = lmi()

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

if __name__ == "__main__":
    main()

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

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

Solution (Python):
N, x = map(int, input().split())
A = list(map(int, input().split()))
pre_a = 0
ans = 0
for a in A:
  if a > x - pre_a:
    ans += a - (x - pre_a)
    pre_a = x - pre_a
  else:
    pre_a = a

print(ans)

Solution (Python):


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

Solution (Python):
import sys

N, x = map(int, sys.stdin.readline().strip().split())
A = list(map(int, sys.stdin.readline().strip().split()))

ans = 0
tmp_sum = A[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] = 0 if A[i+1] < diff else A[i+1] - diff

print(ans)

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

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

print(ans)

Solution (Python):
import sys

def resolve():
    readline=sys.stdin.readline
    n,x=map(int, readline().rstrip().split())
    arr=list(map(int, readline().rstrip().split()))
    ans=0
    for i in range(1,len(arr)):
        xx=arr[i-1]+arr[i]
        if xx > x:
            arr[i]-=(xx-x)
            ans+=(xx-x)
            if arr[i]<0:
                arr[i-1]+=arr[i]
                arr[i]=0
    print(ans)
    return

if 'doTest' not in globals():
    resolve()
    sys.exit()

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

print(ans)

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

Solution (Python):
def eat(a, b):
    e = A[a] + A[b] - X
    if 0 < e <= A[b]:
        A[b] -= e
    elif e > A[b]:
        e -= A[b]
        A[b] = 0
        A[a] -= e

N, X = map(int, input().split())
A = list(map(int, input().split()))
before_eat = sum(A)
eat(-1, -2)
for i in range(N - 1):
    eat(i, i + 1)
print(before_eat - sum(A))

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

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

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

nec = [0 for i in range(n)]
fi = max(-x+(a[0]+a[1]),0)
nec[1] = max(0,a[1]-fi)
for i in range(2,n):
    sec = max(-x+(a[i]+nec[i-1]),0)
    nec[i]=a[i]-sec
    fi += sec

print(fi)

Solution (Python):
def main():
    n, x, *a = map(int, open(0).read().split())
    a = [0] + a
    ans = 0
    tmp = 0
    for i, j in zip(a, a[1:]):
        i -= tmp
        if (i + j - x) > 0:
            c = (i + j - x)
            ans += c
            tmp = c
        else:
            tmp = 0

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    n, x, *a = map(int, open(0).read().split())
    a = [0] + a
    ans = 0
    tmp = 0
    for i, j in zip(a, a[1:]):
        i -= tmp
        if i + j - x > 0:
            c = i + j - x
            ans += c
            tmp = c
        else:
            tmp = 0

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def main():
    n, x, *a = map(int, open(0).read().split())
    a = [0] + a
    ans = 0
    tmp = 0
    for i, j in zip(a, a[1:]):
        i -= tmp
        if i + j > x:
            c = (i + j) - x
            ans += c
            tmp = c
        else:
            tmp = 0

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]

from collections import defaultdict, deque
from sys import exit
import heapq
import math
import copy
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)

N, X = getNM()
A = getList()
ans = 0
flag = True
while flag:
    flag = False
    for i in range(N - 2):
        minus1 = A[i] + A[i + 1] - X
        minus2 = A[i + 1] + A[i + 2] - X
        if minus1 > 0 and minus2 > 0:
            minuspoint = min(minus1, minus2, A[i + 1])
            A[i + 1] -= minuspoint
            ans += minuspoint
            flag = True
between = [A[i] + A[i + 1] - X for i in range(N - 1)]
for i in between:
    if i > 0:
        ans += i
print(ans)

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

print(min(ans,tmp))

Solution (Python):
N,x = map(int,input().split())
a=list(map(int,input().split()))
cnt=0
b=[0]*N


b[0]=a[0] if a[0]<=x else x
cnt+= a[0]-b[0]

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

print(cnt)

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

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

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


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

Solution (Python):
def eat(A,x):
    n = len(A)
    pt = 1
    output = 0
    while pt < n:
        if A[pt-1] + A[pt] > x:
            tmp = A[pt]
            A[pt] = max(0,x-A[pt-1])
            output += (tmp-A[pt])
        pt += 1
    if A[0] + A[1] > x:
        tmp = A[0]
        A[0] = max(0,x-A[1])
        output += (tmp-A[0])
    return output

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

    ans = eat(a,x)

    print(ans)

if __name__ == "__main__":
    main()

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

op=0

for i in range(N-1):
    s=a[i]+a[i+1]
    opc=max(0,s-x)
    a[i+1]=max(0,a[i+1]-opc)
    op+=opc

print(op)

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

Solution (Python):
import sys
import itertools
sys.setrecursionlimit(1000000000)
from heapq import heapify,heappop,heappush,heappushpop
import math
import collections
MOD = 10**9+7

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

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:
        y = s-x
        if A[i] > y:
            A[i] -= y
            A[i-1] -= y
            ans += y
        else:
            z = y - A[i]
            A[i] = 0
            A[i-1] -= A[i]
            A[i-1] -= z
            if i >= 2:
                A[i-2] -= z
            ans += y
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:
        ans+=a[i]+a[i+1]-x
        if a[i]+a[i+1]-x<=a[i+1]:
            a[i+1]-=a[i]+a[i+1]-x
        else:
            a[i]=x
            a[i+1]=0
print(ans)

Solution (Python):
N, x = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
i = 0
while i < N-1:
  if A[i]+A[i+1] > x:
    d = A[i]+A[i+1]-x
    ans += d
    A[i+1] = 0 if A[i+1] < d else A[i+1]-d
  i += 1
print(ans)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    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:
            res += A[i] + A[i - 1] - x
            A[i] -= A[i] + A[i - 1] - x

    print(res)


if __name__ == '__main__':
    resolve()

Solution (Python):
N, x = map(int, input().split())
lst = [int(x) for x in input().split()]
total = sum(lst)
for i in range(N - 1):
    if lst[i] + lst[i + 1] > x:
        lst[i + 1] = lst[i + 1] - min(max(lst[i] + lst[i + 1] - x, 0), lst[i + 1])
    if lst[i] + lst[i + 1] > x:
        lst[i] = lst[i] - min(max(lst[i] + lst[i + 1] - x, 0), lst[i])
print(total - sum(lst))

Solution (Python):
n,x = map(int,input().split())
A = list(map(int,input().split()))
Arev = A[::-1]
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:
        c = A[i] + A[i-1] - x
        A[i] -= c
        ans += c

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

print(min(ans, ans2))

Solution (Python):
printn = lambda x: print(x,end='')
inn = lambda : int(input())
inl   = lambda: list(map(int, input().split()))
inm   = lambda:      map(int, input().split())
ins = lambda : input().strip()
DBG = True 
BIG = 10**18
R = 10**9 + 7

def ddprint(x):
  if DBG:
    print(x)

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

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

import sys

def iterate_tokens():
    for line in sys.stdin:
        for word in line.split():
            yield word


def solve(N: int, x: int):
    tokens = iterate_tokens()
    a = int(next(tokens))
    s = 0
    for _ in range(N-1):
        new = int(next(tokens))
        if a + new > x:
            s += a + new - x
            new -= min(new, a + new - x)
        a = new
    print(s)
    return



def main():
    tokens = iterate_tokens()
    N = int(next(tokens))  
    x = int(next(tokens))  
    solve(N, x)

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

print(ans)

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

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






count = 0

for i in range(1, N-1):
    if As[i-1]+As[i] > x and As[i]+As[i+1] > x:
        minus = min(As[i], As[i-1]+As[i]-x, As[i]+As[i+1]-x)
        As[i] -= minus
        count += minus


if As[0]+As[1] > x:
    minus = min(As[0], As[0]+As[1]-x)
    As[0] -= minus
    count += minus

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

if As[N-2]+As[N-1] > x:
    minus = min(As[N-1], As[N-2]+As[N-1]-x)
    As[N-1] -= minus
    count += minus

print(count)