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()))
c = 0
if a[0] > x:
  c += a[0] - x
  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()))
asum=[0]*(n-1)
for i in range(n-1):
    asum[i]+=(a[i]+a[i+1])
temp=0
for j in range(n-1):
    if asum[j]>=x:
        if j!=n-2:
            asum[j+1]-=min(a[j+1],(asum[j]-x))
        temp+=(asum[j]-x)
            
print(temp)

Submission status: Accepted

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

for i in range(1,n):
  if i==1:
    if a[0]+a[1]>x:
      if a[0]>x:
        a[1]=0
        a[0]=x
      else:
        sa=(a[0]+a[1])-x
        a[1]=a[1]-sa
  else:
    if a[i]+a[i-1]>x:
      sa=(a[i]+a[i-1])-x
      a[i]=a[i]-sa
      
sum2=sum(a)
print(sum1-sum2)

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

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from sys import stdin
import sys
import math
from functools import reduce
import functools
import itertools
from collections import deque,Counter,defaultdict
from operator import mul
import copy
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
import heapq
sys.setrecursionlimit(10**6)
# INF =  float("inf")
INF = 10**18
import bisect
import statistics
mod = 10**9+7
# mod = 998244353

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

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

        if A[i+1] < t:
            A[i+1] = 0
        else:
            A[i+1] -= t
        ans += t

print(ans)

Submission status: Accepted

Solution code:
import sys
from copy import deepcopy
def input(): return sys.stdin.readline().strip()

def main():
    N, x = map(int, input().split())
    A = list(map(int, input().split()))
    if x == 0:
        print(sum(A))
        return

    B = deepcopy(A)
    ans1 = 0
    for i in range(1, N):
        ans1 += max(0, (A[i - 1] + A[i]) - x)
        if A[i] + A[i - 1] > x:
            if A[i] < (A[i - 1] + A[i]) - x:
                A[i] = 0
            else:
                A[i] -= (A[i - 1] + A[i]) - x
    ans2 = 0
    for i in range(N - 2, -1, -1):
        ans2 += max(0, (B[i] + B[i + 1]) - x)
        if B[i] + B[i + 1] > x:
            if B[i] < (B[i] + B[i + 1]) - x:
                B[i] = 0
            else:
                B[i] -= (B[i] + B[i + 1]) - x
    ans = min(ans1, ans2)
    print(ans)



if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
for i, a in enumerate(A):
  if i == 0:
    ans += min(a, x)
    now_x = x - min(a, x)
    continue
    
  ans += min(a, now_x)
  now_x = x - min(a, now_x)

ans2 = 0
for i, a in enumerate(A[::-1]):
  if i == 0:
    ans2 += min(a, x)
    now_x = x - min(a, x)
    continue
    
  ans2 += min(a, now_x)
  now_x = x - min(a, now_x)

print(sum(A) - max(ans, ans2))

Submission status: Accepted

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

if n!=2 and a[n-2]+a[n-1]>=x:
  sa=a[i]+a[i+1]-x
  ans+=sa
  if sa<=a[n-1]:
    a[n-1]-=sa
  else:
    a[n-2]=sa-a[n-1]
    a[n-1]=0
print(ans)

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
sum_val = A[0] + A[1]
if A[0] + A[1] > x:
    if A[1] >= sum_val - x:
        ans += sum_val - x
        A[1] -= sum_val - x
    else:
        ans += A[1] + (A[0] - x)
        A[0] -= (x - A[1])
        A[1] = 0
# print(A)
# print(ans)

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

print(ans)

Submission status: Accepted

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

    print(cnt)

if __name__ == "__main__":
    main()

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#
# abc048 c
#

import sys
from io import StringIO
import unittest


class TestClass(unittest.TestCase):
    def assertIO(self, input, output):
        stdout, stdin = sys.stdout, sys.stdin
        sys.stdout, sys.stdin = StringIO(), StringIO(input)
        resolve()
        sys.stdout.seek(0)
        out = sys.stdout.read()[:-1]
        sys.stdout, sys.stdin = stdout, stdin
        self.assertEqual(out, output)

    def test_入力例_1(self):
        input = """3 3
2 2 2"""
        output = """1"""
        self.assertIO(input, output)

    def test_入力例_2(self):
        input = """6 1
1 6 1 2 0 4"""
        output = """11"""
        self.assertIO(input, output)

    def test_入力例_3(self):
        input = """5 9
3 1 4 1 5"""
        output = """0"""
        self.assertIO(input, output)

    def test_入力例_4(self):
        input = """2 0
5 5"""
        output = """10"""
        self.assertIO(input, output)


def resolve():
    N, x = map(int, input().split())
    a = list(map(int, input().split()))

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

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

    print(ans)


