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

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

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

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

Solution (Python):
N,X = map(int,input().split())
A = list(map(int,input().split()))
wa = []
ans = 0

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

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

for j in range(N-2):
  if wa[j] > X:
    ans += wa[j]-X
    wa[j+1] -= wa[j]-X

if wa[N-2] > X:
  ans += wa[N-2]-X

print(ans)

Solution (Python):
import sys

N, X = map(int, input().split())
A = list(map(int, sys.stdin.readline().rsplit()))
B = A[::]

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

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

print(res)

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

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

print(cnt)

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

if __name__ == "__main__":
    main()

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

Solution (Python):

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

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

Solution (Python):
N, x, *A = map(int, open(0).read().split())
ans = 0
for i in range(1, N):
    required = (A[i] + A[i - 1]) - x
    if required <= 0:
        continue
    elif required <= A[i]:
        A[i] -= required
    else:
        A[i] = 0
        rem = required - A[i]
        A[i - 1] -= rem
    ans += required

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

Solution (Python):
N, x = map(int, input().split())
*A, = map(int, input().split())
ans = 0
for i in range(N - 1):
    w = A[i] + A[i + 1]
    if w <= x: continue
    r = max(0, A[i + 1] - (w - x))
    l = x - r
    ans += A[i] - l + A[i + 1] - r
    A[i + 1] = r
print(ans)

Solution (Python):
n, x = map(int, input().split())
A = tuple(map(int, input().split()))
pre = max(0, A[0] - x)
ans = pre

for i in range(n - 1):
    a = A[i]
    b = A[i + 1]
    a -= pre
    tmp = 0
    if b > x:
        tmp += b - x
        b = x

    if a + b > x:
        tmp += a + b - x
    ans += tmp
    pre = tmp
print(ans)

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

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

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

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

Solution (Python):
import math
from math import gcd,pi,sqrt
INF = float("inf")
MOD = 10**9 + 7

import sys
sys.setrecursionlimit(10**6)
import itertools
import bisect
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]


def main():
    N, X = i_map()
    A = i_list()

    ans = [A[0]]
    cnt = 0

    for i,k in enumerate(A[1:]):
        t = k + ans[i]
        if t <= X:
            ans.append(k)
        else:
            if k - (t-X) >= 0:
                cnt += t - X
                ans.append(k - (t-X))
            else:
                cnt += t - X
                ans[-1] += k - (t-X)
                ans.append(0)
    print(cnt)











if __name__=="__main__":
    main()

Solution (Python):

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

Solution (Python):

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

Solution (Python):

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

Solution (Python):
import sys



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


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    
    n, x = input_int_list()
    A = [0] + input_int_list() + [0]
    ans = 0
    
    
    for i in range(n + 1):
        if A[i] + A[i + 1] > x:
            dif = min(A[i + 1], A[i] + A[i + 1] - x)
            ans += dif
            A[i + 1] -= dif

    print(ans)
    return


if __name__ == "__main__":
    main()

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

if A[0] > x:
  A[0] = x
for i in range(1, len(A)):
  if A[i-1] + A[i] > x:
    A[i] = x - A[i-1]
ans = 0
for a, b in zip(A, B):
  ans += b - a
print(ans)

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

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

Solution (Python):
import sys

input = sys.stdin.readline

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

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

count = 0
if a_list[0] > x:
    count += a_list[0] - x
    a_list[0] -= a_list[0] - x
for i in range(len(a_list)):
    if i+1 == len(a_list):
        break
    if a_list[i] + a_list[i+1] > x:
        count += a_list[i] + a_list[i+1] - x
        a_list[i+1] -= a_list[i] + a_list[i+1] - x
        

print(count)

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

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

print(ans)

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

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

Solution (Python):
n, x = map(int, input().split())
A = list(map(int, input().split()))
cnt = [0] * (n - 1)
ans1 = 0
for i in range(n - 1):
    cnt[i] = A[i] + A[i + 1]
    cnt[i] -= x
    cnt[i] = max(cnt[i], 0)
temp_cnt = cnt[::]

for i in range(n - 1):
    if temp_cnt[i] == 0:
        continue
    if i < n - 2:
        ans1 += temp_cnt[i]
        temp_cnt[i + 1] -= min(A[i + 1], temp_cnt[i])
        temp_cnt[i] = 0
        temp_cnt[i + 1] = max(temp_cnt[i + 1], 0)
    
ans1 += temp_cnt[-1]


print(ans1)

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

Solution (Python):
import sys
from sys import exit
from collections import deque
from copy import deepcopy
from bisect import bisect_left, bisect_right, insort_left, insort_right
from heapq import heapify, heappop, heappush
from itertools import product, permutations, combinations, combinations_with_replacement
from functools import reduce
from math import gcd, sin, cos, tan, asin, acos, atan, degrees, radians

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def lgcd(l):
    return reduce(gcd,l)
def llcm(l):
    return reduce(lcm,l)
