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

Submission status: Accepted

Solution code:
# C問題
n, x = map(int, input().split())
a = list(map(int, input().split()))
new_a = []
new_a += a
if a[0] > x:
    new_a[0] = x
for i in range(n-1):
    while True:
        if new_a[i] + new_a[i+1] > x:
            new_a[i+1] = x-new_a[i]
        if new_a[i] + new_a[i+1] <= x:
            break

print(sum(a) - sum(new_a))

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

ate_n = 0

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

Submission status: Accepted

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

Submission status: Accepted

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

ans = 0
d = 0
if A[0] > X:
    d = A[0] - X
    ans += d
for i in range(N-1):
    s = A[i]+A[i+1]-d
    if s > X:
        d = s - X
        ans += d
    else:
        d = 0
 
print(ans)

Submission status: Accepted

Solution code:
from collections import Counter, defaultdict
import sys
sys.setrecursionlimit(10 ** 5 + 10)
# input = sys.stdin.readline
from math import factorial



def main():
    num, max_num = map(int, input().split())
    data = list(map(int, input().split())) + [0]

    ans = 0
    for i in range(num):
        if data[i] + data[i + 1] > max_num:
            ans += min(data[i + 1], data[i] + data[i + 1] - max_num)
            data[i + 1] -= min(data[i + 1], data[i] + data[i + 1] - max_num)

            if data[i] + data[i + 1] > max_num:
                ans += data[i] - max_num

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
import sys
sys.setrecursionlimit(300000)


def solve(N: int, x: int, a: "List[int]"):
    ret = 0
    for i in range(1, N):
        if a[i] + a[i - 1] > x:
            hoge = (a[i] + a[i - 1]) - x
            ret += hoge
            tmp = min(hoge, a[i])
            a[i] -= tmp
    print(ret)
    return


def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    x = int(next(tokens))  # type: int
    a = [ int(next(tokens)) for _ in range(N) ]  # type: "List[int]"
    solve(N, x, a)

if __name__ == '__main__':
    main()

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

count = 0
for i in range(0, n, 2):
    if i != 0:
        if a[i - 1] + a[i] > x:
            temp = min(a[i - 1] + a[i] - x, a[i])
            a[i] -= temp
            count += temp
            if a[i - 1] + a[i] > x:
                temp = a[i - 1] + a[i] - x
                a[i - 1] -= temp
                count += temp
    if i == n - 1:
        pass
    else:
        if a[i] + a[i + 1] > x:
            temp = min(a[i] + a[i + 1] - x, a[i + 1])
            a[i + 1] -= temp
            count += temp
            if a[i] + a[i + 1] > x:
                temp = a[i] + a[i + 1] - x
                a[i] -= temp
                count += temp