if __name__ == "__main__":
     # unittest.main()
     resolve()

Submission status: Accepted

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

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


print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, x = map(int, input().split())
arr = list(map(int, input().split()))
sum_arr = sum(arr)
arr[0] = min(x, arr[0])


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

print(sum_arr - sum(arr))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,x=map(int,input().split())
l=list(map(int,input().split()))
c=0
t=l[0]
for a in l[1:]:
    d=t+a-x
    if d>0:
        if a<d:
            a=0
        else:
            a-=d
        c+=d
    t=a
print(c)

Submission status: Accepted

Solution code:


n, x = map(int, raw_input().split())
ais = map(int, raw_input().split())


q= sum([ai - min(x,ai) for ai in ais])

ais = [min(x,ai) for ai in ais]

for ii in range(1,len(ais)):
	if ais[ii] + ais[ii-1] > x:
		q += ais[ii] + ais[ii-1] - x
		ais[ii] -= ais[ii] + ais[ii-1] - x
print q

Submission status: Accepted

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

A_sum=sum(A)
A[0]=min(X,A[0])

for i in range(N-1):
    if A[i]+A[i+1]>X:
        A[i+1]=X-A[i]
print(A_sum-sum(A))

Submission status: Accepted

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

Submission status: Accepted

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

res = 0
for i in range(n-1):
    if a[i] + a[i+1] <= x:
        continue

    candy = a[i] + a[i+1] - x
    res += candy
    a[i+1] = max(0, a[i+1] - candy)

print(res)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

z=a[0]+a[1]-x
if z>0:
  ans+=z
  a[1]-=z
  if a[1]<0:
    a[0]-=abs(a[1])
    a[1]=0

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

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

Submission status: Accepted

Solution code:
n,x=map(int,input().split())
a=list(map(int,input().split()))
dp=[0]*(n-1)
ans=0
for i in range(n-1):
  dp[i]=a[i]+a[i+1]
for j in range(n-2):
  if dp[j]>x:
    ans+=min(dp[j]-x,a[j+1])
    dp[j+1]-=min(dp[j]-x,a[j+1])
    dp[j]-=min(dp[j]-x,a[j+1])
if dp[n-2]>x:
  ans+=dp[n-2]-x
  dp[n-2]-=dp[n-2]-x
if dp[0]>x:
  ans+=dp[0]-x
  dp[0]-=dp[0]-x
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] > x):
        cnt += a[i] - x
        a[i] = x
    
    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:
# @oj: atcoder
# @id: hitwanyang
# @email: 296866643@qq.com
# @date: 2020-08-14 17:33
# @url:https://atcoder.jp/contests/abc048/tasks/arc064_a
import sys,os
from io import BytesIO, IOBase
import collections,itertools,bisect,heapq,math,string
from decimal import *
# region fastio

BUFSIZE = 8192

BUFSIZE = 8192

class FastIO(IOBase):
    newlines = 0

    def __init__(self, file):
        self._fd = file.fileno()
        self.buffer = BytesIO()
        self.writable = "x" in file.mode or "r" not in file.mode
        self.write = self.buffer.write if self.writable else None

    def read(self):
        while True:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            if not b:
                break
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines = 0
        return self.buffer.read()

    def readline(self):
        while self.newlines == 0:
            b = os.read(self._fd, max(os.fstat(self._fd).st_size, BUFSIZE))
            self.newlines = b.count(b"\n") + (not b)
            ptr = self.buffer.tell()
            self.buffer.seek(0, 2), self.buffer.write(b), self.buffer.seek(ptr)
        self.newlines -= 1
        return self.buffer.readline()

    def flush(self):
        if self.writable:
            os.write(self._fd, self.buffer.getvalue())
            self.buffer.truncate(0), self.buffer.seek(0)


class IOWrapper(IOBase):
    def __init__(self, file):
        self.buffer = FastIO(file)
        self.flush = self.buffer.flush
        self.writable = self.buffer.writable
        self.write = lambda s: self.buffer.write(s.encode("ascii"))
        self.read = lambda: self.buffer.read().decode("ascii")
        self.readline = lambda: self.buffer.readline().decode("ascii")


sys.stdin, sys.stdout = IOWrapper(sys.stdin), IOWrapper(sys.stdout)
input = lambda: sys.stdin.readline().rstrip("\r\n")
# ------------------------------
def main():
    n,x=map(int,input().split())
    a=list(map(int,input().split()))
    s=a[0]
    ans=0
    for i in range(1,len(a)):
        if s+a[i]>x:
            temp=s+a[i]-x
            ans+=temp
            if temp>=a[i]:
                # ans+=(temp-a[i])
                a[i]=0
            else:
                a[i]=a[i]-temp
        s=a[i]
    print (ans)
