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

Submission status: Accepted

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

cnt, prv = 0, 0

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

print(cnt)

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,ceil,sqrt,factorial,log #log2ないｙｐ
from heapq import heappop, heappush, heappushpop
from collections import Counter,defaultdict,deque
from itertools import accumulate,permutations,combinations,product,combinations_with_replacement
from bisect import bisect_left,bisect_right
from copy import deepcopy
inf=float('inf')
mod = 10**9+7
def pprint(*A): 
    for a in A:     print(*a,sep='\n')
def INT_(n): return int(n)-1
def MI(): return map(int,input().split())
def MF(): return map(float, input().split())
def MI_(): return map(INT_,input().split())
def LI(): return list(MI())
def LI_(): return [int(x) - 1 for x in input().split()]
def LF(): return list(MF())
def LIN(n:int): return [I() for _ in range(n)]
def LLIN(n: int): return [LI() for _ in range(n)]
def LLIN_(n: int): return [LI_() for _ in range(n)]
def LLI(): return [list(map(int, l.split() )) for l in input()]
def I(): return int(input())
def F(): return float(input())
def ST(): return input().replace('\n', '')
def main():
    N,x = MI()
    A=LI()
    ans = 0
   
    for i in range(N-1):
      if A[i]+A[i+1]>x:
        s=A[i]+A[i+1]
        A[i+1]-=min(A[i+1],s-x)
        ans += max(0,s-x)

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

Submission status: Accepted

Solution code:
from sys import stdin

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

point = 0
for i in range(n-1):
    if li[i]+li[i+1] > x:
        total = li[i]+li[i+1]-x
        if total > li[i+1]:
            point += li[i+1]
            total = total-li[i+1]
            if total > li[i]:
                point += li[i]
                li[i] = 0
                li[i+1] = 0
            else:
                point += total
                li[i] -= total
                li[i+1] = 0
        else:
            point += total
            li[i+1] -= total

print(point)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys


def solve():
    readline = sys.stdin.buffer.readline
    mod = 10 ** 9 + 7
    n, x = list(map(int, readline().split()))
    a = list(map(int, readline().split()))
    cnt = 0
    for i in range(n):
        if i == 0:
            if a[i] > x:
                t = abs(x - a[i])
                a[i] -= t
                cnt += t
        else:
            if a[i] + a[i-1] > x:
                t = abs(x - (a[i] + a[i-1]))
                a[i] -= t
                cnt += t
    print(cnt)


if __name__ == '__main__':
    solve()

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: #xを上回る時のみ操作を行う
    temp=a[i]+a[i+1]-x #減らさなきゃいけない個数
    ans+=temp
    if a[i+1]>=temp: #減らすのは出来るだけ右側
      a[i+1]-=temp
    else:
      a[i+1]=0
print(ans)

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

Submission status: Accepted

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

Submission status: Accepted

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

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

if wa[-1] > x:
    ans += wa[-1]-x
    a[-1] = wa[-1]-x
    wa[-1] -= wa[-1]-x

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
import bisect
import heapq
import math
import random
import sys
from collections import Counter, defaultdict, deque
from decimal import ROUND_CEILING, ROUND_HALF_UP, Decimal
from functools import lru_cache, reduce
from itertools import combinations, combinations_with_replacement, product, permutations
from operator import add, mul, sub

sys.setrecursionlimit(100000)
input = sys.stdin.readline
INF = 2**62-1


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


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


def read_float():
    return float(input())


def read_float_n():
    return list(map(float, input().split()))


def read_str():
    return input().strip()


def read_str_n():
    return list(map(str, input().split()))


def error_print(*args):
    print(*args, file=sys.stderr)


def mt(f):
    import time

    def wrap(*args, **kwargs):
        s = time.time()
        ret = f(*args, **kwargs)
        e = time.time()

        error_print(e - s, 'sec')
        return ret

    return wrap


@mt
def slv(N, X, A):
    ans = 0
    for i, a in enumerate(A):
        ans += max(0, a-X)
        A[i] = min(a, X)

    for i in range(1, N):
        if A[i-1] + A[i] > X:
            ans -= X - (A[i-1] + A[i])
            A[i] += X - (A[i-1] + A[i])
    return ans


