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())
a = [int(t) for t in input().split()]

ans = 0

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

print(ans)

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

ans = 0
A.insert(0,0)
for i in range(len(A)-1):
  s = A[i] + A[i+1]
  if s > x:
    diff = min(s - x, A[i+1])
    A[i+1] -= diff
    ans += diff
  
print(ans)

Solution (Python):

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

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

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

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

print(sum(map(lambda x: abs(x[0] - x[1]), zip(a, b))))

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

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

print(count)

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

Solution (Python):
import sys

input = sys.stdin.readline


def main():
    N, x = [int(x) for x in input().split()]
    A = [int(x) for x in input().split()]

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

    print(ans)


    
    


if __name__ == '__main__':
    main()

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

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

print(s)

Solution (Python):
import sys
input = sys.stdin.readline
N, x  = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
ans = 0
for i in range(N):
    if a[i] > x:
        ans += a[i] - x
        a[i] = x
for j in range(1, N):
    if a[j] + a[j-1] > x:
        ans += a[j] + a[j-1] - x
        a[j] -= a[j] + a[j-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):
    w=a[i]+a[i+1]
    if w > x:
        if a[i+1] >= w-x:
            a[i+1]-=(w-x)
        else:
            a[i+1]=0
            a[i]-=(w-x-a[i+1])
        ans+=(w-x)
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):
  sum2 = A[i - 1] + A[i]
  if sum2 > x:
    ans += sum2 - x
    A[i] = x - A[i - 1]
    
print(ans)

Solution (Python):
def main():
    n,x=map(int,input().split())
    A=list(map(int,input().split()))
    c=0
    prev = A[0]
    for a in A[1:]:
        remove = max(0, a + prev - x)
        if prev > x:
            prev = 0
        else:
            prev = a - remove
        c += remove
    print(c)
main()

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

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

print(count)

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

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

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

print(count)

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

sum_A = sum(A)

for i in range(1,N):
    B[i] = max(0, A[i]+A[i-1]-K)


for i in range(1,N-1):
    t = min(B[i],B[i+1], A[i])
    A[i]-=t
    B[i]-=t
    B[i+1]-=t

print(sum_A - sum(A) + sum(B))

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

for i in range(n-1):
  tmp = aa[i+1] + aa[i]
  tmp = tmp - x 
  if tmp > 0:
    sa = aa[i+1] - tmp
    if sa >= 0 :
      aa[i+1] -= tmp
    else:
      sa2 = tmp - aa[i+1]
      aa[i+1] = 0
      aa[i] -= sa2 
      
print(sum_aa - sum(aa))

Solution (Python):
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    N, x = LI()
    A = LI()
    cnt = 0
    for i in range(N-1):
        l = A[i]
        r = A[i+1]
        extra = l + r - x
        if extra <= 0:
            continue
        if extra <= r:
            A[i+1] = r - extra
        else:
            r = 0
            l = l - (extra - r)
            A[i+1] = r
            A[i] = l
        cnt += extra
    print(cnt)
main()

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

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

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

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

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

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

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

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

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

Solution (Python):


import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7

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

ans = 0
for i in range(N-1):
    if A[i] + A[i+1] > x:
        need = A[i] + A[i+1] - x
        ans += need
        
        A[i+1] -= min(A[i+1], need)
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-1]+a[i] > x:
        count += (a[i-1]+a[i]) - x
        a[i] = x - a[i-1]
print(count)

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

answer = 0
for i in range(N - 1):
  d = A[i] + A[i+1] - X
  if d <= 0:
    continue
  if d > A[i+1]:
    A[i] -= d - A[i+1]
    A[i+1] = 0
  else:
    A[i+1] -= d
  answer += d


print(answer)

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

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

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

print(count)

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

result = 0
b = max(a[0] - x, 0)
result += b
a[0] -= b
for i in range(1, N):
    b = max(a[i - 1] + a[i] - x, 0)
    result += b
    a[i] -= b
print(result)

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

Solution (Python):
from _collections import deque


def parser():
    while 1:
        data = list(input().split(' '))
        for number in data:
            if len(number) > 0:
                yield (number)


input_parser = parser()


def gw():
    global input_parser
    return next(input_parser)


def gi():
    data = gw()
    return int(data)


MOD = int(1e9 + 7)

import numpy
from collections import deque
from math import sqrt
from math import floor


"""
need to consider the case that ticket is not enough to lower everything
"""

N = gi()
x = gi()
A = [0] * N

for i in range(N):
    A[i] = gi()

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

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

    diff = A[i] + A[i - 1] - x
    if diff > 0:
        cost -= min(diff, A[i - 1])
        A[i - 1] -= cost
        ans += cost
print(ans)

Solution (Python):







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

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

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

print(ans)

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

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


ans = 0
for i in range(len(A)-1):
    if A[i] + A[i+1] > x:
        if A[i] >= x:
            ans += A[i] - x
            ans += A[i+1]
            A[i+1] = 0
        else:
            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
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
input = sys.stdin.readline


def main():
    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
        
    

    temp = a[0] + a[1] - x
    if temp >= 0:
        res += temp
        a[1] -= temp
    

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



    
    print(res)



main()

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