if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import numpy as np
def input(): return sys.stdin.readline().rstrip()
def ii(): return int(input())
def mi(): return map(int, input().split())
def li(): return list(mi())


def main():
    n, x = mi()
    a = li()
    cnt = 0
    for i in range(1,n):
        if a[i-1]+a[i]>x:
            cnt += a[i-1]+a[i]-x
            a[i] = max(0, x-a[i-1])
    print(cnt)
        
if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

import copy
b = copy.deepcopy(a) 

#a[0]から貪欲
i=1
cnt=0
while i<n:
    if a[i]+a[i-1]>x:
        tmp=(a[i]+a[i-1])-x
        cnt+=tmp
        a[i]=max(0,a[i]-tmp)
    i+=1

#a[n-1]から貪欲
b=b[::-1]
i=1
cnt1=0
while i<n:
    if b[i]+b[i-1]>x:
        tmp=(b[i]+b[i-1])-x
        cnt1+=tmp
        b[i]=max(0,b[i]-tmp)
    i+=1

print(min(cnt,cnt1))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,X = (int(T) for T in input().split())
A = [int(T) for T in input().split()]
Eat = 0
for AT in range(0,N-1):
    if A[AT]+A[AT+1]>X:
        Need = A[AT]+A[AT+1]-X
        Eat += Need
        A[AT] -= max(0,Need-A[AT+1])
        A[AT+1] = max(0,A[AT+1]-Need)
print(Eat)

Submission status: Accepted

Solution code:
def solve():
    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):
        sumvalue = A[i] + A[i + 1]
        if sumvalue <= x:
            continue
        if i + 1 == N - 1:
            A[i + 1] -= sumvalue - x
        else:
            if A[i + 1] < sumvalue - x:
                A[i] -= sumvalue - x - A[i + 1]
                A[i + 1] = 0
            else:
                A[i + 1] -= sumvalue - x
        ans += sumvalue - x
    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Accepted

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

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

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

Submission status: Accepted

Solution code:
N,X = (int(T) for T in input().split())
A = [int(T) for T in input().split()]
Eat = 0
for AN in range(0,N-1):
    if A[AN]+A[AN+1]>X:
        Need = A[AN]+A[AN+1]-X
        if A[AN+1]>=Need:
            A[AN+1] -= Need
        else:
            A[AN] -= Need-A[AN+1]
            A[AN+1] = 0
        Eat += Need
print(Eat)

Submission status: Accepted

Solution code:
N, M = map(int, input().split())
L = list(map(int, input().split()))

count = 0

if L[0] > M:
	count+= (L[0]-M)
	L[0]-= (L[0]-M)

for i in range (1, N):
	if L[i]+L[i-1] > M:
		count+= (L[i]+L[i-1])-M
		L[i]-= (L[i]+L[i-1])-M
    
print(count)

Submission status: Accepted

Solution code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N, x = mapint()
As = list(mapint())

last = As[0]
ans = 0
for i in range(1, N):
    a = As[i]
    if last+a>x:
        diff = (last+a)-x
        ans += diff
        last = max(0, a-diff)
    else:
        last = 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(0,n-1):
    if a[i] + a[i+1] > x:
        rem = (a[i]+a[i+1]) - x
        if rem <= a[i+1]:
            ans += rem
            a[i+1] -= rem
        else:
            ans += rem
            a[i+1] = 0

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
for i, j in zip(range(N - 1), range(1, N)):
    Asum = A[i] + A[j]
    if Asum <= K:
        continue
    eat = Asum - K
    d = min(eat, A[j])
    A[j] -= d
    eat -= d
    ans += d
    d = min(eat, A[i])
    A[i] -= d
    ans += d

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
values = map(int, input().split())
count = 0
v1 = next(values)
for v2 in values:
    surplus = max(v1 + v2 - x, 0)
    count += surplus
    v1 = max(0, v2 - surplus)
print(count)

Submission status: Accepted

Solution code:
import sys
import math
import copy
import heapq
from functools import cmp_to_key
from bisect import bisect_left, bisect_right
from collections import defaultdict, deque, Counter
sys.setrecursionlimit(1000000)

# input aliases
input = sys.stdin.readline
getS = lambda: input().strip()
getN = lambda: int(input())
getList = lambda: list(map(int, input().split()))
getZList = lambda: [int(x) - 1 for x in input().split()]

INF = float("inf")
MOD = 10**9 + 7
divide = lambda x: pow(x, MOD-2, MOD)

def nck(n, k, kaijyo):
    return (npk(n, k, kaijyo) * divide(kaijyo[k])) % MOD