print(count)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
from collections import defaultdict
from collections import deque
from heapq import heappush, heappop
import sys
import math
import bisect
import random
import itertools
sys.setrecursionlimit(10**5)
stdin = sys.stdin
bisect_left = bisect.bisect_left
bisect_right = bisect.bisect_right
def LI(): return list(map(int, stdin.readline().split()))
def LF(): return list(map(float, stdin.readline().split()))
def LI_(): return list(map(lambda x: int(x)-1, stdin.readline().split()))
def II(): return int(stdin.readline())
def IF(): return float(stdin.readline())
def LS(): return list(map(list, stdin.readline().split()))
def S(): return list(stdin.readline().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = float('INF')

#A
def A():
    a, b, h = IR(3)
    print((a+b)*h//2)
    return

#B
def B():
    d = defaultdict(int)
    d["a"] = deque(S())
    d["b"] = deque(S())
    d["c"] = deque(S())
    tern = "a"
    while d[tern]:
        tern =  d[tern].popleft()
    print(str.capitalize(tern))
    return

#C
def C():
    n, x = LI()
    a = LI()
    ans = 0
    for i in range(n-1):
        if a[i] + a[i + 1] > x:
            if x > a[i]:
                ans += a[i] + a[i + 1] - x
                a[i + 1] = x - a[i]
            else:
                ans += a[i] + a[i + 1] - x
                a[i + 1] = 0
    print(ans)
    return

#D
def D():
    return

#Solve
if __name__ == '__main__':
    C()

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import copy

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

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

ans = sum(abs(x - y) for x, y in zip(a, b))
print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/env python3
#ABC48 C

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

b = a.copy()
if a[0] > x:
    b[0] = x
for i in range(n-1):
    if b[i] + b[i+1] > x:
        b[i+1] -= (b[i]+b[i+1] - x)
        if b[i+1] < 0:
            b[i] -= abs(b[i+1])
            b[i+1] += abs(b[i+1])
ans = 0
for i in range(n):
    ans += abs(b[i] - a[i])
print(ans)

Submission status: Accepted

Solution code:

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

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


ans = 0
las = 0

for a in A:

    if las + a <= x:
        las = a

    else:

        las = x - las

        ans += a - las

print (ans)

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, x = map(int, raw_input().split())
arr = map(int, raw_input().split())
ops = 0
for i in range(n-1):
    sum2 = arr[i] + arr[i+1]
    if sum2 > x:
        ops += min(sum2 - x, arr[i+1])
        arr[i+1] -= min(sum2 - x, arr[i+1])
    if arr[i] > x:
        ops += min(arr[i]-x, arr[i])
        arr[i] -= min(arr[i]-x, arr[i])
print ops

Submission status: Accepted

Solution code:
def main():
    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
        res += a[i]+a[i+1]-x
        if x-a[i] >=0:
            a[i+1] = x-a[i]
        else:
            a[i+1] = 0
            a[i] = 2*a[i]-x
    print(res)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import sys
import numpy as np


def main():
    n, x = [int(e) for e in input().split()]
    boxes = [int(e) for e in input().split()]
    boxesrev = list(reversed(boxes))
    ans1 = 0
    ans1 += max(0, boxes[0] - x)
    boxes[0] = min(boxes[0], x)
    ans1 += max(0, boxes[n - 1] - x)
    boxes[n - 1] = min(boxes[n - 1], x)
    for i in range(1, n - 2):
        ans1 += max(0, (boxes[i] + boxes[i - 1]) - x)
        boxes[i] = min(boxes[i], x - boxes[i - 1])
    if n > 2:
        ans1 += max(0, (boxes[n - 2] + max(boxes[n - 3], boxes[n - 1])) - x)
        boxes[n - 2] = min(boxes[n - 2], x - max(boxes[n - 3], boxes[n - 1]))
    else:
        ans1 += max(0, boxes[n - 2] + boxes[n - 1] - x)
        boxes[n - 1] = min(boxes[n - 1], x - boxes[n - 2])
    print(ans1)


main()

Submission status: Accepted

Solution code:
N,x = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
cnt = 0
for ind in range(len(a)-1):
    if(ind==0):
        if(a[1]+a[0]>x):
            cnt += a[1]+a[0]-x
            a[1]-=min(a[1]+a[0]-x,a[1])
            a[0]-=max((a[1]+a[0])-x,0)
    if(a[ind+1]+a[ind]>x):
        cnt += a[ind+1]+a[ind]-x
        a[ind+1] -= a[ind+1]+a[ind]-x
print(cnt)

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

Submission status: Accepted

Solution code:
# ARC064C - Boxes and Candies (ABC048C)
def main():
    n, x = map(int, input().split())
    A = list(map(int, input().split()))

    # modify A[0] if >x
    ans = 0 if A[0] < x else A[0] - x
    A[0] = min(A[0], x)

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ARC064C - Boxes and Candies (ABC048C)
def main():
    n, x = list(map(int, input().rstrip().split()))
    A = list(map(int, input().rstrip().split()))

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


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ARC064C - Boxes and Candies (ABC048C)
def main():
    n, x = list(map(int, input().split()))
    A = list(map(int, input().split()))
    ans = A[0] - x if A[0] > x else 0
    A[0] = min(A[0], x)
    for i in range(n - 1):
        dif = A[i] + A[i + 1] - x
        if dif > 0:
            A[i + 1] -= dif
            ans += dif
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
# ABC048C - Boxes and Candies
def main():
    n, x = list(map(int, input().rstrip().split()))
    A = list(map(int, input().rstrip().split()))
    ans = 0
    if A[0] > x:
        ans += A[0] - x
        A[0] = x
    for i in range(n - 1):
        dif = A[i] + A[i + 1] - x
        if dif > 0:
            A[i + 1] -= dif
            ans += dif
    print(ans)


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import itertools
N,x = list(map(int,input().split(" ")))
As = list(map(int,input().split(" ")))
ans = 0
for i in range(0,N-1):
    l = As[i]
    r = As[i+1]
    diff = max((r+l) - x,0)
    if diff >= r:
        ans += r

        diff -= r
        As[i + 1] = r = 0
        ans += diff

    else:
        r -= diff
        As[i + 1] = r
        ans += 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-1):
    if a[i]+a[i+1]<=x:
        pass
    else:
        if (a[i+1]+a[i])-x > a[i+1]:
            cnt += (a[i+1]+a[i])-x
            a[i+1] = 0

        else:
            cnt += (a[i+1]+a[i])-x
            a[i+1] -= (a[i+1]+a[i])-x

print(cnt)

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

cnt = 0

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

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

Submission status: Accepted

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

cnt = 0

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

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

Submission status: Accepted

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

ans=0
for i in range(1,len(A)):
    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()))
