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:
# int(input()) # 入力が1つ
from copy import deepcopy
n, x = map(int, input().split()) # 入力が複数
box = [int(i) for i in input().split()] # 配列で数字
org = deepcopy(box)


for i in range(n - 1):
    if box[i] + box[i + 1] > x:
        t = max(x - box[i], 0)
        box[i + 1] = t
        if box[i] > x:
            box[i] = x
ans = 0
for i, j in zip(box, org):
    ans += (j - 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):
    if A[i] + A[i-1] > x:
        if A[i-1] > x:
            ans += A[i-1] - x
            A[i-1] = x
        ans += A[i] + A[i-1] - x
        A[i] = x - A[i-1]
print(ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
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 LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n,x=MI()
    aa=LI()
    ans=0
    for i in range(n-1):
        cur=max(aa[i]+aa[i+1]-x,0)
        ans+=cur
        aa[i+1]-=min(cur,aa[i+1])
    print(ans)

main()

Submission status: Accepted

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

result = 0

if x == 0:
  result = sum(li)
  pass
else:
  for i in range(N-1):
    S = li[i] + li[i+1]
    if S <= x:
      pass
    else:
      eat = S - x
      result += eat
      if eat <= li[i+1]:
        li[i+1] = li[i+1] - eat
      else:
        li[i+1] = 0

print(result)

Submission status: Accepted

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

def solve(N,x,A):
    ans = max(A[0]-x,0)
    A[0] -= ans
    for i in range(1,N):
        p = max(A[i]-(x-A[i-1]),0)
        A[i] -= p
        ans += p
    return ans
print(solve(N,x,A))

Submission status: Accepted

Solution code:
from copy import copy


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

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

a = copy(A)
ans2 = 0
for i in range(N-1):
    if a[N-i-1] + a[N-i-2] > x:
        surplus = a[N-i-1] + a[N-i-2] - x
        a[N-i-2] -= min(a[N-i-2], surplus)
        ans2 += surplus

print(min(ans1, ans2))

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

if __name__ ==  '__main__':
  main()

Submission status: Accepted

Solution code:
import sys

n, x, *a = map(int, sys.stdin.read().split())
a = [0] + a

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

if __name__ ==  '__main__':
  main()

Submission status: Accepted

Solution code:
import sys

n, x, *a = map(int, sys.stdin.read().split())
a = [0] + a

def main():
    tot = 0
    for i in range(1, n + 1):
        tot += max(0, a[i] + a[i-1] - x)
        a[i] = min(a[i], x - a[i-1])
    print(tot)

if __name__ ==  '__main__':
    main()

Submission status: Accepted

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

ans=0

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

for i in range(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 collections
import sys

N, x = map(int, input().split())
A_lis = list(map(int, input().split()))
A_q = collections.deque(A_lis)

sys.setrecursionlimit(10 ** 9)

con = 0
tmp1 = A_q.popleft()
for i in range(N-1):
    tmp2 = A_q.popleft()
    if tmp1 + tmp2 > x:
        con += int((tmp1 + tmp2) - x)
        tmp1 = max(0, x - tmp1)
    else:
        tmp1 = tmp2

print(con)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
#
# Written by NoKnowledgeGG @YlePhan
# ('ω')
#
#import math
#mod = 10**9+7
#import itertools
#import fractions
#import numpy as np
#mod = 10**4 + 7
"""def kiri(n,m):
  r_ = n / m
  if (r_ - (n // m)) > 0:
    return (n//m) + 1
  else:
    return (n//m)"""

""" n! mod m 階乗
mod = 1e9 + 7
N = 10000000
fac = [0] * N
def ini():
  fac[0] = 1 % mod
  for i in range(1,N):
    fac[i] = fac[i-1] * i % mod"""

"""mod = 1e9+7
N = 10000000
pw = [0] * N
def ini(c):
  pw[0] = 1 % mod
  for i in range(1,N):
    pw[i] = pw[i-1] * c % mod"""

"""
def YEILD():
  yield 'one'
  yield 'two'
  yield 'three'
generator = YEILD()
print(next(generator))
print(next(generator))
print(next(generator))
"""
"""def gcd_(a,b):
  if b == 0:#結局はc,0の最大公約数はcなのに
    return a
  return gcd_(a,a % b) # a = p * b + q"""
"""def extgcd(a,b,x,y):
  d = a
  if b!=0:
    d = extgcd(b,a%b,y,x)
    y -= (a//b) * x
    print(x,y)
  else:
    x = 1
    y = 0
  return d"""


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

def main():
  n,x = readInts()
  A = readInts()
  ans = 0
  i = 0
  if sum(A[i:i+2]) > x:
    sum_ = sum(A[i:i+2])
    # preprocessing
    
    if A[i+1] - (sum_ - x) < 0:
      A[i+1] = 0
      ans += A[i+1]
      nya = sum_ - x
      A[i] -= nya
      ans += nya
    else:
      nya = sum_ - x
      A[i+1] -= nya
      ans += nya
      
  for i in range(1,n-1):
    if sum(A[i:i+2]) > x:
      sum_ = sum(A[i:i+2])
      nya = sum_ - x
      A[i+1] -=nya
      ans += nya
  print(ans)
  
if __name__ == '__main__':
  main()

Submission status: Accepted

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

ans = 0
for i in range(n-1):
    target = a_ls[i:i+2]
    eat = sum(target) - x
    if eat > 0:
        ans += eat
        if eat < target[1]:
            target[1] -= eat
        else:
            rest = eat - target[1]
            target[1] = 0
            target[0] -= rest
    a_ls[i:i+2] = target
print(ans)

Submission status: Accepted

Solution code:
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from fractions import gcd
from math import factorial, sqrt
from functools import lru_cache, reduce
INF = 1 << 60
mod = 1000000007
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

# UnionFind
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# ダイクストラ
def dijkstra_heap(s, edge, n):
    #始点sから各頂点への最短距離
    d = [10**20] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d

# 素因数分解
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

# 2数の最小公倍数
def lcm(x, y):
    return (x * y) // gcd(x, y)

# リストの要素の最小公倍数
def lcm_list(numbers):
    return reduce(lcm, numbers, 1)

# リストの要素の最大公約数
def gcd_list(numbers):
    return reduce(gcd, numbers)

# ここから書き始める
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:
        # print("a =", a, "ans =", ans)
        continue
    ans += a[i] + a[i - 1] - x
    # if a[i] > x:
    #     a[i] -= a[i] + a[i - 1] - x
    #     a[i] = max(a[i], 0)
    # elif a[i] + a[i - 1] > x:
    #     a[i] = 0
    a[i] = max(x - a[i - 1], 0)
    # print("a =", a, "ans =", ans)
print(ans)

Submission status: Accepted

Solution code:

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

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

print(cdy)

Submission status: Accepted

Solution code:
N,X = map(int,input().split())
A = list(map(int,input().split()))
A = [0] + A + [0]
cnt = 0
for i in range(1,N+1):
    S = A[i]+A[i+1]
    if S>X:
        d = S-X
        cnt += d
        A[i+1] = max(0,A[i+1]-d)
print(cnt)

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
count1=count
a1=a.copy()
for i in range(0, N, 2):
    ai=a1[i]
    if i==0:
        a1[i]=min(ai, x-a1[i+1])
    elif i==N-1:
        a1[i]=min(ai, x-a1[i-1])
    else:
        a1[i]=min(x-a1[i-1], x-a1[i+1], ai)
    count1+=ai-a1[i]

count2=count
a2=a.copy()
for i in range(1, N, 2):
    ai=a2[i]
    if i==0:
        a[i]=min(ai, x-a2[i+1])
    elif i==N-1:
        a2[i]=min(x-a2[i-1], ai)
    else:
        a2[i]=min(x-a2[i-1], x-a2[i+1], ai)
    count2+=ai-a2[i]

count3=count
prev=a[0]
for i in range(1, N):
    ai=a[i]
    if x-prev>=0 and x-prev<ai:
        a[i]=x-prev
    count3+=ai-a[i]
    prev=a[i]
print(count3)

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
count1=count
a1=a.copy()
for i in range(0, N, 2):
    ai=a1[i]
    if i==0:
        a1[i]=min(ai, x-a1[i+1])
    elif i==N-1:
        a1[i]=min(ai, x-a1[i-1])
    else:
        a1[i]=min(x-a1[i-1], x-a1[i+1], ai)
    count1+=ai-a1[i]

count2=count
a2=a.copy()
for i in range(1, N, 2):
    ai=a2[i]
    if i==0:
        a[i]=min(ai, x-a2[i+1])
    elif i==N-1:
        a2[i]=min(x-a2[i-1], ai)
    else:
        a2[i]=min(x-a2[i-1], x-a2[i+1], ai)
    count2+=ai-a2[i]

count3=count
prev=a[0]
for i in range(1, N):
    ai=a[i]
    if x-prev>=0 and x-prev<ai:
        a[i]=x-prev
    count3+=ai-a[i]
    prev=a[i]
print(min(count1, count2, count3))

Submission status: Accepted

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

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

if x < t[0]:
    count = t[0]-x
    t[0] = x
else:
    count = 0

for i in range(1,n):
    if x < t[i]+t[i-1]:
        count += -x+t[i-1]+t[i]
        t[i] = x-t[i-1]
print(count)

Submission status: Accepted

Solution code:
X = [int(e) for e in input().split()]
candies = [int(f) for f in input().split()]
i=1
ans=0
t = 0
while i<X[0]:
    if(candies[i-1]+candies[i]) > X[1] :
        t = (candies[i-1]+candies[i]) - X[1]
        ans += t
        if(t>=candies[i]):
            candies[i] = 0
        else:
            candies[i] -= t
    i+=1
print(ans)

Submission status: Accepted

Solution code:
import sys

sys.setrecursionlimit(10 ** 5 + 10)


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


def resolve():
    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:
            cnt += A[i] + A[i + 1] - x
            A[i + 1] = x - A[i]
    print(cnt)


resolve()

Submission status: Accepted

Solution code:
def main():
    import sys
    input = sys.stdin.readline
    sys.setrecursionlimit(10**7)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations, permutations, accumulate, groupby
    #from itertools import product
    from bisect import bisect_left,bisect_right
    from heapq import heapify, heappop, heappush
    from math import floor, ceil
    #from operator import itemgetter

    #inf = 10**17
    #mod = 10**9 + 7

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

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


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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, x = map(int, input().split())
a = list(map(lambda x: int(x), input().split()))
#in1 = '5 9'
#in2 = '3 1 4 1 5'
#n, x = map(int, in1.split())
#a = list(map(lambda x: int(x), in2.split()))

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

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

print(RC)

Submission status: Accepted

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

Submission status: Accepted

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

print(ans)

Submission status: Accepted

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

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

print(count)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

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()))
    
    if A[0] > x:
        yield A[0] - x
        A[0] = x

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

    
print(sum(main()))

