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,k=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))

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 -= X - (A[i] + A[i + 1])
        A[i+1] += X - (A[i] + A[i+1])
 
print(ans)

Solution (Python):
def c_Boxes_and_Candies(N, X, A):
    a = [x for x in A]
    if a[0] > X:
        
        a[0] = X
    for i in range(0, N - 1):
        if a[i] + a[i + 1] > X:
            
            a[i + 1] = X - a[i]
    ans = 0
    for i in range(N):
        ans += A[i] - a[i]
    return ans

N,X = [int(i) for i in input().split()]
A = [int(i) for i in input().split()]
print(c_Boxes_and_Candies(N, X, A))

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 -= X - (A[i] + A[i + 1])
        A[i+1] += X - (A[i] + A[i+1])

print(ans)

Solution (Python):
def main():
    """
    端の最適解は決定している
        idx= 0~ 1:sumがx以下になるようにがA[ 1]を減らす、それでもx以下にならないならA[ 0]を減らす
        idx=-2~-1:sumがx以下になるようにがA[-2]を減らす、それでもx以下にならないならA[-1]を減らす

        -> 左端は決定しているので右端まで減らす
        -> 右端から行っても減らす数は変わらない
        -> 端から2番目を優先に減らせばどちらでもよい？
    """
    N, x = map(int, input().split())
    A = list(map(int, input().split()))

    ans = f(N, x, A)
    print(ans)


def f(N, x, A):
    ans = 0
    for i in range(1, N):
        s = sum(A[i - 1:i + 1])
        if s > x:
            
            n = min(s - x, A[i])
            A[i] -= n
            ans += n

            s = sum(A[i - 1:i + 1])
            if s > x:
                n = min(s - x, A[i - 1])
                A[i - 1] -= n
                ans += n

    return ans


