Problem:
 Score : 300 points    Problem Statement There are N boxes arranged in a row.
Initially, the i -th box from the left contains a_i candies.  Snuke can perform the following operation any number of times:   Choose a box containing at least one candy, and eat one of the candies in the chosen box.   His objective is as follows:   Any two neighboring boxes contain at most x candies in total.   Find the minimum number of operations required to achieve the objective.      Constraints  2 ≤ N ≤ 10^5  0 ≤ a_i ≤ 10^9  0 ≤ x ≤ 10^9         Input The input is given from Standard Input in the following format:  N  x  a_1  a_2  ...  a_N       Output Print the minimum number of operations required to achieve the objective.        Sample Input 1 3 3
2 2 2      Sample Output 1 1  Eat one candy in the second box.
Then, the number of candies in each box becomes (2, 1, 2) .       Sample Input 2 6 1
1 6 1 2 0 4      Sample Output 2 11  For example, eat six candies in the second box, two in the fourth box, and three in the sixth box.
Then, the number of candies in each box becomes (1, 0, 1, 0, 0, 1) .       Sample Input 3 5 9
3 1 4 1 5      Sample Output 3 0  The objective is already achieved without performing operations.       Sample Input 4 2 0
5 5      Sample Output 4 10  All the candies need to be eaten.

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

eat = 0
for i in range(1, N):
    cur = candies[i] + candies[i - 1]
    if cur <= x:
        continue
    eat_i = cur - x
    c = min(eat_i, candies[i])
    candies[i] -= c
    eat += eat_i
print(eat)

Solution (Python):
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

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

if x ==0:
    print(sum(a))
    exit()

ans = 0

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

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

print(ans)

Solution (Python):




def main():
    N, X = map(int, input().split())
    A = list(map(int, input().split())) + [0]

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


if __name__ == "__main__":
    main()

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

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

print(count)

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

def SUM(a):
    L=len(a)
    ans=0
    for i in range(L):
        ans+=a[i]
    return ans

def check(a):
    if all(A[i-1]+A[i]<=x for i in range(1,N)):
        return 1
    else:
        return 0

if check(A)==1:
    print(0)
    exit()

SA=SUM(A)
count=0

for i in range(N):
    if A[i]>x:
        A[i]=x

count=SA-SUM(A)

if check(A)==1:
    print(count)
    exit()
else:
    count=0

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

count=SA-SUM(A)

print(count)

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

Solution (Python):
import io

nim,mike = map(int,input().split())
kite = list(map(int,input().split()))
counter1 = 0
result = 0
for i in kite:
  highest = max(0,counter1+i-mike)
  counter1 = i - highest
  result+= highest
print(result)

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

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

print(ans)

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

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

print(ans)

Solution (Python):
N, x = map(int, input().split())
A = input().split()
alist = []
count = 0

for i in range(N):
    a = int(A[i])
    alist.append(a)

for ii in range(N - 1):
    if alist[int(ii)] + alist[int(ii) + 1] <= x:
        pass
    else:
        while alist[int(ii)] + alist[int(ii) + 1] > x:
            if alist[int(ii) + 1] > 0:
                k = alist[int(ii)] + alist[int(ii) + 1] - x
                if k <= alist[int(ii) + 1]:
                    alist[int(ii) + 1] -= k
                    count += k
                else:
                    alist[int(ii)] -= k - alist[int(ii) + 1]
                    alist[int(ii) + 1] = 0
                    count += k
            
print(count)

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

Solution (Python):

import math
import heapq
import bisect
import numpy as np
from collections import Counter, deque
import itertools


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

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

print(ans)

Solution (Python):

from copy import deepcopy
n, x = map(int, input().split()) 
box = [int(i) for i in input().split()] 
org = deepcopy(box)


for i in range(n - 1):
    if box[i] + box[i + 1] > x:
        t = max(x - box[i], 0)
        box[i + 1] = t
        if box[i] > x:
            box[i] = x
ans = 0
for i, j in zip(box, org):
    ans += (j - 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):
    if A[i] + A[i-1] > x:
        if A[i-1] > x:
            ans += A[i-1] - x
            A[i-1] = x
        ans += A[i] + A[i-1] - x
        A[i] = x - A[i-1]