Submission status: Accepted

Solution code:
f=lambda:map(int,input().split());n,x=f();t=a=0
for i in f():t=min(i,x-t);a+=i-t
print(a)

Submission status: Accepted

Solution code:
f=lambda:map(int,input().split())
n,x=f()
t=a=0
for i in f(): t=min(i,x-t); a+=i-t
print(a)

Submission status: Accepted

Solution code:
f=lambda:map(int,input().split())
n,x=f()
l=f()
t=a=0
for i in l:
  t=min(i,x-t)
  a+=i-t
print(a)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

ans = sum(a)

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

ans -= sum(a)

print(ans)

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
from sys import stdin
from operator import itemgetter
import math
# stdin = open("sample.txt")

N, x = [int(x) for x in stdin.readline().rstrip().split()]
box = list(map(int,[int(x) for x in stdin.readline().rstrip().split()]))
box.insert(0,0)
ans = 0
for i in range(len(box)-1):
    a = box[i]
    b = box[i+1]
    # print(a,b)
    if a+b > x:
        box[i+1] -=  a+b-x
        ans += a+b-x
    else:
        continue
# print(box)
print(ans)

Submission status: Accepted

Solution code:
import sys

input_methods=['clipboard','file','key']
using_method=0
input_method=input_methods[using_method]

