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:
# -*- coding: utf-8 -*-
"""
Created on Mon Aug 13 14:21:46 2018
ABC048C
@author: maezawa
"""

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

cnt = 0
pre = 0
for i in a:
    if pre + i <= x:
        pre = i
        continue
    elif x-pre >= 0:
        cnt += i - (x-pre)
        pre = x-pre
    else:
        cnt += pre + i - x
        pre = 0

print(cnt)

Submission status: Accepted

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

def main():
    n, x = map(int, input().split())
    a_list = list(map(int, input().split()))
    a_list_cp = a_list[:]
    ans = 0

    for i in range(1, n):
        temp = a_list[i-1] + a_list[i]
        if temp > x:
            if x - a_list[i-1] >= 0:
                a_list[i] = x - a_list[i-1]
            else:
                a_list[i] = 0
                a_list[i-1] = a_list[i-1] + x - a_list[i-1]
    for a, b in zip(a_list, a_list_cp):
        ans += b - a
    print(ans)

if __name__ == '__main__':
    main()

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N,X=map(int,input().split(" "))
A=input()
A=A.rstrip().split(" ")
listA=[]
cnt=0
for i in range(N):
    listA.append(int(A[i]))
    if i==0:
        if listA[0]>X:
            cnt+=listA[0]-X
            listA[0]=X
    else:
        if listA[i-1]+listA[i]>X:
            cnt+=(listA[i-1]+listA[i]-X)
            listA[i]=X-listA[i-1]
print(cnt)

Submission status: Accepted

Solution code:
N,x = map(int,input().split())
a = list(map(int,input().split()))
a_sum = 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] -= max(0,(a[i]+a[i+1]-x))
    else:
        pass
print(a_sum-sum(a))

Submission status: Accepted

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

Submission status: Accepted

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

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

cnt = 0
# 初期値設定
if aN[0] > x:
    cnt += aN[0] - x
    aN[0] -= aN[0] - x

# 2番目以降を一つ前と比べていく
for i in range(1,N):
    if aN[i-1] + aN[i] > x:
        cnt += aN[i-1] + aN[i] - x
        aN[i] -= aN[i-1] + aN[i] - x

print(cnt)

Submission status: Accepted

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

ans = 0
y = x

for a in A:
  if a > y:
    ans += a - y
  y = x - min(a, y)
  
print(ans)

Submission status: Accepted

Solution code:
a,b=map(int,input().split())
k=list(map(int,input().split()))

ans=0

for i in range(0,a-1):
    now=k[i]+k[i+1]
    if now > b:
        c=now-b
        if k[i+1]<c:
            k[i+1]=0
        else:
            k[i+1]-=c
        ans+=c
#print(k)
print(ans)

Submission status: Accepted

Solution code:
N,x = map(int, input().split())
A = [int(x) for x in input().split()]
 
ans = 0
p = 0
for a in A:
    e = max(0, a + p - x)
    ans += e
    p = a - e
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:
    eat = A[0] - x
    ans += eat
    A[0] -= eat
for i in range(1, n):
    s = A[i-1] + A[i]
    if s > x:
        eat = s - x
        A[i] -= eat
        ans += eat
print(ans)

Submission status: Accepted

Solution code:

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

    ans=0

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

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

    print(ans)

if __name__ == "__main__":
        main()

Submission status: Accepted

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

ans = 0
p = 0
for a in A:
    s = a + p
    p = a
    if s > x:
        e = s - x
        ans += e
        p = a - e
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:
        diff = (a[i + 1] + a[i]) - x
        if diff <= a[i + 1]:
            a[i + 1] = a[i + 1] - diff
        else:
            a[i + 1] = 0
            a[i] = a[i] - (diff - a[i + 1])
        cnt += diff

print(cnt)

Submission status: Accepted

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

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

print(count)

Submission status: Accepted

