CodeEff/ECCO huggingface dataset:
# Original solution
import sys
input=sys.stdin.readline
n,q=list(map(int,input().split()))
s=input().rstrip()
td=[]
for _ in range(q):
	t,d=input().split()
	td.append((t,d))
l=-1;r=n+1
while r-l>1:#l以上r未満
	mid=(l+r)//2
	now=mid
	if not 0<=now<n:
		if now==-1:
			l=mid
		else:
			r=mid
		break
	for i in range(q):
		if s[now]==td[i][0]:
			if td[i][1]=="L":
				now-=1
			else:
				now+=1
		if not 0<=now<n:
			f=0
			break
	if now==-1:
		l=mid
	else:
		r=mid
Left=l
l=-2;r=n
while r-l>1:#l over r以下
	mid=(l+r)//2
	now=mid
	if not 0<=now<n:
		if now==n:
			r=mid
		else:
			l=mid
		break
	for i in range(q):
		if s[now]==td[i][0]:
			if td[i][1]=="L":
				now-=1
			else:
				now+=1
		if not 0<=now<n:
			f=0
			break
	if now==n:
		r=mid
	else:
		l=mid
Right=r#Right,Leftはどこまで排除されるか
print((Right-Left-1))

# Optimized solution
import sys
input=sys.stdin.readline
n,q=list(map(int,input().split()))
s=input().rstrip()
td=[]
for _ in range(q):
	t,d=input().split()
	td.append((t,d))
l=-1;r=n+1
while r-l>1:#l以上r未満
	mid=(l+r)//2
	now=mid
	if not 0<=now<n:
		if now==-1:
			l=mid
		else:
			r=mid
		continue
	for i in range(q):
		if s[now]==td[i][0]:
			if td[i][1]=="L":
				now-=1
			else:
				now+=1
		if not 0<=now<n:
			f=0
			break
	if now==-1:
		l=mid
	else:
		r=mid
Left=l
l=-2;r=n
while r-l>1:#l over r以下
	mid=(l+r)//2
	now=mid
	if not 0<=now<n:
		if now==n:
			r=mid
		else:
			l=mid
		continue
	for i in range(q):
		if s[now]==td[i][0]:
			if td[i][1]=="L":
				now-=1
			else:
				now+=1
		if not 0<=now<n:
			f=0
			break
	if now==n:
		r=mid
	else:
		l=mid
Right=r#Right,Leftはどこまで排除されるか
print((Right-Left-1))
# Original solution
import sys
from collections import defaultdict
from string import ascii_uppercase
stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N,Q = read_int()
s = read_str()
td = [list(read_str().split()) for _ in range(Q)]
d = defaultdict(list)
for a in ascii_uppercase:
    d[a] = []
for i in range(N):
    d[s[i]].append(i)
d = dict(d)
lr = {"L":-1, "R":1}

dp = [1 for _ in range(N)]
def solve():
    for i in range(Q):
        char = td[i][0]
        if not d[char]:
            continue

        move = lr[td[i][1]]
        plus = []
        minus = []
        for cur in d[char]:
            if dp[cur] < 1:
                continue
            if cur + move >= 0 and cur + move < N:
                plus.append([cur + move, dp[cur]])
            minus.append([cur, dp[cur]])
        while plus:
            p = plus.pop()
            dp[p[0]] += p[1]
        while minus:
            p = minus.pop()
            dp[p[0]] -= p[1]

if __name__ == "__main__":
    solve()
    print((sum(dp)))

# Optimized solution
import sys
from collections import defaultdict
from string import ascii_uppercase
stdin = sys.stdin
read_int = lambda : list(map(int,stdin.readline().split()))
read_str = lambda : stdin.readline().rstrip()

N,Q = read_int()
s = read_str()
td = [list(read_str().split()) for _ in range(Q)]
lr = {"L":-1, "R":1}