def powmod(n,i,mod=MOD):
    return pow(n,mod-1+i,mod) if i<0 else pow(n,i,mod)
def div2(x):
    return x.bit_length()
def div10(x):
    return len(str(x))-(x==0)
def intput():
    return int(input())
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x,d=1):
        self.setdefault(x,0)
        self[x] += d
    def list(self):
        l = []
        for k in self:
            l.extend([k]*self[k])
        return l
class comb():
    def __init__(self, n, mod=None):
        self.l = [1]
        self.n = n
        self.mod = mod
    def get(self,k):
        l,n,mod = self.l, self.n, self.mod
        k = n-k if k>n//2 else k
        while len(l)<=k:
            i = len(l)
            l.append(l[i-1]*(n+1-i)//i if mod==None else (l[i-1]*(n+1-i)*powmod(i,-1,mod))%mod)
        return l[k]
def pf(x,mode='counter'):
    C = counter()
    p = 2
    while x>1:
        k = 0
        while x%p==0:
            x //= p
            k += 1
        if k>0:
            C.add(p,k)
        p = p+2-(p==2) if p*p<x else x
    if mode=='counter':
        return C
    S = set([1])
    for k in C:
        T = deepcopy(S)
        for x in T:
            for i in range(1,C[k]+1):
                S.add(x*(k**i))
    if mode=='set':
        return S
    if mode=='list':
        return sorted(list(S))



N,x=mint()
A=lint()
ans=max(0,A[0]+A[1]-x)
A[1]-=min(A[1],ans)
for i in range(2,N):
    tmp=max(0,A[i-1]+A[i]-x)
    A[i]-=min(A[i],tmp)
    ans+=tmp
print(ans)

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

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

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

        if A[i]+A[i+1] > X:
            A[i] -= temp
            cnt += temp

print(cnt)

Solution (Python):

def resolve():
    N, X = map(int, input().split())
    A = list(map(int, input().split()))

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

    print(ans)


if __name__ == "__main__":
    resolve()

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

Solution (Python):

import sys

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


sys.setrecursionlimit(10**7)









INF = 1 << 50
EPS = 1e-8
mod = 10 ** 9 + 7

def count(arr, x):
    ans1 = 0
    pre = 0
    N = len(arr)
    for i in range(1, N):
        val = max(0, arr[i - 1] + arr[i] - pre - x)
        if val > arr[i]:
            pre = arr[i]
        else:
            pre = val
        ans1 += val
    return ans1

def run():
    N, x, *A = map(int, read().split())

    print(min(count(A, x), count(A[::-1], x)))

if __name__ == "__main__":
    run()

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

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

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

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

print(Sum)

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

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

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

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


print(count)

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

if a[1] + a[0] > x and x >= a[0] :
    k = x - a[0]
    cnt += a[1] + a[0] - x

elif a[1] + a[0] > x and x < a[0] :
    k = 0
    cnt += 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 :
        cnt += k + a[i + 1] - x
        k = x - k

    else :
        cnt += k + a[i + 1] - x
        k = 0
    

print(cnt)

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

if a[1] + a[0] > x and x >= a[0] :
    k = x - a[0]
    cnt += a[1] + a[0] - x

elif a[1] + a[0] > x and x < a[0] :
    k = 0
    cnt += 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 :
        cnt += k + a[i + 1] - x
        k = x - k

    else :
        cnt += k + a[i + 1] - x
        k = 0
    

print(cnt)

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

Solution (Python):
N, x = map(int, input().split())
a1 = list(map(int, input().split()))
a2 = a1[::-1]
ans1 = 0
ans2 = 0

for i in range(N-1):
    tmp = a1[i] + a1[i+1] - x
    if tmp <= 0:
        continue
    red = min(a1[i+1], tmp)
    a1[i+1] -= red
    ans1 += red
    if red < tmp:
        a1[i] -= tmp-red
        ans1 += tmp-red
    
for i in range(N-1):
    tmp = a2[i] + a2[i+1] - x
    if tmp <= 0:
        continue
    red = min(a2[i+1], tmp)
    a2[i+1] -= red
    ans2 += red
    if red < tmp:
        a2[i] -= tmp-red
        ans2 += tmp-red
print(min(ans1, ans2))

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

Solution (Python):
import copy

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

a=[int(x) for x in input().split()]
aa=copy.deepcopy(a)

ans=0
for i in range(n):
  if x<a[i]:
    a[i]=x

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

ans=sum(aa[i]-a[i] for i in range(n))
print(ans)

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

left = 0
total = 0
for i in range(N):
    cnt = max(a[i]-(x-left),0)
    total += cnt
    left = a[i]-cnt
print(total)

Solution (Python):
f=lambda:map(int,input().split())
N,X=f()
*A,=f()
A=[0]+A

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

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

B=[a if a<=X else X for a in A]
c=sum(A)-sum(B)
for i in range(N-1):
 t=B[i]+B[i+1]
 if X < t:
    d=t-X
    B[i+1]-=d
    c+=d
print(c)

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

B=[a if a<=X else X for a in A]
c=sum(A)-sum(B)
for i in range(N-1):
 t=B[i]+B[i+1]
 if X < t:
    d=t-X
    B[i+1]-=d
    c+=d
print(c)

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

B=[0]*N
for i in range(N):
    B[i] = A[i] if A[i]<=X else X

cnt=sum(A)-sum(B)    
for i in range(N-1):
    if X < B[i]+B[i+1]:
        d=B[i]+B[i+1]-X
        B[i+1]-=d
        cnt+=d
print(cnt)

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(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()))
asum=[0]*(n-1)
for i in range(n-1):
    asum[i]+=(a[i]+a[i+1])
