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 = list(map(int, input().split()))

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

    candy = a[i] + a[i+1] - x
    res += candy
    a[i+1] = max(0, a[i+1] - candy)

print(res)

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

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

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

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

z=a[0]+a[1]-x
if z>0:
  ans+=z
  a[1]-=z
  if a[1]<0:
    a[0]-=abs(a[1])
    a[1]=0

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

print(ans)

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

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

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

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

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

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

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

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

print(cnt)

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

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

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

Solution (Python):





import sys,os
from io import BytesIO, IOBase
import collections,itertools,bisect,heapq,math,string
from decimal import *


BUFSIZE = 8192

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")

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

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

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

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

print(ans)

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

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

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

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

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

Solution (Python):
import sys
import numpy as np
def input(): return sys.stdin.readline().rstrip()
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return list(mi())


def main():
    n, x = mi()
    a = li()
    cnt = 0
    for i in range(1,n):
        if a[i-1]+a[i]>x:
            cnt += a[i-1]+a[i]-x
            a[i] = max(0, x-a[i-1])
    print(cnt)
        
if __name__ == '__main__':
    main()

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

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

import copy
b = copy.deepcopy(a) 


i=1
cnt=0
while i<n:
    if a[i]+a[i-1]>x:
        tmp=(a[i]+a[i-1])-x
        cnt+=tmp
        a[i]=max(0,a[i]-tmp)
    i+=1


b=b[::-1]
i=1
cnt1=0
while i<n:
    if b[i]+b[i-1]>x:
        tmp=(b[i]+b[i-1])-x
        cnt1+=tmp
        b[i]=max(0,b[i]-tmp)
    i+=1

print(min(cnt,cnt1))

Solution (Python):
import math
N, x = 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)

Solution (Python):
N,X = (int(T) for T in input().split())
A = [int(T) for T in input().split()]
Eat = 0
for AT in range(0,N-1):
    if A[AT]+A[AT+1]>X:
        Need = A[AT]+A[AT+1]-X
        Eat += Need
        A[AT] -= max(0,Need-A[AT+1])
        A[AT+1] = max(0,A[AT+1]-Need)
print(Eat)

Solution (Python):
def solve():
    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):
        sumvalue = A[i] + A[i + 1]
        if sumvalue <= x:
            continue
        if i + 1 == N - 1:
            A[i + 1] -= sumvalue - x
        else:
            if A[i + 1] < sumvalue - x:
                A[i] -= sumvalue - x - A[i + 1]
                A[i + 1] = 0
            else:
                A[i + 1] -= sumvalue - x
        ans += sumvalue - x
    print(ans)


if __name__ == "__main__":
    solve()

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

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

    if i - 1 >= 0:
        if a[i] + a[i-1] > x:
            count += (a[i] + a[i-1]) - x
            a[i] -= (a[i] + a[i-1]) - x

print(count)

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

Solution (Python):
N,X = (int(T) for T in input().split())
A = [int(T) for T in input().split()]
Eat = 0
for AN in range(0,N-1):
    if A[AN]+A[AN+1]>X:
        Need = A[AN]+A[AN+1]-X
        if A[AN+1]>=Need:
            A[AN+1] -= Need
        else:
            A[AN] -= Need-A[AN+1]
            A[AN+1] = 0
        Eat += Need
print(Eat)

Solution (Python):
N, M = map(int, input().split())
L = list(map(int, input().split()))

count = 0

if L[0] > M:
	count+= (L[0]-M)
	L[0]-= (L[0]-M)

for i in range (1, N):
	if L[i]+L[i-1] > M:
		count+= (L[i]+L[i-1])-M
		L[i]-= (L[i]+L[i-1])-M
    
print(count)

Solution (Python):
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, x = mapint()
As = list(mapint())

last = As[0]
ans = 0
for i in range(1, N):
    a = As[i]
    if last+a>x:
        diff = (last+a)-x
        ans += diff
        last = max(0, a-diff)
    else:
        last = a
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:
        rem = (a[i]+a[i+1]) - x
        if rem <= a[i+1]:
            ans += rem
            a[i+1] -= rem
        else:
            ans += rem
            a[i+1] = 0

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

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

ans = 0
for i, j in zip(range(N - 1), range(1, N)):
    Asum = A[i] + A[j]
    if Asum <= K:
        continue
    eat = Asum - K
    d = min(eat, A[j])
    A[j] -= d
    eat -= d
    ans += d
    d = min(eat, A[i])
    A[i] -= d
    ans += d

print(ans)

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

Solution (Python):
N, x = map(int, input().split())
values = map(int, input().split())
count = 0
v1 = next(values)
for v2 in values:
    surplus = max(v1 + v2 - x, 0)
    count += surplus
    v1 = max(0, v2 - surplus)
print(count)

Solution (Python):
import sys
import math
import copy
import heapq
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
sys.setrecursionlimit(1000000)


input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)

def nck(n, k, kaijyo):
    return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD

