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(n-1):
    j = i + 1
    if j < n and a[i]+a[j] > x:
        d = min(a[j], (a[i]+a[j]) - x)
        a[j] -= d
        ans += d
    if a[i]+a[j] > x:
        d = min(a[i], (a[i]+a[j]) - x)
        a[i] -= d
        ans += d
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
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 heapq import heapify, heappop, heappush
 
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 = 0
tmp = sum(A)
for i in range(N-1):
    # 減らす分, 減らさなくてこともある
    a = max(A[i] + A[i+1] - X, 0)

    A[i+1] = max(0, A[i+1]-a)
    # A[i]で残り減らす必要ある分
    b = max(0, a - A[i+1])
# print(A)
# print(ans)
c = 0
if A[0] + A[1] > X:
    c = A[0] + A[1] - X
A[0] = A[0] - c
print(tmp-sum(A))

Submission status: Accepted

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

ans = 0
for i in range(N - 1):
    j = i + 1
    if j < N and A[i] + A[j] > X:
        d = min(A[j], (A[i] + A[j]) - X)
        ans += d
        A[j] -= d
    if A[i] + A[j] > X:
        d = min(A[i], (A[i] + A[j]) - X)
        ans += d
        A[i] -= d

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

ans = 0
for k in range(1,N):
    if a[k-1] + a[k] > x:
        t = (a[k-1] + a[k]) - x
        ans += t
        if a[k] >= t:
            a[k] -= t
        else:
            a[k-1] -= t-a[k]
            a[k] = 0
print(ans)

Submission status: Accepted

Solution code:
# Beginner 50 C
N, x = map(int, input().split())
A = list(map(int, input().split()))
sum_s = sum(A)

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

sum_e = sum(A)
print(sum_s - sum_e)

Submission status: Accepted

Solution code:

si = lambda: input().strip()
ii = lambda: int(input())
mii = lambda: map(int, input().split())
lmii = lambda: list(map(int, input().split()))
lmsi = lambda: list(map(str, input().split()))
smii = lambda: sorted(map(int, input().split()))

# ----------

N, x = mii()
A = lmii()

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

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#%% for atcoder uniittest use
import sys
input= lambda: sys.stdin.readline().rstrip()
def pin(type=int):return map(type,input().split())
def tupin(t=int):return tuple(pin(t))
#%%code
def resolve():
    N,x=pin()
    A=list(pin())

    temp=A[0]
    ans=0
    if temp>x:
        A[0]=x
        ans+=temp-x
    
    for ai in range(1,N):
        temp=A[ai]+A[ai-1]
        if temp>x:
            A[ai]=x-A[ai-1]
            ans+=temp-x
        temp-=A[ai-1]
    print(ans)
        
#%%submit!
resolve()

Submission status: Accepted

Solution code:
N,X=map(int,input().split())
a=list(map(int,input().split()))
total=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]
print(total-sum(a))

Submission status: Accepted

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

    ans = []
    if a[0] <= x:
        ans.append(a[0])
    else:
        ans.append(x)
    for i in range(1,n):
        if ans[i-1] + a[i] <= x:
            ans.append(a[i])
        else:
            ans.append(x-ans[i-1])
    print(sum(a)-sum(ans))

resolve()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(1 << 25)
read = sys.stdin.readline
ra = range
enu = enumerate


def read_ints():
    return list(map(int, read().split()))


def read_a_int():
    return int(read())


