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):
_, x = map(int, input().split())
a = [int(i) for i in input().split()]
cnt = 0

for i in range(len(a) - 1):
    s = a[i] + a[i + 1]
    if x < s:
        sub = s - x
        a[i + 1] = max(0, a[i + 1] - sub)
        cnt += sub
print(cnt)

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

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

print(ans)

Solution (Python):
N, x = map(int, raw_input().split())
a = map(int, raw_input().split())
ans = 0
if a[0] > x:
    ans += a[0] - x
    a[0] = x
if a[-1] > x:
    ans += a[-1] - x
    a[-1] = x
for i in xrange(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):

num_box, num_ans_candy = map(int, input().split())
l_box = list(map(int, input().split()))

eat_counter = 0
for index in range(num_box - 1):
    double_candy = l_box[index] + l_box[index + 1]
    if double_candy > num_ans_candy:
        num_eat_candy = double_candy - num_ans_candy
        if l_box[index + 1] < num_eat_candy:
            num_eat_candy -= l_box[index + 1]
            eat_counter += l_box[index + 1]
            l_box[index + 1] = 0
            l_box[index] -= num_eat_candy
        else:
            l_box[index + 1] -= num_eat_candy
        eat_counter += num_eat_candy

print(eat_counter)

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

    result = 0


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

    print(result)

Solution (Python):
n, x = map(int, raw_input().split())
a = map(int, raw_input().split())
ans = 0
for i in xrange(n - 1):
    o = a[i] + a[i + 1] - x
    if o > 0:
        ans += o
        a[i + 1] -= min(o, a[i + 1])

print ans

Solution (Python):
NandX=list(map(int,input().split()))
N=NandX[0]
x=NandX[1]
A=list(map(int,input().split()))

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

print(ans)

Solution (Python):



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

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


print(cnt)

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

if a[0] > x:
    a_min[0] = x
else:
    a_min[0] = a[0]

ans = a[0] - a_min[0]

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

print(ans)

Solution (Python):
n,x=map(int,raw_input().split())
a=map(int,raw_input().split())
i=0
num=0

if a[0] > x:
        ans=a[0]-x
        a[0]=a[0]-ans
        num=num+ans
while i<n-1:
        if a[i] + a[i+1] > x:
                ans=a[i] + a[i+1] - x
                a[i+1]=a[i+1]-ans
                num=num+ans
        else:
                i=i+1

print num

Solution (Python):

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

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

ans = 0

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

print(ans)

Solution (Python):
n, x = map(int, raw_input().split())
a = map(int, raw_input().split())
counter = 0
for i in range(n-1):
    decrease = a[i] + a[i+1] - x
    if decrease > 0:
        counter += decrease
        if a[i+1] >= decrease:
            a[i+1] -= decrease
        else:
            a[i] -= decrease - a[i+1];
            a[i+1] = 0
print counter

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

count = 0

i = 0
pre = 0
while i < N:
    if pre + a[i] > x:
        pre = x - pre
        count += a[i] - pre
    else:
        pre = a[i]
    i += 1

print(count)

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

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

c = 0

for i in xrange(1, len(a)):
    while a[i] + a[i-1] > x:
        if a[i] > 0:
            c += min(a[i], a[i] + a[i-1] - x)
            a[i] -= min(a[i], a[i] + a[i-1] - x)
        else:
            c +=  a[i] + a[i-1] - x
            a[i-1] -=  a[i] + a[i-1] - x
print c

Solution (Python):


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

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

prev = a_s.pop()
result = max(prev - x, 0)
prev -= result
while a_s:
    cur = a_s.pop()
    if prev + cur > x:
        cur_res = prev + cur - x
        cur -= cur_res
        result += cur_res
    prev = cur

print(result)

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

res = 0
for i in range(len(a)-1):
    b = a[i]+a[i+1]
    if x < b:
        res += b-x
        a[i+1] -= min(b-x, a[i+1])

print(res)

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

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

def solve(N,x,a):
	a.insert(0,0)
	count=0
	for i in range(len(a)-1):
		if a[i]+a[i+1]>x:
			eat=a[i+1]+a[i]-x
			count+=eat
			a[i+1]-=eat
	return(count)


