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 -*-
"""
C - Boxes and Candies
https://atcoder.jp/contests/abc048/tasks/arc064_a

"""
import sys


def solve(N, x, candies):
    i = 1
    ans = 0
    while i < N:
        gap = candies[i - 1] + candies[i] - x
        if gap > 0:
            if gap <= candies[i]:
                candies[i] -= gap
                ans += gap
            else:
                ans += gap
                candies[i] = 0
        i += 1
    return ans


def main(args):
    N, x = map(int, input().split())
    candies = [int(x) for x in input().split()]
    ans = solve(N, x, candies)
    print(ans)


if __name__ == '__main__':
    main(sys.argv[1:])

Submission status: Accepted

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

c = 0
for i in range(n-1):
    sum2 = vs[i] + vs[i+1]
    if sum2 <= x:
        continue

    surplus = sum2 - x
    c += surplus
    vs[i+1] -= min(vs[i+1], surplus)

print(c)

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

if a[0] > x:
    cnt = a[0] - x
    a[0] = x
else:
    cnt = 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:
import numpy as np
N, x = map(int, input().split())
A = np.array(list(map(int, input().split())))

count = np.sum((A - x)[A - x > 0])
A[A > x] = x
for i in range(1, N):
    if A[i] + A[i - 1] > x:
        decrease = A[i] + A[i - 1] - x
        A[i] = A[i] - decrease
        count += decrease
print(count)

Submission status: Accepted

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

B = [min(a, x) for a in A]
for i in range(N - 1):
    su = B[i] + B[i + 1]
    if su > x:
        B[i + 1] -= su - x

print(sum(a - b for a, b in zip(A, B)))

Submission status: Accepted

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

res = 0
pre = 0
for i in a:
    if i+pre>x:
        res += (i+pre-x)
        pre = i-(i+pre-x) 
    else:
        pre = i
print(res)

Submission status: Accepted

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

print(eat)

Submission status: Accepted

Solution code:
import sys
N, x = map(int, sys.stdin.readline().split())
a = list(map(int, sys.stdin.readline().split()))
a.insert(0,0)
ans = 0
for i in range(1,len(a)):
    if x < a[i-1] + a[i]:
        ans += (a[i-1] + a[i] - x)
        a[i] -= (a[i-1] + a[i] - 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):
    eat = 0
    if a[i] + a[i+1] > x:
        eat = a[i] + a[i+1] - x
        if eat <= a[i+1]:
            a[i+1] -= eat
        else:
            a[i+1] = 0
            a[i] -= (eat - a[i+1])
    ans += eat

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:


def read_input():
    n, x = map(int, input().split())
    alist = list(map(int, input().split()))

    return n, x, alist

def submit():
    n, x, alist = read_input()

    edit = 0

    if alist[0] > x:
        edit += alist[0] - x
        alist[0] = x

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

    print(edit)


if __name__ == '__main__':
    submit()

Submission status: Accepted

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

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

print(count_a)

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):
    num = max(a[i-1] + a[i] - x, 0)
    ans += num
    a[i] = max(0, a[i] - num)
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(1, N):
    candy = a[i-1] + a[i]
    if candy > x:
        ans += candy - x
        tmp = a[i]
        if tmp >= candy - x:
            a[i] -= candy - x
        else:
            a[i] = 0

print(ans)

Submission status: Accepted

Solution code:
from itertools import accumulate
n,x = map(int,input().split())
a = list(map(int,input().split()))
b = list(accumulate(a))
b = [0]+b
tmp = 0
cnt = 0
for i in range(n-1):
    t = b[i+2]-b[i]-x-tmp
    tmp = 0
    if t>0:
        cnt += t
        tmp += min(t,a[i+1])    
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(1,n):
    sum = a[i-1]+a[i]
    if sum>x:
        if sum-x<=a[i]:
            ans += sum-x
            a[i] -= sum-x
        else:
            ans += sum-x
            a[i] = 0
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(1,n):
  k=max(0,a[i]+a[i-1]-x)
  ans+=k
  a[i]=max(0,a[i]-k)
print(ans)

Submission status: Accepted

Solution code:
import functools
n, x = map(int, input().split())
a = [(0, 0)] + list(map(int, input().split()))

def f(a, b):
    d = max(0, (b + a[0]) - x)
    return max(0, b - d), a[1] + d