print(ans)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
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 LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n,x=MI()
    aa=LI()
    ans=0
    for i in range(n-1):
        cur=max(aa[i]+aa[i+1]-x,0)
        ans+=cur
        aa[i+1]-=min(cur,aa[i+1])
    print(ans)

main()

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

result = 0

if x == 0:
  result = sum(li)
  pass
else:
  for i in range(N-1):
    S = li[i] + li[i+1]
    if S <= x:
      pass
    else:
      eat = S - x
      result += eat
      if eat <= li[i+1]:
        li[i+1] = li[i+1] - eat
      else:
        li[i+1] = 0

print(result)

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

def solve(N,x,A):
    ans = max(A[0]-x,0)
    A[0] -= ans
    for i in range(1,N):
        p = max(A[i]-(x-A[i-1]),0)
        A[i] -= p
        ans += p
    return ans
print(solve(N,x,A))

Solution (Python):
from copy import copy


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

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

a = copy(A)
ans2 = 0
for i in range(N-1):
    if a[N-i-1] + a[N-i-2] > x:
        surplus = a[N-i-1] + a[N-i-2] - x
        a[N-i-2] -= min(a[N-i-2], surplus)
        ans2 += surplus

print(min(ans1, ans2))

Solution (Python):
import sys

n, x, *a = map(int, sys.stdin.read().split())

def main():
  cnt = 0
  lim = x
  for c in a:
    if c > lim:
      cnt += c - lim
      lim = x - lim
    else:
      lim = x - c
  print(cnt)

if __name__ ==  '__main__':
  main()

Solution (Python):
import sys

n, x, *a = map(int, sys.stdin.read().split())
a = [0] + a

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

if __name__ ==  '__main__':
  main()

Solution (Python):
import sys

n, x, *a = map(int, sys.stdin.read().split())
a = [0] + a

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

if __name__ ==  '__main__':
    main()

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

ans=0

if A[0] > x:
    ans += A[0]-x
    A[0]=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 collections
import sys

N, x = map(int, input().split())
A_lis = list(map(int, input().split()))
A_q = collections.deque(A_lis)

sys.setrecursionlimit(10 ** 9)

con = 0
tmp1 = A_q.popleft()
for i in range(N-1):
    tmp2 = A_q.popleft()
    if tmp1 + tmp2 > x:
        con += int((tmp1 + tmp2) - x)
        tmp1 = max(0, x - tmp1)
    else:
        tmp1 = tmp2

print(con)

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

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

Solution (Python):










"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""

""" n! mod m 階乗
mod = 1e9 + 7
N = 10000000
fac = [0] * N
def ini():
  fac[0] = 1 % mod
  for i in range(1,N):
    fac[i] = fac[i-1] * i % mod"""

"""mod = 1e9+7
N = 10000000
pw = [0] * N
def ini(c):
  pw[0] = 1 % mod
  for i in range(1,N):
    pw[i] = pw[i-1] * c % mod"""