def solve():
    # left
    lb = -1
    ub = N
    lefr = (lb + ub) // 2
    while ub - lb > 1:
        lefr = (lb + ub) // 2
        md = lefr
        over = False
        for i in range(Q):
            t, d = td[i]
            if t != s[md]:
                continue
            md += lr[d]
            if md > N - 1:
                over = True
                break
            elif md < 0:
                break
        if over:
            ub = lefr
        else:
            lb = lefr
    l = ub
	
    # right
    lb = -1
    ub = N
    refl = (lb + ub) // 2
    while ub - lb > 1:
        refl = (lb + ub) // 2
        md = refl
        over = False
        for i in range(Q):
            t, d = td[i]
            if t != s[md]:
                continue
            md += lr[d]
            if md < 0:
                over = True
                break
            elif md > N - 1:
                break
        if over:
            lb = refl
        else:
            ub = refl
    r = lb
    res = l - r - 1
    return res

if __name__ == "__main__":
    print((solve()))
# Original solution
DEBUG = False


def get_pos(index):
    """初期位置indexのロボットのクエリ後の配置を返す
    is_whick_sideの返り値として返す"""
    if DEBUG: print()
    iws = is_whick_side(index)
    if iws != 0:
        if DEBUG: print(iws)
        return iws
    for t, d in e:
        if DEBUG: print(index, s[index], t, d, end='')
        if t == s[index]:
            if d == 'R':
                index += 1
            else:
                index -= 1
        if DEBUG: print('->', index, s[index] if 0 <= index < N else '==Removed')
        iws = is_whick_side(index)
        if iws != 0:
            if DEBUG: print(iws)
            return iws
    if DEBUG: print(0)
    return 0


def is_whick_side(index):
    if index == -1:
        return -1
        # 左消滅
    elif index == N:
        return 1
        # 右消滅
    else:
        return 0
        # 非消滅
    # 二分探索中に左右どちらから消滅したか把握しておかないと
    # midの初期値が消滅だった場合に
    # indexの-1側,N側どちら側に次の探索範囲を設ければよいのか判断できない


N, Q = map(int, input().split())
s = input()

e = [input().split() for _ in range(Q)]
# t: 文字 <- siと比較
# d: 'L', 'R'

# right
left = -1
right = N
while right - left > 1:
    mid = (left + right) // 2
    if get_pos(mid) == 1:
        # 右消滅
        right = mid
    else:
        left = mid
    if DEBUG: print('lr', left, right)
ar = right
if DEBUG: print('right', ar)
# 右消滅開始位置

# left
left = -1
right = N
while right - left > 1:
    mid = (left + right) // 2
    if get_pos(mid) == -1:
        left = mid
    else:
        right = mid
    if DEBUG: print('lr', left, right)
al = left
if DEBUG: print('left', al)
# 左消滅開始位置
# (al, ar) = 残存区間

print((ar - 1) - (al + 1) + 1)


# Optimized solution
DEBUG = False


def get_pos(index):
    """初期位置indexのロボットのクエリ後の配置を返す
    is_whick_sideの返り値として返す"""
    if DEBUG: print()
    iws = -1 if index == -1 else 1 if index == N else 0
    if iws != 0:
        if DEBUG: print(iws)
        return iws
    for t, d in e:
        if DEBUG: print(index, s[index], t, d, end='')
        if t == s[index]:
            if d == 'R':
                index += 1
            else:
                index -= 1
        if DEBUG: print('->', index, s[index] if 0 <= index < N else '==Removed')
        iws = -1 if index == -1 else 1 if index == N else 0
        if iws != 0:
            if DEBUG: print(iws)
            return iws
    if DEBUG: print(0)
    return 0


N, Q = map(int, input().split())
s = input()

e = [input().split() for _ in range(Q)]
# t: 文字 <- siと比較
# d: 'L', 'R'

# right
left = -1
right = N
while right - left > 1:
    mid = (left + right) // 2
    if get_pos(mid) == 1:
        # 右消滅
        right = mid
    else:
        left = mid
    if DEBUG: print('lr', left, right)
ar = right
if DEBUG: print('right', ar)
# 右消滅開始位置

# left
left = -1
right = N
while right - left > 1:
    mid = (left + right) // 2
    if get_pos(mid) == -1:
        left = mid
    else:
        right = mid
    if DEBUG: print('lr', left, right)
al = left
if DEBUG: print('left', al)
# 左消滅開始位置
# (al, ar) = 残存区間

print((ar - 1) - (al + 1) + 1)
# Original solution
import sys
input = sys.stdin.readline
import bisect
sys.setrecursionlimit(10**8)