def npk(n, k, kaijyo):
    if k == 0 or k == n:
        return n % MOD
    return (kaijyo[n] * divide(kaijyo[n-k])) % MOD

def fact_and_inv(SIZE):
    inv = [0] * SIZE  
    fac = [0] * SIZE  
    finv = [0] * SIZE  
    inv[1] = 1
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    for i in range(2, SIZE):
        inv[i] = MOD - (MOD // i) * inv[MOD % i] % MOD
        fac[i] = fac[i - 1] * i % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD

    return fac, finv

def renritsu(A, Y):
    
    
    
    A = np.matrix(A)
    Y = np.matrix(Y)
    Y = np.reshape(Y, (-1, 1))
    X = np.linalg.solve(A, Y)

    
    return X.flatten().tolist()[0]

class TwoDimGrid:
    
    def __init__(self, h, w, wall="
        self.h = h
        self.w = w
        self.size = (h+2) * (w+2)
        self.wall = wall
        self.get_grid()
        

    def get_grid(self):
        grid = [self.wall * (self.w + 2)]
        for i in range(self.h):
            grid.append(self.wall + getS() + self.wall)

        grid.append(self.wall * (self.w + 2))
        self.grid = grid
    def init_cost(self):
        self.cost = [INF] * self.size

    def pos(self, x, y):
        
        return y * (self.w + 2) + x
    def getgrid(self, x, y):
        return self.grid[y][x]
    def get(self, x, y):
        return self.cost[self.pos(x, y)]
    def set(self, x, y, v):
        self.cost[self.pos(x, y)] = v
        return
    def show(self):
        for i in range(self.h+2):
            print(self.cost[(self.w + 2) * i:(self.w + 2) * (i+1)])
    def showsome(self, tgt):
        for t in tgt:
            print(t)
        return
    def showsomejoin(self, tgt):
        for t in tgt:
            print("".join(t))
        return

    def search(self):
        grid = self.grid
        move = [(0, 1), (0, -1), (1, 0), (-1, 0)]
        move_eight = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
        
        
        
        
        

def solve():
    n, x = getList()
    nums = getList()
    ans = 0
    if nums[0] > x:
        ans += nums[0] - x
        nums[0] = x
    for i in range(1, n):
        t = nums[i] + nums[i-1] - x
        if t > 0:
            ans += t
            nums[i] -= t

    print(ans)

def main():
    n = getN()
    for _ in range(n):
        solve()

    return
if __name__ == "__main__":
    
    solve()

Solution (Python):
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N, x = LI()
    a = LI()

    
    s = [a[i] + a[i+1] for i in range(N - 1)]
    
    s_after = [min(i, x) for i in s]

    
    a_after = [0] * N
    a_after[0] = min(a[0], s_after[0])
    for i in range(N - 1):
        a_after[i+1] = min(s_after[i] - a_after[i], a[i+1])

    ans = sum(a) - sum(a_after)
    print(ans)

if __name__ == '__main__':
    resolve()

Solution (Python):
def candy():
    boxNum, x = [int(i) for i in input().split()]
    ain = input()
    alist = ain.split(' ')

    s = 0
    
    for i in range(0, boxNum-1):
        if int(alist[i+1]) + int(alist[i]) > x:
            s = s + (int(alist[i+1]) + int(alist[i]) - x)
            alist[i+1] = x - int(alist[i])
            if int(alist[i+1]) < 0:
                alist[i] = int(alist[i]) + int(alist[i+1])
                alist[i+1] = 0
        
    print(s)
    
candy()

Solution (Python):
def boxes():
    N, x = input().split()
    N, x = int(N), int(x)
    arr = [int(a) for a in input().split()]

    total = 0
    for i in range(1, N):
        a, b = arr[i-1], arr[i]
        extra = a + b - x
        if extra > 0:
            total += extra
            if b >= extra:
                arr[i] -= extra
            else:
                arr[i] = 0
    print(total)
boxes()

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

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

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

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

result = 0

if a[0] > k:
        result = a[0] -k
        a[0] = k

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

print(result)

Solution (Python):
def candy():
    n = []
    n = input().split()
    N = int(n[0])
    x = int(n[1])
    alist = [int(i) for i in input().split()]

    s = 0
    for i in range(0, N - 1):
        c = int(alist[i])
        d = int(alist[i + 1])
        if int( alist[i] + alist[i + 1] ) > x:
            if int( alist[i] ) >= x:
                alist[i] = x
                alist[i + 1] = 0
            else :
                alist[i + 1] = int(x - alist[i] )
            s += c - int(alist[i]) + d - int(alist[i + 1])
        """
        if int(alist[i]) > x:
            s = s + int(alist[i+1])
            alist[i+1] = 0

        if int(alist[i+1]) + int(alist[i]) > x:
            s = s + (int(alist[i+1]) + int(alist[i]) - x)
            alist[i+1] = x - int(alist[i])
        """
    print(s)
candy()

Solution (Python):
def solve():
  ans = 0
  N, x = map(int, input().split())
  A = list(map(int, input().split()))
  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] = x-A[i-1]
  return ans
print(solve())

Solution (Python):
import collections

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

ans = 0
q = collections.deque()
if a[0] <= x:
    q.append(a[0])
else:
    ans += a[0]- x
    q.append(x)

for i in range(1,n):
    tmp = q[-1] + a[i] 
    if tmp <= x:
        q.append(a[i])
    else:
        ans += tmp-x
        q.append(x-q[-1])
    q.popleft()


print(ans)

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

t = 0
ans = 0
for i in a:
  s = max(0, t+i-x) 
  t = i - s
  ans += s
print(ans)

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

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

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

r1 = sum(a)
flow = 0

for i in range(1, N):
    t = (a[i] + a[i - 1]) - x
    if flow != 0:
        a[i-1] -= flow
        t -= flow
        flow = 0

    if t > 0:
        if t > a[i]:
            t -= a[i]
            a[i - 1] -= t
            t = a[i]

        flow = t

a[-1] -= flow

r2 = sum(a)

print(r1 - r2)

Solution (Python):


N, X = (int(x) for x in input().split())
A = [int(x) for x in input().split()]

ret = 0
for i in range(N-1):
    if A[i] + A[i+1] > X:
        tmp = A[i] + A[i+1] - X
        ret += tmp
        A[i+1] -= min(A[i+1], tmp)


print(ret)

Solution (Python):


N, X = (int(x) for x in input().split())
A = [int(x) for x in input().split()]

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

ret = 0
for i in range(N-1):
    tmp = min(diffs[i], A[i+1])
    ret += tmp
    diffs[i]   -= tmp
    if i+1 < len(diffs):
        diffs[i+1] -= min(tmp, diffs[i+1]) 
    ret += diffs[i]

print(max(0, ret))

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

Solution (Python):
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n, x = MAP()
a = LIST()
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(0, x - a[i])
print(ans)

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

add_a=[]
for i in range(n-1):
    add_a.append(max(a[i]+a[i+1]-x,0))

ans=0
for i in range(n-1):
    ans+=min(add_a[i],a[i+1])
    if i!=n-2:
        add_a[i+1]=max(0,add_a[i+1]-min(add_a[i],a[i+1]))
    add_a[i]-=min(add_a[i],a[i+1])
ans+=add_a[0]

print(ans)

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

cnt = max(0, a[0] - x)
a[0] -= cnt
for i in range(1, n):
    cnt_now = max(0, a[i-1] + a[i] - x)
    a[i] -= cnt_now
    cnt += cnt_now

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

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

    print(ans)


if __name__ == "__main__":
    main()

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

Solution (Python):
import sys, math
from collections import defaultdict
from itertools import product
sys.setrecursionlimit(500000)
MOD = 10**9+7

def input():
    return sys.stdin.readline()[:-1]

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]

