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:
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

def main():
    mod=10**9+7
    N,x=MI()
    a=LI()
    ans=0
    if a[0]>x:
        ans+=a[0]-x
        a[0]=x
    
    for i in range(N-1):
        b=a[i]+a[i+1]
        if b>x:
            ans+=b-x
            a[i+1]-=b-x
            
    print(ans)
    

main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def resolve():
    n, x = map(int, input().split())
    S = list(map(int, input().split()))
    ans = 0
    for i in range(n):
        if i == 0:
            pass
        elif i == 1 and S[i] + S[i-1] > x:
            diff = (S[i] + S[i - 1]) - x
            ans += diff
            S[i] = max(S[i]-diff, 0)
        else:
            if S[i] + S[i-1] > x:
                diff = (S[i] + S[i-1]) - x
                S[i] -= diff
                ans += diff
    print(ans)

if __name__ == '__main__':
    resolve()

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

Submission status: Accepted

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

N,x = list(map(int, input().rstrip().split()))
a_list = list(map(int, input().rstrip().split()))
#-----

cnt=0
if a_list[0] > x:
    cnt += (a_list[0] - x)
    a_list[0] = x

for i in range(1,len(a_list)):
    sum2 = a_list[i-1] + a_list[i]
    if sum2 > x:
        diff = sum2 - x
        
        # a_list[i-1] < x のため、a_list[i]のみ操作
        a_list[i] -= diff
        cnt += diff
        
print(cnt)

Submission status: Accepted

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

n,m = map(int,readline().split())
lst1 = list(map(int,readline().split()))
ans = 0
for i in range(1,n):
    if lst1[i-1]+lst1[i] > m:
        res = max(0,m-lst1[i-1])
        ans += lst1[i]-res
        lst1[i] = res

if lst1[0]+lst1[1] > m:
    ans += lst1[0]-m

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,x=map(int,input().split())
A=list(map(int,input().split()))
import copy
C=copy.copy(A)
B=[0 for k in range(N-1)]
for k in range(N-1):
    B[k]=A[k]+A[k+1]
#print(B)
for k in range(N-1):
    if B[k]-x>0:
        if B[k]-x>A[k+1]:
            A[k]=x
            A[k+1]=0
        else:
            A[k+1]=A[k+1]-B[k]+x
        if k!=N-2:
            B[k+1]=A[k+1]+A[k+2]
#print(A)
ans=0
for k in range(N):
    ans+=C[k]-A[k]
print(ans)

Submission status: Accepted

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

ans = 0
for i in range(N-1) :
    temp = a[i] + a[i+1]
    if temp <= x :
        continue
    elif a[i] <= x :
        ans += a[i] + a[i+1] - x
        a[i+1] -= a[i] + a[i+1] - x
    else :
        ans += a[i] + a[i+1] - x
        a[i+1] = 0
        a[i] -= 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):
    if a[i]+a[i+1]>x:
        if a[i]+a[i+1]-x<=a[i+1]:
            ans+=(a[i]+a[i+1]-x)
            a[i+1]-=(a[i]+a[i+1]-x)
        else:
            ans+=(a[i]+a[i+1]-x)
            a[i+1]=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[1]
    a[1] = 0
    ans += (a[0] - x)
    a[0] = x
elif (a[0] + a[1]) > x:
    ans += (a[0] + a[1] - x)
    a[1] = x - a[0]
for i in range(2,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()))
cnt=0
for i in range(N-1):
    l=a[i]+a[i+1]
    if(l>x):
        cnt+=l-x
        if(a[i+1]>=l-x):
            a[i+1]-=l-x
        else:
            a[i+1]=0
            
print(cnt)

Submission status: Accepted

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

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

if __name__=='__main__':
  main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    box, limit = map(int, input().split())
    candies = list(map(int, input().split()))
    answer = 0
    for i in range(1, box):
        candy_sum = candies[i - 1] + candies[i]
        if candy_sum > limit:
            candies[i] -= min(candy_sum - limit, candies[i])
            answer += candy_sum - limit
    print(answer)


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()]
ans = 0 

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N,x=map(int,input().split())
a=input().split()
numbers=[int(b) for b in a]
count=0
n=len(numbers)
if numbers[0]>x:
    count = numbers[0] - x
    numbers[0]=x
for c in range(n-1):
    if numbers[c] + numbers[c+1]>x:
        count+=numbers[c+1]-(x-numbers[c])
        numbers[c+1]=x-numbers[c]
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):
  if i != N -1:
    if a[i] + a[i + 1] > x:
      ans += a[i] + a[i + 1] - x
      a[i + 1] -= min(a[i + 1], a[i] + a[i + 1] - x)
print(ans)

Submission status: Accepted

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

ans = 0
for i in range(-1, N - 2):
    # 必要な操作回数
    count = a[i + 1] + a[i + 2] - x
    if count <= 0:
        continue

    ans += count

    # 右の項を引くだけで完結する場合
    if count < a[i + 2]:
        a[i + 2] -= count

    # 両方の項から引かなければいけない場合
    else:
        a[i + 1] = a[i + 1] - count + a[i + 2]
        a[i + 2] = 0

