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:
def boxes():
    N, x = input().split()
    N, x = int(N), int(x)
    arr = [int(a) for a in input().split()]

    total = 0
    for i in range(1, N):
        a, b = arr[i-1], arr[i]
        extra = a + b - x
        if extra > 0:
            total += extra
            if b >= extra:
                arr[i] -= extra
            else:
                arr[i] = 0
    print(total)
boxes()

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
    else:
        continue
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
    else:
        continue
print(ans)

Submission status: Accepted

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

result = 0

if a[0] > k:
        result = a[0] -k
        a[0] = k

for i in range(n-1):
    if a[i] + a[i+1] > k:
        result += a[i] + a[i+1] - k #a[i] + a[i+1] -x = k
        a[i+1] = -a[i] + k

print(result)

Submission status: Accepted

Solution code:
def candy():
    n = []
    n = input().split()
    N = int(n[0])
    x = int(n[1])
    alist = [int(i) for i in input().split()]

    s = 0
    for i in range(0, N - 1):
        c = int(alist[i])
        d = int(alist[i + 1])
        if int( alist[i] + alist[i + 1] ) > x:
            if int( alist[i] ) >= x:
                alist[i] = x
                alist[i + 1] = 0
            else :
                alist[i + 1] = int(x - alist[i] )
            s += c - int(alist[i]) + d - int(alist[i + 1])
        """
        if int(alist[i]) > x:
            s = s + int(alist[i+1])
            alist[i+1] = 0

        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])
        """
    print(s)
candy()

Submission status: Accepted

Solution code:
def solve():
  ans = 0
  N, x = map(int, input().split())
  A = list(map(int, input().split()))
  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] = x-A[i-1]
  return ans
print(solve())

Submission status: Accepted

Solution code:
import collections

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

ans = 0
q = collections.deque()
if a[0] <= x:
    q.append(a[0])
else:
    ans += a[0]- x
    q.append(x)

for i in range(1,n):
    tmp = q[-1] + a[i] 
    if tmp <= x:
        q.append(a[i])
    else:
        ans += tmp-x
        q.append(x-q[-1])
    q.popleft()


print(ans)

Submission status: Accepted

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

t = 0
ans = 0
for i in a:
  s = max(0, t+i-x) # 一つ前の値の差分を含めて、xとの差分を取得し、比較
  t = i - s
  ans += s
print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

r1 = sum(a)
flow = 0

for i in range(1, N):
    t = (a[i] + a[i - 1]) - x
    if flow != 0:
        a[i-1] -= flow
        t -= flow
        flow = 0

    if t > 0:
        if t > a[i]:
            t -= a[i]
            a[i - 1] -= t
            t = a[i]

        flow = t

a[-1] -= flow

r2 = sum(a)

print(r1 - r2)

Submission status: Accepted

Solution code:
#coding: utf-8

N, X = (int(x) for x in input().split())
A = [int(x) for x in input().split()]

ret = 0
for i in range(N-1):
    if A[i] + A[i+1] > X:
        tmp = A[i] + A[i+1] - X
        ret += tmp
        A[i+1] -= min(A[i+1], tmp)


print(ret)

Submission status: Accepted

Solution code:
#coding: utf-8

N, X = (int(x) for x in input().split())
A = [int(x) for x in input().split()]

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

ret = 0
for i in range(N-1):
    tmp = min(diffs[i], A[i+1])
    ret += tmp
    diffs[i]   -= tmp
    if i+1 < len(diffs):
        diffs[i+1] -= min(tmp, diffs[i+1]) 
    ret += diffs[i]

print(max(0, ret))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from math import ceil,floor,factorial,gcd,sqrt,log2,cos,sin,tan,acos,asin,atan,degrees,radians,pi,inf
from itertools import accumulate,groupby,permutations,combinations,product,combinations_with_replacement
from collections import deque,defaultdict,Counter
from bisect import bisect_left,bisect_right
from operator import itemgetter
from heapq import heapify,heappop,heappush
from queue import Queue,LifoQueue,PriorityQueue
from copy import deepcopy
from time import time
import string
import sys
sys.setrecursionlimit(10 ** 7)
def input() : return sys.stdin.readline().strip()
def INT()   : return int(input())
def MAP()   : return map(int,input().split())
def LIST()  : return list(MAP())

n, x = MAP()
a = LIST()
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(0, x - a[i])
print(ans)

Submission status: Accepted

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

add_a=[]
for i in range(n-1):
    add_a.append(max(a[i]+a[i+1]-x,0))