def WA(N, x, A):
    ans = 0
    for i in range(N // 2):
        s = sum(A[i:i + 2])
        if x <= s:
            n = s - x
            A[i + 1] -= n
            ans += n

            s = sum(A[i:i + 2])
            if x <= s:
                n = s - x
                A[i] -= n
                ans += n

        if i == 0:
            
            s = sum(A[-2:])
        else:
            s = sum(A[-(i + 2):-i])
        if x <= s:
            n = s - x
            A[-(i + 2)] -= n
            ans += n

            if i == 0:
                s = sum(A[-2:])
            else:
                s = sum(A[-(i + 2):-i])
            if x <= s:
                n = s - x
                A[-(i + 1)] -= n
                ans += n

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

print(ans)

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

a=map(int,raw_input().split())


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

print cnt

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

sums = [a[0]]
for i in range(len(a) - 1):
    sums.append(a[i] + a[i + 1])
sums.append(a[-1])



cnt = 0
for i in range(len(sums)):
    if sums[i] > x:
        diff = sums[i] - x
        sums[i] -= diff
        sums[i + 1] -= diff
        cnt += diff
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:
  ans+=a[i]+a[i+1]-x
  if a[i] > x:
   a[i+1] = 0
  else:
   a[i+1] -= a[i] + a[i+1] - x

print(ans)

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

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

ans = 0

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

print(ans)

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

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

Solution (Python):
import sys




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


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


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


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

Solution (Python):
import sys




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


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


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


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

Solution (Python):
from collections import defaultdict


def main():
    N, x = map(int, input().split())
    a_list = list(map(int, input().split()))

    pre_total = sum(a_list)

    a_list = [min(a, x) for a in a_list]
    for i in range(1, len(a_list)):
        t = a_list[i - 1] + a_list[i]
        a_list[i] -= max(0, t - x)

    print(pre_total - sum(a_list))


if __name__ == '__main__':
    main()

Solution (Python):
from collections import defaultdict


def main():
    N, x = map(int, input().split())
    a_list = list(map(int, input().split()))
    ans = 0
    for i in range(len(a_list)):
        d = max(0, a_list[i] - x)
        a_list[i] -= d
        ans += d

    for i in range(1, len(a_list)):
        total = a_list[i - 1] + a_list[i]
        if total > x:
            d = total - x
            a_list[i] -= d
            ans += d

    print(ans)


if __name__ == '__main__':
    main()

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

count = 0
prev_eat = 0
for i in range(N-1):
    a = candies[i]
    b = candies[i+1]
    eat = max((a + b - prev_eat) - x, 0)
    count += eat
    prev_eat = min(eat, b)
print count

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

Solution (Python):


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

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

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

Solution (Python):
from collections import defaultdict


def main():
    N, x = map(int, input().split())
    c = list(map(int, input().split()))
    pre = sum(c)

    c = [min(i, x) for i in c]
    ans = pre - sum(c)

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

    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
import sys

def debug(x, table):
    for name, val in table.items():
        if x is val:
            print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
            return None

def solve():
    N, x = map(int, input().split())
    A = [int(i) for i in input().split()]
    ans = 0

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

    print(ans)


if __name__ == '__main__':
    solve()

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

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

print(res)

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

ans = 0

for i in range(l-1):
    a_0 = a[i]
    a_1 = a[i+1]
    dif = x - a_0
    if a_1 > dif:
        ans += a_1 - dif
        if dif > 0:
            a[i+1] = dif
        else:
            a[i+1] = 0

print(ans)

Solution (Python):

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

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

print(Nc)

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

a += [0]
counter = 0

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

print(counter)

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

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

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

seq = [int(i) for i in input().split()]

ans = 0

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

print(ans)

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

result = 0
for i in range(N):
	result = result+max(0, A[i]-x)
	A[i] = min(x, A[i])

for i in range(N-1):
	if A[i]+A[i+1]<=x:
		continue
	result = result+A[i]+A[i+1]-x
	A[i+1] = A[i+1]-(A[i]+A[i+1]-x)

print(result)

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

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

b_list = [a_list[i]+a_list[i+1] for i in range(0,N-1)]

excess_list = [max(0,val -x) for val in b_list]
ans = 0
for i in range(0,len(excess_list)-1):
    min_val = min(a_list[i+1],excess_list[i],excess_list[i+1])
    a_list[i+1] -= min_val
    excess_list[i] -= min_val
    excess_list[i+1] -= min_val
    ans += min_val
ans += sum(excess_list)
print(ans)

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

Solution (Python):



def min_turns(n, x, ys):
    zs = ys[:]
    if zs[0] > x:
        zs[0] = x
    for i in range(n - 1):
        if zs[i] + zs[i + 1] > x:
            zs[i + 1] = x - zs[i]
    ans = sum(y - z for y, z in zip(ys, zs))
    return ans


def main():
    n, x = (int(z) for z in input().split())
    ys = [int(z) for z in input().split()]
    print(min_turns(n, x, ys))


if __name__ == '__main__':
    main()

Solution (Python):


n,x=map(int,raw_input().split())
a=map(int,raw_input().split())
res0=sum(a)
if a[0]>x:
 a[0]=x
for i in range(n-1):
 if a[i]+a[i+1]>x:
  a[i+1]=x-a[i]
  if a[i+1]<0:
   a[i+1]=0
res=res0-sum(a)
print res

Solution (Python):
import sys

line = sys.stdin.readline()

num = int(line.split()[0])
x = int(line.split()[1])

line = sys.stdin.readline()

nums = map(int, line.split())

cnt = 0
for i,v in enumerate(nums):
  if i == len(nums)-1:
    break
  if nums[i] + nums[i+1] > x:
    eat = nums[i] + nums[i+1] - x
    if nums[i+1] > eat:
      nums[i+1] -= eat
    else :
      nums[i] = nums[i] - (eat - nums[i+1])
      nums[i+1] = 0
    cnt += eat

print cnt

Solution (Python):

N,x= [int(i) for i in input().split(" ")]
list = [int(i) for i in input().split(" ")]
count = 0 
for i in range(N):
    if(i==0):
        if list[i]>x:
            temp = list[i]-x
            count+=temp
            list[i]-=temp
    else:
        temp = list[i]+list[i-1]-x
        if(temp>0):
            count+=temp
            list[i]-=temp    
print(count)

Solution (Python):

N,x=input().split(" ")
s = input().split(" ")
list = []
for i in range(int(N)):
    list.append(int(s[i]))
count = 0

if(list[0]>int(x)):
    count+=list[0]-int(x)
    list[0]-=list[0]-int(x)

for i in range(1,int(N)):
    temp = list[i]+list[i-1]
    if(temp>int(x)):
        count+=temp-int(x)
        list[i]-=temp-int(x)        
print(count)

Solution (Python):
import sys
import math

n, x = map(int, sys.stdin.readline().split())
s = map(int, sys.stdin.readline().split())
su = [0  for i in range(n+1)]
su[0] = s[0]
su[n] = s[n-1]
for i in range(1,n):
    su[i] = s[i-1]+s[i]

ans = 0
for i in range(n+1):
    if su[i]>x :
        t = su[i]-x
        ans += t
        su[i+1] -= t

print ans

Solution (Python):
N, x = map(int, raw_input().split())
candies = map(int, raw_input().split())
counter = 0
for i  in range(0, len(candies)-1):
    delta = candies[i+1] + candies[i] - x
    if delta > 0:
        counter += delta
        candies[i+1] = x - candies[i]
        if candies[i+1] < 0:
            candies[i+1] = 0
print counter

Solution (Python):
n, x = map(int, input().split())
a = list(map(int, input().split()))
counter = 0
pointer = 1
if a[0] > x:
    counter += a[0] - x
    a[0] = x
while pointer <= n - 1:
    if a[pointer - 1] + a[pointer] > x:
        eat = a[pointer - 1] + a[pointer] - x
        counter += eat
        a[pointer] -= eat
    pointer += 1
print(counter)

Solution (Python):




def main():
    n, x = [int(i) for i in raw_input().split()]
    a_list = [int(i) for i in raw_input().split()]
    rel_sums = [a_list[i] if i == 0 else a_list[i-1]+a_list[i] for i in xrange(n)]
    count = 0
    for i in xrange(n-1):
        if rel_sums[i] > x:
            count += rel_sums[i] - x
            rel_sums[i+1] -= rel_sums[i] - x
    if rel_sums[n-1] > x:
        count += rel_sums[n-1] - x
        rel_sums[n-1] -= rel_sums[n-1] - x
    print count

if __name__ == '__main__':
    main()

Solution (Python):
import sys
from functools import reduce

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

cnt, prv = 0, 0

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

print(cnt)

Solution (Python):
import sys
from functools import reduce

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

if x == 0:
    print(reduce(lambda acc, x: acc + x, arr))
    sys.exit()

cnt = 0
prv = 0

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

print(cnt)

Solution (Python):









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

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

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

print(ans)

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

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):

