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.



Sample input:
3 3
2 2 2


Sample output:
1

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
	n, x = map(int, input().split())
	a = list(map(int, input().split()))
	ans = 0
	for i in range(1, n):
		temp = x - a[i] - a[i-1]
		if temp < 0:
			ans += min(-1*temp, a[i])
			a[i] -= min(-1*temp, a[i])
		temp = x - a[i] - a[i-1]
		if temp < 0:
			ans += min(-1*temp, a[i-1])
			a[i-1] -= min(-1*temp, a[i-1])
	# print(a)
	print(ans)

main()

Submission status: Accepted

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

ans = 0
left_minus = 0
right_minus = 0
for i in range(N - 1):
    left = A[i]
    right = A[i + 1]
    over = left + right - left_minus - x
    if over > 0:
        if over <= right:
            right_minus = over
        else:
            right_minus = right
            left_minus += over - right
    else:
        right_minus = 0

    ans += left_minus
    left_minus = right_minus

    if i == N - 2:
        ans+= left_minus

print(ans)

Submission status: Accepted

Solution code:
def solve(N, x, As):
    ans = max(0, As[0] - x)
    As[0] -= ans
    for i in range(0, N - 1):
        d = As[i] + As[i + 1]
        ans += max(0, d - x)
        As[i + 1] -= max(0, d - x)
    return ans


if __name__ == "__main__":
    N, x = map(int, input().split(" "))
    As = list(map(int, input().split(" ")))
    print(solve(N, x, As))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
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(1, N):
        d = a[i] + a[i-1] - X
        if d > 0:  # need to eat
            a[i] -= d
            ans += d
            if a[i] < 0:
                a[i-1] += a[i]
                a[i] = 0
    return ans


def main():
    print(solve())


if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i in range(N-2):
    if b[i] <= 0:
        continue
    c = min(b[i],a[i+1])
    ans += b[i]
    b[i] = 0
    b[i+1] -= c
    a[i+1] -= c

ans += max(b[-1],0)

print(ans)

Submission status: Accepted

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

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

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

print(cnt)

Submission status: Accepted

Solution code:
from collections import deque
n, x = map(int, input().split())
*a, = map(int, input().split())
a = deque(a)
a.appendleft(0)
a.append(0)
ans = 0

for k in range(1, n+1):
    s = a[k-1]+a[k]-x
    if s > 0:
        a[k] -= s
        ans += s

print(ans)

Submission status: Accepted

Solution code:
n,x=map(int,input().split())
*a,=map(int,input().split())
c=0

for i in range(n-1):
    v=a[i]+a[i+1]
    if i and v-x>a[i+1]:
        a[i+1]=x-a[i]
        c+=(v-x)
    else:
        if 0<v-x<=a[i+1]:
            a[i+1]-=(v-x)
            c+=(v-x)
        elif v-x>a[i+1]:
            a[i]-=(v-x-a[i+1])
            a[i+1]=0
            c+=(v-x)

print(c)

Submission status: Accepted

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

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

if A[0] > x:
        need = A[0] - x
        sub = min(A[0], need)
        A[0] -= sub
        ans += sub

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
s = 0
n,k =  map(int, input().split())
x = list(map(int, input().split()))
for i in range(1,n):
    if x[i-1]> k:
        s+=x[i-1]-k
        s+=x[i]
        x[i] = 0
    elif x[i-1]+x[i]>k:
        s+=x[i]+x[i-1]-k
        x[i] -=x[i]+x[i-1]-k
        #print(i,s)
print(s)

Submission status: Accepted

Solution code:
n,x = (int(i) for i in input().split())
a = list(int(i) for i in input().split())
ct = 0
for i in range(n-1):
  if a[i]+a[i+1] > x:
    ct += a[i]+a[i+1]-x
    a[i+1] -= a[i]+a[i+1]-x
    if a[i+1]<0:
      a[i+1] =0

print(ct)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(ANS)

Submission status: Accepted

Solution code:
N,x=map(int,input().split())
A=list(map(int,input().split()))
A2=A[::-1]

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



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