ans=0
for i in range(n-1):
    ans+=min(add_a[i],a[i+1])
    if i!=n-2:
        add_a[i+1]=max(0,add_a[i+1]-min(add_a[i],a[i+1]))
    add_a[i]-=min(add_a[i],a[i+1])
ans+=add_a[0]

print(ans)

Submission status: Accepted

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

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

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

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

    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys, math
from collections import defaultdict
from itertools import product
sys.setrecursionlimit(500000)
MOD = 10**9+7

def input():
    return sys.stdin.readline()[:-1]

def mi():
    return map(int, input().split())

def ii():
    return int(input())

def i2(n):
    tmp = [list(mi()) for i in range(n)]
    return [list(i) for i in zip(*tmp)]

def main():
    N, x = mi()
    a = list(mi())

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

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


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0

if a[0] - x > 0:
    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 = list(map(int,input().split()))
A = list(map(int,input().split()))

ans=0

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

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

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    N, x, *A = map(int, read().split())

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

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

    print(min(ans1, ans2))
    return


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

print(sm)

Submission status: Accepted

Solution code:
N,x,*a = map(int, open(0).read().split())
temp = 0
ans = 0
for c in a:
    t1 = temp
    temp += c
    if temp > x:
        ans += temp - x
        temp = x - t1
    else:
        temp -= t1
print(ans)

Submission status: Accepted

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

for a in A:
  eat = max(0,pre+a-x)
  pre = a - eat
  ans+= eat

print(ans)

Submission status: Accepted

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

B = list(A)

ans1 = ans2 = 0
for i in range(N-1):
  if B[i]+B[i+1] > x:
    b = B[i]+B[i+1] - x
    ans1 += b
    B[i+1] -= b
    if B[i+1] < 0: B[i+1] = 0


B = list(A[::-1])
for i in range(N-1):
  if B[i]+B[i+1] > x:
    b = B[i]+B[i+1] - x
    ans2 += b
    B[i+1] -= b
    if B[i+1] < 0: B[i+1] = 0

print( min(ans1, ans2) )

Submission status: Accepted

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

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


def calculate(n, x, arr):
    result = 0
    for i in range(1, n):
        sSum = arr[i - 1] + arr[i]
        if sSum <= x:
            continue

        diff = sSum - x

        if diff <= arr[i]:
            arr[i] -= diff
            result += diff
        else:
            result += diff
            arr[i] = 0

    print(result)


calculate(N, X, ARR)

Submission status: Accepted

Solution code:
def solve():
    n, x = map(int, input().split())
    s = list(map(int, input().split()))
    cnt, tmp, prv = 0, 0, 0

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

    for y in s:
        if prv + y > x:
            tmp = prv + y - x
            cnt += tmp
            prv = y - tmp
        else:
            prv = y
    print(cnt)


solve()

Submission status: Accepted

Solution code:
def solve():
    n, x = map(int, input().split())
    s = list(map(int, input().split()))
    cnt, tmp, flg = 0, 0, False
    for i in range(n - 1):
        if i == 0 and s[i] > x:
            cnt += s[i] - x
            s[i] = x
        if s[i] + s[i + 1] > x:
            tmp = s[i] + s[i + 1] - x
            cnt += tmp
            s[i + 1] -= tmp
    print(cnt)



solve()

Submission status: Accepted

Solution code:
import sys
def LI():
    return list(map(int,sys.stdin.readline().rstrip().split()))  #空白あり

N,x = LI()
a = LI()

ans = 0

# 左から2番目以降貪欲に

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:
n,x = map(int, input().split())
al = list(map(int, input().split()))

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys
 
def eat(i,j,candies,counter):
    s=candies[i]+candies[j]
    if s>x:
        if candies[i]>=s-x:
            candies[i]-=s-x
        else:
            candies[j]-=s-x-candies[i]
            candies[i]=0
        counter+=s-x
    return counter
 
def main(n,x,candies):
    counter=0
    for i in range(1,n):
        counter=eat(i,i-1,candies,counter)
    return counter
 
n,x=map(int,sys.stdin.readline().strip().split())
candies=list(map(int,sys.stdin.readline().strip().split()))
print(main(n,x,candies))

Submission status: Accepted

Solution code:
import sys

def eat(i,candies,counter):
    s=candies[i]+candies[i-1]
    nc=max(0,s-x)
    counter+=nc
    candies[i]-=nc
    return counter