def main():
    N, X = read_int_n()
    A = read_int_n()
    print(slv(N, X, A))


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,x = map(int, input().split())
a = list(map(int, input().split()))
now = min(a[0],x)
ans = max(0,a[0]-x)
for i in a[1:]:
  SUM = now + i
  res = SUM - x
  if res > 0:
    ans += res
    now = i - res
  else:
    now = 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):
    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())
arr = list(map(int, input().split()))
c = 0
for i in range(1, n):
    if arr[i - 1] + arr[i] > x:
        dif = arr[i - 1] + arr[i] - x
        if arr[i] >= dif:
            # 自分だけ犠牲になればいい
            arr[i] -= dif
        else:
            # 自分だけじゃ足りないぶん
            new_dif = dif - arr[i]
            arr[i] = 0
            arr[i - 1] -= new_dif
        c += dif
print(c)

Submission status: Accepted

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

cost = 0
for i in range(len(a)):
    if a[i] > x:  # そもそもx個より多い場合はx個に減らす
        cost += a[i] - x
        a[i] = x
    if i > 0 and a[i - 1] + a[i] > x:  # 1つ前との和がx個になるまで減らす
        cost += a[i - 1] + a[i] - x
        a[i] = x - a[i - 1]

print(cost)

Submission status: Accepted

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

for i in range(N):
    A[i] = B[i]
    if A[i] > x:
        A[i] = x
for i in range(N - 1):
    tmp = A[i] + A[i + 1]
    if tmp <= x: continue
    A[i + 1] -= tmp - x
print(sum([B[_] - A[_] for _ in range(N)]))

Submission status: Accepted

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

if(n==1):
    print(a[0]-x)
    exit()
ope=0
if(a[0]>x):
    ope+=a[0]-x
    a[0]=x

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

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):
  t = a[i-1]+a[i]-x
  if t <= 0:
    continue
  elif t <= a[i]:
    a[i] -= t
    ans += t
  else:
    a[i] = 0
    a[i-1] -= t-a[i]
    ans += t
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(1, N):
	s = a[i-1] + a[i]
	ov = s - x
	if ov > 0:
		cnt += ov
		if a[i] < ov:
			a[i] = 0
		else:
			a[i] -= ov
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,x=map(int,input().split())
a=list(map(int,input().split()))
count=0
for i in range(n):
    if a[i]>x:
        count+=a[i]-x
        a[i]=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:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(10000000)
INF = 1<<32


def solve(N: int, x: int, a: "List[int]"):
    ans = 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] -= (a[i]+a[i+1]-x)

    print(ans-sum(a))
    return



def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    x = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, x, a)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def solve():
    N, X = list(map(int, input().split()))
    AS = tuple(map(int, input().split()))
    # print(N, X)
    # print(AS)

    cost = 0
    if AS[0] + AS[1] > X:
        should = AS[0] + AS[1] - X
        cost += should
        if AS[1] > should:
            b = AS[1] - should
        else:
            b = 0
    else:
        b = AS[1]

    for i in range(1, len(AS) - 1, 1):
        a = b
        b = AS[i + 1]
        if a + b > X:
            should = a + b - X
            cost += should
            if b > should:
                b = b - should
            else:
                b = 0

    return cost


if __name__ == '__main__':
    res = solve()
    print(res)

Submission status: Accepted

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

Submission status: Accepted

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

count=0
if(a[0]>x):
    count=a[0]-x
    a[0]=x
    
for ind,i in enumerate(a):
    if(ind==len(a)-1):
      break
    s=i+a[ind+1]
    if(s>x):
        count+=s-x
        a[ind+1]-=s-x
        #s=i+a[ind+1]
print(count)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
# https://abc048.contest.atcoder.jp/tasks/arc064_a
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()))
C=A[:]
s2=0
for i in range(n-1):
    v=C[n-1-i]+C[n-2-i]
    if v>x:
        s2+=v-x
        C[n-2-i]=0 if x<=C[n-1-i] else x-C[n-1-i]
print(s2)

Submission status: Accepted

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

s1=0

for i in range(n-1):
    w=B[i]+B[i+1]
    if w>x:
        s1+=w-x
        B[i+1]=0 if x<=B[i] else x-B[i]

print(s1)

Submission status: Accepted

