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()))
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

for i in range(N-1):
  if B[i] > x:
    ansB += B[i] - x
    B[i] -= B[i] - x
  
  s = B[i] + B[i+1]
  if s > x:
    ansB += s - x
    B[i+1] -= s - x
  
print(min(ansA, ansB))

Submission status: Accepted

Solution code:
N,x=map(int,input().split())
A=list(map(int,input().split()))
cnt=0
for i in range(N):
  cnt+=max(A[i]-x,0)
  A[i]=min(A[i],x)
for i in range(1,N):
  d=max(A[i]+A[i-1]-x,0)
  A[i]-=d
  cnt+=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(1, N-1):
  v1 = max(A[i-1]+A[i]-x,0)
  v2 = max(A[i+1]+A[i]-x,0)
  if v1>0 and v2>0:
    m = min(v1, v2, A[i])
    A[i] -= m
    ans += m

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

Submission status: Accepted

Solution code:
N,x=map(int,input().split())
a=[int(x) for x in 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:
        cnt += a[i] + a[i+1] - x
        a[i+1]=x-a[i]
print(cnt)

Submission status: Accepted

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

ans = 0

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

print(ans)

Submission status: Accepted

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

ans = 0
A.insert(0,0)
for i in range(len(A)-1):
  s = A[i] + A[i+1]
  if s > x:
    diff = min(s - x, A[i+1])
    A[i+1] -= diff
    ans += diff
  
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

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

print(sum(map(lambda x: abs(x[0] - x[1]), zip(a, b))))

Submission status: Accepted

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

count = 0
for i in range(N):
    if a[i] > x:
        count += a[i] - x
        a[i] = x

for i in range(N-1):
    if a[i] + a[i+1] > x:
        count += a[i] + a[i+1] - x
        a[i+1] = 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()))
s1 = sum(a)
for i in range(0,N-2):
    if a[i]+a[i+1] > x:
        a[i] = min(a[i],x)
        a[i+1] = x - a[i]
    else: pass
if a[N-1]+a[N-2]>x:
    a[N-1] = min(a[N-1],x)
    a[N-2] = x - a[N-1]
s2 = sum(a)
print(s1-s2)

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline


def main():
    N, x = [int(x) for x in input().split()]
    A = [int(x) for x in input().split()]

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

    print(ans)


    
    


if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

print(s)

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
N, x  = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
ans = 0
for i in range(N):
    if a[i] > x:
        ans += a[i] - x
        a[i] = x
for j in range(1, N):
    if a[j] + a[j-1] > x:
        ans += a[j] + a[j-1] - x
        a[j] -= a[j] + a[j-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(n-1):
    w=a[i]+a[i+1]
    if w > x:
        if a[i+1] >= w-x:
            a[i+1]-=(w-x)
        else:
            a[i+1]=0
            a[i]-=(w-x-a[i+1])
        ans+=(w-x)
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):
  sum2 = A[i - 1] + A[i]
  if sum2 > x:
    ans += sum2 - x
    A[i] = x - A[i - 1]
    #print(A)
print(ans)

Submission status: Accepted

Solution code:
def main():
    n,x=map(int,input().split())
    A=list(map(int,input().split()))
    c=0
    prev = A[0]
    for a in A[1:]:
        remove = max(0, a + prev - x)
        if prev > x:
            prev = 0
        else:
            prev = a - remove
        c += remove
    print(c)
main()

Submission status: Accepted

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

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

print(count)

Submission status: Accepted

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

while a[0] > x:
    count += a[0] - x
    a[0] = x

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

print(count)

Submission status: Accepted

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

sum_A = sum(A)

for i in range(1,N):
    B[i] = max(0, A[i]+A[i-1]-K)


for i in range(1,N-1):
    t = min(B[i],B[i+1], A[i])
    A[i]-=t
    B[i]-=t
    B[i+1]-=t

print(sum_A - sum(A) + sum(B))

Submission status: Accepted

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

for i in range(n-1):
  tmp = aa[i+1] + aa[i]
  tmp = tmp - x #食べないといけない量
  if tmp > 0:
    sa = aa[i+1] - tmp
    if sa >= 0 :
      aa[i+1] -= tmp
    else:
      sa2 = tmp - aa[i+1]
      aa[i+1] = 0
      aa[i] -= sa2 
      
print(sum_aa - sum(aa))

Submission status: Accepted

Solution code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time,copy,functools
from collections import deque

sys.setrecursionlimit(10**7)
inf = 10**20
mod = 10**9 + 7

DR = [1, -1, 0, 0]
DC = [0, 0, 1, -1]

def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def S(): return input()
     
def main():
    N, x = LI()
    A = LI()
    cnt = 0
    for i in range(N-1):
        l = A[i]
        r = A[i+1]
        extra = l + r - x
        if extra <= 0:
            continue
        if extra <= r:
            A[i+1] = r - extra
        else:
            r = 0
            l = l - (extra - r)
            A[i+1] = r
            A[i] = l
        cnt += extra
    print(cnt)
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,len(a)):
    if a[i]+a[i-1]>x:
        ans+=a[i]+a[i-1]-x
        a[i]=x-a[i-1]
        if a[i]<0:
            a[i]=0
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

