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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline

#n = int(input())
#l = list(map(int, input().split()))

'''
a=[]
b=[]
for i in range():
    A, B = list(map(int, input().split()))map(int, input().split())
    a.append(A)   
    b.append(B)'''

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

Submission status: Accepted

Solution code:
num = [0]
N, X = map(int, input().split())
[num.append(i) for i in list(map(int, input().split()))]

cost = 0
for i in range(1, N+1):
    if num[i-1] + num[i] > X:
        newcost = num[i-1] + num[i] - X
        num[i] -= newcost
        cost += newcost

print(cost)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!python3

LI = lambda: list(map(int, input().split()))

# input
N, x = LI()
A = LI()


def main():
    ans = 0
    for i in range(1, N):
        s = A[i - 1] + A[i]
        d = max(0, s - x)
        ans += d
        A[i] -= min(d, A[i])
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n, x = map(int, input().split())
arr = list(map(int, input().split()))
sum_arr = sum(arr)

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

print(sum_arr - sum(arr))

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

Submission status: Accepted

Solution code:
n, x = map(int,input().split())
origdat = list(map(int, input().split()))
from copy import deepcopy

res = 0

cnt = 0
dat = deepcopy(origdat)
for i in range(n):
    if dat[i] == 0:
        continue
    if dat[i] > x: # もし、xよりも大きな数なら、xにする
        res += dat[i] - x
        dat[i] = x
    if i == n-1:
        break
    if dat[i] + dat[i+1] > x:
        res += (dat[i] + dat[i+1]) - x
        dat[i+1] -= (dat[i] + dat[i+1]) - x
#print(dat)
print(res)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
candi_num, can_exist_num = list(map(int, input().split()))
A = list(map(int, input().split()))

ans = 0

# first candi
ofs = max(0, A[0] + A[1] - can_exist_num)
if ofs > A[1]:
  A[0] -= ofs - A[1]
  ans += ofs - A[1]

for i in range(1, candi_num):
  two_sum = A[i - 1] + A[i]
  ofs = max(0, two_sum - can_exist_num)
  A[i] -= ofs
  ans += ofs

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

Submission status: Accepted

Solution code:
import numpy as np
import math
import sys
def sinput(): return sys.stdin.readline()
def iinput(): return int(sinput())
def imap(): return map(int, sinput().split())
def fmap(): return map(float, sinput().split())
def iarr(): return list(imap())
def farr(): return list(fmap())
def sarr(): return sinput().split()

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

print(c)

Submission status: Accepted

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

cost = max(0, A[0] + A[1] - X)
A[1] = max(0, A[1] - cost)

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

print(cost)

Submission status: Accepted

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

cost = 0
diff = max(0, sum(A[0:2]) - X)
A[0] -= max(0, diff - A[1])
A[1] -= min(A[1], diff)
cost += diff

for i in range(2, N):
    diff = max(0, A[i - 1] + A[i] - X)
    A[i - 1] -= max(0, diff - A[i])
    A[i] -= min(A[i], diff)
    cost += diff

print(cost)

Submission status: Accepted

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

Submission status: Accepted

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

count = 0

for i, c in enumerate(a):
	try:
		j = a[i + 1]
	except IndexError:
		break
	while (a[i + 1] + c) > x:
		if (a[i + 1] + c) == x:
			break
		count += abs(x - (a[i + 1] + c))
		a[i + 1] -= abs(x - (a[i + 1] + c))
		if a[i + 1] <= 0:
			a[i + 1] = 0
			break
	else:
		continue

print(count)

Submission status: Accepted

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

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

print(result)

Submission status: Accepted

Solution code:
def main():
    n, x = list(map(int, input().split()))
    A = list(map(int, input().split()))
    B = [min(A[0], x), ]
    for i in range(n - 1):
        if B[i] + A[i + 1] > x:
            B.append(x - B[i])
        else:
            B.append(A[i + 1])
    ans = sum([a - b for a, b in zip(A, B)])
    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):
    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,*A = map(int,open(0).read().split())
A+=[0]
s = 0