IN=lambda : map(int, input().split())
mod=1000000007

#+++++

def main():
	#a = int(input())
	num_of_boxes , max_candies = IN()
	candies_array = list(IN())
	total_eat_candies = 0
	pre = 0
	for nc in candies_array:
		num_eat = max(0, nc+pre-max_candies)
		total_eat_candies+=num_eat
		pre = nc - num_eat
	print(total_eat_candies)
	
	
	
#+++++
isTest=False

def pa(v):
	if isTest:
		print(v)
		
def input_clipboard():
	import clipboard
	input_text=clipboard.get()
	input_l=input_text.splitlines()
	for l in input_l:
		yield l

if __name__ == "__main__":
	if sys.platform =='ios':
		if input_method==input_methods[0]:
			ic=input_clipboard()
			input = lambda : ic.__next__()
		elif input_method==input_methods[1]:
			sys.stdin=open('inputFile.txt')
		else:
			pass
		isTest=True
	else:
		pass
		#input = sys.stdin.readline
			
	ret = main()
	if ret is not None:
		print(ret)

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

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=list(map(int,input().split()))
ans=0
for i in range(n-1):
    if a[i]>x:
        ans+=a[i]-x
        a[i]=x
    if a[i]+a[i+1]>x:
        ans+=a[i]+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()))
if n==2:
  print(max(0,sum(A)-x))
  exit()