N,Q=list(map(int,input().split()))
S=input().strip()
S=[ord(s)-65 for s in S]
M=[input().split() for i in range(Q)]

MLIST=[[] for i in range(26)]

for i in range(Q):
    m,lr=M[i][0],M[i][1]
    MLIST[ord(m)-65].append(i)

dist=dict()
def distinct(PLACE,TURN):
    #print(PLACE,TURN)

    x=bisect.bisect_left(MLIST[S[PLACE]],TURN)

    if x==len(MLIST[S[PLACE]]):
        #dist[(PLACE,TURN)]=1
        return 1

    TURN=MLIST[S[PLACE]][x]

    if dist.get((PLACE,TURN))!=None:
        return dist.get((PLACE,TURN))
    
    if PLACE==0 and M[TURN][1]=="L":
        dist[(PLACE,TURN)]=0
        return 0
    
    if PLACE==N-1 and M[TURN][1]=="R":
        dist[(PLACE,TURN)]=0
        return 0

    if M[TURN][1]=="L":
        dist[(PLACE,TURN)]=distinct(PLACE-1,TURN+1)
        return dist[(PLACE,TURN)]

    if M[TURN][1]=="R":
        dist[(PLACE,TURN)]=distinct(PLACE+1,TURN+1)
        return dist[(PLACE,TURN)]

ANS=0
for i in range(N):
    ANS+=distinct(i,0)
    #print(i,dist)

print(ANS)
    

# Optimized solution
import sys
input = sys.stdin.readline

N,Q=list(map(int,input().split()))
S=input().strip()
M=[input().split() for i in range(Q)]

PLACE=-1

for i in range(Q-1,-1,-1):

    if PLACE+1<N and M[i][0]==S[PLACE+1] and M[i][1]=="L":
        PLACE+=1

    elif PLACE>=0 and M[i][0]==S[PLACE] and M[i][1]=="R":
        PLACE-=1

    if PLACE==N:
        print((0))
        sys.exit()

PLACE2=N

for i in range(Q-1,-1,-1):

    if PLACE2-1>=0 and M[i][0]==S[PLACE2-1] and M[i][1]=="R":
        PLACE2-=1

    elif PLACE2<=N-1 and M[i][0]==S[PLACE2] and M[i][1]=="L":
        PLACE2+=1

    if PLACE==-1:
        print((0))
        sys.exit()

print((max(0,N-(PLACE+1+(N-PLACE2)))))
# Original solution
def main():
    def solve(mid,g):
        for t,d in zip(x,y):
            if s[mid]==t:
                mid-=(d<'R')or-1
            if mid==g:
                return 1
        return 0
    n,q,s,*z=open(0).read().split()
    x,y=z[::2],z[1::2]
    n,q=int(n),int(q)
    s=' %s '%s
    ok=0
    ng=n+1
    while abs(ok-ng)>1:
        mid=(ok+ng)//2
        if solve(mid,0):
            ok=mid
        else:
            ng=mid
    a=ok
    ok=n+1
    ng=0
    while abs(ok-ng)>1:
        mid=(ok+ng)//2
        if solve(mid,n+1):
            ok=mid
        else:
            ng=mid
    print((max(0,ok-a-1)))
if __name__=='__main__':
    main()

# Optimized solution
def main():
    def solve(mid,g):
        for t,d in zip(x,y):
            if s[mid]==t:
                mid-=(d<'R')or-1
        return mid==g
    n,q,s,*z=open(0).read().split()
    x,y=z[::2],z[1::2]
    n,q=int(n),int(q)
    s=' %s '%s
    ok=0
    ng=n+1
    while abs(ok-ng)>1:
        mid=(ok+ng)//2
        if solve(mid,0):
            ok=mid
        else:
            ng=mid
    a=ok
    ok=n+1
    ng=0
    while abs(ok-ng)>1:
        mid=(ok+ng)//2
        if solve(mid,n+1):
            ok=mid
        else:
            ng=mid
    print((max(0,ok-a-1)))
if __name__=='__main__':
    main()