print(solve(N, x, a))

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

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

for i in range(1, len(a)):
    much = a[i] + a[i-1] - x
    if 0 < much <= a[i]:
        ans += much
        a[i] -= much
    elif a[i] < much:
        t = a[i]
        a[i] = 0
        ans += t
        much -= t
        a[i-1] -= much
        ans += much
print(ans)

Solution (Python):

N, x = map(int, raw_input().split())
A = map(int, raw_input().split())


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

print res

Solution (Python):
n,x = map(int, input().split())
a = list(map(int, input().split()))
b = a[::-1]

def solve(a):
    total = 0
    if a[0] > x:
        total += a[0]-x
        a[0] = x

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

print(min(solve(a), solve(b)))

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

Solution (Python):

import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys

def s():
    return raw_input().strip()
def n():
    return int(raw_input())
def d():
    return float(raw_input())

def ls():
    return raw_input().strip().split()
def ln():
    return map(int, raw_input().strip().split())
def ld():
    return map(float, raw_input().strip().split())

def fs():
    return [raw_input().strip() for i in xrange(input())]
def fn():
    return [int(raw_input().strip()) for i in xrange(input())]
def fd():
    return [float(raw_input().strip()) for i in xrange(input())]

N, x = ln()
X = ln()

A = X[:]
result = 0
for i in xrange(1, N):
    if A[i] + A[i - 1] > x:
        diff = A[i] + A[i - 1] - x
        
        A[i] -= diff
        A[i] = max(A[i], 0)
        result += diff

print result

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

Solution (Python):
n,x=map(int,raw_input().split())
a=map(int,raw_input().split())
ans=0
if a[0]+a[1]>x:
    ans+=a[0]+a[1]-x
    a[1]=max(a[1]-ans,0)
for i in xrange(1,n-1):
    if a[i]+a[i+1]>x:
        ans+=a[i]+a[i+1]-x
        tmp=a[i]+a[i+1]-x
        a[i+1]=max(a[i+1]-tmp,0)

print(ans)

Solution (Python):


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

Solution (Python):
n, x = map(int, raw_input().split())
A = map(int, raw_input().split())
ans = 0
if x < A[0]:
    ans += A[0] - x
    A[0] = x
if x < A[-1]:
    ans += A[-1] - x
    A[-1] = x
for i in xrange(1, n):
    if x - A[i-1] < A[i]:
        ans += A[i] - (x - A[i-1])
        A[i] = x - A[i-1]
print ans

Solution (Python):
from sys import stdin
n,m = map(int,stdin.readline().split())
a = map(int,stdin.readline().split())
ans = 0
if a[0] > m:
    ans += a[0] - m 
    a[0] = m 
for i in xrange(1,n):
    cur = a[i-1] + a[i]
    if cur >m:
        ans += cur - m 
        a[i] = m - a[i-1]
print ans

Solution (Python):
from sys import exit, setrecursionlimit
from functools import reduce
from itertools import *
from collections import defaultdict
from bisect import bisect

def read():
  return int(input())

def reads():
  return [int(x) for x in input().split()]

setrecursionlimit(1000000)

(N, x) = reads()
a = [0] + reads()

result = 0

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

print(result)

Solution (Python):

def main():
    n, x = map(int, input().split())
    al = list(map(int, input().split()))
    ans = 0
    for i in range(n - 1):
        l, r = i, i + 1
        exc = al[l] + al[r] - x
        if exc <= 0:
            continue
        rd = min(al[r], exc)
        al[r] -= rd
        ans += rd
        exc -= rd
        if exc > 0:
            al[l] -= exc
            ans += exc
    print(ans)


if __name__ == '__main__':
    main()

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

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

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

print ans

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

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

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


def main():
    n,x = list(map(int, input().split()))
    a = list(map(int, input().split()))
    r = 0
    for i in range(n-1):
        wa = a[i] + a[i+1]
        if wa <= x:
            continue
        sa = wa - x
        r += sa
        if sa <= a[i+1]:
            a[i+1] -= sa
        else:
            a[i+1] = 0
    return r


print(main())