temp=0
for j in range(n-1):
    if asum[j]>=x:
        if j!=n-2:
            asum[j+1]-=min(a[j+1],(asum[j]-x))
        temp+=(asum[j]-x)
            
print(temp)

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

for i in range(1,n):
  if i==1:
    if a[0]+a[1]>x:
      if a[0]>x:
        a[1]=0
        a[0]=x
      else:
        sa=(a[0]+a[1])-x
        a[1]=a[1]-sa
  else:
    if a[i]+a[i-1]>x:
      sa=(a[i]+a[i-1])-x
      a[i]=a[i]-sa
      
sum2=sum(a)
print(sum1-sum2)

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

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

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

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

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

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

Solution (Python):
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy


import heapq
sys.setrecursionlimit(10**6)

INF = 10**18
import bisect
import statistics
mod = 10**9+7


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

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

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

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

        if A[i+1] < t:
            A[i+1] = 0
        else:
            A[i+1] -= t
        ans += t

print(ans)

Solution (Python):
import sys
from copy import deepcopy
def input(): return sys.stdin.readline().strip()

def main():
    N, x = map(int, input().split())
    A = list(map(int, input().split()))
    if x == 0:
        print(sum(A))
        return

    B = deepcopy(A)
    ans1 = 0
    for i in range(1, N):
        ans1 += max(0, (A[i - 1] + A[i]) - x)
        if A[i] + A[i - 1] > x:
            if A[i] < (A[i - 1] + A[i]) - x:
                A[i] = 0
            else:
                A[i] -= (A[i - 1] + A[i]) - x
    ans2 = 0
    for i in range(N - 2, -1, -1):
        ans2 += max(0, (B[i] + B[i + 1]) - x)
        if B[i] + B[i + 1] > x:
            if B[i] < (B[i] + B[i + 1]) - x:
                B[i] = 0
            else:
                B[i] -= (B[i] + B[i + 1]) - x
    ans = min(ans1, ans2)
    print(ans)



if __name__ == "__main__":
    main()

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

Solution (Python):
N, x = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
for i, a in enumerate(A):
  if i == 0:
    ans += min(a, x)
    now_x = x - min(a, x)
    continue
    
  ans += min(a, now_x)
  now_x = x - min(a, now_x)

ans2 = 0
for i, a in enumerate(A[::-1]):
  if i == 0:
    ans2 += min(a, x)
    now_x = x - min(a, x)
    continue
    
  ans2 += min(a, now_x)
  now_x = x - min(a, now_x)

print(sum(A) - max(ans, ans2))

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

if n!=2 and a[n-2]+a[n-1]>=x:
  sa=a[i]+a[i+1]-x
  ans+=sa
  if sa<=a[n-1]:
    a[n-1]-=sa
  else:
    a[n-2]=sa-a[n-1]
    a[n-1]=0
print(ans)

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



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

print(ans)

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

    print(cnt)

if __name__ == "__main__":
    main()

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

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

Solution (Python):




import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """3 3
2 2 2"""
        output = """1"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """6 1
1 6 1 2 0 4"""
        output = """11"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """5 9
3 1 4 1 5"""
        output = """0"""
        self.assertIO(input, output)

    def test_入力例_4(self):
        input = """2 0
5 5"""
        output = """10"""
        self.assertIO(input, output)


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

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

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

    print(ans)


if __name__ == "__main__":
     
     resolve()

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

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


print(ans)

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

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


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

print(sum_arr - sum(arr))

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

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

Solution (Python):


n, x = map(int, raw_input().split())
ais = map(int, raw_input().split())


q= sum([ai - min(x,ai) for ai in ais])

ais = [min(x,ai) for ai in ais]

for ii in range(1,len(ais)):
	if ais[ii] + ais[ii-1] > x:
		q += ais[ii] + ais[ii-1] - x
		ais[ii] -= ais[ii] + ais[ii-1] - x
print q

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

A_sum=sum(A)
A[0]=min(X,A[0])

for i in range(N-1):
    if A[i]+A[i+1]>X:
        A[i+1]=X-A[i]
print(A_sum-sum(A))

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