def main(n,x,candies):
    counter=0
    if candies[0]>x:
        counter=candies[0]-x
        candies[0]=x
    for i in range(1,n):
        counter=eat(i,candies,counter)
    return counter

n,x=map(int,sys.stdin.readline().strip().split())
candies=list(map(int,sys.stdin.readline().strip().split()))
print(main(n,x,candies))

Submission status: Accepted

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

ans = 0
p = 0

for ai in a:
  if p + ai > x:
    d = p + ai - x
    ans += d
    p = ai - d
  else:
    p=ai
    
print(ans)

Submission status: Accepted

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

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

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

print(ans)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

Solution code:
import sys

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline

in_n = lambda: int(readline())
in_nn = lambda: map(int, readline().split())
in_nl = lambda: list(map(int, readline().split()))
in_na = lambda: map(int, read().split())
in_s = lambda: readline().rstrip().decode('utf-8')


def main():
    N, X = in_nn()
    A = in_nl()

    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] - (X - A[i - 1])
            A[i] = X - A[i - 1]

    # print(A)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
import itertools
# import numpy as np
import time
import math
import heapq
from collections import defaultdict
sys.setrecursionlimit(10 ** 7)
 
INF = 10 ** 18
MOD = 10 ** 9 + 7
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

# map(int, input().split())

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

total = 0
goal = x * (N - 1)
for i in range(N):
    if i == 0 or i == N - 1:
        total += A[i]
    else:
        total += 2 * A[i]

if total <= goal:
    print(0)
    exit()

ans = 0
if A[0] > x:
    diff = A[0] - x
    ans += diff
    A[0] -= diff
# if A[N - 1] > x:
#     diff = A[N - 1] - x
#     ans += diff
#     A[N - 1] -= diff

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

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

Submission status: Accepted

Solution code:
n,x=map(int,input().split())
*a,=map(int,input().split())
b=[]
b.append(min(a[0],x))
for i in range(1,n):
    b.append(max(min(x-b[i-1], a[i]),0))

print(sum(a)-sum(b))

Submission status: Accepted

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

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

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

    # print(i)
    # print(a)
    # print(ans)

print(ans)

Submission status: Accepted

Solution code:
N,x = list(map(int,input().split()))
a =  list(map(int,input().split()))
counter = 0
for i in range(N-1):
    sum_a = a[i] + a[i+1] 
    if sum_a > x:
        if sum_a - x > a[i+1]:
            counter += sum_a -x
            a[i] = a[i] -(sum_a-x-a[i+1]) #この行はなくてもいい
            a[i+1] = 0
        else:
            counter += sum_a-x
            a[i+1] = a[i+1] - (sum_a -x)
print(counter)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n,x = map(int, input().split())
a = list(map(int, input().split()))
def sub(p,q):
    if p+q<=x:
        ans = (0, (p,q))
    elif (p+q-x)<=q:
        ans = (p+q-x, (p, x-p))
    else:
        ans = (p+q-x, (x, 0))
    return ans
ans = 0
a1,a2 = a[:2]
num, (a1,a2) = sub(a1,a2)
ans += num
for i in range(n-2):
    a1,a2 = a2, a[i+2]
    num, (a1,a2) = sub(a1,a2)
    ans += num
print(ans)

Submission status: Accepted

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

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

    print(ans)
if __name__ == '__main__':
    solve()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys

readline = sys.stdin.readline
MOD = 10 ** 9 + 7
INF = float('INF')
sys.setrecursionlimit(10 ** 5)


def main():
    n, x = map(int, readline().split())
    a = list(map(int, readline().split()))

    ans = 0

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys
import numpy as np
import random
from decimal import Decimal
import itertools
import re
import bisect
from collections import deque, Counter
from functools import lru_cache

sys.setrecursionlimit(10**9)
INF = 10**13
def LI(): return list(map(int, sys.stdin.buffer.readline().split()))
def I(): return int(sys.stdin.buffer.readline())
def LS(): return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()
def S(): return sys.stdin.buffer.readline().rstrip().decode('utf-8')
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
def MSRL(n): return [[int(j) for j in list(S())] for i in range(n)]
def SERIES(n): return np.fromstring(sys.stdin.buffer.read(), dtype=np.int32, sep=' ')
def GRID(h,w): return np.fromstring(sys.stdin.buffer.read(), dtype=np.int32, sep=' ').reshape(h,-1)[:,:w]
def GRIDfromString(h,w): return np.frombuffer(sys.stdin.buffer.read(), 'S1').reshape(h,-1)[:,:w]
MOD = 1000000007