# Original solution
def main():
    def solve(mid,g):
        for t,d in zip(x,y):
            if s[mid]==t:
                mid-=(d<'R')or-1
                if mid==g:
                    return 1
        return 0
    n,q,s,*z=open(0).read().split()
    x,y=z[::2],z[1::2]
    n,q=int(n),int(q)
    s=' %s '%s
    ok=0
    ng=n+1
    while abs(ok-ng)>1:
        mid=(ok+ng)//2
        if solve(mid,0):
            ok=mid
        else:
            ng=mid
    a=ok
    ok=n+1
    ng=0
    while abs(ok-ng)>1:
        mid=(ok+ng)//2
        if solve(mid,n+1):
            ok=mid
        else:
            ng=mid
    print((max(0,ok-a-1)))
if __name__=='__main__':
    main()

# Optimized solution
I=input;n,q=list(map(int,I().split()));s=' %s '%I();l,r=0,n+1
for t,_,d in eval('I(),'*q)[::-1]:L,R=d<'R',d>'L';l+=(t==s[l+1])*L-(t==s[l])*R;r+=(t==s[r])*L-(t==s[r-1])*R
print((max(0,r-l-1)))
# Original solution
I=input;n,q=list(map(int,I().split()));s=' %s '%I();l,r=0,n+1
for t,_,d in eval('I(),'*q)[::-1]:L,R=d<'R',d>'L';l+=(t==s[l+1])*L-(t==s[l])*R;r+=(t==s[r])*L-(t==s[r-1])*R
print((max(0,r-l-1)))

# Optimized solution
I=input;n,q=list(map(int,I().split()));s=' %s '%I();l,r=0,n+1
for t,_,d in eval('I(),'*q)[::-1]:L=d<'R';R=1-L;l+=(t==s[l+1])*L-(t==s[l])*R;r+=(t==s[r])*L-(t==s[r-1])*R
print((max(0,r-l-1)))
# Original solution
N, x = list(map(int, input().split()))
num_list = [int(x) for x in input().split()]
count = 0
if x == 0:
  print((sum(num_list)))
else:
  while True:
    mark = 0
    for i, num in enumerate(num_list[0:-1]):
      if (num_list[i] + num_list[i+1] > x):
        if num_list[i+1] != 0:
          num_list[i+1] = num_list[i+1] - 1
          count+=1
          mark = 1
    if mark == 0:
      break

  print(count)

# Optimized solution
N, x = list(map(int, input().split()))
num_list = [int(x) for x in input().split()]
count = 0

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

print(count)
# Original solution
n, x = list(map(int,input().split()))
A = list(map(int,input().split()))
sum = []
ans = 0
for i in range(n-1):
    sum.append(A[i]+A[i+1])
for j in range(n-1):
    if j != n-2:
        while sum[j] > x:
            sum[j] -= 1
            sum[j+1] -= 1
            ans += 1
    else:
        while sum[j] > x:
            sum[j] -= 1
            ans += 1

print(ans)

# Optimized solution
n, x = list(map(int,input().split()))
A = list(map(int,input().split()))
if A[0] > x:
    ans = A[0] - x
    A[0] -= ans
else:
    ans = 0
for j in range(n):
    if j != n-1:
        if A[j]+A[j+1]> x:
            diff = A[j+1]+A[j]-x
            ans += diff
            if A[j+1] - diff > 0:
                A[j+1] -= diff
            else:
                A[j+1] = 0
    else:
        if A[j]> x:
            diff = A[j]-x
            ans += diff
print(ans)
# Original solution
N, x = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 0

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

# Optimized solution
N, x = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 0

for i in range(N - 1):
  total = a[i] + a[i + 1]
  if total > x:
    a[i + 1] = max(0, a[i + 1] - (total - x))
    ans += (total - x)