if n==3:
  if A[0]+A[1]<=x:
    print(max(0,A[1]+A[2]-x))
  elif A[0]<=x:
    ans=A[1]-x+A[0]
    A[1]=x-A[0]
    print(ans+max(0,A[1]+A[2]-x))
  else:
    ans=A[0]+A[1]-x
    A[1]=0
    A[0]=x
    print(ans+max(0,A[1]+A[2]-x))
  exit()
ans=0
for i in range(n-1):
  if A[i]+A[i+1]<=x:
    pass
  elif A[i]<=x:
    ans=ans+A[i]-x+A[i+1]
    A[i+1]=x-A[i]
  else:
    ans=ans+A[i]+A[i+1]-x
    A[i+1]=0
    A[i]=x
print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# def calc(ran, count, cans, X):
#   for n in ran:
#     if n == 0:
#       while cans[n+1] + cans[n] > X:
#         cans[n] -= 1
#         count += 1  
#       continue
#     if n + 1 == N:
#       while cans[n-1] + cans[n] > X:
#         cans[n] -= 1
#         count += 1  
#       continue
#     while cans[n-1] + cans[n] > X or cans[n+1] + cans[n] > X:
#       cans[n] -= 1
#       count += 1
#   return count, cans

# N, X = map(int, input().split())
# cans = list(map(int, input().split()))

# count = 0

# # odd_range = range(1, N, 2)
# # even_range = range(0, N, 2)
# # count, cans = calc(odd_range, count, cans, X)
# # count, cans = calc(even_range, count, cans, X)

# # even_count = count
# # print(min(odd_count, even_count))

# for i in range(0, N-1):
#   if cans[i] + cans[i+1] > X:
#     cans[i+1] -= X - cans[i]
#     count += 1
# print(count)
n, x = map(int, input().split())
l = list(map(int, input().split()))
 
y = 0
if l[0] > x:
    w = l[0] - x
    y += w
    l[0] -= w
 
for i in range(n-1):
    if l[i] + l[i+1] > x:
        z = l[i+1] + l[i] - x
        y += z
        l[i+1] -= z
 
print(y)

Submission status: Accepted

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

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

print(count)

Submission status: Accepted

Solution code:
N,X=map(int,input().split())
L=list(map(int,input().split()))
count=0
for i in range(N-1):
	if i==0:
		if L[i]>X:
			count+=L[i]-X
			L[i]=X
	if L[i+1]+L[i]>X:
		count+=(L[i+1]-(X-L[i]))
		L[i+1]=X-L[i]
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):
    point=A[i]+A[i+1]
    cnt +=max(0,point-x)
    if point>x:A[i+1]=max(0,x-A[i])

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

print(cnt)

Submission status: Accepted

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

y = 0
if l[0] > x:
    w = l[0] - x
    y += w
    l[0] -= w

for i in range(n-1):
    if l[i] + l[i+1] > x:
        z = l[i+1] + l[i] - x
        y += z
        l[i+1] -= z

print(y)

Submission status: Accepted

Solution code:
x,y=list(map(int, input().split()))
z=list(map(int, input().split()))
ans=0
if z[0]>=y:
	ans=ans+z[1]
	z[1]=0
	ans=ans+(z[0]-y)
	z[0]=y

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

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

Submission status: Accepted

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

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

Submission status: Accepted

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

ans = 0

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

print(ans)

Submission status: Accepted

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

for i in range(1, N):
    diff = A[i - 1] + A[i] - x
    if diff <= 0:
        continue
    v1 = min(A[i], diff)
    v2 = diff - v1
    A[i] -= v1
    A[i - 1] -= v2
    # print('A', A)
ans = sum_A - sum(A)
print(ans)

Submission status: Accepted

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

# if len(A) % 2 == 0:
#     A = A + [0]
# print('A', A)

ans = 0
for i in range(1, N):
    total = A[i - 1] + A[i]
    diff = total - x
    if diff <= 0:
        continue
    v1 = min(A[i], diff)
    v2 = diff - v1
    A[i] -= v1
    A[i - 1] -= v2
    ans += v1 + v2
    # print('A', A)
print(ans)

Submission status: Accepted

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


cnt = 0
cnt += max(0,As[0] - x) 
As[0] = min(As[0],x)

# print(cnt)
for i in range(N-1):
    if As[i] + As[i+1] <= x:
        continue
    else:
        n = As[i] + As[i+1] - x
        As[i+1] -= n
        cnt += n
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] > 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] = x - a[i]

print(ans)

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
alist = list(map(int, input().split()))
ans = 0
for i in range(N):
    sbn = max(0, alist[i] - x)
    ans += sbn
    alist[i] -= sbn

for i in range(1, N):
    val = alist[i] + alist[i-1]
    if val > x:
        sbn = val - x
        ans += sbn
        alist[i] -= sbn
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)