def read_tuple(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(tuple(map(int, read().split())))
    return ret


def read_col(H):
    '''
    H is number of rows
    A列、B列が与えられるようなとき
    ex1)A,B=read_col(H)    ex2) A,=read_col(H) #一列の場合
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return tuple(map(list, zip(*ret)))


def read_matrix(H):
    '''
    H is number of rows
    '''
    ret = []
    for _ in range(H):
        ret.append(list(map(int, read().split())))
    return ret
    # return [list(map(int, read().split())) for _ in range(H)] # 内包表記はpypyでは遅いため


MOD = 10**9 + 7
INF = 2**31  # 2147483648 > 10**9
# default import
from collections import defaultdict, Counter, deque
from operator import itemgetter
from itertools import product, permutations, combinations
from bisect import bisect_left, bisect_right  # , insort_left, insort_right

# https://atcoder.jp/contests/abc048/tasks/arc064_a
N, x = read_ints()
A = read_ints()

# 前処理ですべてx以下にしてから
# 漸化式 a[i+1] <= x-a[i]を満たすようにa[i+1]を決定していく(等号が成り立つときが一番いい)
# 右からやったり左からやったときに結果って変わらないのかな...?(大丈夫、無駄に多く減らすことはない)

# 前処理
ans = 0
for i in range(N):
    ans += max(A[i] - x, 0)
    A[i] = min(A[i], x)

# 漸化式を解く
for i in range(N - 1):
    new = x - A[i]
    if A[i + 1] <= new:
        continue  # もともと条件は満たされている
    ans += A[i + 1] - new
    A[i + 1] = new  # x>=A[i]なので大丈夫
print(ans)

Submission status: Accepted

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

ans = 0
for i in range(n):
    ans += a[i] - tmp[i]
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
for i in range(n):
    ans += a[i] - tmp[i]
print(ans)

Submission status: Accepted

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

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

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

    diff = (a[i] + a[i + 1]) - x
    ans += diff

    tmp = min(diff, a[i + 1])
    a[i + 1] -= tmp
    diff -= tmp
    a[i] -= diff
      
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:
    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()))

B = []
for i in range(N):
    if i == 0:
        B.append(A[i])
    else:
        B.append(A[i]+A[i-1])
B.append(A[-1])

ans = 0
for i in range(N):
    if B[i] > x:
        a = B[i] - x
        ans += a
        B[i] -= a
        B[i+1] -= a
if B[N] > x:
    ans += B[N]-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):
    a = A[i - 1]
    b = A[i]
    if a + b <= x:
        continue
    over = (a + b) - x
    ans += over
    A[i] = max(0, b - over)

print (ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

    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):
    L = A[i]
    R = A[i + 1]
    eat = max(0, L + R - x)
    ans += eat
    A[i + 1] = max(0, R - eat)

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 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:
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,m = inpl()
a = inpl()
res = 0
for i in range(n-1):
    if a[i+1] + a[i] > m:
        r = a[i+1] + a[i] - m
        res += r
        if a[i] <= m:
            a[i+1] -= r
        else:
            a[i+1] = 0
            a[i] = m
print(res)

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

Submission status: Accepted

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

abc048_c()

Submission status: Accepted

Solution code:
import sys
import math  # noqa
import bisect  # noqa
import queue  # noqa


def input():
    return sys.stdin.readline().rstrip()


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

    A_origin = A.copy()

    if A[0] > x:
        A[0] = x

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

    res = 0
    for i in range(N):
        res += abs(A_origin[i] - A[i])

    print(res)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

    total = sum(a)

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

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

    print(total-sum(a))

Submission status: Accepted

Solution code:
def resolve():
	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:
			st = (a[i] + a[i + 1]) - x
			a[i+1] = a[i+1] - st if a[i+1] > st else 0
			c += st
	print(c)
resolve()

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for _ in range(j)] for _ in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from itertools import accumulate #list(accumulate(A))

N, x = mi()
A = li()
adj = []
'''
for i in range(N-1):
    adj.append(A[i]+A[i+1])

ans = 0
for i, num in enumerate(adj):
    if num > x:
        ans += num - x
        if i < N-2:
            adj[i+1] = max(adj[i+1] - (num-x), A[i+2])
print(ans)
'''
ans = 0
for i in range(N-1):
    if A[i] + A[i+1] > x:
        wa = A[i] + A[i+1]
        ans += wa - x
        if A[i+1] < wa - x:
            A[i+1] = 0
        else:
            A[i+1] -= wa - x

print(ans)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10**8)
def ii(): return int(sys.stdin.readline())
def mi(): return map(int, sys.stdin.readline().split())
def li(): return list(map(int, sys.stdin.readline().split()))
def li2(N): return [list(map(int, sys.stdin.readline().split())) for _ in range(N)]
def dp2(ini, i, j): return [[ini]*i for _ in range(j)]
def dp3(ini, i, j, k): return [[[ini]*i for _ in range(j)] for _ in range(k)]
#import bisect #bisect.bisect_left(B, a)
#from collections import defaultdict #d = defaultdict(int) d[key] += value
#from itertools import accumulate #list(accumulate(A))

N, x = mi()
A = li()
adj = []

for i in range(N-1):
    adj.append(A[i]+A[i+1])

ans = 0
for i, num in enumerate(adj):
    if num > x:
        ans += num - x
        if i < N-2:
            adj[i+1] = max(adj[i+1] - (num-x), A[i+2])
print(ans)

Submission status: Accepted

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

#2つどこを見ても和がx以下
ans=0
pre=0
for i in range(N):
    if i==0:
        if A[i]>x:
            ans+=A[i]-x
            A[i]=x
        pre=A[i]
    else:
        if pre+A[i]>x:
            ans+=A[i]-(x-pre)
            A[i]=x-pre
        pre=A[i]
print(ans)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n, x, *a = map(int, read().split())
memo = [0] * n
ans = 0
if a[0] > x:
    ans += a[0] - x
    a[0] = x
for i in range(n - 1):
    memo[i] = a[i] + a[i + 1]
for i, check in enumerate(memo):
    if check > x:
        ans += check - x
        memo[i + 1] -= check - x
print(ans)

Submission status: Accepted

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

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

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

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

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

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

Submission status: Accepted

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

B = [A[0]]
for i in range(N-1):
    B.append(A[i] + A[i+1])
B.append(A[-1])

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

Submission status: Accepted

Solution code:
N,x= map(int, input().split())
Alist = list(map(int, input().split())) 
res = 0
if Alist[0] >x:
    res = Alist[0]-x
    Alist[0] = x
    
for i in range(0,N-1):    
    if Alist[i] + Alist[i+1] > x:
        de = Alist[i+1] + Alist[i] - x
        Alist[i+1] = Alist[i+1] - de
        res = res + de 

print(res)

Submission status: Accepted

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

for i in range(N - 1):
    taberu = ai[i] + ai[i+1] - x
    if taberu > 0:
        gokei += taberu
        ai[i+1] = ai[i+1] - taberu 
print(gokei)

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,=map(int,input().split())

ans=0

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

i=0
while i+1<=N-1:
    if A[i]+A[i+1]>X:
        ans+=A[i]+A[i+1]-X
        A[i+1]=X-A[i]
    i+=1

print(ans)

Submission status: Accepted

Solution code:
n,x,*a = map(int,open(0).read().split())
b = [0]*n
b[0] = min(x,a[0])
for i in range(n-1):
  b[i+1] = max(min(x-b[i],a[i+1]),0)
  b[i] = min(b[i],x-b[i+1])
ans = 0
for i,j in zip(a,b):
  ans += i-j
print(ans)

Submission status: Accepted

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

if a_lst[0] > x:
  cnt = a_lst[0] - x
  a_lst[0] = x
else:
  cnt = 0

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

Submission status: Accepted

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

ans=0
for i in range(1,n):
    if a[i] + a[i - 1] > x:
        # どれだけ減らさないといけないか
        v = a[i] + a[i - 1] - x
        ans += v
        if i == n - 1:
            a[n - 1] -= v
        else:
            # できるだけ次の値から減算する
            if a[i] - v >= 0:
                a[i] -= v
            else:
                # a[i+1]の項で削りきれなかった分。
                a[i-1] -= v - a[i]
                a[i] = 0
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)]
ans = 0
for i in range(N-2):
    if b[i] > x:
        temp = min(b[i] - x, a[i+1])
        ans += temp
        b[i] -= temp
        b[i+1] -= temp
for i in range(N-1):
    if b[i] > x:
        ans += b[i] - x
print(ans)

Submission status: Accepted

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

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

actions = 0

for a in range(1, n):
    diff = alist[a] + alist[a - 1] - x
    if diff > 0:
        actions += diff
        alist[a] = max(0, alist[a] - diff)

print(actions)

Submission status: Accepted

Solution code:
import sys
import heapq, math
from itertools import zip_longest, permutations, combinations, combinations_with_replacement
from itertools import accumulate, dropwhile, takewhile, groupby
from functools import lru_cache
from copy import deepcopy

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

s = sum(A)
for i in range(1, N):
    if A[i] + A[i - 1] > x:
        d = sum(A[i - 1:i + 1]) - x
        for j in range(2):
            if d <= 0:
                break
            A[i - j], d = A[i - j] - min(A[i - j], d), d - min(A[i - j], d)

print(s - sum(A))

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):
    a0 = A[i]
    a1 = A[i + 1]
    if a0 + a1 > x:
        d = min(a1, (a0 + a1) - x)
        cnt += d
        a1 -= d
        A[i + 1] = a1
    if a0 + a1 > x:
        d = min(a0, (a0 + a1) - x)
        cnt += d
        a0 -= d
        A[i] = a0

print(cnt)

Submission status: Accepted

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

ans = 0

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

print(ans)

Submission status: Accepted

Solution code:
n,x=map(int,input().split())
a=list(map(int,input().split()))
asum=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]
        
asum=asum-sum(a)
print(asum)

Submission status: Accepted

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

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)

Submission status: Accepted

Solution code:
n,x,*a=map(int,open(0).read().split());r=0
for i in range(1,n):e=max(a[i]+a[i-1]-x,0);a[i]-=min(e,a[i]);r+=e
print(r)

Submission status: Accepted

Solution code:
(N,x),a=[list(map(int,input().split()))for _ in[0]*2];r=0
for i in range(1,N):e=max(a[i]+a[i-1]-x,0);a[i]-=min(e,a[i]);r+=e
print(r)

Submission status: Accepted

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

eat = 0
for i in range(1, N):
    cur = candies[i] + candies[i - 1]
    if cur <= x:
        continue
    eat_i = cur - x
    c = min(eat_i, candies[i])
    candies[i] -= c
    eat += eat_i
print(eat)

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

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

if x ==0:
    print(sum(a))
    exit()

ans = 0

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

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

print(ans)

Submission status: Accepted

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


def main():
    N, X = map(int, input().split())
    A = list(map(int, input().split())) + [0]

    result = 0
    for i in range(N):
        tmp = max(0, A[i] + A[i - 1] - X)
        result += tmp
        A[i] -= tmp
    print(result)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
def resolve():
	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] + a[i-1]) > x:
				diff = a[i] + a[i-1] - x
				ans += diff
				a[i] = a[i] - diff
		if a[0] > x:
			ans += a[0] - x
			a[0] = x
	print(ans)
resolve()

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

print(count)

Submission status: Accepted

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

def SUM(a):
    L=len(a)
    ans=0
    for i in range(L):
        ans+=a[i]
    return ans

def check(a):
    if all(A[i-1]+A[i]<=x for i in range(1,N)):
        return 1
    else:
        return 0

if check(A)==1:
    print(0)
    exit()

SA=SUM(A)
count=0

for i in range(N):
    if A[i]>x:
        A[i]=x

count=SA-SUM(A)

if check(A)==1:
    print(count)
    exit()
else:
    count=0

for j in range(1,N):
    if A[j-1]+A[j]>x:
        s=A[j]+A[j-1]
        A[j]=A[j]+x-s
    else:
        pass

count=SA-SUM(A)

print(count)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import io

nim,mike = map(int,input().split())
kite = list(map(int,input().split()))
counter1 = 0
result = 0
for i in kite:
  highest = max(0,counter1+i-mike)
  counter1 = i - highest
  result+= highest
print(result)

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n, x, *a = map(int, read().split())
memo = 0
cnt = 0
if a[0] > x:
    cnt += a[0] - x
    a[0] = x
for i in range(n - 1):
    if a[i + 1] > x:
        cnt += a[i + 1] - x
        a[i + 1] = x
    v = a[i] + a[i + 1] - memo
    if v > x:
        memo = v - x
    else:
        memo = 0
    cnt += memo
print(cnt)

Submission status: Accepted

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

ans = 0

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
A = input().split()
alist = []
count = 0

for i in range(N):
    a = int(A[i])
    alist.append(a)

for ii in range(N - 1):
    if alist[int(ii)] + alist[int(ii) + 1] <= x:
        pass
    else:
        while alist[int(ii)] + alist[int(ii) + 1] > x:
            if alist[int(ii) + 1] > 0:
                k = alist[int(ii)] + alist[int(ii) + 1] - x
                if k <= alist[int(ii) + 1]:
                    alist[int(ii) + 1] -= k
                    count += k
                else:
                    alist[int(ii)] -= k - alist[int(ii) + 1]
                    alist[int(ii) + 1] = 0
                    count += k
            
print(count)

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

Submission status: Accepted

Solution code:
#coding: utf-8
import math
import heapq
import bisect
import numpy as np
from collections import Counter, deque
import itertools
#from scipy.misc import comb

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

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

print(ans)