import sys

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

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

ans = 0
for i in range(N-1):
    if A[i] + A[i+1] > x:
        need = A[i] + A[i+1] - x
        ans += need
        # 先にある方から優先して減らす
        A[i+1] -= min(A[i+1], need)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

#print(A)
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(1,N):
    if A[i-1] + A[i] > x:
        if A[i] >= A[i-1]+A[i]-x: #右の箱だけ食べて条件を満たせる
            ans += A[i-1]+A[i]-x
            A[i] -= A[i-1]+A[i]-x
        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 = list(map(int, input().split()))

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

print(count)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from _collections import deque


def parser():
    while 1:
        data = list(input().split(' '))
        for number in data:
            if len(number) > 0:
                yield (number)


input_parser = parser()


def gw():
    global input_parser
    return next(input_parser)


def gi():
    data = gw()
    return int(data)


MOD = int(1e9 + 7)

import numpy
from collections import deque
from math import sqrt
from math import floor
#https://atcoder.jp/contests/arc081/tasks/arc081_b
#D - Coloring Dominoes
"""
need to consider the case that ticket is not enough to lower everything
"""

N = gi()
x = gi()
A = [0] * N

for i in range(N):
    A[i] = gi()

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

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

    diff = A[i] + A[i - 1] - x
    if diff > 0:
        cost -= min(diff, A[i - 1])
        A[i - 1] -= cost
        ans += cost
print(ans)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc048/tasks/arc064_a
# 問題文により、操作後の数列は以下を満たす
# x>=a[i]+a[i+1]
# よって、x-a[i] >= a[i+1]を満たすようにa[i+1]を決定していけば良い
# 実際に与えられたaと満たすように作ったaの差が答え


N, x = list(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):
    new = x - A[i]
    if new >= A[i + 1]:
        # 条件を満たしているのでok
        continue
    ans += A[i + 1] - new
    A[i + 1] = new

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

Submission status: Accepted