Solution code:
n, x = list(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:
        eat = min(a[i]+a[i-1]-x, a[i])
        ans += eat
        a[i] -= eat
    if a[i]+a[i-1] > x:
        eat = min(a[i]+a[i-1]-x, a[i-1])
        ans += eat
        a[i-1] -= eat

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

eat_count = 0

for i, a in enumerate(candy_list[:-1]):
  gap = candy_list[i] + candy_list[i+1] - x
  if gap <= 0:
    continue
  eat_count += gap
  if gap > 0:
    if candy_list[i+1] - gap >= 0:
      candy_list[i+1] -= gap
    else:
      candy_list[i+1] = 0
      gap -= candy_list[i+1]
      candy_list[i] -= gap

print(eat_count)

Submission status: Accepted

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

ans = 0

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

print(ans)

Submission status: Accepted

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

result = 0
mode = 0
if a[0] > x:
    result += a[1]
    a[1] = 0
    result += a[0] - x
    a[0] = x
for i in range(1, n):
    s = a[i] + a[i - 1]
    if s > x:
        t = s - x
        result += t
        a[i] -= t

print(result)

Submission status: Accepted

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

Ans = 0
# 初項が超過している場合を考慮
d = max(0,A[0]-X)
A[0] -= d
Ans += d
# 第2項以降は貪欲に処理
for i in range(1,N):
    d = max(0,A[i]+A[i-1]-X)
    A[i] -= d
    Ans += d

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print(sum(a)-sum(v))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def m():
    n, x = map(int, input().split())
    a = list(map(int, input().split()))
    b = a.copy()
    ans = 0
    for i in range(n):
        if b[i] > x:
            b[i] = x

    for j in range(1, n):
        if b[j-1] + b[j] > x:
            b[j] -= (b[j-1] + b[j]) - x
        else:
            pass
    for k in range(n):
        ans += a[k] - b[k]
    return ans

print(m())

Submission status: Accepted

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

prev = ans1 = 0
for a in src:
    if prev+a > X:
        eat = prev+a - X
        ans1 += eat
        prev = max(0, a-eat)
    else:
        prev = a

prev = ans2 = 0
for a in src[::-1]:
    if prev+a > X:
        eat = prev+a - X
        ans2 += eat
        prev = max(0, a-eat)
    else:
        prev = a

print(min(ans1,ans2))

Submission status: Accepted

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

ans1 = 0
tmp = 0
for a in src:
    eat = max(0, tmp + a - X)
    ans1 += eat
    tmp = a - eat
ans2 = 0
tmp = 0
for a in reversed(src):
    eat = max(0, tmp + a - X)
    ans2 += eat
    tmp = a - eat
print(min(ans1,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(1, n):
    if a[i]+a[i-1] > x:
        d = a[i]+a[i-1]-x
        a[i] -= d
        ans += d
print(ans)

Submission status: Accepted

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

# AtCoder Beginner Contest
# Problem C

if __name__ == '__main__':
    n, x = list(map(int, input().split()))
    a = list(map(int, input().split()))
    count = 0

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

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

        if diff > 0:
            count += diff
            a[i + 1] -= diff

    print(count)

Submission status: Accepted

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

# AtCoder Beginner Contest
# Problem C

if __name__ == '__main__':
    n, x = list(map(int, input().split()))
    a = list(map(int, input().split()))
    count = 0

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

    for i in range(n - 1):
        summed = a[i] + a[i + 1]

        if summed > x:
            diff = summed - x
            count += diff
            a[i + 1] -= diff

    print(count)

Submission status: Accepted

Solution code:
from sys import stdin

N, x = [int(x) for x in stdin.readline().rstrip().split()]
candys = [int(x) for x in stdin.readline().rstrip().split()]

count = 0

# xより多く入っている箱のキャンディは必ず食べる必要がある。
for i in range(N):
    if candys[i] > x:
        num = candys[i] - x
        candys[i] = x
        count += num
        # print(candys)

# print('count = ', count)
# print('---------------------------')

# 隣り合った箱を見て、xを越えていれば食べる
for i in range(N-1):
    if candys[i] + candys[i+1] > x:
        num = candys[i] + candys[i+1] - x
        count += num
        candys[i+1] = candys[i+1] - num
        # print(candys)
# 結果
print(count)

Submission status: Accepted

Solution code:
N, x = map(int, input().split(' '))
Candy = list(map(int, input().split(' ')))
count = 0
if Candy[0] > x:
    count += Candy[0] - x
    Candy[0] = x

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

print(count)

Submission status: Accepted

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


def put_sum_candies(candies):
    base = 0
    for i, _ in enumerate(candies):
        if i == 0:
            continue
        if candies[i-1] + candies[i] <= x:
            continue
        if candies[i-1] > x:
            base += candies[i-1] - x
            candies[i-1] -= candies[i-1] - x
            base += candies[i] + candies[i-1] - x
            candies[i] -= candies[i] + candies[i-1] - x
        elif candies[i-1] <= x:
            base += candies[i] + candies[i-1] - x
            candies[i] -= candies[i] + candies[i-1] - x
    return base


base = put_sum_candies(candies)
print(base)

Submission status: Accepted

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

    bef = sum(a)
    for i in range(n-1):
        if a[i] + a[i+1] > x:
            a[i+1] -= min(a[i]+a[i+1]-x, a[i+1])
            a[i] -= a[i] + a[i+1] - x

    print(bef-sum(a))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def c_boxes_and_candies(N, X, A):
    ans = 0
    if A[0] > X:  # 最初からXを超えていたら、減らすしかない
        ans += A[0] - X
        A[0] = X
    for i in range(N - 1):  # A[i]+A[i+1]==Xになるまで減らす
        if A[i] + A[i + 1] > X:  # A[i+1]だけ減らしたほうが得する
            ans += A[i + 1] - (X - A[i])
            A[i + 1] = X - 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))

Submission status: Accepted

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

b = [0]
for i in range(n):
    b.append(a[i])
b.append(0)    
counter = 0

if b[1] > x:
    counter = b[1] - x
    b[1] = x
for i in range(n+1):
    if b[i] + b[i+1] > x:
        s = b[i] + b[i+1] - x
        if b[i+1] - s > -1:
            b[i+1] = b[i+1] -s
            counter = counter + s
        else:
            k = b[i+1]
            counter = counter + s
            b[i+1] = 0
            b[i] = x
        
print(counter)

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

print(ans)

Submission status: Accepted

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

ans = 0

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

Submission status: Accepted

Solution code:
N,M=map(int,input().split())
s=list(map(int,input().split()))
w=0
if s[0]+s[1]>M and s[0]>M:
    w+=s[1]+s[0]-M
    s[1]=0
    s[0]=M
for i in range(0,N-1):
    if s[i]+s[i+1]>M:
        w+=s[i]+s[i+1]-M
        s[i+1]=M-s[i]
print(w)

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
a=list(map(int,input().split()))
total=sum(a)
if m<a[0]:
    a[0]=m
for i in range(1,n):
    if m<a[i]+a[i-1]:
        a[i]=m-a[i-1]
tot=sum(a)
print(total-tot)

Submission status: Accepted

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

ans = 0
for i in range(1,N):
    if src[i-1] + src[i] <= X: continue
    eat = src[i-1] + src[i] - X
    src[i] = max(0, src[i] - eat)
    ans += eat
print(ans)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:
n,x=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]
y=0
a1=0
a2=0
for a3 in a:
  s = max(a1+a2-x,a2+a3-x,0)
  y += s
  a1 = a2 
  a2 = a3 - s
