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().strip().split(' '))
A = [0] + list(map(int, input().strip().split(' ')))
cost = 0
for i in range(N):
  if A[i] + A[i+1] > X:
    cost += A[i+1]-(X-A[i])
    A[i+1] = X-A[i]
print(cost)

Submission status: Accepted

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

Submission status: Accepted

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

while(True):
    if len(a)<2:
        break
    
    minus = a[0]+a[1]-x
    if minus<=0:
        a.remove(a[0])
    elif minus>=a[1]:
        count+=minus
        a[1]=0
        a.remove(a[0])
    else:
        count += minus
        a[1] -= minus

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

Submission status: Accepted

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

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

print(count)

Submission status: Accepted

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

    moves = 0
    if boxes[0] > x:
        moves = boxes[0] - x
        boxes[0] = x
    for i in range(n - 1):
        y = boxes[i] + boxes[i + 1] - x
        if y > 0:
            boxes[i + 1] -= y
            moves += y
    print(moves)

Submission status: Accepted

Solution code:
#ABC048 C - Boxes and Candies
N,x = map(int,input().split())
A = list(map(int,input().split()))
A_ = A[:]
ans = 0

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# coding: utf-8
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 and a[i + 1] >= 1: # 左の組から順に、組の右側から食べていく
        ans += min(a[i] + a[i + 1] - x, a[i + 1])
        a[i + 1] -= min(a[i] + a[i + 1] - x, a[i + 1])
    if a[i] + a[i + 1] > x: # それでも足りないなら組の左側も食べる
        ans += a[i] + a[i + 1] - x
        a[i] -= a[i] + a[i + 1] - x
print(ans)

Submission status: Accepted

Solution code:
n, x = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
dio = 0 #お前は今までに食べたキャンディの数を覚えているのか？
if(a[0] > x):
    dio += a[0] - x
    a[0] = x
for i in range(n - 1):
    extra = a[i] + a[i + 1] - x
    if(extra > 0):
        a[i + 1] -= extra
        dio += extra
print(dio)

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,x =map(int,input().split())
a=list(map(int,input().split()))
cnt=0
for i in range(N-1):
    if a[i]+a[i+1]>x:
        #マイナスになるのを防ぐ
        if a[i+1]<(a[i]+a[i+1]-x):
            cnt+=a[i+1]
            a[i+1]=0
        else:
            cnt+=a[i]+a[i+1]-x
            a[i+1]-=a[i]+a[i+1]-x
#端はこの操作では実現できない可能性があるので別に
if a[0]+a[1]>x:
    cnt+=a[0]+a[1]-x
    a[0]-=a[0]+a[1]-x
if a[-1]+a[-2]>x:
    cnt+=a[-1]+a[-2]-x
    a[-1]-=a[-1]+a[-2]-x
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(1, N):
    calc = max(0, a[i-1] + a[i] - x)
    if a[i-1] <= calc <= a[i]:
        a[i] -= calc
        ans[i] = calc
    elif a[i] < calc:
        a[i] = 0
        ans[i] = calc
    else:
        a[i] -= calc
        ans[i] = calc

print(sum(ans))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
a = list(map(int, input().split()))
candy = min(a[0], x)
ans = a[0] - candy
a[0] = candy

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

print(ans)

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
a = list(map(int, input().split()))
candy = min(a[0], x)
ans = a[0] - candy
a[0] = candy

for i in range(1, N):
    candy += a[i]
    if candy > x:
        m = candy - x
        ans += m
        a[i] -= m
        candy -= a[i - 1] + m
    else:
        candy -= 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):
    num_eat_candy = max(0, (a[i - 1] + a[i]) - x)
    ans += num_eat_candy
    a[i] = max(0, a[i] - num_eat_candy)
    # a[i] -= num_eat_candy
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):
    num = a[i] + a[i+1]
    if num > x:
        ans += num - x
        if a[i+1] - (num - x) < 0:
            a[i+1] = 0
        else:
            a[i+1] -= num - x
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(N-1):
  if A[i]+A[i+1]>K:
    tmp=min(A[i+1],A[i]+A[i+1]-K)
    A[i+1]-=tmp
    ans+=tmp
  if A[i]+A[i+1]>K:
    tmp=min(A[i],A[i]+A[i+1]-K)
    A[i]-=tmp
    ans+=tmp
    