Solution code:
# ABC48 C - Boxes and Candies
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 = a[i]+a[i+1]-X
        a[i+1] = max(0, a[i+1]-d)
        ans += d
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:
      ans+=min(a[i+1],a[i]+a[i+1]-x)
      a[i+1]-=min(a[i+1],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 = map(int, input().split())
A = list(map(int, input().split()))


ans = 0
for i in range(len(A)-1):
    if A[i] + A[i+1] > x:
        if A[i] >= x:
            ans += A[i] - x
            ans += A[i+1]
            A[i+1] = 0
        else:
            tmp = A[i] + A[i+1] - x
            ans += tmp
            A[i+1] -= tmp
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:
# import numpy as np
# import math
import sys
input = sys.stdin.readline


def main():
    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
        # print(res)
    # print(res)

    temp = a[0] + a[1] - x
    if temp >= 0:
        res += temp
        a[1] -= temp
    # print(res)

    for i in range(1,N-1):
        temp = a[i] + a[i+1] - x
        if temp > 0:
            res += temp
            if a[i+1] >= temp:
                a[i+1] -= temp
            elif a[i+1] < temp:
                a[i+1] = 0
                a[i] -= temp - a[i+1]



    # print(a)
    print(res)



main()

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

Submission status: Accepted

Solution code:
import math
import bisect
import collections
import itertools
def gcd(a,b):return math.gcd #最大公約数
def lcm(a,b):return (a*b) // math.gcd(a,b) #最小公倍数
def iin(): return int(input()) #整数読み込み
def imn(): return map(int, input().split()) #整数map取得
def iln(): return list(map(int, input().split())) #整数リスト取得
def iln_s(): return sorted(iln()) # 昇順の整数リスト取得
def iln_r(): return sorted(iln(), reverse=True) # 降順の整数リスト取得
def join(l, s=''): return s.join(l) #リストを文字列に変換
def perm(l, n): return itertools.permutations(l, n) # 順列取得
def comb(l, n): return itertools.combinations(l, n) # 組み合わせ取得
def divisors(n):
    divisors = []
    for i in range(1, int(n**0.5)+1):
        if n % i == 0:
            divisors.append(i)
            if i != n // i:
                divisors.append(n//i)
    return divisors


N,x = imn()
a = iln()

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

Submission status: Accepted

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

Submission status: Accepted

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

if alist[0] > x:
    ans += (alist[0] - x)
    alist[0] -= (alist[0] - x)

for al in range(1,N):
    if (alist[al-1]+alist[al]) > x:
        ans += (alist[al-1]+alist[al]) - x
        alist[al] -= (alist[al-1]+alist[al]) - x

print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

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


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

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

print(cnt)

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):
    sm = A[i] + A[i+1]
    if sm > x:
        diff = sm - x
        if A[i+1] >= diff:
            A[i+1] -= diff
        else:
            diff2 = diff - A[i+1]
            A[i+1] = 0
            A[i] -= diff2
        cnt += diff

print(cnt)

Submission status: Accepted

Solution code:
import copy

N, x = map(int,input().split())
a = list(map(int, input().split()))
a.append(0) # たぶん要らないけど念の為にダミー
b = copy.deepcopy(a) # bに対して操作して最後にaと比較

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

for i in range(N - 1): # 左から順に和がx以下になるように数字を変える
    if (b[i] + b[i + 1]) > x:
        b[i + 1] = x - b[i]

ans = 0
for i in range(N): # 操作したbと元のaの差を比較
    ans += a[i] - b[i]

print(str(ans))

Submission status: Accepted

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

s=0
if(A[0]>x):
    s+=A[0]-x
    A[0]=x

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

print(s)

Submission status: Accepted

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

Submission status: Accepted

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

for i in range(N):
    diff = max(a[i] + prev - x, 0)
    answer += diff
    a[i] -= diff
    prev = a[i]

print(answer)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys, math, itertools, collections, bisect
input = lambda: sys.stdin.buffer.readline().rstrip().decode('utf-8')
inf = float('inf')
ans = 0 ;count = 0 ;pro = 1

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

Submission status: Accepted

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

cnt = 0
for i in range(1, N+1):
    if i == 1:
        if a[i]>x:
            cnt += a[i]-x
            a[i] -= a[i]-x
        continue
    if a[i-1]+a[i]>x:
        cnt += a[i]+a[i-1]-x
        a[i] -= a[i]+a[i-1]-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 A[i] + A[i+1] > x:
        # 超えている分を引く
        sa = A[i] + A[i+1] - x
        if A[i+1] >= sa:  # A[i+1]だけでカバーできる場合
            A[i+1] -= sa
            ans += sa
            continue
        else:
            ans += A[i+1]
            sa -= A[i+1]
            A[i+1] = 0
            ans += sa
            A[i] -= sa
    else:
        continue
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:
        continue
    rem = x - a[i]
    eat = a[i + 1] - rem
    cnt += eat
    a[i + 1] = rem

print(cnt)

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

print(cnt)

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
import sys

n, x, *a = map(int, sys.stdin.read().split())

def main():
    cnt = 0
    lim = x
    for c in a:
        if c > lim:
            cnt += c - lim
            lim = x - lim
        else:
            lim = x - c
            
    return cnt

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

Submission status: Accepted

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

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

# print(a, ans)

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

    # while a[i-1] + a[i] > x:
    #     a[i] -= 1
    #     ans += 1

print(ans)

Submission status: Accepted

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

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

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

print(c)

Submission status: Accepted

Solution code:
n, x = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
c = 0
for i in range(1, len(a)):
	b = a[i - 1] + a[i]
	d = b - x
	if d > 0:
		c += d
		e = min(a[i], d)
		a[i] -= e
		d -= e
		if d > 0:
			a[i - 1] -= d
print(c)

Submission status: Accepted

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

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

# 食べる数
tabeta = 0

# xを超えている数を出す
for n in range(N):
    if a[n] > x:
        # 超えている分は食べる
        tabeta += a[n] - x
        # 食べたので、残りはt個
        a[n] = x
        
# 隣り合っている数がXを超えているかチェック
for n in range(0, N-1):
    # 隣り合う数を足してもxを超える分は食べる
    if a[n] + a[n+1] >= x:
        ima_taberu = a[n] + a[n+1] -x
        # いっぱいある方を食べる
        if ima_taberu <= a[n+1]:
            a[n+1] = a[n+1] - ima_taberu
        elif ima_taberu >= a[n+1]:
            a[n] = a[n] - ima_taberu - a[n+1]
        elif ima_taberu == a[i+1]:
            a[n+1] = a[n+1] - ima_taberu
        tabeta += ima_taberu 

print(tabeta)

Submission status: Accepted

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

ans = 0
for i in range(N):
  if i == 0:
    if A[i] <= x:
      pass
    else:
      ans += A[i] -x
      A[i] = x
  else:
    rest = x - A[i-1]
    if A[i] <= rest:
      pass
    else:
      ans += A[i] - rest
      A[i] = rest
print(ans)

Submission status: Accepted

Solution code:
import sys
from functools import reduce
import copy
import math
from pprint import pprint
import collections
import bisect
import itertools


sys.setrecursionlimit(4100000)


def inputs(num_of_input):
    ins = [input() for i in range(num_of_input)]
    return ins


def int_inputs(num_of_input):
    ins = [int(input()) for i in range(num_of_input)]
    return ins


def solve(inputs):
    [N, X] = string_to_int(inputs[0])
    A = string_to_int(inputs[1])

    count = 0
    for i in range(N-1):
        if A[i] > X:
            count += abs(X - A[i])
            A[i] = X
        if A[i] + A[i+1] >= X:
            count += abs(X - (A[i+1] + A[i]))
            A[i+1] = X - A[i]
    return count


def string_to_int(string):
    return list(map(lambda x: int(x), string.split()))


if __name__ == "__main__":
    ret = solve(inputs(2))
    print(ret)

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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