print(ans)

Submission status: Accepted

Solution code:
n, x = map(int, input().split())
a_i = list(map(int, input().split()))
ansList = [0] * n
ansList[0] = max(a_i[0] - x, 0)
for id in range(n - 1):
    ansList[id + 1] = max((a_i[id] - ansList[id]) + a_i[id + 1] - x, 0)
print(sum(ansList))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(1, n):
    if a[i-1]+a[i] > x:
        dif = a[i-1]+a[i]-x
        if dif <= a[i]:
            a[i] -= dif
        else:
            a[i-1] -= dif-a[i]
            a[i] = 0
print(result-sum(a))

Submission status: Accepted

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

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

ans += max(0, A[-1] + A[-2] - x)

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

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

for a in A[1:]:
    if _a + a > x:
        dif = _a + a - x
        cnt += dif
        _a = a - dif
    else:
        _a = a

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

total = 0
for i in range(len(a)-1):
    diff = a[i+1] + a[i] - x
    if diff>0:
        total += diff
        if a[i+1]>=diff:
            a[i+1] = a[i+1]-diff
        else:
            a[i+1] = 0
            a[i] = a[i] -(diff-a[i+1])
    else:
        pass
    
print(total)

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

Submission status: Accepted

Solution code:
n,x = map(int,input().split())
l = list(map(int,input().split()))
ans = 0
if l[0] > x:
  ans += l[0]-x
  l[0] -= ans
for i in range(1,n):
  if l[i]+l[i-1] > x:
    b = l[i]+l[i-1]-x
    ans += b
    l[i] -= b
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):
    total = a[i] + a[i+1]
    if total > x:
        diff = total - x
        ans += diff
        a[i+1] = max(0, a[i+1] - diff)
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(1, n):
    diff = A[i] + A[i - 1] - x
    if diff <= 0:
        continue
    count += diff
    A[i] -= min(diff, A[i])
print(count)

Submission status: Accepted

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

ans = 0

for index, a in enumerate(A):
  if index == 0:
    if a > x:
      ans += abs(x - a)
      A[index] = x
    continue

  if a + A[index-1] > x:
    ans += abs(x - (a + A[index-1]))
    A[index] = x - A[index-1]

print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
def EatCandy(a, b, x):
    if a + b <= x:
        pass
    else:
        if a <= x:
            b = x - a
        else:
            b = 0
            a = x
    return (a, b)

def solve():
    N, x = [int(_) for _ in input().split()]
    A = [int(_) for _ in input().split()]
    st = sum(A)
    for i in range(N - 1):
        A[i], A[i + 1] = EatCandy(A[i], A[i + 1], x)
    return st - sum(A)

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

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, floor
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
 
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-1]+a[i]-x)
	a[i] = min(a[i], x-a[i-1])
print(ans)

Submission status: Accepted

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, floor
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7
 
N, x = MAP()
a = LIST()

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
A = list(map(int, input().split()))
A.append(0)
CA = [a for a in A]
for i in range(N):
    total = CA[i] + CA[i+1]
    if x < total:
        if CA[i + 1] <= (total - x):
            rest = total - x - CA[i + 1]
            CA[i + 1] = 0
            CA[i] -= rest
        else:
            CA[i + 1] -= total - x


cnt = 0
for a, ca in zip(A, CA):
    cnt += (a - ca)
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:
    d=a[0]-x
    ans+=d
    a[0]=x

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

print(ans)

Submission status: Accepted

Solution code:
import sys
stdin = sys.stdin
from itertools import product

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

n, x = na()
a = na()
apre = 0
ans = 0
for ai in a:
    eat = max(0, apre + ai - x)
    ans += eat
    apre = ai - eat
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
# Your code here!
N,x=map(int,input().split())

A=[0]+list(map(int,input().split()))

ans=0

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

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

print(cnt)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
n,x = map(int,input().split())	
a = list(map(int,input().split()))
ans = 0
for i in range(n-1):
    ans += max(0,a[i]+a[i+1]-x)
    a[i+1] = max(0,a[i+1]-max(0,a[i]+a[i+1]-x))
print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
a,b = map(int,input().split())
l = list(map(int,input().split()))
t = l[0]
ans = max(0,t-b)
t = min(t,b)
for i in l[1:]:
    # print(ans,t)
    ans += max(0,(t+i)-b)
    t = max(0,i-max(0,(t+i)-b))
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]+a[i-1]>x:
        if x-a[i-1]>0:
            ans+=a[i]+a[i-1]-x
            a[i]+=x-a[i-1]-a[i]
        else:
            ans+=a[i]+a[i-1]-x
            a[i]=0
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from fractions import gcd
# from datetime import date, timedelta
# from heapq import *
import heapq
import math
from collections import defaultdict, Counter, deque
from bisect import *
import itertools
import fractions
import sys
sys.setrecursionlimit(10 ** 7)
MOD = 10 ** 9 + 7
# input = sys.stdin.readline