print(min(ANS,ANS2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    n, x = map(int, input().split())
    aa = list(map(int, input().split()))
    res = 0
    for i in range(1, n):
        sub = aa[i - 1] + aa[i] - x
        if sub > 0:
            aa[i] -= sub
            if aa[i] < 0:
                aa[i - 1] += aa[i]
                aa[i] = 0
            res += sub
    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n, x = map(int, input().split())
a = input().split()
a = [int(m) for m in a]
ans = 0
for i in range(1, n):
    t = a[i] + a[i - 1]
    if t <= x:
        pass
    else:
        ans += a[i] - (x - a[i - 1])
        if i == 1:
            if x - a[i - 1] < 0:
                a[i] = 0
                a[i - 1] = x
            else:
                a[i] = x - a[i - 1]
        else:
            a[i] = x - a[i - 1]
print(ans)

Submission status: Accepted

Solution code:
N, X = map(int, input().split())
a = list(map(int, input().split()))

ans = 0

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

Submission status: Accepted

Solution code:
n, x = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
if n==2:
    print(max(0, sum(a)-x))
    exit()
for i in range(n-1):
    s = a[i] + a[i+1]
    if s <= x:
        continue
    else:
        d = s-x
        ans += d
        if a[i+1] >= 1:
            if a[i+1] >= d:
                a[i+1] -= d
            else:
                a[i+1] = 0
                a[i] -= d-a[i+1]
        else:
            a[i] -= d
print(ans)

Submission status: Accepted

Solution code:
# 後ろの方を減らした方が次に役立つよねってことね(貪欲法)

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

a += [0]

count = 0
for i in range(N):
    extra = a[i]+a[i+1] - X
    if extra <= 0:
        continue
    count += extra
    if a[i+1] >= extra:
        a[i+1] -= extra
    else:
        a[i] -= extra-a[i+1]
        a[i+1] = 0
    # print(a)
print(count)

Submission status: Accepted

Solution code:
# encoding: utf-8
N, x = map(int, input().split())
a = list(map(int, input().split()))

sums = [0] * (N - 1)
for i, ai in enumerate(a):
    if i == 0: prev = ai
    else:
        sums[i - 1] = ai + prev
        prev = ai

ans = 0

if a[0] > x:
    ans += (a[0] - x)
    sums[0] -= (a[0] - x)

for i in range(N - 1):
    # skip
    if sums[i] <= x: continue
    # eat
    ans += (sums[i] - x)
    if i < N - 2: sums[i + 1] -= (sums[i] - x)
    sums[i] = x

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print(count)

Submission status: Accepted

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

Submission status: Accepted

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

b = a[:]

if b[0] > x:
    b[0] = x

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

print(ans)

Submission status: Accepted

Solution code:
N, x = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]
eat = [0] * N
eat[0] = max(A[0] - x, 0)
for i in range(N - 1):
    eat[i + 1] = max(A[i + 1] + A[i] - eat[i] - x, 0)
print(sum(eat))

Submission status: Accepted

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

# 左から順にみていき、左隣のaが多いなら、自分の飴を減らす
count = 0
for i in range(1, n):
    if a[i - 1] + a[i] > x:
        # print("hit:{}".format(i))
        if a[i - 1] <= x:
            count += a[i - 1] + a[i] - x
            a[i] -= a[i - 1] + a[i] - x
        else:
            count += a[i]
            a[i] = 0
            count += a[i-1] - x
            a[i-1] = x
    # print(a)
print(count)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n,x = map(int,raw_input().split())
arr = map(int,raw_input().split())
su = arr[0]
ans = 0
for i in range(1,n):
    su += arr[i]
    if su > x:
        diff = min(arr[i],su-x)
        arr[i] -= diff
        su = arr[i-1]+arr[i]
        if su > x:
            diff1 = min(arr[i-1],su-x)
            arr[i-1] -= diff1
            su = arr[i-1]+arr[i]
            ans += diff1
        ans += diff
    su = arr[i]
print ans

Submission status: Accepted

Solution code:
N, x = [int(x) for x in input().split()]
A = [int(x) for x in input().split()]
s = sum(A)
cnt = 0
for i in range(1, len(A)):
    if A[i-1] + A[i] >= x:
        if x >= A[i-1]:
            A[i] -= A[i-1] + A[i] - x
        else:
            A[i] = 0
            A[i-1] -= A[i-1] - x

print(s - sum(A))

Submission status: Accepted

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

l_A = len(A)
cnt = 0
for i in range(l_A):
    j = l_A - i - 1
    if i >= j:
        break
    # left
    if A[i] + A[i+1] > x:
        diff = (A[i] + A[i+1]) - x
        if A[i+1] >= diff:
            cnt += diff
            A[i+1] = A[i+1] - diff
        else:
            remain_diff = diff - A[i+1]
            A[i+1] = 0
            A[i] = A[i] - remain_diff
            cnt += diff

    # right
    if A[j-1] + A[j] > x:
        diff = (A[j-1] + A[j]) - x
        if A[j-1] >= diff:
            cnt += diff
            A[j-1] = A[j-1] - diff
        else:
            remain_diff = diff - A[j-1]
            A[j-1] = 0
            A[j] = A[j] - remain_diff
            cnt += diff

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# input

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8

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

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

for i in range(len(a) - 1):
    if a[i] + a[i + 1] > x:
        tmp_num = max(0, a[i] + a[i + 1] - x)
        count += tmp_num
        a[i + 1] -= tmp_num
print(count)

Submission status: Accepted

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

count = max(0, a[0] - x)
a[0] = min(a[0], x)
for i in range(1, n):
    sub = max(0, a[i - 1] + a[i] - x)
    count += sub
    a[i] -= sub

print(count)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
N, x = map(int, input().split())
an = list(map(int, input().split()))
an_s = list(an)

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

ans = 0
for org,aft in zip(an,an_s):
    ans += abs(org - aft)

# print(an_s)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

if a[0] > x:
	eat += a[0] - x
	a[0] = x
#if a[-1] > x:
#	eat += a[-1] -x
#	a[-1] = x

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

Submission status: Accepted

Solution code:
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 and x < a[i] :
        ans += a[i+1]+a[i]-x
        
        a[i] = a[i]-(x-a[i+1])
        a[i+1] = 0
        
    elif (a[i]+a[i+1]) > x:
        ans += a[i+1]+a[i]-x
        
        a[i+1] = x-a[i]
        
print(ans)

Submission status: Accepted

Solution code:
def 解():
    iN,ix = [int(_) for _ in input().split()]
    aA =[int(_) for _ in input().split()]

    #if iN*ix < sum(aA)
    if ix == 0 :
        print(sum(aA))
    else:
        iCount = 0
        for i in range(1,iN):
            i差 = aA[i-1] + aA[i] -ix
            if 0 < i差  :
                if aA[i] <= i差:
                    aA[i] = 0
                    iCount += i差
                else:
                    iCount += i差
                    aA[i] -= i差

        print(iCount)
解()

Submission status: Accepted

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

AA = [0 for i in range(N-1)]
for i in range(N-1):
    AA[i] = A[i] + A[i+1]

cnt = 0

for i in range(N-1):
    aa = AA[i]
    diff = aa - x

    if aa > x:
        cnt += diff
        if A[i+1] >= diff:
            A[i+1] -= diff
        else:
            A[i]   -= diff - A[i+1]
            A[i+1]  = 0

        if i < N -2:
            AA[i] = A[i] + A[i+1]
            AA[i+1] = A[i+1] + A[i+2]
        else:
            AA[i] = A[i] + A[i+1]

print(cnt)

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
n,x = map(int, input().split())
list_a = list(map(int, input().split()))
list_sum = [list_a[0]]
for i in range(n-1):
    list_sum.append(list_a[i+1] + list_a[i])
list_sum.append(10**10)
count = 0
for i in range(n):
    if list_sum[i] > x:
        count += list_sum[i] - x
        list_sum[i+1] -= list_sum[i] - x
        list_sum[i] = x
print(count)

Submission status: Accepted

Solution code:
#!/usr/bin/env python

from collections import deque
import itertools as it
import sys
import math

sys.setrecursionlimit(1000000)

INF = 10 ** 18

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

print ans

Submission status: Accepted

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

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

print(eat)

Submission status: Accepted

Solution code:
from collections import defaultdict, Counter
from itertools import product, groupby, count, permutations, combinations
from math import pi, sqrt
from collections import deque
from bisect import bisect, bisect_left, bisect_right
from string import ascii_lowercase
from functools import lru_cache
import sys
sys.setrecursionlimit(10000)
INF = float("inf")
YES, Yes, yes, NO, No, no = "YES", "Yes", "yes", "NO", "No", "no"
dy4, dx4 = [0, 1, 0, -1], [1, 0, -1, 0]
dy8, dx8 = [0, -1, 0, 1, 1, -1, -1, 1], [1, 0, -1, 0, 1, 1, -1, -1]


def inside(y, x, H, W):
    return 0 <= y < H and 0 <= x < W


def ceil(a, b):
    return (a + b - 1) // b


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

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys


Nx = input().split()
an = input().split()

anx = list()

for item in an:
    anx.append(int(item))

x = int(Nx[1])
N = int(Nx[0])
i = 0
count = 0
while i < N - 1:
    sum = anx[i] + anx[i + 1]
    
    if sum > x:
        count += sum - x
        num = sum - x
        anxi = anx[i+1]
        anxi -= num
        if anxi < 0:
            anxi2 = anx[i] + anxi
            anx[i] = anxi2
            anxi = 0
        anx[i+1] = anxi
    i += 1
print(count)

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-

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

ans = 0
if a[0] > x:
    ans += a[0] - x
    a[0] = x
for i, v in enumerate(a[:-1]):
    if a[i] + a[i + 1] > x:
        ans += a[i] + a[i + 1] - x
        a[i + 1] = x - a[i]

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

res = float('inf')
for b in (a,a[::-1]):
    tmp = 0
    for i in range(n-1):
        if b[i]+b[i+1]>x:
            tmp += b[i] + b[i+1] - x
            if b[i]>x:
                b[i],b[i+1] = x,0
            else:
                b[i+1] = x - b[i]
    res = min(res,tmp)

print(res)

Submission status: Accepted

Solution code:
il = lambda: list(map(int, input().split()))
ils = lambda n, s="int(input())": [eval(s) for _ in range(n)]

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

Submission status: Accepted

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

ans = 0
prev = A[0]
if prev > X:
  ans = prev - X
  prev = X
  
for i in range(1,N):
  now = A[i]
  eat = max(0, prev + now - X)
  ans += eat
  prev = now - eat
  
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

result = 0

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

print(result)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,x=map(int, input().split())
a=list(map(int, input().split()))
b=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]
ans=0
for i in range(n):
    ans+=b[i]-a[i]