import sys

_, x = raw_input().split(' ')
x = long(x)
aa = raw_input().split(' ')

ret = long(0)
last = 0
for i, a in enumerate(aa):
    a = long(a)
    if i == 0:
       last = min(a, x)
       ret += a - last
    else:   
       last = min(a, x - last)
       ret += a - last

print ret

Solution (Python):

N, x = map(int, input().split())
num_list = [int(x) for x in input().split()]
if x < num_list[0]:
    count = num_list[0] - x
    num_list[0] = x
else:
    count = 0

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

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

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

Solution (Python):
n, x = map(int, input().split())
a = list(map(int, input().split()))
eat = 0
i = 0
total = 0
while i < (n - 1):
  a[i] -= eat
  if (a[i] < 0):
    a[i] = 0
  if (a[i] + a[i+1]) > x:
    eat = a[i] + a[i+1] - x
    total += eat
  else:
    eat = 0
  i += 1
print(str(total))

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

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

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

Solution (Python):
st = input().split()
N = int(st[0])
x = int(st[1])
A = input().split()
a = []
for i in range(N):
     a.append(int(A[i]))   
a.append(0)
a.append(0)
ret = 0
for i in range(N):
    if a[i] + a[i + 1] > x :
        d1 = max((a[i] + a[i + 1]) - x,0)
        ret +=  min(a[i+1],d1)
        a[i+1] -=  min(a[i+1],d1)
        d1 = max((a[i] + a[i + 1]) - x,0)
        ret +=  min(a[i],d1)
        a[i] -=  min(a[i],d1)
print(ret)

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

"""Boxes and Candies

@author: Liz
@modified: 12-04-2016

"""


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

N, x = readline()
candies = readline()


def solve():

    count = 0

    for i in range(N - 1):

        stc = candies[i] + candies[i + 1]
        if stc > x:
            count += stc - x
            candies[i] += min(0, candies[i + 1] - (stc - x))
            candies[i + 1] = max(0, candies[i + 1] - (stc - x))

    return count

print(solve())

Solution (Python):
a,x=map(int,raw_input().split())
l=map(int,raw_input().split())
k=0
for i in range(1,a):
    b=l[i-1]
    c=l[i]
    d=b+c
    if(d>x):
        e=d-x
        k=k+e
        if(c>=e):
            f=c-e
            l[i]=f
        else:
            f=0
            g=b-(e-c)
            l[i]=f
            l[i-1]=g
print k

Solution (Python):


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


counter = 0


dif1 = a[N-2] + a[N-1] - x
if dif1 > 0:
        a1 = a[N-2] - dif1
        if a1 > 0:
            a[N-2] -= dif1
            counter  += dif1
        else:
            a[N-1]-= -a1
            a[N-2] = 0
            counter += dif1