if A[-1]>K:
  tmp=A[-1]-K
  A[-1]-=tmp
  ans+=tmp
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, x = map(int, input().split(" "))
list_a = list(map(int, input().split(" ")))
count = 0
last = 0
for a in list_a:
  act = max(0, last + a - x)
  last = a - act
  count += act
print(count)

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

zan = x - a[0]

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

print(res)

Submission status: Accepted

Solution code:
"""
# 標準入力取得
## 文字列
 = sys.stdin.readline().rstrip()
 = list(sys.stdin.readline().rstrip())

## 数値
 = int(sys.stdin.readline())
 = map(int, sys.stdin.readline().split())
 = list(map(int, sys.stdin.readline().split()))
 = [list(map(int,list(sys.stdin.readline().split()))) for i in range(h)]  # 二次元配列入力　二次元マップみたいな入力のとき

# 0埋め, 小数点出力桁指定時のときの出力
a = 100
b = 0.987654321
print("{0:06d}-{1:6f}".format(a,b))
000100-0.987654

# 文字列をリストに格納
char_list = list("abcd") # ["a","b","c","d"]
"""

import math
import sys
import itertools
import queue
from fractions import gcd

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

mod = 1000000007

if __name__ == "__main__":
    N,x = map(int, sys.stdin.readline().split())
    a = list(map(int, sys.stdin.readline().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,*A=map(int,open(0).read().split());s=sum(A);A+=0,
for i in range(N):A[i]-=max(0,A[i]+A[i-1]-x)
print(s-sum(A))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

li[0] = min(x, a[0])
for i in range(N-1):
  if li[i] + a[i + 1] > x:
    li[i + 1] = x - li[i]
  else:
    li[i + 1] = a[i + 1]
    
cnt = 0
for ai, lii in zip(a, li):
  cnt += ai - lii

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

Submission status: Accepted

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

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

Submission status: Accepted

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

tmp = A[0] + A[1]
red = max(0,tmp - x)
ans = red
if red > 0:
    if red <= A[1]:
        A[1] -= red
    else:
        A[0] -= red - A[1]
        A[1] = 0
tmp = A[0] + A[1]


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

Submission status: Accepted

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

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

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

print(cnt)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

Solution code:

import sys
input = sys.stdin.readline
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
# A = [ int(input()) for _ in range(N) ]
##############################

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

count = 0

# いきなりオーバー
if A[0] > x:
    diff = A[0]-x
    count += diff
    A[0] = x

for i in range(1, N):
    # 超えてたら、ギリまで下げる
    if A[i]+A[i-1] > x:
        diff = (A[i]+A[i-1])-x
        A[i] -= diff
        count += diff

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

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

Submission status: Accepted

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

def calc(A,x):
  res = 0
  for i in range(len(A)-1):
    if A[i] + A[i+1] > x:
      if A[i] < x:
        res += A[i+1] - (x - A[i])
        A[i+1] = x - A[i]
      else:
        res += A[i] - x + A[i+1]
        A[i] = x
        A[i+1] = 0
  return res

print(calc(a,x))

Submission status: Accepted

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

def calc(A,x):
  res = 0
  for i in range(len(A)-1):
    if A[i] + A[i+1] > x:
      if A[i] < x:
        res += A[i+1] - (x - A[i])
        A[i+1] = x - A[i]
      else:
        res += A[i] - x + A[i+1]
        A[i] = x
        A[i+1] = 0
  return res

ar = a[::-1]
print(min(calc(a,x),calc(ar,x)))

Submission status: Accepted

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

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

print(answer)

Submission status: Accepted

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

answer = 0
if A[0] > X:
    answer += A[0] - X
    A[0] = X
    
for i in range(N-1):
    diff = A[i] + A[i+1] - X
    if diff > 0:
        answer += diff
        A[i+1] -= diff

print(answer)

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:
        t=a[i] + a[i + 1] - x
        a[i + 1] -= t
        ans += t

print(ans)

Submission status: Accepted

Solution code:
n,x=[int(i) for i in input().split()]
a=[0]+[int(i) for i in 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:
from collections import Counter,defaultdict,deque
import sys,heapq,bisect,math,itertools,string,queue
sys.setrecursionlimit(10**8)
mod = 10**9+7
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))
def inpl_str(): return list(sys.stdin.readline().split())
def inpln(n): return list(int(sys.stdin.readline()) for i in range(n))

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
sys.setrecursionlimit(2147483647)
INF=float("inf")
MOD=10**9+7
input=lambda :sys.stdin.readline().rstrip()
def resolve():
    n,x=map(int,input().split())
    A=list(map(int,input().split()))
    res=0
    for i in range(1,n):
        c=A[i]+A[i-1]-x
        if c>0:
            res+=c
            A[i]=max(A[i]-c,0)
    print(res)
resolve()

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0

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

print(ans)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
l = list(map(int, input().split()))
l.append(0)
hiku_sum = 0
# print(l)
for i in range(N):
    l_sum = l[i] + l[i + 1]
    if l_sum > x:
        hiku_min = l_sum - x
        hiku_sum += hiku_min
        if l[i + 1] >= hiku_min:
            l[i + 1] -= hiku_min
        else:
            l[i] -= hiku_min - l[i + 1]
            l[i + 1] = 0
print(hiku_sum)

Submission status: Accepted

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

for i in range(N-1):
  add = max(a_list[i] + a_list[i+1] - x, 0)
  a_list[i+1] -= add
  count += add

print(count)

Submission status: Accepted

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

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

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


if __name__ == "__main__":
    main()

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

        else:
            A[i+1] = 0

print(ans)

Submission status: Accepted

Solution code:
N,x= map(int,input().split())
array = list(map(int,input().split()))
array2=array.copy()
result=0
#TLE pattern
# for i in range(0,N-1):
#     #i と i+1 が閾値以下じゃなかったら 
#     while array[i] + array[i+1] > x:
#         if array[i+1]!=0:
#             array[i+1]-=1
#         else:
#             array[i]-=1
#         result+=1
# print(result)

for i in range(0,N-1):
    if array[i] + array[i+1] > x:
        # if array[i+1] - (array[i] + array[i+1] - x) >= 0:
        #     array[i+1]=array[i+1] - (array[i] + array[i+1] - x)
        if (array[i]+array[i+1])-x <= array[i+1]:
            array[i+1]= array[i+1] - (array[i]+array[i+1]-x)
        else:
            # array[i]=array[i] - (array[i]+array[i+1]) - x - array[i+1]
            array[i]= array[i]-(array[i]-x)
            array[i+1]=0

# print(array2)
# print(array)
print(sum(array2)-sum(array))

Submission status: Accepted

Solution code:
def solve(N, LIM):
    ax = list(map(int, input().split()))
    ans = 0
    left = ax[0]

    for i in range(N-2):
        # left right
        right = ax[i+1]
        total = left + right
        should_eat = total - LIM
        if should_eat > 0:
            ans += should_eat
            if should_eat < right:
                left = right - should_eat
            else:
                left = 0
        else:
            left = right

    ans += (left + ax[-1]) - LIM if (left+ax[-1]) > LIM else 0
    return ans


if __name__ == '__main__':
    n, x = map(int, input().split())
    print(solve(n, x))

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

a = [int(i) for i in input().split()]

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

original_amount = sum(a)

for i in range(N - 1):
    pair_amount = a[i] + a[i + 1]
    if x >= pair_amount:
        continue
    diff = pair_amount-x
    a[i] = min(a[i], a[i]-(diff-a[i + 1]))
    a[i + 1] = max(a[i + 1] - diff, 0)

result_amount = sum(a)

print(original_amount-result_amount)