def lcm(a, b):
    return a * b / fractions.gcd(a, b)


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)

    # divisors.sort()
    return divisors


def factorize(n):
    fct = []  # prime factor
    b, e = 2, 0  # base, exponent
    while b * b <= n:
        while n % b == 0:
            n = n // b
            e = e + 1
        if e > 0:
            fct.append((b, e))
        b, e = b + 1, 0
    if n > 1:
        fct.append((n, 1))
    return fct


def is_prime(n):
    if n == 1:
        return False

    for k in range(2, int(math.sqrt(n)) + 1):
        if n % k == 0:
            return False

    return True


def primes(n):
    is_prime = [True] * (n + 1)
    is_prime[0] = False
    is_prime[1] = False
    for i in range(2, int(n**0.5) + 1):
        if not is_prime[i]:
            continue
        for j in range(i * 2, n + 1, i):
            is_prime[j] = False
    return [i for i in range(n + 1) if is_prime[i]]

def modpow(a, n, mod):
    res = 1
    while n > 0:
        if n & 1:
            res = res * a % mod
        a = a * a % mod
        n >>= 1
    return res


def modinv(a, mod):
    return modpow(a, mod - 2, mod)


def cnk(a, b):
    MOD = 10**9+7
    ret = 1
    for i in range(b):
        ret *= (a-i)
        ret %= MOD
        ret = ret * modinv(i+1, MOD) % MOD
    return ret




def main():
    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:
            continue

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

Submission status: Accepted

Solution code:
#
# 　　  ⋀_⋀　 
#　　  (･ω･)  
# .／ Ｕ ∽ Ｕ＼
#  │＊　合　＊│
#  │＊　格　＊│ 
#  │＊　祈　＊│ 
#  │＊　願　＊│ 
#  │＊　　　＊│ 
#      ￣
#
import sys
sys.setrecursionlimit(10**6)
input=sys.stdin.readline
from math import floor,sqrt,factorial,hypot,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
from fractions import gcd
from random import randint
def ceil(a,b): return (a+b-1)//b
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,a in enumerate(A):
        if a>X:
            ans += A[i]-X
            A[i]=X
    for i,a in enumerate(A[1:],start=1):
        if a+A[i-1]>X:
            sub = (a+A[i-1])-X
            A[i]-=min(a,sub)
            ans += min(a,sub)
            if A[i-1]+A[i]>X:
                sub = (A[i]+A[i-1])-X
                A[i-1]-=sub
                ans += sub

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

Submission status: Accepted

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


if __name__ == '__main__':
    main()

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

    # Kより少ないならそのまま
    if candy < K:
        continue

    # 右にあるやつをたべる
    r = min(A[i], candy - K)
    candy -= r
    A[i] -= r
    ans += r
    if candy < K:
        continue

    # 左にあるやつを食べる
    l = min(A[i - 1], candy - K)
    candy -= l
    A[i - 1] -= l
    ans += l
    if candy < K:
        continue

print(ans)

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

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline

    N, x = map(int, input().split())
    A = list(map(int, input().split()))
    S = sum(A)

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


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
n, x = map(int, input().split())
a = list(map(int, input().split()))
sum_a = 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] += x - (a[i] + a[i+1])
r = sum_a - sum(a)
    
print(r)

Submission status: Accepted

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

ans=0

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

for i in range(1,len(a)):
    sm = a[i-1]+a[i]
    if sm <=x:
        continue
    else:
        eat = sm-x
        a[i] -= eat
        ans+=eat
print(ans)

Submission status: Accepted

Solution code:
n,x = map(int, input().split())
a = []
cnt = 0
for i in map(int, input().split()):
    if x < i:
        cnt += i-x
        i = x
    a.append(i)

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

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
INF = float("inf")


def solve(N: int, x: int, a: "List[int]"):
    # 左から貪欲
    counter = 0
    for i in range(N-1):
        # i, i+1について
        if a[i] + a[i+1] <= x:
            continue
        if a[i] > x:
            counter += a[i] - x
            a[i] = x
            if a[i+1] != 0:
                counter += a[i+1]
                a[i+1] = 0
        else:
            counter += a[i]+a[i+1]-x
            a[i+1] = x-a[i]

    print(counter)
    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 = 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:
        tmp = A[i] + A[i-1] - x
        ans += tmp
        A[i] -= tmp

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

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
A = list(map(int, input().split()))
acc = 0
ans = 0
for a in A:
    eat = max(acc + a - x, 0)
    ans += eat
    acc = a - eat
acc = 0
ans2 = 0
for a in A:
    eat = max(acc + a - x, 0)
    ans2 += eat
    acc = a - eat
print(min(ans, ans2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:

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

ansA = 0
ansB = 0

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

print(ansA)