print(ans)
# Original solution
n, x = list(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:
    tmp = a[i]
    a[i] = max(x-a[i-1], 0)
    ans += tmp - a[i]
  if a[i] == 0 and a[i-1] > x:
    tmp = a[i-1]
    a[i-1] = x
    ans += tmp - a[i-1]
print(ans)

# Optimized solution



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

print(ans)
# Original solution
N, x = list(map(int, input().split()))
A = list(map(int, input().split()))
ans = 0

for i in range(1, N):
    s = A[i] + A[i-1]
    if s > x:
        y = s-x
        if A[i] > y:
            A[i] -= y
            A[i-1] -= y
            ans += y
        else:
            z = y - A[i]
            A[i] = 0
            A[i-1] -= A[i]
            A[i-1] -= z
            if i >= 2:
                A[i-2] -= z
            ans += y
print(ans)




# Optimized solution
N, x = list(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)
# Original solution
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(1,n):
  if a[i-1]+a[i]>x:
    ans+=a[i-1]+a[i]-x
    a[i]=x-a[i-1]
print(ans)

# Optimized solution
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(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)
# Original solution
N,x=list(map(int,input().split()))
S = [int(i) for i in input().split()]
pre,ans = 0,0
for i in range(N):
    tmp = pre+S[i]
    if tmp > x:
        ans += tmp-x
        S[i] -= tmp-x
    pre = S[i]
print(ans)

# Optimized solution
N,x=list(map(int,input().split()))
S = list(map(int,input().split()))
pre,ans = 0,0
for i in range(N):
    tmp = pre+S[i]
    if tmp > x:
        ans += tmp-x
        S[i] -= tmp-x
    pre = S[i]
print(ans)
# Original solution
import math
N, x = list(map(int, input().split()))
L = [int(x) for x in input().split()]
ans = 0
for i in range(1,N):
    if L[i-1] + L[i] > x:
        if L[i-1] > x:
            ans += L[i-1] - x
            ans += L[i]
            L[i] = 0
        else:
            ans += L[i] + L[i-1] - x
            L[i] = x - L[i-1]
print(ans)


# Optimized solution
N, x = list(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)
# Original solution
# ABC048C - Boxes and Candies
def main():
    n, x = list(map(int, input().rstrip().split()))
    A = list(map(int, input().rstrip().split()))
    ans = 0
    if A[0] > x:
        ans += A[0] - x
        A[0] = x
    for i in range(n - 1):
        dif = A[i] + A[i + 1] - x
        if dif > 0:
            A[i + 1] -= dif
            ans += dif
    print(ans)


if __name__ == "__main__":
    main()

# Optimized solution
# ARC064C - Boxes and Candies (ABC048C)
def main():
    n, x = list(map(int, input().split()))
    A = list(map(int, input().split()))
    ans = A[0] - x if A[0] > x else 0
    A[0] = min(A[0], x)
    for i in range(n - 1):
        dif = A[i] + A[i + 1] - x
        if dif > 0:
            A[i + 1] -= dif
            ans += dif
    print(ans)


if __name__ == "__main__":
    main()
# Original solution
# ARC064C - Boxes and Candies (ABC048C)
def main():
    n, x = list(map(int, input().split()))
    A = list(map(int, input().split()))
    ans = A[0] - x if A[0] > x else 0
    A[0] = min(A[0], x)
    for i in range(n - 1):
        dif = A[i] + A[i + 1] - x
        if dif > 0:
            A[i + 1] -= dif
            ans += dif
    print(ans)


if __name__ == "__main__":
    main()

# Optimized solution
# ARC064C - Boxes and Candies (ABC048C)
def main():
    n, x = list(map(int, input().rstrip().split()))
    A = list(map(int, input().rstrip().split()))

    # modify A[0] if >x
    ans = 0 if A[0] < x else A[0] - x
    A[0] = min(A[0], x)
    
    prv = A[0]
    for i in range(1, n):
        dif = prv + A[i] - x
        if dif > 0:
            A[i] -= dif
            ans += dif
        prv = A[i]
    print(ans)


if __name__ == "__main__":
    main()
# Original solution
def main():
    n, x, *a = list(map(int, open(0).read().split()))
    a = [0] + a
    ans = 0
    tmp = 0
    for i, j in zip(a, a[1:]):
        i -= tmp
        if i + j - x > 0:
            c = i + j - x
            ans += c
            tmp = c
        else:
            tmp = 0

    print(ans)


if __name__ == '__main__':
    main()


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

    print(ans)


if __name__ == '__main__':
    main()
# Original solution
n,x=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]

count=0
if(a[0]>x):
    count=a[0]-x
    a[0]=x
    
for ind,i in enumerate(a):
    if(ind==len(a)-1):
      break
    s=i+a[ind+1]
    while(s>x):
        count+=1
        a[ind+1]-=1
        s=i+a[ind+1]
print(count)
    


# Optimized solution
n,x=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]

count=0
if(a[0]>x):
    count=a[0]-x
    a[0]=x
    