Solution code:
# python3 (3.4.3)
import sys
input = sys.stdin.readline

# 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:
        s = A[i]+A[i-1] - X
        ans += s
        A[i] -= s

print(ans)

Submission status: Accepted

Solution code:
def examC():
    N, x = LI()
    a = LI()
    cur = int(0)
    if a[0]>x:
        cur += a[0]-x
        a[0] = x
    for i in range(N-1):
        cur += max((a[i]+a[i+1])-x,0)
        a[i+1] = min(x-a[i],a[i+1])
    print(cur)


import sys
def I(): return int(sys.stdin.readline())
def LI(): return list(map(int,sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
mod = 10**9 + 7
inf = float('inf')

examC()

Submission status: Accepted

Solution code:
N,X = map(int,input().split())
Data = list(map(int,input().split()))
ans = 0
for i in range(N-1):
    a1 = Data[i]
    a2 = Data[i+1]

    diff = a1+a2-X
    if diff > 0:
        ans += diff
        if a2 >= diff:
            Data[i+1] -= diff
        else:
            Data[i+1] = 0
            Data[i] -= (a2-diff)
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

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

Submission status: Accepted

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

n, x = map(int,put().split())
ls = list(map(int,put().split()))
ls.append(0)
ans = 0

for i in range(n):
    if ls[i] != 0:
        if x < ls[i]+ls[i+1] :
            ans += (ls[i]+ls[i+1]) - x
            ls[i+1] = max(x - ls[i],0)
print(ans)

Submission status: Accepted

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

n, x = map(int,input().split())
ls = list(map(int,input().split()))
ls.append(0)
ans = 0

for i in range(n):
    if ls[i] != 0:
        if x < ls[i]+ls[i+1] :
            ans += (ls[i]+ls[i+1]) - x
            ls[i+1] = max(x - ls[i],0)
print(ans)

Submission status: Accepted

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

n, x = map(int,input().split())
ls = list(map(int,input().split()))
ls.append(0)
ans = 0

for i in range(n):
    if ls[i] != 0:
        if x < ls[i]+ls[i+1] :
            ans += (ls[i]+ls[i+1]) - x
            ls[i+1] = max(x - ls[i],0)
print(ans)

Submission status: Accepted

Solution code:
import math
import copy
import sys
import bisect
input = sys.stdin.readline

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

if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
n, x = map(int, input().split())
a = list(map(int, input().split()))
cnt = 0
for i in range (1, len(a)):
	if a[i-1] + a[i] < x:
		continue
	else:
		sa = a[i-1] + a[i] -x
		a[i] -= sa
		if(a[i] < 0):
			a[i-1] -= abs(a[i])
			a[i] = 0
		cnt += sa
print (cnt)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

print(ans)

Submission status: Accepted

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

out = 0
diff = 0
for i in range(n-1):
  if L[i] > x:
    diff = L[i] - x
    out += diff
    L[i] = x
  if L[i] + L[i+1] > x:
    diff = L[i] + L[i+1] - x
    out += diff
    L[i+1] = x - L[i]
print(out)

Submission status: Accepted

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

eat=0

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

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

Submission status: Accepted

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

import sys

def input(): return sys.stdin.readline().strip()
def list2d(a, b, c): return [[c] * b for i in range(a)]
def list3d(a, b, c, d): return [[[d] * c for j in range(b)] for i in range(a)]
def list4d(a, b, c, d, e): return [[[[e] * d for j in range(c)] for j in range(b)] for i in range(a)]
def ceil(x, y=1): return int(-(-x // y))
def INT(): return int(input())
def MAP(): return map(int, input().split())
def LIST(N=None): return list(MAP()) if N is None else [INT() for i in range(N)]
def Yes(): print('Yes')
def No(): print('No')
def YES(): print('YES')
def NO(): print('NO')
sys.setrecursionlimit(10 ** 7)
INF = 10 ** 18
MOD = 10 ** 9 + 7

N, K = MAP()
A = LIST()

cnt = 0
for i in range(1, N):
    # 減らす必要のある数
    tmp = (A[i-1]+A[i]) - K
    if tmp > 0:
        cnt += tmp
        # 右から引けるだけ引く
        A[i] -= min(A[i], tmp)
print(cnt)

Submission status: Accepted

Solution code:
def main():
    N, x, *a = map(int, open(0).read().split())
    ans = a[0] - x if x < a[0] else 0
    pre = min(a[0], x)
    for i in range(1, N):
        dif = pre + a[i] - x
        if 0 < dif:
            ans += dif
            a[i] -= dif
        pre = a[i]
    print(ans)
    return


main()

Submission status: Accepted

Solution code:
def main():
    N, x, *a = map(int, open(0).read().split())
    ans = 0
    pre = a[0]
    for i in range(1, N):
        if x < pre + a[i]:
            cur = pre + a[i] - x
            ans += cur
            a[i] = a[i] - cur if cur < a[i] else 0
        pre = a[i]
    print(ans)
    return


main()

Submission status: Accepted

Solution code:
def main():
    N, x, *an = map(int, open(0).read().split())
    ans = 0
    for i in range(N - 1):
        if x < an[i] + an[i + 1]:
            cur = an[i] + an[i + 1] - x
            ans += cur
            if an[i + 1] <= cur:
                an[i + 1] = 0
            else:
                an[i + 1] -= cur
    print(ans)
    return


main()

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

Submission status: Accepted

Solution code:
n,x = map(int,input().split())
A = [0]+list(map(int,input().split()))
count = 0
for i in range(n):
    if A[i]+A[i+1] > x:
        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()))
cnt = 0
if A[0]>x:
    cnt += A[0]-x
    A[0] = x
if A[-1]>x:
    cnt += A[-1]-x
    A[-1] = x
for i in range(1,N-1):
    if A[i-1]+A[i]>x:
        cnt += A[i-1]+A[i]-x
        A[i] = x-A[i-1]
if A[N-1]+A[N-2]>x:
    cnt += A[N-1]+A[N-2]-x
    A[N-2] = x-A[N-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, len(a)):
    # 2組の合計がxよりも大きいなら
    if a[i-1] + a[i] > x:
        # 右側の数が0より大きいなら食べる
        if a[i] > 0:
            # 右側の箱の飴をすべて食べてもx以上ならすべて食べる
            if (a[i-1] + a[i] - x) > a[i]:
                ans += a[i]
                a[i] = 0
            # 右側の箱の飴を食べるだけでx以下になるならその分だけ食べる
            else:
                ans += (a[i-1] + a[i] - x)
                a[i] = a[i] - (a[i-1] + a[i] - x)

        # 右の箱を食べただけでx以下にならない時は左側の箱の飴も食べる
        if a[i-1] + a[i] > x:
            ans += (a[i-1] + a[i] - x)
            a[i-1] = a[i-1] - (a[i-1] + a[i] - x)
            # 左側の箱の飴をすべて食べる
            # if (a[i-1] + a[i] - x) > a[i-1]:
            #     ans += a[i-1]
            #     a[i-1] = 0
            # 左側の箱の飴をx以下になるだけ食べる
            # else:
            #     ans += (a[i-1] + a[i] - x)
            #     a[i-1] = a[i-1] - (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(1, len(a)):
    # 2組の合計がxよりも大きいなら
    if a[i-1] + a[i] > x:
        # 右側の数が0より大きいなら食べる
        if a[i] > 0:
            # 右側の箱の飴をすべて食べてもx以上ならすべて食べる
            if (a[i-1] + a[i] - x) > a[i]:
                ans += a[i]
                a[i] = 0
            # 右側の箱の飴を食べるだけでx以下になるならその分だけ食べる
            else:
                ans += (a[i-1] + a[i] - x)
                a[i] = a[i] - (a[i-1] + a[i] - x)

        # 右の箱を食べただけでx以下にならない時は左側の箱の飴も食べる
        if a[i-1] + a[i] > x:
            # 左側の箱の飴をすべて食べる
            if (a[i-1] + a[i] - x) > a[i-1]:
                ans += a[i-1]
                a[i-1] = 0
            # 左側の箱の飴をx以下になるだけ食べる
            else:
                ans += (a[i-1] + a[i] - x)
                a[i-1] = a[i-1] - (a[i-1] + a[i] - x)

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, x: int, a: "List[int]"):
    answer =0
    for i in range(N-1):
        consum = a[i]+a[i+1]
        if consum > x:
            a[i+1]-= min(consum-x,a[i+1])
            answer+=consum-x
        
    print(answer)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    x = int(next(tokens))  # type: int
    a = [int(next(tokens)) for _ in range(N)]  # type: "List[int]"
    solve(N, x, a)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

count = 0
previous = 0

for i in a:
    if i + previous > x: 
        excess = i + previous - x
        count += excess
        previous = x - previous
    else: # if not exceeding x.
        previous = i

print(count)

Submission status: Accepted

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

count = 0
for index, i in enumerate(a):
  if index+2 > len(a):
    continue
  total = i + a[index+1]
  if total <= x:
    continue
  m = total - x
  count += m
  if i >= a[index+1]:
    i -= m
    if i < 0:
      i = 0
      m = m - i
    a[index] = i  
    if m > 0:
      a[index+1] -= m
      
  if a[index+1] > i:
    a[index+1] -= m
    if a[index+1] < 0:
      a[index+1] = 0
      m = m - a[index+1]
      if m > 0:
        a[index] -= m

print(count)

Submission status: Accepted

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

result = 0
# a[0] が x 以下でないと、a[1] を空にしても条件を満たせない
t = a[0] - x
if t > 0:
    result += t
    a[0] -= t
for i in range(1, N):
    # 箱に入っているキャンディの数が食べれる上限
    t = min(a[i - 1] + a[i] - x, a[i])
    if t > 0:
        result += t
        a[i] -= t
print(result)

Submission status: Accepted

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

cnt=0
pre=0

for i in range(N):
  dif=max(A[i]+pre-x,0)
  cnt+=dif
  A[i]-=dif
  pre=A[i]
  
print(cnt)

Submission status: Accepted

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

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

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

if SA[N-2]>x:
  ans+=SA[N-2]-x
print(ans)

Submission status: Accepted

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

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
n, x = [int(i) for i in sys.stdin.readline().split()]
a_ls = [int(i) for i in sys.stdin.readline().split()]
_sum = max(0, a_ls[0] - x)
last = min(x, a_ls[0])
for a in a_ls[1:]:
    if last + a > x:
        _sum += last + a - x
        last = x - last
    else:
        last = a
print(_sum)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
input = sys.stdin.buffer.readline
inputs= sys.stdin.buffer.readlines
#rstrip().decode('utf-8')

#import numpy as np
#import operator
#import bisect
#from heapq import heapify,heappop,heappush
#from math import gcd
#from fractions import gcd
#from collections import deque
#from collections import defaultdict
#from collections import Counter
#from itertools import accumulate
#from itertools import groupby
#from itertools import permutations
#from itertools import combinations
#scipy.sparse import csr_matrix
#scipy.sparse.csgraph import floyd_warshall
#from scipy.sparse.csgraph import csgraph_from_dense
#from scipy.sparse.csgraph import dijkstra
#sys.setrecursionlimit(10**7)

#map(int,input().split())

def main():
    N,x,*a=map(int,read().split())
    t=0
    ans=0
    for i in range(N-1):
        now=max(0,a[i]-t)
        t=max(0,now+a[i+1]-x)
        ans+=t
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

ans = 0
before = 0

for i in a:
    if i + before > x:
        ans += i + before - x
        before = x - before
    else:
        before = i

print(ans)

Submission status: Accepted

Solution code:
n,k=map(int,input().split())
l=[int(i) for i in input().split()]
sm=0 
for i in range(n-1):
    if l[i]+l[i+1]<=k:
        continue 
    else: 
        sm+=l[i]+l[i+1]-k 
        req=l[i]+l[i+1]-k 
        if l[i+1]>=req: 
            l[i+1]-=req 
            req=0 
        else: 
            #l[i+1]=0 
            req-=l[i+1]
            l[i]-=req
            l[i+1]=0
print(sm)

Submission status: Accepted

Solution code:
# C - Boxes and Candies

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

ans = 0
for idx in range(1,N):
    if a[idx-1] + a[idx] > x:
        n_eat = a[idx-1] + a[idx] - x
        ans += n_eat
        a[idx] = max(0, a[idx] - n_eat)        
        
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

    elif lists[i]+lists[i+1]>x and lists[i]>x:
        count+=(lists[i]+lists[i+1]-x)
        lists[i+1]=0
print(count)