print(functools.reduce(f, a)[1])

Submission status: Accepted

Solution code:
import functools
n, x = map(int, input().split())
a = [0] + list(map(int, input().split()))

result = 0

def f(a, b):
    global result
    d = max(0, (b + a) - x)
    result += d
    return max(0, b - d)

functools.reduce(f, a)
print(result)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

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

    ans = 0
    a = 0
    for i in range(N):
        diff = (A[i] + a) - x
        diff = max(0, diff)
        ans += diff
        a = A[i] - diff

    return ans


if __name__ == '__main__':
    print(main())

Submission status: Accepted

Solution code:
N, K = map(int, input().split())
a = list(map(int, input().split()))
s = sum(a)

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

print(s - sum(a))

Submission status: Accepted

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

Submission status: Accepted

Solution code:


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

count_candy_eat = 0

for i in range(N):
    if(i == 0):
        if(candy_list[0] > x):
            count_candy_eat += candy_list[0] - x
            candy_list[0] = x
    else:
        if(candy_list[i-1] + candy_list[i] > x):
            if(x - candy_list[i-1] > 0):
                count_candy_eat += candy_list[i-1] + candy_list[i] - x
                candy_list[i] = x - candy_list[i-1]
            else:
                count_candy_eat += candy_list[i]
                candy_list[i] = 0


print(count_candy_eat)

Submission status: Accepted

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

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

count = 0
for i in range(N):
  count += A0[i]-A[i]
print(count)

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]=a[0]-x,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:
import sys
import collections
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())

n, x = nm()
a = nl()
ans = 0
for i in range(n):
    if i == 0:
        if a[i] <= x:
            tmp = a[i]
        else:
            if a[i] > x:
                ans += a[i] - x
                tmp = x
            else:
                tmp = a[i]
    else:
        if tmp + a[i] <= x:
            tmp = a[i]
        else:
            ans += tmp + a[i] - x
            tmp = x - tmp
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] = 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 = [int(i) for i in input().split()]

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

Submission status: Accepted

Solution code:
N,x = map(int,input().split())
a = [0] + list(map(int,input().split()))
import copy
tmpa = copy.deepcopy(a)

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

ans = 0
for i in range(N+1):
    ans += abs(a[i] - tmpa[i])
print(ans)

Submission status: Accepted

Solution code:
import sys, re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits

def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(): return list(map(int, input().split()))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, x = MAP()
a = LIST()
count = 0
def reduce_candy(i): # iとi+1のキャンディを減らす
	global count
	if sum_near[i] <= x: # 減らす必要なし
		pass 
	elif sum_near[i] - a[i] <= x: # 左のボックスから全部減らすと条件クリアの場合
		if not i == 0:
			sum_near[i-1] -= sum_near[i] - x
		a[i] -= sum_near[i] - x
		count += sum_near[i] - x
		sum_near[i] = x
	else: # 左のボックスと右のボックスの両方から取り出す必要あり
		if not i == 0:
			sum_near[i-1] -= a[i]
		sum_near[i] -= a[i]
		count += a[i]
		a[i] = 0
		a[i+1] -= sum_near[i] - x
		count += sum_near[i] - x
		sum_near[i] = x
sum_near = []
for i in range(N-1):
	sum_near.append(a[i] + a[i+1])

for i in range(N-2, -1, -1):
	reduce_candy(i)
	# print(count)
	# print(sum_near)
print(count)

Submission status: Accepted

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