for n in range(N):
  d=max(0,A[n]+A[n-1]-X)
  s+=d
  A[n]-=d

print(s)

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

i=1
while i<N:
    if A[i]+A[i-1]>X:
        ans+=A[i]+A[i-1]-X
        A[i]=X-A[i-1]
    i+=1
        
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] > x:
        count += a[i] - x
        a[i] = x
        
        count += a[i+1]
        a[i+1] = 0
        
    elif a[i]+a[i+1] > x:
        count = count + a[i+1] + a[i] - 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()))

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

Submission status: Accepted

Solution code:
import sys

# import re
import math
import collections
# import decimal
import bisect
import itertools
import fractions
# import functools
import copy
import heapq
import decimal
# import statistics
import queue
import numpy as np

sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    n, x = ns()
    a = na()

    ans = 0

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
from sys import stdin
import collections
import math


n,x   = [int(x) for x in stdin.readline().rstrip().split()]
arr = [int(x) for x in stdin.readline().rstrip().split()]

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

print(ans)

Submission status: Accepted

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

moves = 0
if aa[0] > x:
    moves += aa[0] - x
    aa[0] = x

for i in range(n-1):
    # invariant: aa[i] <= x
    if aa[i] + aa[i+1] > x:
        diff = (aa[i] + aa[i+1]) - x
        aa[i+1] -= diff
        moves += diff

print(moves)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
def main():
    N,x=tuple(map(int,sys.stdin.readline().split()))
    A=list(map(int,sys.stdin.readline().split()))
    pa,r=0,0
    for a in A:
        c=a+pa-x
        if c>0:
            r+=c
            pa=a-c
        else:pa=a
    print(r)
if __name__=='__main__':main()

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil,pi,factorial
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def SI(): return input().rstrip()
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7

n,x=MI()
lis=LI()
ans=0
lis=[0]+lis+[0]
for i in range(n+1):
    #print(lis[i]+lis[i+1])
    if lis[i]+lis[i+1]>x:
        ans+=lis[i+1]+lis[i]-x
        lis[i+1]-=lis[i+1]+lis[i]-x
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
def main():
    #入力
    readline=stdin.readline
    n,x=map(int,readline().split())
    a=list(map(int,readline().split()))

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

    print(ans)
    
if __name__=="__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

#############
# Libraries #
#############

import sys
input = sys.stdin.readline

import math
#from math import gcd
import bisect
from collections import defaultdict
from collections import deque
from functools import lru_cache


#############
# Constants #
#############

MOD = 10**9+7
INF = float('inf')

#############
# Functions #
#############

######INPUT######
def I(): return int(input().strip())
def S(): return input().strip()
def IL(): return list(map(int,input().split()))
def SL(): return list(map(str,input().split()))
def ILs(n): return list(int(input()) for _ in range(n))
def SLs(n): return list(input().strip() for _ in range(n))
def ILL(n): return [list(map(int, input().split())) for _ in range(n)]
def SLL(n): return [list(map(str, input().split())) for _ in range(n)]

######OUTPUT######
def P(arg): print(arg); return
def Y(): print("Yes"); return
def N(): print("No"); return
def E(): exit()
def PE(arg): print(arg); exit()
def YE(): print("Yes"); exit()
def NE(): print("No"); exit()

#####Shorten#####
def DD(arg): return defaultdict(arg)


#####Inverse#####
def inv(n): return pow(n, MOD-2, MOD)

######Combination######
kaijo_memo = []
def kaijo(n):
  if(len(kaijo_memo) > n):
    return kaijo_memo[n]
  if(len(kaijo_memo) == 0):
    kaijo_memo.append(1)
  while(len(kaijo_memo) <= n):
    kaijo_memo.append(kaijo_memo[-1] * len(kaijo_memo) % MOD)
  return kaijo_memo[n]

gyaku_kaijo_memo = []
def gyaku_kaijo(n):
  if(len(gyaku_kaijo_memo) > n):
    return gyaku_kaijo_memo[n]
  if(len(gyaku_kaijo_memo) == 0):
    gyaku_kaijo_memo.append(1)
  while(len(gyaku_kaijo_memo) <= n):
    gyaku_kaijo_memo.append(gyaku_kaijo_memo[-1] * pow(len(gyaku_kaijo_memo),MOD-2,MOD) % MOD)
  return gyaku_kaijo_memo[n]