print(ans)

Submission status: Accepted

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

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

print(count)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

ans = 0

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

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

print(ans)

Submission status: Accepted

Solution code:
f,g = list(map(int,input().split()))

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

c = 0
p = 1
if a[0] > g:
    c += a[0] - g
    a[0] = g
while p <= f-1:
    if a[p-1] + a[p] > g:
        eat = a[p-1] + a[p] - g
        c += eat
        a[p] -= eat
    p += 1
print(c)

Submission status: Accepted

Solution code:
n, x = map(int, input().split())
a = list(map(int, input().split()))
if a[0] > x:
    pre = x
    num = a[0] - x
else:
    pre = a[0]
    num = 0

for i in range(1, n):
    if a[i] + pre > x:
        num += a[i] + pre - x
        pre = a[i] - (a[i] + pre - x)
    else:
        pre = a[i]

print(num)

Submission status: Accepted

Solution code:
N,x = (int(i) for i in input().split())
a = [int(i) for i in input().split()]
count = 0
if a[1] > x:
    difference = x-a[1]
    count += difference
    a[1] -= difference
     
for i in range(N-1):
    difference = x - (a[i] + a[i+1])
    if difference < 0:
        count += abs(difference)
        a[i+1] += difference
        if a[i+1] < 0:
            a[i] += a[i]
            a[i+1] = 0 


print(count)