B=[a for a in 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 a,b in zip(A,B):
    ans+=(a-b)

print(ans)

Submission status: Accepted

Solution code:
#C問題
N,x=map(int,input().split())
cand=list(map(int,input().split()))
count=0
for i in range(N-1):
    if cand[i]+cand[i+1]<=x:
        pass
    else:
        sa=x-cand[i]
        if sa >= 0:
            count+=(cand[i+1]-sa)
            cand[i+1]=sa
        else:
            count=(cand[i]+cand[i+1]-x)
            cand[i+1]=0
        
print(count)

Submission status: Accepted

Solution code:
n,x,*a=map(int,open(0).read().split())
a=[0]+a
c=0
for i in range(n):
  c+=max(0,a[i+1]+a[i]-x)
  a[i+1]=min(a[i+1],x-a[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):
	diff = (A[i] + A[i + 1]) - X
	if 0 < diff:
		count += diff
		if diff <= A[i + 1]:
			A[i + 1] -= diff
		else:
			diff -= A[i + 1]
			A[i + 1] = 0
			A[i] -= diff
print(count)

Submission status: Accepted

Solution code:
def boxes_and_candies(N: int, x: int, A: list) -> int:
    count = 0
    for i in range(1, N):
        a1, a2 = A[i - 1], A[i]
        # print(a1, a2)

        if a1 + a2 <= x:
            # OK
            # print('--> OK')
            continue

        diff = (a1 + a2) - x
        count += diff
        # print('--> {}'.format(diff))

        if a2 > diff:
            A[i] -= diff
        else:
            A[i-1] -= diff - A[i]
            A[i] = 0

    return count


if __name__ == "__main__":
    N, x = map(int, input().split())
    A = [int(s) for s in input().split()]
    ans = boxes_and_candies(N, x, A)
    print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from functools import reduce

def main():
    # 切り捨て
    # 4 // 3
    # 切り上げ
    #-(-4 // 3)
    
    # 初期値用:十分大きい数(100億)
    # 1e10

    # 初期値用:十分小さい数(-100億)
    # -1e10
    
    # 1文字のみを読み込み
    # 入力:2
    # a = input().rstrip()
    # 変数:a='2'
    
    # スペース区切りで標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a, b, c, d = (int(_) for _ in input().split())  
    # 変数:a=2 b=4 c=5 d =7
    
    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2 4 5 7
    # a = list(int(_) for _ in input().split())
    # 変数:a = [2, 4, 5, 7]    

    # 1文字ずつ標準入力を配列として読み込み
    # 入力:2457
    # a = list(int(_) for _ in input())
    # 変数:a = [2, 4, 5, 7]
    
    N,x = (int(_) for _ in input().split())
    a = list(int(_) for _ in input().split())
    cost = 0
    for i in range(1,N):
        tmp_sum = a[i-1] + a[i]
        if(tmp_sum > x):
            cost += tmp_sum - x
            a[i] = a[i] - (tmp_sum - x)
            if(a[i]<0):
                a[i-1] = a[i-1] + a[i]
                a[i] = 0
                
    print(cost)
    
if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N , X = map(int,input().split())
alist = list(map(int,input().split()))
first = sum(alist)
# 単独でXを超えている場合は減らす
for i in range(N):
    if alist[i] > X:
        alist[i] = X
# alistは最大値がXのリストになっている
for i in range(N-1):
    if alist[i]+alist[i+1] > X:
        alist[i+1] = (X - alist[i]) 

print(first - sum(alist))

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

Submission status: Accepted

Solution code:
# https://qiita.com/_-_-_-_-_/items/34f933adc7be875e61d0
# abcde	s=input()	s='abcde'
# abcde	s=list(input())	s=['a', 'b', 'c', 'd', 'e']
# 5(1つだけ)	a=int(input())	a=5
# 1 2	| x,y = s_inpl()   |	x=1,y=2
# 1 2 3 4 5 ... n 　	li = input().split()	li=['1','2','3',...,'n']
# 1 2 3 4 5 ... n 　	li = inpl()	li=[1,2,3,4,5,...,n]
# FFFTFTTFF 　	li = input().split('T')	li=['FFF', 'F', '', 'FF']

# INPUT
# 3
# hoge
# foo
# bar
# ANSWER
# n=int(input())
# string_list=[input() for i in range(n)]

import math
import copy
from collections import defaultdict, Counter
# 直積 A={a, b, c}, B={d, e}:のとき，A×B={(a,d),(a,e),(b,d),(b,e),(c,d),(c,e)}: product(A, B)
from itertools import product
# 階乗 P!: permutations(seq), 順列 {}_len(seq) P_n: permutations(seq, n)
from itertools import permutations
# 組み合わせ {}_len(seq) C_n: combinations(seq, n)
from itertools import combinations
from bisect import bisect_left, bisect_right
# import numpy as np

def s_inpl(): return map(int,input().split())
def inpl(): return list(map(int, input().split()))
INF = float("inf")

N, x = s_inpl()
a = inpl()

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:
        tmp = copy.deepcopy(a[i+1])
        a[i+1] = x - a[i]
        ans += tmp - a[i+1]

print(ans)

Submission status: Accepted

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

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

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

print(count)

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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


print(cnt)

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


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#両端を食べるのは非効率なので、両端はx個まで食べたうえで真ん中を食べる
N,x=(int(_) for _ in input().split())
a=[int(_) for _ in input().split()]
eat=0
if a[0]>x:
    eat+=a[0]-x
    a[0]=x
for i in range(0,N-1):
    wa=a[i]+a[i+1]
    if wa>x:
        a[i+1]=x-a[i]
        eat+=wa-x
print(eat)

Submission status: Accepted

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

res = 0
for i in range(N-1):
    if A[i] + A[i+1] > x:
        eat = A[i]+A[i+1]-x
        A[i+1] = max(0,A[i+1]-eat)
        res += eat
        if A[i+1]-eat < 0:
            A[i] -= abs(A[i+1]-eat)

#print(A)
print(res)

Submission status: Accepted

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

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

print(cnt)

Submission status: Accepted

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

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

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#ABC048C
N,x=map(int,input().split())
A=list(map(int,input().split()))
ans=0
for i in range(len(A)-1):
    t=max(0,sum(A[i:i+2])-x)
    ans+=t
    if A[i+1]<t:
        A[i+1]=0
    else:
        A[i+1]-=t
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    _, x = map(int, input().split())
    a = [int(s) for s in input().split()]

    print(solve(a, x))

def solve(a, x):
    b = 0
    s = 0

    for t in a:
        b = min(x - b, t)
        s += t - b
    
    return s

main()

Submission status: Accepted

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

def main():
	N, x = map(int,input().split(" "))
	tmp = input().split(" ")
	candy = [int(s) for s in tmp]
	
	ans = 0
	for i, val in enumerate(candy):
		if i+1 == len(candy): break
		
		tmp = candy[i] + candy[i+1]
		if x < tmp and candy[i+1] >= tmp - x:
			candy[i+1] = candy[i+1] - (tmp - x)
			ans = ans + (tmp - x)
		elif x < tmp:
			candy[i] = candy[i] - ((tmp - x) - candy[i+1])
			candy[i+1] = 0
			ans = ans + (tmp - x)
	
	print(ans)

if __name__ == "__main__":
	main()

Submission status: Accepted

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

def calc(A, X):
    a = A[:]
    r = 0
    for i in range(len(A) - 1):
        t = a[i] + a[i + 1]
        if t > X:
            d = t - X
            if d <= a[i + 1]:
                r += d
                a[i + 1] -= d
            else:
                r += d - a[i + 1] + a[i + 1]
                a[i] -= d - a[i + 1]
                a[i + 1] = 0
    #print(a)
    return r

result = calc(A, X)

print(result)

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:
        cnt += (A[i] + A[i + 1]) - x
        A[i + 1] = max(0, A[i + 1] - ((A[i] + A[i + 1]) - x))
        #print(A, cnt)
        
print(cnt)

Submission status: Accepted

Solution code:
#!usr/bin/env python3
from collections import defaultdict
from heapq import heappush, heappop
import sys
import math
import bisect
import random
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LS():return list(map(list, sys.stdin.readline().split()))
def S(): return list(sys.stdin.readline())[:-1]
def IR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = I()
    return l
def LIR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = LI()
    return l
def SR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = S()
    return l
def LSR(n):
    l = [None for i in range(n)]
    for i in range(n):l[i] = SR()
    return l
mod = 1000000007

#A

#B

#C
n,x = LI()
a = LI()
ans = 0
for i in range(n-1):
    if a[i]+a[i+1] > x:
        ans += a[i+1]+a[i]-x
        if x > a[i]:
            a[i+1] = x-a[i]
        else:
            a[i+1] = 0
            a[i] = x
print(ans)
#D

#E

#F

#G

#H

#I

#J

#K

#L

#M

#N

#O

#P

#Q

#R

#S

#T

Submission status: Accepted

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

ans = 0

for i in range(0,n-1):
  if a[i]+a[i+1]>x:
    ans += a[i+1]- max(0,x-a[i])
    a[i+1] = max(0,x-a[i])
    if a[i]>x:
      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()))
s=0
for i in range(len(a)-1):
    if a[i]+a[i+1]>x:
        if a[i]>x:
            s+=a[i+1]+a[i]-x
            a[i+1]=0
            a[i]=x
        else:
            s+=a[i+1]+a[i]-x
            a[i+1]=x-a[i]
print(s)