for ind,i in enumerate(a):
    if(ind==len(a)-1):
      break
    s=i+a[ind+1]
    if(s>x):
        count+=s-x
        a[ind+1]-=s-x
        #s=i+a[ind+1]
print(count)
# Original solution
n,x=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]

count=0
if(a[0]>x):
    count=a[0]-x
    a[0]=x
    
for ind,i in enumerate(a):
    if(ind==len(a)-1):
      break
    s=i+a[ind+1]
    if(s>x):
        count+=s-x
        a[ind+1]-=s-x
        #s=i+a[ind+1]
print(count)
    


# Optimized solution
n,x=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]

if(n==1):
    print((a[0]-x))
    exit()
ope=0
if(a[0]>x):
    ope+=a[0]-x
    a[0]=x

for i in range(n-1):
    c=a[i]+a[i+1]
    if(c>x):
        a[i+1]-=c-x
        ope+=c-x
print(ope)
# Original solution
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):
    if A[i] + A[i+1] > X:
        ans -= X - (A[i] + A[i + 1])
        A[i+1] += X - (A[i] + A[i+1])
 
print(ans)

# Optimized solution
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
v=sum(a)
if k<a[0]:
    a[0]=k
for i in range(n-1):
    if k<a[i]+a[i+1]:
        a[i+1]=k-a[i]
print((v-sum(a)))
# Original solution
n,k=list(map(int,input().split()))
a=list(map(int,input().split()))
v=sum(a)
if k<a[0]:
    a[0]=k
for i in range(n-1):
    if k<a[i]+a[i+1]:
        a[i+1]=k-a[i]
print((v-sum(a)))

# Optimized solution
n,m=list(map(int,input().split()))
a=list(map(int,input().split()))
total=sum(a)
if m<a[0]:
    a[0]=m
for i in range(1,n):
    if m<a[i]+a[i-1]:
        a[i]=m-a[i-1]
tot=sum(a)
print((total-tot))
# Original solution
n,x=list(map(int,input().split()))
*a,=list(map(int,input().split()))

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

while not all(b):
    f=True
    for i in range(n-2):
        if not b[i] and not b[i+1]:
            v[i+1]=x-v[i]
            f=False
            break
    if f:
        for i in range(n-1):
            if not b[i]:
                v[i]=min(x-v[i-1],x-v[i+1])
                break
    b=[v[i]+v[i+1]<=x for i in range(n-1)]     
    
print((sum(a)-sum(v)))

# Optimized solution
n,x=list(map(int,input().split()))
*a,=list(map(int,input().split()))

v=[i if i<x else x for i in a]

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

print((sum(a)-sum(v)))
# Original solution
import sys

# sys.stdin = open('c1.in')


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


def read_str_list():
    return input().split()


def read_int():
    return int(eval(input()))


def read_str():
    return eval(input())


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


main()


# Optimized solution
import sys

# sys.stdin = open('c1.in')


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


def read_str_list():
    return input().split()


def read_int():
    return int(eval(input()))


def read_str():
    return eval(input())


def main():
    n, x = read_int_list()
    a = read_int_list()
    a.insert(0, 0)
    n += 1
    res = 0
    for i in range(1, n):
        if a[i - 1] + a[i] > x:
            diff = a[i - 1] + a[i] - x
            res += diff
            a[i] -= diff
    print(res)


main()
# Original solution
def solve():
    n, x = list(map(int, input().split()))
    s = list(map(int, input().split()))
    cnt, tmp, flg = 0, 0, False
    for i in range(n - 1):
        if i == 0 and s[i] > x:
            cnt += s[i] - x
            s[i] = x
        if s[i] + s[i + 1] > x:
            tmp = s[i] + s[i + 1] - x
            cnt += tmp
            s[i + 1] -= tmp
    print(cnt)



solve()

# Optimized solution
def solve():
    n, x = list(map(int, input().split()))
    s = list(map(int, input().split()))
    cnt, tmp, prv = 0, 0, 0

    if s[0] > x:
        cnt += s[0] - x
        s[0] = x

    for y in s:
        if prv + y > x:
            tmp = prv + y - x
            cnt += tmp
            prv = y - tmp
        else:
            prv = y
    print(cnt)


solve()