def main():
    N,x = map(int,input().split())
    a = list(map(int,input().split()))
    
    ans = 0
    for i in range(N-1):
        if (a[i+1] + a[i] > x):
            eat = a[i+1]+a[i]-x
            ans += eat
            if eat <= a[i+1]:
                a[i+1] -= eat
            else:
                a[i+1] = 0
                
    print(ans)
    
if __name__ == "__main__":
    main()

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

Solution (Python):
import math
import bisect
import collections
import itertools
def gcd(a,b):return math.gcd 
def lcm(a,b):return (a*b) // math.gcd(a,b) 
def iin(): return int(input()) 
def imn(): return map(int, input().split()) 
def iln(): return list(map(int, input().split())) 
def iln_s(): return sorted(iln()) 
def iln_r(): return sorted(iln(), reverse=True) 
def join(l, s=''): return s.join(l) 
def perm(l, n): return itertools.permutations(l, n) 
def comb(l, n): return itertools.combinations(l, n) 
def 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


N,x = imn()
a = iln()

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

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

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] -= (alist[0] - x)

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

print(ans)

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

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

print(ans)

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

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

print(ans)

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

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


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

print(ans)

Solution (Python):
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):
    sm = A[i] + A[i+1]
    if sm > x:
        diff = sm - x
        A[i+1] -= diff
        cnt += diff

print(cnt)

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

cnt = 0
for i in range(N - 1):
    sm = A[i] + A[i+1]
    if sm > x:
        diff = sm - x
        if A[i+1] >= diff:
            A[i+1] -= diff
        else:
            diff2 = diff - A[i+1]
            A[i+1] = 0
            A[i] -= diff2
        cnt += diff

print(cnt)

Solution (Python):
import copy

N, x = map(int,input().split())
a = list(map(int, input().split()))
a.append(0) 
b = copy.deepcopy(a) 

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

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

ans = 0
for i in range(N): 
    ans += a[i] - b[i]

print(str(ans))

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

s=0
if(A[0]>x):
    s+=A[0]-x
    A[0]=x

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

print(s)

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

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

for i in range(N):
    diff = max(a[i] + prev - x, 0)
    answer += diff
    a[i] -= diff
    prev = a[i]

print(answer)

Solution (Python):

import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf')
ans = 0 ;count = 0 ;pro = 1

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

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

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

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

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

print(cnt)

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

print(cnt)

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

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

print(res)

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

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
            
    return cnt

if __name__ == '__main__':
    ans = main()
    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(len(a)-1):
    if a[i] + a[i+1] > x:
        ans += (a[i+1] + a[i] - x)
        a[i+1] -= (a[i+1] + a[i] - x)
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):
    total = a[i-1] + a[i]
    if total > x:
        
        a[i] -= total - x
        ans += total - x

    
    
    

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 = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
c = 0
for i in range(1, len(a)):
	b = a[i - 1] + a[i]
	d = b - x
	if d > 0:
		c += d
		e = min(a[i], d)
		a[i] -= e
		d -= e
		if d > 0:
			a[i - 1] -= d
print(c)

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

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


tabeta = 0


for n in range(N):
    if a[n] > x:
        
        tabeta += a[n] - x
        
        a[n] = x
        

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

print(tabeta)

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

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

Solution (Python):
import sys
from functools import reduce
import copy
import math
from pprint import pprint
import collections
import bisect
import itertools


sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def int_inputs(num_of_input):
    ins = [int(input()) for i in range(num_of_input)]
    return ins


def solve(inputs):
    [N, X] = string_to_int(inputs[0])
    A = string_to_int(inputs[1])

    count = 0
    for i in range(N-1):
        if A[i] > X:
            count += abs(X - A[i])
            A[i] = X
        if A[i] + A[i+1] >= X:
            count += abs(X - (A[i+1] + A[i]))
            A[i+1] = X - A[i]
    return count


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


if __name__ == "__main__":
    ret = solve(inputs(2))
    print(ret)

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

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

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

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

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

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

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

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

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

cnt, prv = 0, 0

for i in range(N):
    diff = max(arr[i] + prv - x, 0)
    cnt += diff
    arr[i] -= diff
    prv = arr[i]

print(cnt)

Solution (Python):











import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log 
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N,x = MI()
    A=LI()
    ans = 0
   
    for i in range(N-1):
      if A[i]+A[i+1]>x:
        s=A[i]+A[i+1]
        A[i+1]-=min(A[i+1],s-x)
        ans += max(0,s-x)

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

Solution (Python):
from sys import stdin

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

point = 0
for i in range(n-1):
    if li[i]+li[i+1] > x:
        total = li[i]+li[i+1]-x
        if total > li[i+1]:
            point += li[i+1]
            total = total-li[i+1]
            if total > li[i]:
                point += li[i]
                li[i] = 0
                li[i+1] = 0
            else:
                point += total
                li[i] -= total
                li[i+1] = 0
        else:
            point += total
            li[i+1] -= total

print(point)

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

Solution (Python):
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n, x = list(map(int, readline().split()))
    a = list(map(int, readline().split()))
    cnt = 0
    for i in range(n):
        if i == 0:
            if a[i] > x:
                t = abs(x - a[i])
                a[i] -= t
                cnt += t
        else:
            if a[i] + a[i-1] > x:
                t = abs(x - (a[i] + a[i-1]))
                a[i] -= t
                cnt += t
    print(cnt)


if __name__ == '__main__':
    solve()

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

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

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

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