def nCr(n,r):
  if(n == r):
    return 1
  if(n < r or r < 0):
    return 0
  ret = 1
  ret = ret * kaijo(n) % MOD
  ret = ret * gyaku_kaijo(r) % MOD
  ret = ret * gyaku_kaijo(n-r) % MOD
  return ret

######Factorization######
def factorization(n):
  arr = []
  temp = n
  for i in range(2, int(-(-n**0.5//1))+1):
    if temp%i==0:
      cnt=0
      while temp%i==0: 
        cnt+=1 
        temp //= i
      arr.append([i, cnt])
  if temp!=1:
    arr.append([temp, 1])
  if arr==[]:
    arr.append([n, 1])
  return arr

#####MakeDivisors######
def make_divisors(n):
  divisors = []
  for i in range(1, int(n**0.5)+1):
    if n % i == 0:
      divisors.append(i)
      if i != n // i: 
        divisors.append(n//i)
  return divisors

#####MakePrimes######
def make_primes(N):
  max = int(math.sqrt(N))
  seachList = [i for i in range(2,N+1)]
  primeNum = []
  while seachList[0] <= max:
    primeNum.append(seachList[0])
    tmp = seachList[0]
    seachList = [i for i in seachList if i % tmp != 0]
  primeNum.extend(seachList)
  return primeNum

#####GCD#####
def gcd(a, b):
    while b:
        a, b = b, a % b
    return a

#####LCM#####
def lcm(a, b):
    return a * b // gcd (a, b)

#####BitCount#####
def count_bit(n):
  count = 0
  while n:
    n &= n -1
    count += 1
  return count

#####ChangeBase#####
def base_10_to_n(X, n):
  if X//n:
    return base_10_to_n(X//n, n)+[X%n]
  return [X%n]

def base_n_to_10(X, n):
  return sum(int(str(X)[-i-1])*n**i for i in range(len(str(X))))

#####IntLog#####
def int_log(n, a):
  count = 0
  while n>=a:
    n //= a
    count += 1
  return count

#############
# Main Code #
#############

N,x = IL()
A = IL()

ans = 0

prev = 0
for a in A:
  if prev+a>x:
    ans += (prev+a)-x
    prev = a-((prev+a)-x)
  else:
    prev = a
print(ans)

Submission status: Accepted

Solution code:
n, x = map(int, input().split())
l = [int(i)for i in input().split()]
last = 0
z = 0
for n in l:
    if n + last > x:
        z += n + last - x
        last = x - last
    else:
        last = n
print(z)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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


state = 0

ans = 0

for i in range(n-1):
    if a[i] + a[i+1] > x:
        if a[i] > x:
            ans += a[i] - x
            a[i] = x
        ans += a[i] + a[i+1] - x
        a[i+1] = x - a[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#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy, copy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
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()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
#import numpy as np
#from decimal import *

N, x = MAP()
a = LIST()
ans = max(0, a[0]-x)
a[0] = min(a[0], x)

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

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#, log2
from itertools import accumulate, permutations, combinations, combinations_with_replacement, product, groupby
from operator import itemgetter, mul
from copy import deepcopy, copy
from string import ascii_lowercase, ascii_uppercase, digits
from bisect import bisect, bisect_left, insort, insort_left
from fractions import gcd
from heapq import heappush, heappop
from functools import reduce
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()))
def ZIP(n): return zip(*(MAP() for _ in range(n)))
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
#import numpy as np
#from decimal import *

N, x = MAP()
a = LIST()
ans = max(0, a[0]-x)
a[0] = min(a[0], x)

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

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

print(ans)

Submission status: Accepted

Solution code:
# i番目とi - 1番目の箱の中身の和がxを超えているとき、i番目から余剰分を引く

import sys
readline = sys.stdin.readline

N,x = map(int,readline().split())
A = list(map(int,readline().split()))

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

for i in range(1,len(A)):
  if A[i] + A[i - 1] > x:
    diff = (A[i] + A[i - 1]) - x 
    ans += diff
    A[i] -= diff

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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):
    eated=max(0,a[i]+a[i-1]-x)
    ans+=eated
    a[i]-=eated
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):
    gap = A[i] + A[i+1] - x
    if gap > 0:
        ans += gap
        A[i+1] -= gap
        if A[i+1] < 0:
            A[i+1] = 0

print(ans)

Submission status: Accepted

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

cnt = 0
for i in range(n-1):
    total = a_list[i] + a_list[i+1]
    if total > x:
        diff = total - x
        cnt += diff

        if a_list[i+1] >= diff:
            a_list[i+1] -= diff
        else:
            a_list[i] = diff - a_list[i+1]
            a_list[i+1] = 0
print(cnt)

Submission status: Accepted

Solution code:
import sys
#import numpy as np
#import math
#from fractions import Fraction
#import itertools
from collections import deque
#import heapq
from fractions  import gcd

input=sys.stdin.readline
n,x=map(int,input().split())
a=list(map(int,input().split()))
res=0
for i in range(n-1):
    s=a[i]+a[i+1]
    if s>x:
        a[i+1]-=(s-x)
        res+=(s-x)
        if a[i+1]<0:
            a[i+1]=0
print(res)

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

Submission status: Accepted

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


ans = 0

for i in range(num - 1):
    tar = a[i] + a[i + 1]
    if (tar <= x):
        continue
    else:
        dif = tar - x
        if (a[i + 1] >= dif):
            ans += dif
            a[i + 1] -= dif

        else:
            ans += a[i + 1]
            dif -= a[i + 1]
            a[i + 1] = 0

            a[i] -= dif
            ans += dif


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

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10000000)
import os
import math
import bisect
import collections
import itertools
import heapq
import re
import queue

# import fractions

ii = lambda: int(sys.stdin.buffer.readline().rstrip())
il = lambda: list(map(int, sys.stdin.buffer.readline().split()))
fl = lambda: list(map(float, sys.stdin.buffer.readline().split()))
iln = lambda n: [int(sys.stdin.buffer.readline().rstrip()) for _ in range(n)]

iss = lambda: sys.stdin.buffer.readline().decode().rstrip()
sl = lambda: list(map(str, sys.stdin.buffer.readline().decode().split()))
isn = lambda n: [sys.stdin.buffer.readline().decode().rstrip() for _ in range(n)]

lcm = lambda x, y: (x * y) // math.gcd(x, y)
# lcm = lambda x, y: (x * y) // fractions.gcd(x, y)

MOD = 10 ** 9 + 7
MAX = float('inf')


def main():
    if os.getenv("LOCAL"):
        sys.stdin = open("input.txt", "r")

    N, X = il()
    A = il()

    ret = 0
    if A[0] > X:
        ret += A[0] - X
        A[0] = X

    for n in range(N - 1):
        a = A[n] + A[n + 1]
        if a > X:
            A[n + 1] -= a - X
            ret += a - X
    print(ret)


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

    if s <= x:
        continue
    else:
        a[i+1] = max(0,a[i+1]-(s-x))
        ans += s-x

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
_n, x = map(int, input().split())
boxes = list(map(int, input().split()))
cnt = 0
for idx, _b  in enumerate(boxes):
  if idx == 0:
    continue
  else:
    if boxes[idx] + boxes[idx - 1] >= x:
      sum = boxes[idx] + boxes[idx - 1]
      if boxes[idx] - (sum - x) >= 0:
        boxes[idx] -= sum - x
      else:
        boxes[idx] = 0
      cnt += sum - x
    else:
      continue

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

Submission status: Accepted

Solution code:
#6問目
N, x = map(int, input().split())
a = list(map(int, input().split()))
count = 0
for i in range(N-1):
    #まず一番前の要素がxより小さくなければならない
    if(i == 0 and 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] = x - a[i]
print(count)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n,x,*a=map(int,open(0).read().split());t=r=0
for i in a:t=min(i,x-t);r+=i-t
print(r)