print(y)

Submission status: Accepted

Solution code:
n,x = map(int,input().split())
a = list(map(int, input().split()))
ans = 0
if x<a[0]:
    ans = abs(x-a[0])
    a[0] -= abs(x-a[0])

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

print(ans)

Submission status: Accepted

Solution code:
N,x=map(int,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 in range(1,N):
  if a[i-1]+a[i]>x:
    ans+=a[i]+a[i-1]-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()))
ar = a[::-1]
ans = [0, 0]
for i in range(1, n):
    if a[i] + a[i-1] > x:
        ans[0] += (a[i] + a[i-1]) - x
        a[i] = max(0, x - a[i-1])
    if ar[i] + ar[i-1] > x:
        ans[1] += (ar[i] + ar[i-1]) - x
        ar[i] = max(0, x - ar[i-1])

print(min(ans))

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
def inpl(): return map(int, input().split())

N, x = inpl()
A = [0] + list(inpl())

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

Submission status: Accepted

Solution code:
inline = list(map(int,input().split()))
N = inline[0]
x = inline[1]
inline = list(map(int,input().split()))
two = []
cost = 0;
for i in range(N-1):
    two.append(inline[i]+inline[i+1])

for i in range(N-2):
    if two[i] > x:
        cost += (two[i]-x)
        two[i+1] -= min((two[i]-x),inline[i+1])

if two[-1]>x:
    cost += (two[-1]-x)

print(cost)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def eat_candies(candies, x):
    eaten = max(0, candies[0] - x)
    candies[0] -= eaten
    for i in range(1, len(candies)):
        extra = max(0, candies[i-1] + candies[i] - x)
        candies[i] -= extra
        eaten += extra
    return eaten
        
        
if __name__ == '__main__':
    n, x = list(map(int, input().split()))
    candies = list(map(int, input().split()))
    print(eat_candies(candies, x))

Submission status: Accepted

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

Submission status: Accepted

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

A = [int(i) for i in input().split()]
cnt = 0
for i in range(n-1):
    a = (A[i]+A[i+1])-x
    if a > 0:
        b = A[i+1] - a
        if b < 0:
            cnt += a
            A[i+1] = 0
            A[i] += b
        else:
            cnt += a
            A[i+1] -= a
      

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

share = 0
rm = [0] * (N-1)
for i in range(N-1):
	rm[i] = max(0,(a[i]+a[i+1])-x)


for i in range(N-2):
	s = min([rm[i],rm[i+1],a[i+1]])
	share += s
	rm[i] -= s
	rm[i+1] -= s

print(share + sum(rm))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
def c_Boxes_and_Candies(N, X, A):
    a = [x for x in A]
    if a[0] > X:
        # 最初からXを超えていた場合、減らさなければ条件を満たせない
        a[0] = X
    for i in range(0, N - 1):
        if a[i] + a[i + 1] > X:
            # a[i]+a[i+1]==Xになるまで減らす。a[i+1]だけ減らしたほうが得する
            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))

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

print(ans)

Submission status: Accepted

Solution code:
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:
            # slice(-2, 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()

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

Submission status: Accepted

Solution code:
N,x=map(int,raw_input().split())
#print N,x
a=map(int,raw_input().split())
#print a

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

Submission status: Accepted

Solution code:
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])
# print(a)
# print(sums)

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)

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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


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


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


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

Submission status: Accepted

Solution code:
import sys

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


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


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


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

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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()

Submission status: Accepted

Solution code:
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