def npk(n, k, kaijyo):
    if k == 0 or k == n:
        return n % MOD
    return (kaijyo[n] * divide(kaijyo[n-k])) % MOD

def fact_and_inv(SIZE):
    inv = [0] * SIZE  # inv[j] = j^{-1} mod MOD
    fac = [0] * SIZE  # fac[j] = j! mod MOD
    finv = [0] * SIZE  # finv[j] = (j!)^{-1} mod MOD
    inv[1] = 1
    fac[0] = fac[1] = 1
    finv[0] = finv[1] = 1
    for i in range(2, SIZE):
        inv[i] = MOD - (MOD // i) * inv[MOD % i] % MOD
        fac[i] = fac[i - 1] * i % MOD
        finv[i] = finv[i - 1] * inv[i] % MOD

    return fac, finv

def renritsu(A, Y):
    # example 2x + y = 3, x + 3y = 4
    # A = [[2,1], [1,3]])
    # Y = [[3],[4]] または [3,4]
    A = np.matrix(A)
    Y = np.matrix(Y)
    Y = np.reshape(Y, (-1, 1))
    X = np.linalg.solve(A, Y)

    # [1.0, 1.0]
    return X.flatten().tolist()[0]

class TwoDimGrid:
    # 2次元座標 -> 1次元
    def __init__(self, h, w, wall="#"):
        self.h = h
        self.w = w
        self.size = (h+2) * (w+2)
        self.wall = wall
        self.get_grid()
        # self.init_cost()

    def get_grid(self):
        grid = [self.wall * (self.w + 2)]
        for i in range(self.h):
            grid.append(self.wall + getS() + self.wall)

        grid.append(self.wall * (self.w + 2))
        self.grid = grid
    def init_cost(self):
        self.cost = [INF] * self.size

    def pos(self, x, y):
        # 壁も含めて0-indexed 元々の座標だけ考えると1-indexed
        return y * (self.w + 2) + x
    def getgrid(self, x, y):
        return self.grid[y][x]
    def get(self, x, y):
        return self.cost[self.pos(x, y)]
    def set(self, x, y, v):
        self.cost[self.pos(x, y)] = v
        return
    def show(self):
        for i in range(self.h+2):
            print(self.cost[(self.w + 2) * i:(self.w + 2) * (i+1)])
    def showsome(self, tgt):
        for t in tgt:
            print(t)
        return
    def showsomejoin(self, tgt):
        for t in tgt:
            print("".join(t))
        return

    def search(self):
        grid = self.grid
        move = [(0, 1), (0, -1), (1, 0), (-1, 0)]
        move_eight = [(0, 1), (0, -1), (1, 0), (-1, 0), (1, 1), (1, -1), (-1, 1), (-1, -1)]
        # for i in range(1, self.h+1):
        #     for j in range(1, self.w+1):
        #         cx, cy = j, i
        #         for dx, dy in move_eight:
        #             nx, ny = dx + cx, dy + cy

def solve():
    n, x = getList()
    nums = getList()
    ans = 0
    if nums[0] > x:
        ans += nums[0] - x
        nums[0] = x
    for i in range(1, n):
        t = nums[i] + nums[i-1] - x
        if t > 0:
            ans += t
            nums[i] -= t

    print(ans)

def main():
    n = getN()
    for _ in range(n):
        solve()

    return
if __name__ == "__main__":
    # main()
    solve()

Submission status: Accepted

Solution code:
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N, x = LI()
    a = LI()

    # 隣り合う2つの和
    s = [a[i] + a[i+1] for i in range(N - 1)]
    # 隣り合う2つの和をx以下にしたもの
    s_after = [min(i, x) for i in s]

    # 隣り合う2つの和をx以下にしたものから元の数列を復元
    a_after = [0] * N
    a_after[0] = min(a[0], s_after[0])
    for i in range(N - 1):
        a_after[i+1] = min(s_after[i] - a_after[i], a[i+1])

    ans = sum(a) - sum(a_after)
    print(ans)

if __name__ == '__main__':
    resolve()

Submission status: Accepted

Solution code:
def candy():
    boxNum, x = [int(i) for i in input().split()]
    ain = input()
    alist = ain.split(' ')

    s = 0
    
    for i in range(0, boxNum-1):
        if int(alist[i+1]) + int(alist[i]) > x:
            s = s + (int(alist[i+1]) + int(alist[i]) - x)
            alist[i+1] = x - int(alist[i])
            if int(alist[i+1]) < 0:
                alist[i] = int(alist[i]) + int(alist[i+1])
                alist[i+1] = 0
        
    print(s)
    
candy()