def main():
	n, x = LI()
	a_list = LI()
	count = 0

	for i in range(n-1):
		a1, a2 = a_list[i], a_list[i+1]
		if a1 + a2 <= x:
			continue
		else:
			if a1 <= x:
				eat_candy = (a1+a2) - x
				a_list[i+1] -= eat_candy
				count += eat_candy
			else:
				a_list[i] = x
				a_list[i+1] = 0
				count += a1 + a2 - x

	print(count)


if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
def main():
    N, x = map(int, input().split())
    A = list(map(int, input().split()))
    ans = 0
    if A[0] > x:
        diff = A[0] - x
        A[0] = x
        ans += diff
    if A[-1] > x:
        diff = A[-1] - x
        A[-1] = x
        ans += diff
    B = [a for a in A]
    ans1 = 0
    for i in range(N-1):
        s = A[i] + A[i + 1]
        if s <= x:
            continue
        diff = s - x
        if diff > A[i + 1]:
            A[i] -= diff - A[i + 1]
            A[i + 1] = 0
        else:
            A[i + 1] -= diff
        ans1 += diff

    ans2 = 0
    for i in range(N - 1):
        index = N - 1 - i
        s = B[index] + B[index - 1]
        if s <= x:
            continue
        diff = s - x
        if diff > B[index - 1]:
            B[index] -= diff - B[index - 1]
            B[index - 1] = 0
        else:
            B[index - 1] -= diff
        ans2 += diff
    ans += min(ans1, ans2)

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math
#import numpy as np
import queue
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
ipt = stdin.readline
setrecursionlimit(10**7)

def main():
    n,x = map(int,ipt().split())
    a = [int(i) for i in ipt().split()]
    ans = 0
    if a[0]+a[1] > x:
        ans += a[0]+a[1]-x
        a[1] = max(0,x-a[0])
    for i in range(1,n-1):
        ai = a[i]
        ai1 = a[i+1]
        if ai+ai1 > x:
            ans += ai+ai1-x
            a[i+1] = x-ai
    print(ans)



    return

if __name__ == '__main__':
    main()

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# input()
# int(input())
# map(int, input().split())
# list(map(int, input().split()))
# list(map(int, list(input()))) # スペースがない数字リストを読み込み
import math
import fractions
import sys
import bisect
import heapq  # 優先度付きキュー(最小値取り出し)
import collections
from collections import Counter
from collections import deque
import pprint

sr = lambda: input()
ir = lambda: int(sr())
lr = lambda: list(map(int, sr().split()))

"""nを素因数分解"""
"""2以上の整数n => [[素因数, 指数], ...]の2次元リスト"""


def factorization(n):
    arr = []
    temp = n
    if n == 1:
        return arr

    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


# a^n
def power(a, n, mod):
    x = 1
    while n:
        if n & 1:
            x *= a % mod
        n >>= 1
        a *= a % mod
    return x % mod


# n*(n-1)*...*(l+1)*l
def kaijo(n, l, mod):
    if n == 0:
        return 1
    a = n
    tmp = n - 1
    while (tmp >= l):
        a = a * tmp % mod
        tmp -= 1
    return a


inf = 10 ** 18
mod = 10 ** 9 + 7

n,x = lr()
a = lr()
w = []
ans=0
for i in range(n-1):
    w.append((a[i]+a[i+1])-x)
for i in range(n-1):
    s,t = a[i],a[i+1]
    ret = 0
    if w[i] > 0:
        if t >= w[i]:
            ans+=w[i]
            ret=w[i]
            a[i+1]-=w[i]
        else:
            ans+=w[i]
            ret=t
            a[i+1]=0
    if i < n-2:
        w[i+1]-=ret
print(ans)

Submission status: Accepted

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

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

s = sum(As)

print(f-s)

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

Submission status: Accepted

Solution code:


def read_int():
    return int(input().strip())


def read_ints():
    return list(map(int, input().strip().split(' ')))


def solve():
    N, x = read_ints()
    A = read_ints()
    answer = 0
    for i in range(1, N):
        if A[i-1]+A[i] > x:
            eat = A[i]+A[i-1]-x
            if eat <= A[i]:
                A[i] -= eat
                answer += eat
            else:
                eat -= A[i]
                answer += A[i]
                A[i] = 0
                A[i-1] -= eat
                answer += eat
    return answer


if __name__ == '__main__':
    print(solve())