"""
def YEILD():
  yield 'one'
  yield 'two'
  yield 'three'
generator = YEILD()
print(next(generator))
print(next(generator))
print(next(generator))
"""
"""def gcd_(a,b):
  if b == 0:
    return a
  return gcd_(a,a % b) 
"""def extgcd(a,b,x,y):
  d = a
  if b!=0:
    d = extgcd(b,a%b,y,x)
    y -= (a//b) * x
    print(x,y)
  else:
    x = 1
    y = 0
  return d"""


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

def main():
  n,x = readInts()
  A = readInts()
  ans = 0
  i = 0
  if sum(A[i:i+2]) > x:
    sum_ = sum(A[i:i+2])
    
    
    if A[i+1] - (sum_ - x) < 0:
      A[i+1] = 0
      ans += A[i+1]
      nya = sum_ - x
      A[i] -= nya
      ans += nya
    else:
      nya = sum_ - x
      A[i+1] -= nya
      ans += nya
      
  for i in range(1,n-1):
    if sum(A[i:i+2]) > x:
      sum_ = sum(A[i:i+2])
      nya = sum_ - x
      A[i+1] -=nya
      ans += nya
  print(ans)
  
if __name__ == '__main__':
  main()

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

ans = 0
for i in range(n-1):
    target = a_ls[i:i+2]
    eat = sum(target) - x
    if eat > 0:
        ans += eat
        if eat < target[1]:
            target[1] -= eat
        else:
            rest = eat - target[1]
            target[1] = 0
            target[0] -= rest
    a_ls[i:i+2] = target
print(ans)

Solution (Python):
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from fractions import gcd
from math import factorial, sqrt
from functools import lru_cache, reduce
INF = 1 << 60
mod = 1000000007
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline


class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())


def dijkstra_heap(s, edge, n):
    
    d = [10**20] * n
    used = [True] * n 
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d


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 lcm(x, y):
    return (x * y) // gcd(x, y)


def lcm_list(numbers):
    return reduce(lcm, numbers, 1)


def gcd_list(numbers):
    return reduce(gcd, numbers)


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

Solution (Python):

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

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

print(cdy)

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

Solution (Python):
N, x = map(int, input().split())
a=list(map(int, input().split()))
count = 0
for i in range(N):
    if a[i]>x:
        count+=a[i]-x
        a[i]=x
count1=count
a1=a.copy()
for i in range(0, N, 2):
    ai=a1[i]
    if i==0:
        a1[i]=min(ai, x-a1[i+1])
    elif i==N-1:
        a1[i]=min(ai, x-a1[i-1])
    else:
        a1[i]=min(x-a1[i-1], x-a1[i+1], ai)
    count1+=ai-a1[i]

count2=count
a2=a.copy()
for i in range(1, N, 2):
    ai=a2[i]
    if i==0:
        a[i]=min(ai, x-a2[i+1])
    elif i==N-1:
        a2[i]=min(x-a2[i-1], ai)
    else:
        a2[i]=min(x-a2[i-1], x-a2[i+1], ai)
    count2+=ai-a2[i]

count3=count
prev=a[0]
for i in range(1, N):
    ai=a[i]
    if x-prev>=0 and x-prev<ai:
        a[i]=x-prev
    count3+=ai-a[i]
    prev=a[i]
print(count3)

Solution (Python):
N, x = map(int, input().split())
a=list(map(int, input().split()))
count = 0
for i in range(N):
    if a[i]>x:
        count+=a[i]-x
        a[i]=x
count1=count
a1=a.copy()
for i in range(0, N, 2):
    ai=a1[i]
    if i==0:
        a1[i]=min(ai, x-a1[i+1])
    elif i==N-1:
        a1[i]=min(ai, x-a1[i-1])
    else:
        a1[i]=min(x-a1[i-1], x-a1[i+1], ai)
    count1+=ai-a1[i]

count2=count
a2=a.copy()
for i in range(1, N, 2):
    ai=a2[i]
    if i==0:
        a[i]=min(ai, x-a2[i+1])
    elif i==N-1:
        a2[i]=min(x-a2[i-1], ai)
    else:
        a2[i]=min(x-a2[i-1], x-a2[i+1], ai)
    count2+=ai-a2[i]

count3=count
prev=a[0]
for i in range(1, N):
    ai=a[i]
    if x-prev>=0 and x-prev<ai:
        a[i]=x-prev
    count3+=ai-a[i]
    prev=a[i]
print(min(count1, count2, count3))

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

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

if x < t[0]:
    count = t[0]-x
    t[0] = x
else:
    count = 0

for i in range(1,n):
    if x < t[i]+t[i-1]:
        count += -x+t[i-1]+t[i]
        t[i] = x-t[i-1]
print(count)

Solution (Python):
X = [int(e) for e in input().split()]
candies = [int(f) for f in input().split()]
i=1
ans=0
t = 0
while i<X[0]:
    if(candies[i-1]+candies[i]) > X[1] :
        t = (candies[i-1]+candies[i]) - X[1]
        ans += t
        if(t>=candies[i]):
            candies[i] = 0
        else:
            candies[i] -= t
    i+=1
print(ans)

Solution (Python):
import sys

sys.setrecursionlimit(10 ** 5 + 10)


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


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


resolve()

Solution (Python):
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    
    from itertools import combinations, permutations, accumulate, groupby
    
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    

    
    

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

if __name__ == '__main__':
    main()

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

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

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

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


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

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

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





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

for idx1 in range(1, len(a)):
    if a[idx1 - 1] + a[idx1] > x:
        RC += a[idx1 - 1] + a[idx1] - x
        a[idx1] = x - a[idx1 - 1]

print(RC)

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

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=[int(i) for i in input().split()]
ans=0
if a[0] >x:
  ans += (a[0]-x) 
  a[0] -= (a[0]-x) 
  

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

print(ans)

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

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

print(count)

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

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

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

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

print(count)

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

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

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

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

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

    
print(sum(main()))

Solution (Python):
f=lambda:map(int,input().split());n,x=f();t=a=0
for i in f():t=min(i,x-t);a+=i-t
print(a)

Solution (Python):
f=lambda:map(int,input().split())
n,x=f()
t=a=0
for i in f(): t=min(i,x-t); a+=i-t
print(a)

Solution (Python):
f=lambda:map(int,input().split())
n,x=f()
l=f()
t=a=0
for i in l:
  t=min(i,x-t)
  a+=i-t
print(a)

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

        else:
            a[i] = x - a[i+1]
            a[i+1] = 0
        ans += sa - x
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(n-1):
    if a[i] + a[i+1] > x:
        val = a[i] + a[i+1] - x
        res += val
        a[i+1] -= val

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

print(ans)

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

ans = sum(a)

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

ans -= sum(a)

print(ans)

Solution (Python):

from sys import stdin
from operator import itemgetter
import math


N, x = [int(x) for x in stdin.readline().rstrip().split()]
box = list(map(int,[int(x) for x in stdin.readline().rstrip().split()]))
box.insert(0,0)
ans = 0
for i in range(len(box)-1):
    a = box[i]
    b = box[i+1]
    
    if a+b > x:
        box[i+1] -=  a+b-x
        ans += a+b-x
    else:
        continue

print(ans)

Solution (Python):
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

IN=lambda : map(int, input().split())
mod=1000000007



def main():
	
	num_of_boxes , max_candies = IN()
	candies_array = list(IN())
	total_eat_candies = 0
	pre = 0
	for nc in candies_array:
		num_eat = max(0, nc+pre-max_candies)
		total_eat_candies+=num_eat
		pre = nc - num_eat
	print(total_eat_candies)
	
	
	

isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		
			
	ret = main()
	if ret is not None:
		print(ret)

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

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

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

print(count)

Solution (Python):
n, x = map(int, input().split())
a = 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] -= a[i] + a[i+1] - 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):
    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=list(map(int,input().split()))
if n==2:
  print(max(0,sum(A)-x))
  exit()
if n==3:
  if A[0]+A[1]<=x:
    print(max(0,A[1]+A[2]-x))
  elif A[0]<=x:
    ans=A[1]-x+A[0]
    A[1]=x-A[0]
    print(ans+max(0,A[1]+A[2]-x))
  else:
    ans=A[0]+A[1]-x
    A[1]=0
    A[0]=x
    print(ans+max(0,A[1]+A[2]-x))
  exit()
ans=0
for i in range(n-1):
  if A[i]+A[i+1]<=x:
    pass
  elif A[i]<=x:
    ans=ans+A[i]-x+A[i+1]
    A[i+1]=x-A[i]
  else:
    ans=ans+A[i]+A[i+1]-x
    A[i+1]=0
    A[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(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())
l = list(map(int, input().split()))
 
y = 0
if l[0] > x:
    w = l[0] - x
    y += w
    l[0] -= w
 
for i in range(n-1):
    if l[i] + l[i+1] > x:
        z = l[i+1] + l[i] - x
        y += z
        l[i+1] -= z
 
print(y)

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

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

print(count)

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

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

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

print(cnt)

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

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

print(cnt)

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

y = 0
if l[0] > x:
    w = l[0] - x
    y += w
    l[0] -= w

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

print(y)

Solution (Python):
x,y=list(map(int, input().split()))
z=list(map(int, input().split()))
ans=0
if z[0]>=y:
	ans=ans+z[1]
	z[1]=0
	ans=ans+(z[0]-y)
	z[0]=y

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

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

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

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

print(ans)

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

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

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

print(ans)

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

for i in range(1, N):
    diff = A[i - 1] + A[i] - x
    if diff <= 0:
        continue
    v1 = min(A[i], diff)
    v2 = diff - v1
    A[i] -= v1
    A[i - 1] -= v2
    
ans = sum_A - sum(A)
print(ans)