sum_a = sum(a)

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

print(sum_a - sum(a))

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
*A, = map(int, input().split())
ans = max(A[0] - x, 0)
if A[0] > x: A[0] = x
for i in range(N-1):
    S = A[i] + A[i+1]
    if S > x:
        ans += S - x
        A[i+1] -= min(S - 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(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:
C=0;
p=input().rstrip().split(' ')
x=input().rstrip().split(' ')
for i in range(0,len(x)-1):
    t=int(x[i]) + int(x[i+1])
    if t>int(p[1]):
        g=t-int(p[1])
        C+=g;
        if g>int(x[i+1]):
            g=g-int(x[i+1])
            x[i+1]=0;
            x[i]=int(x[i])-g;
        else:
            x[i+1]=int(x[i+1])-g;
            
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(N-1):
    if a[i] + a[i+1] > x:
        if a[i+1] >= a[i] + a[i+1] - x:
            count += a[i] + a[i+1] - x
            a[i+1] -= a[i] + a[i+1] - x
        else:
            count += a[i] + a[i+1] - x
            a[i+1] = 0
            a[i] = x

print (count)

Submission status: Accepted

Solution code:
import sys
N, x = map(int, sys.stdin.readline().split())
a = list(map(int, sys.stdin.readline().split()))
minus_count = 0
for i in range(N-1):
    temp_sum = a[i] + a[i+1]
    minus_target = temp_sum - x
    if minus_target <= 0:
        continue
    else:
        minus_count += minus_target
        a[i+1] = a[i+1] - minus_target
        if a[i+1] < 0:
            a[i+1] = 0

print(minus_count)

Submission status: Accepted

Solution code:
# greedy

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

prev = 0
answer = 0
for a in A:
  eat = max(0, prev + a - x)
  prev = a - eat
  answer += eat

print(answer)

Submission status: Accepted

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

Submission status: Accepted

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

ans = max(A[0] - X, 0)
B = [min(A[0], X)]
for i in range(1, N):
  var = max(A[i] + B[-1] - X, 0)
  ans += var
  B.append(A[i] - var)
  
print(ans)
#print(B)

Submission status: Accepted

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

for i in range(1, n):
    total = a[i-1] + a[i]
    sa = total - x
    if sa > 0:
        result += sa
        a[i-1] = 0
        a[i] -= sa - a[i-1]
        if a[i] < 0:
            a[i] = 0

    else:
        a[i-1] = 0
print(result)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
"""貪欲法
"""
def solve():
    N, x = list(map(int, input().split()))
    A = list(map(int, input().split()))

    ans = 0

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

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

    for i in range(2, N):
        total = A[i] + A[i-1]

        if total > x:
            diff = total - x
            A[i] -= diff
            ans += diff
    
    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

sumA=[A[i]+A[i+1] for i in range(N-1)]

count=0
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
        if A[i+1]<0:
            A[i]+=A[i+1]
            A[i+1]=0
print(count)

Submission status: Accepted

Solution code:
def smaller(x, y):
	if x < y: return x
	return y

def larger(x, y):
	if x > y: return x
	return y

N, x = map(int, input().split())
A = list(map(int, input().split()))
sum1 = 0
for i in range(0, N):
	sum1 += A[i]
for i in range(1, N-1):
	leftadd = A[i-1] + A[i]
	rightadd = A[i]+ A [i+1]
	left = leftadd - x
	if left < 0: left = 0
	if A[i] < left: left = A[i]
	right = rightadd - x
	if right < 0: right = 0
	if A[i] < right: right = A[i]
	A[i] -= smaller(left, right)
for i in range(1, N-1):
	leftadd = A[i-1] + A[i]
	rightadd = A[i]+ A [i+1]
	left = leftadd - x
	if left < 0: left = 0
	if A[i] < left: left = A[i]
	right = rightadd - x
	if right < 0: right = 0
	if A[i] < right: right = A[i]
	A[i] -= larger(left, right)
if A[0]+A[1] > x:
	A[0] -= smaller(A[0], A[0]+A[1]-x)
	if A[0]+A[1] > x:
		A[1] -= A[0]+A[1]-x
if A[N-2]+A[N-1] > x:
	A[N-1] -= smaller(A[N-1], A[N-2]+A[N-1]-x)
	if A[N-2]+A[N-1] > x:
		A[N-2] -= A[N-2]+A[N-1]-x
sum2 = 0
for i in range(0, N):
	sum2 += A[i]
print(sum1-sum2)

Submission status: Accepted

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

cost = 0
num = 0

for i in range(N-1):
    cost = max(0, a[i] + a[i+1] - x)
    num += cost
    a[i+1] = max(0, a[i+1] - cost)

print(num)

Submission status: Accepted

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

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

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n,m=LI()

  l=LI()

  cnt=0

  for i in range(n-2):
    if l[i]+l[i+1]<=m:
      continue
    else:
      if l[i]>m:
        a=l[i+1]
        b=max(0,l[i]+l[i+1]-m)
        cnt+=b
        l[i+1]=0
      else:
        b=max(0,l[i]+l[i+1]-m)
        cnt+=b
        l[i+1]-=b

  cnt+=max(0,l[-1]+l[-2]-m)

  return cnt

print(main())

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n,x = list(map(int,input().split()))
a =  list(map(int,input().split()))
ret = 0
if a[0] > x:
    ret+=a[0]-x
    a[0]=x
for i in range(1,len(a)):
    if a[i-1]+a[i]>x:
        ret+=(a[i-1]+a[i]-x)
        a[i]=x-a[i-1]
print(ret)

Submission status: Accepted

Solution code:
# 隣り合う2個というが、頭からみていったときに前のやつは流用できないので、あとのやつを優先的に減らしたほうがいい
n, x = map(int, input().split())
arr = list(map(int, input().split()))

cost = 0
for i in range(1, n):
    if arr[i] + arr[i - 1] > x:
        abure = arr[i] + arr[i-1] - x
        if arr[i] >= abure:
            arr[i] -= abure
        else:
          # 足りないぶんはひとつ前からも減らさねばならない
            arr[i - 1] -= abure - arr[i]
            arr[i] = 0
        cost += abure
print(cost)

Submission status: Accepted

Solution code:

n, x = [ int(v) for v in input().split() ]
candy_list =  [ int(v) for v in input().split() ]


def eating(c):
	global x
	a, b = candy_list[i], candy_list[i+1]
	y = a + b - x
	if y >= 0:
		b -= y
		a += min(0,b)
		b = max(0,b)
		candy_list[i], candy_list[i+1] = a, b
	else:
		y = 0
	return y

ans = 0
for i in range(n-1):
	ans += eating(i)

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

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

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

print(ans)

Submission status: Accepted

Solution code:
#!/usr/bin/python3
# -*- coding: utf-8 -*-


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

print(ans)

Submission status: Accepted

Solution code:
n,x=map(int,input().split())
a=[int(i) for i in input().split()]
ans=0
i=0
if a[0]>x:
    ans+=a[0]-x
    a[0]-=ans
#b=[i[0]+i[1] for i in zip(a,a[1:])]+[x]
while i<len(a)-1:
    d=a[i]+a[i+1]-x
    if d>0:
        a[i+1]-=d
        ans+=d
        #print(i,d)
        
    i+=1
    
print(ans)

Submission status: Accepted

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

N,x = map(int,input().split())
a = [int(i) for i in input().split()]
part_sum = [0 for i in range(N)]

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


print(ans)

"""
1 6 1 2 0 4
1 0 1 0 0 1
0 1 0 1 0 1


"""

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

Submission status: Accepted

Solution code:
N, x, *a = map(int, open(0).read().split())
ans = 0

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

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

Submission status: Accepted

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

c = A[0] - x
if c > 0:
    cost += c
    A[0] -= c

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

print(cost)

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import sys, os

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

def solve():
    n,x = f()
    a = f()
    sum1 = sum(a)

    a = [ min(x, i) for i in a ]
    ans = sum1 - sum(a)

    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]
    print(ans)

solve()

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())
alis = list(map(int,input().split()))

ans = 0
for num in range(N-1):
	if alis[num] + alis[num+1] <= x: continue;
	
	if alis[num] > x:
		ans += alis[num] - x
		alis[num] = x
	if alis[num] + alis[num+1] > x:
		ans += alis[num]+alis[num+1] - x
		alis[num+1] -= alis[num]+alis[num+1] - x
print(ans)