def main():
    N, x = mi()
    a = list(mi())

    cnt = max(a[0]-x, 0)
    a[0] = min(a[0], x)

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


if __name__ == '__main__':
    main()

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

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

ans = 0

if a[0] - x > 0:
    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 = list(map(int,input().split()))
A = list(map(int,input().split()))

ans=0

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

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

Solution (Python):
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


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

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

    if A[-1] > x:
        ans2 = A[-1] - x
        prev = x
    else:
        ans2 = 0
        prev = A[-1]
    for i in range(N - 2, -1, -1):
        if prev + A[i] > x:
            ans2 += prev + A[i] - x
            prev = x - prev
        else:
            prev = A[i]

    print(min(ans1, ans2))
    return


if __name__ == '__main__':
    main()

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

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

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

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

print(sm)

Solution (Python):
N,x,*a = map(int, open(0).read().split())
temp = 0
ans = 0
for c in a:
    t1 = temp
    temp += c
    if temp > x:
        ans += temp - x
        temp = x - t1
    else:
        temp -= t1
print(ans)

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

for a in A:
  eat = max(0,pre+a-x)
  pre = a - eat
  ans+= eat

print(ans)

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

B = list(A)

ans1 = ans2 = 0
for i in range(N-1):
  if B[i]+B[i+1] > x:
    b = B[i]+B[i+1] - x
    ans1 += b
    B[i+1] -= b
    if B[i+1] < 0: B[i+1] = 0


B = list(A[::-1])
for i in range(N-1):
  if B[i]+B[i+1] > x:
    b = B[i]+B[i+1] - x
    ans2 += b
    B[i+1] -= b
    if B[i+1] < 0: B[i+1] = 0

print( min(ans1, ans2) )

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

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


def calculate(n, x, arr):
    result = 0
    for i in range(1, n):
        sSum = arr[i - 1] + arr[i]
        if sSum <= x:
            continue

        diff = sSum - x

        if diff <= arr[i]:
            arr[i] -= diff
            result += diff
        else:
            result += diff
            arr[i] = 0

    print(result)


calculate(N, X, ARR)

Solution (Python):
def solve():
    n, x = 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()

Solution (Python):
def solve():
    n, x = 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()