for i in range(N-2):
    dif1 = a[i] + a[i+1] - x
    if dif1 > 0:
        a1 = a[i+1] - dif1
        if a1 > 0:
            a[i+1] -= dif1
            counter  += dif1
        else:
            a[i]-= -a1
            a[i+1] = 0
            counter += dif1

       
while a[N-2] + a[N-1] > x:
    if a[N-2] > 0:
        a[N-2] -= 1
    else :
        a[N-1] -= 1
    
    counter += 1

print(counter)

Solution (Python):
s=input().split()
n=int(s[0])
x=int(s[1])
s=input().split()
ans=max(0,int(s[0])+int(s[1])-x)
prev=max(0,int(s[1])-ans)
for i in range(2,len(s)):
 now=max(0,int(s[i])+prev-x)
 ans+=now
 prev=int(s[i])-now
print(ans)

Solution (Python):
ini=map(int, raw_input().split())
box=map(int, raw_input().split())

time=0

for i in range(ini[0]-1):
	pre_time=0
	if box[i]+box[i+1]>ini[1]:
		pre_time+=box[i]+box[i+1]-ini[1]
		box[i+1]-=pre_time
		if box[i+1]<0:
			box[i]+=box[i+1]
			box[i+1]=0
	time+=pre_time

print time

Solution (Python):
_, x = tuple(int(x) for x in input().split())
a_lst = [int(a) for a in input().split()]

cnt = 0

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

    if a_lst[i] + a_lst[i + 1] > x:
        d = a_lst[i] + a_lst[i + 1] - x
        cnt += d
        a_lst[i + 1] -= d

print(cnt)

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

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

Solution (Python):
import math;
N,x = map(int, raw_input().split());
a = map(int, raw_input().split());
cnt = 0;
for i in range(N):
    if(a[i] > x):
        cnt += a[i] - x;
        a[i] = x;
    if(i > 0 and 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()))
s1 = sum(a)

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

print(s1-s2)

Solution (Python):


l = list(map(int, input().split()))
a = [0]
a.extend(list(map(int, input().split())))

num = 0
f = True
while(f):
    f = False
    for i in range(l[0]):
        if(((a[i] + a[i+1]) > l[1])):
            f = True
            sub = (a[i] + a[i+1]) - l[1]
            a[i+1] -= sub
            num += sub
print(num)

Solution (Python):
N,x=map(int,raw_input().split())
a=map(int,raw_input().split())
ans=0
for looking in range(1,len(a)):
    calc=a[looking]+a[looking-1]
    if calc<=x:
        continue
    sa=calc-x
    taberu=min(a[looking],sa)
    a[looking]-=taberu
    ans+=taberu
    sa-=taberu
    a[looking-1]-=sa
    ans+=sa
print ans

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

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

print(count)

Solution (Python):

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

over = a[0]-x
if over < 0:
    over = 0
eat, a[0] = over, a[0]-over
for i in range(N-1):
    over = a[i]+a[i+1]-x
    if over < 0:
        over = 0
    eat, a[i+1] = eat + over, a[i+1]-over
print(eat)

Solution (Python):


n, x = [int(w) for w in raw_input().split()]
a = [int(w) for w in raw_input().split()]

b = [a[i] + a[i+1] for i in xrange(n - 1)]

ans = 0
for i in xrange(n - 1):
    if b[i] > x:
        d = b[i] - x
        ans += d
        b[i] = x
        if i < n - 2:
            b[i + 1] = b[i + 1] - min(d, 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
for i in xrange(len(a)-1):
    if sum(a[i:i+2]) > x:
        ans += (sum(a[i:i+2]) - x)
        a[i+1] -= (sum(a[i:i+2]) - x)

print ans

Solution (Python):

import math
import string
import time
import fractions

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


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

Solution (Python):



import sys

def rl(proc=None):
    if proc is not None:
        return proc(sys.stdin.readline())
    else:
        return sys.stdin.readline().rstrip()

def srl(proc=None):
    if proc is not None:
        return map(proc, rl().split())
    else:
        return rl().split()

def solve(A, x):
    p = min(A[0], x)
    r = A[0] - p
    for i in xrange(1, len(A)):
        c = A[i]
        eat = max(0, c + p - x)
        p = c - eat
        r += eat
    return r

def main():
    N, x = srl(int)
    A = srl(int)
    print min(solve(A, x), solve(list(reversed(A)), x))

if __name__ == '__main__':
    main()