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: Wrong Answer

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] = x - a[i]

print(ans)

Submission status: Wrong Answer

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] = x-A[i]
print(ans)

Submission status: Wrong Answer

Solution code:
n, x =map(int, input().split())
a = list(map(int, input().split()))
s = [0]*(n-1)
ans=0
for i in range(1, n-1):
  temp = a[i]
  left = a[i-1]+temp
  right = a[i+1]+temp
  if left>x and right>x:
    t=min(right, left)-x
    ans+=t
    a[i]-=t
  #print(a)
#print(" ")
for i in range(n-1):
  temp = a[i]+a[i+1]
  if temp>x:
    t = temp-x
    ans+=t
    a[i]-=t
  #print(a)
print(ans)

Submission status: Wrong Answer

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

ans = 0

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

print(ans)

Submission status: Wrong Answer

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

ans = 0

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

print(ans)

Submission status: Wrong Answer

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

print(ans)

Submission status: Wrong Answer

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

print(ans)

Submission status: Wrong Answer

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

b = [0]*(N-1)
for i in range(N-1):
    b[i] = a[i] + a[i+1]

ans = 0
for i in range(N-1):
    if b[i] > x:
        c = b[i] - x
        if i<N-2: b[i+1] -= c
        ans += c

print(ans)

Submission status: Wrong Answer

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

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

print(cnt)

Submission status: Wrong Answer

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
        if A[i] > A[i + 1]:
            d = A[i] + A[i + 1] - x
            if A[i] - d < 0:
                A[i + 1] -= d - A[i]
                A[i] = 0
            else:
                A[i] -= d

        else:
            d = A[i] + A[i + 1] - x
            if A[i + 1] - d < 0:
                A[i] -= d - A[i + 1]
                A[i + 1] = 0
            else:
                A[i + 1] -= d

print(ans)

Submission status: Wrong Answer

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
        if A[i] > A[i + 1]:
            d = A[i] + A[i + 1] - x
            if A[i] - d < 0:
                A[i + 1] -= d - A[i]
                A[i] = 0
            else:
                A[i] -= d

        else:
            d = A[i] + A[i + 1] - x
            if A[i + 1] - d < 0:
                A[i] -= d - A[i + 1]
                A[i + 1] = 0
                print('h')
            else:
                A[i + 1] -= d

print(ans)

Submission status: Wrong Answer

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
        if A[i] > A[i + 1]:
            d = A[i] + A[i + 1] - x
            if d < 0:
                A[i + 1] -= x - A[i]
                A[i] = 0
            else:
                A[i] -= d

        else:
            d = A[i] + A[i + 1] - x
            if d < 0:
                A[i] -= x - A[i + 1]
                A[i + 1] = 0
            else:
                A[i + 1] -= d

print(ans)

Submission status: Wrong Answer

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
        if A[i] >= A[i + 1]:
            d = A[i] + A[i + 1] - x
            if d < 0:
                A[i] = 0
                A[i + 1] -= x - A[i]
            else:
                A[i] -= d

        else:
            d = A[i] + A[i + 1] - x
            if d < 0:
                A[i + 1] = 0
                A[i] -= x - A[i + 1]
            else:
                A[i + 1] -= d

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

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

ans=0
for i in range(n-1):
    next_sum=A[i]+A[i+1]
    if next_sum>x:
        if next_sum-x<=A[i+1]:
            A[i+1]=A[i+1]-(next_sum-x)
            ans=ans+(next_sum-x)
        else:
            ans=ans+A[i+1]
            A[i+1]=0
            if next_sum-x-A[i+1]<=A[i]:
                A[i]=A[i]-(next_sum-x-A[i+1])
                ans=ans+(next_sum-x-A[i+1])
            else:
                print(0)
                sys.exit()
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

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

ans=0
for i in range(n-1):
    next_sum=A[i]+A[i+1]
    if next_sum>x:
        if next_sum-x<=A[i+1]:
            A[i+1]=A[i+1]-(next_sum-x)
            ans=ans+(next_sum-x)
        else:
            A[i+1]=0
            ans=ans+A[i+1]
            if next_sum-x-A[i+1]<=A[i]:
                A[i]=A[i]-(next_sum-x-A[i+1])
                ans=ans+(next_sum-x-A[i+1])
            else:
                print(0)
                sys.exit()
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline

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

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.buffer.readline

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

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
input = sys.stdin.readline
n, x = map(int,input().split())
a = [int(i) for i in input().split()]
ans = 0
now = 0
for i in range(n-1):
  m = a[i]+a[i+1]-now
  if m <= x:
    now = 0
  else:
    now = m - x
    ans += now
    
print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    n, x = map(int, input().split())
    a = [int(x) for x in input().split()]
    target = [[x // 2, 0]]
    target[0][1] = x - target[0][0]
    target.append(list(reversed(target[-1])))
    answer = [0, 0]
    for i in range(n):
        for j in range(2):
            answer[j] += max(0, a[i] - target[j][i % 2])
    print(min(answer))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
N,x = map(int,input().split())
a = list(map(int,input().split()))
ans = sum(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: Wrong Answer

Solution code:
def main():
    n, x = map(int, input().split())
    a = [int(x) for x in input().split()]
    for i in range(1, n):
        a[i] += a[i - 1]
    answer = 0
    before_diff = 0
    a = [0] + a
    for i in range(2, n + 1):
        diff = a[i] - a[i - 2] - before_diff
        if diff > x:
            answer += diff - x
            before_diff = diff - x
        else:
            before_diff = 0
    print(answer)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys,queue,math,copy,itertools
from fractions import gcd
sys.setrecursionlimit(10**7)
INF = 10**18
MOD = 10**9 + 7
LI = lambda : [int(x) for x in sys.stdin.readline().split()]
_LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
NI = lambda : int(sys.stdin.readline())
N,X = LI()
A = LI()
ans = 0
for i in range(1, N):
    if A[i-1] + A[i] > X:
        sa = (A[i-1]+A[i]) - X
        A[i] -= sa
        ans += sa
print (ans)

Submission status: Wrong Answer

Solution code:

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

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

#print(a)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)

# 反転 (ライツアウト)
# 2回やったら元に戻る

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

ans = 0

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
sys.setrecursionlimit(10 ** 7)

# 反転 (ライツアウト)
# 2回やったら元に戻る

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys
import math
import os
import itertools
import _collections
import string
from functools import lru_cache
import heapq

class cin():
    def int():
        return int(sys.stdin.readline().rstrip())
    def string():
        return sys.stdin.readline().rstrip()
    def mapInt():
        return [int(x) for x in cin.string().split()]
    def stringList(n):
        return [input() for i in range(n)]
    def intListList(n):
        return [cin.mapInt() for i in range(n)]
    def intColsList(n):
        return [int(input()) for i in range(n)]
        
class Math():
    def gcd(a,b):
        if b == 0:
            return a
        return Math.gcd(b,a % b)
    def lcm(a,b):
        return (a * b) // Math.gcd(a,b)
    def roundUp(a,b):
        return -(-a // b)
    def toUpperMultiple(a,x):
        return Math.roundUp(a,x) * x
    def toLowerMultiple(a,x):
        return (a // x) * x
    def nearPow2(n):
        if n <= 0:
            return 0
        if n & (n - 1) == 0:
            return n
        ret = 1
        while(n > 0):
            ret <<= 1
            n >>= 1
        return ret
    def isPrime(n):
        if n < 2:
            return False
        if n == 2:
            return True
        if n % 2 == 0:
            return False
        d = int(n ** 0.5) + 1
        for i in range(3,d + 1,2):
            if n % i == 0:
                return False
        return True


MOD = int(1e09) + 7

def main():
    N,X = cin.mapInt()
    A = cin.mapInt()
    ans = sum(A)
    for i in range(N - 1):
        if A[i + 1] + A[i] <= X:
            continue
        s = A[i + 1] + A[i] - X
        n = max(s,A[i + 1])
        A[i + 1]-=n
        s -= n
        A[i] -= s
    ans = ans - sum(A)
    print(ans)
    return

if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans=0
if s[0]>x:
    print(s[0])
    ans=s[0]-x
    s[0]=x
for i in range(len(s)-1):
    if s[i]+s[i+1]>x:
        ans+=s[i]+s[i+1]-x
        print(ans)
        s[i+1]=x-s[i]
print(ans)

Submission status: Wrong Answer

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

for i in range(len(s)-1):
    while s[i]+s[i+1]>x:
        s[i+1]-=1
        ans+=1

        
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans = 0
c = sum(A)
for i,a in enumerate(A) :
    if i == 0 :
        if A[i] <= x :
            continue
        else :
            A[i] -= x
    else :
        if A[i-1] + A[i] <= x :
            continue
        else :
            A[i] -= (A[i-1]+A[i]-x)

ans = c - sum(A)

print(ans)

Submission status: Wrong Answer

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

ans = 0
c = sum(A)
for i,a in enumerate(A) :
    if i == 0 :
        if A[i] < x :
            continue
        else :
            A[i] -= x
    else :
        if A[i-1] + A[i] < x :
            continue
        else :
            A[i] -= (A[i-1]+A[i]-x)

ans = c - sum(A)
#print("(i,A[i])=({},{})".format(i,tmp))
print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans = 0
sb0 = a[0] + a[1]-x
a[1] -= min(a[1],sb0)
ans += sb0
for i in range(1,n-1):
  sb = max(0,a[i]+a[i+1]-x)
  a[i+1] -= sb
  ans += sb

print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

print(count)

Submission status: Wrong Answer

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

print(count)

Submission status: Wrong Answer

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

s = [0] * (N-1)
for i in range(N-1):
    s[i] = a[i] + a[i+1]

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

print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,x = [int(x) for x in input().split()]
a = [int(x) for x in input().split()]
if(N==2):
    print(sum(a)-x)
else:
    cnt = 0
    for i in range(len(a)-1):
        if(a[i]+a[i+1]>x):
            over = a[i]+a[i+1]-x
            cnt += over
            a[i+1] -= over
    print(cnt)

Submission status: Wrong Answer

Solution code:
a = input().split()
N = int(a[0])
x = int(a[1])
numstr = input().split()
nums = [int(num) for num in numstr]

def main():
    arr2 = []
    for i in range(N-1):
        arr2.append(nums[i] + nums[i+1])
    a=0
    for j in range(len(arr2)-1):
        c = arr2[j]
        if c > x:
            a += c - x
            arr2[j+1] -= c - x
    return a


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

Submission status: Wrong Answer

Solution code:
a = input().split()
N = int(a[0])
x = int(a[1])
numstr = input().split()
nums = [int(num) for num in numstr]
print(nums)

def main():
    arr2 = []
    for i in range(N-1):
        arr2.append(nums[i] + nums[i+1])
    a=0
    for j in range(len(arr2)-1):
        c = arr2[j]
        if c > x:
            a += c - x
            arr2[j+1] -= c - x
    return a


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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

bef = a[0]
cnt = 0

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

Submission status: Wrong Answer

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

bef = a[0]
cnt = 0

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

Submission status: Wrong Answer

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

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

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

print(ans)

Submission status: Wrong Answer

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

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
N,x=map(int,input().split(' '))
A=list(map(int,input().split(' ')))
B=[a for a in A]
if N==2:
	print(A[0]+A[1]-x)
else:
	for i in range(N-3):
		B[i+1] -= min(B[i+1], max(0,(B[i]+B[i+1]-x)))
		B[i] -= max(B[i]-x, 0)
	B[-2] -= min(B[-2], max(0, (B[-3]+B[-2]-x), (B[-1]+B[-2]-x)))
	B[-3] -= max(0, (B[-3]-x))
	B[-1] -= max(0, (B[-1]-x))
	print(sum([A[i]-B[i] for i in range(N)]))

Submission status: Wrong Answer

Solution code:
N,x=map(int,input().split(' '))
A=list(map(int,input().split(' ')))
B=[a for a in A]
if N==2:
	print(A[0]+A[1]-x)
else:
	for i in range(N-3):
		B[i+1] -= min(B[i+1], max(0,(B[i]+B[i+1]-x)))
		B[i] -= max(B[i]-x, 0)
	B[-2] -= min(B[-2], max(0, (B[-3]+B[-2]-x), (B[-1]+B[-2]-x)))
	B[-3] -= max(0, (B[-3]-x))
	B[-1] -= max(0, (B[-1]-x))
	print(sum(A)-sum(B))

Submission status: Wrong Answer

Solution code:
N,x=map(int,input().split(' '))
A=list(map(int,input().split(' ')))
B=[a for a in A]
if N==2:
	print(A[0]+A[1]-x)
else:
	if N>3:
		for i in range(N-3):
			B[i+1] -= min(B[i+1], max(0,(B[i]+B[i+1]-x)))
			B[i] -= max(B[i]-x, 0)
	B[-2] -= min(B[-2], max(0, (B[-3]+B[-2]-x), (B[-1]+B[-2]-x)))
	B[-3] -= max(0, (B[-3]-x))
	B[-1] -= max(0, (B[-1]-x))
	print(sum(A)-sum(B))

Submission status: Wrong Answer

Solution code:
N,x=map(int,input().split(' '))
A=list(map(int,input().split(' ')))
B=[ A[i]+A[i+1] for i in range(N-1) ]
D=[]

if N==2:
	D.append(B[0]-x)
elif N==3:
	D.append(max(B[0]-x, B[1]-x))
else:
	for i in range(N-2): #operation c1,...,cN-2 = i=0,1,...,N-4,N-3
		if i==0:
			D.append(max(B[i]-x, 0))
		elif 0<i<N-3:
			D.append(max(B[i]-D[i-1]-x, 0))
		elif i==N-3:
			D.append(max(B[i]-D[i-1]-x, B[i+1]-x, 0))
print(sum(D))

Submission status: Wrong Answer

Solution code:
n,x=map(int,input().split())
A=list(map(int,input().split()))
cnt=0
for i in range(1,len(A),2):
  if A[i]+A[i-1]>x:
    diff=A[i]+A[i-1]-x
    if A[i]-diff>=0:
      A[i]-=diff
    else:
      dif2=abs(diff-A[i])
      A[i]=0
      A[i-1]-=dif2
    cnt+=diff

# print(A)

if A[-1]+A[-2]>x:
  diff=A[-1]+A[-2]-x
  if A[-1]-diff>=0:
    A[-1]-=diff
  else:
    dif2=abs(diff-A[-1])
    A[-1]=0
    A[-2]-=dif2
  cnt+=diff
# print(A)
for i in range(0,len(A)-1,2):
  if A[i]+A[i+1]>x:
    diff=A[i]+A[i+1]-x
    if A[i]-diff>=0:
      A[i]-=diff
    else:
      dif2=abs(diff-A[i])
      A[i]=0
      A[i+1]-=dif2
    cnt+=diff
# print(A)
print(cnt)

Submission status: Wrong Answer

Solution code:
n,x=map(int,input().split())
A=list(map(int,input().split()))
cnt=0
for i in range(1,len(A),2):
  if A[i]+A[i-1]>x:
    diff=A[i]+A[i-1]-x
    if A[i]-diff>=0:
      A[i]-=diff
    else:
      dif2=abs(diff-A[i])
      A[i]=0
      A[i-1]-=dif2
    cnt+=diff

# print(A)

if A[-1]+A[-2]>x:
  diff=A[-1]+A[-2]-x
  if A[-1]-diff>=0:
    A[-1]-=diff
  else:
    dif2=abs(diff-A[-1])
    A[-1]=0
    A[-2]-=dif2
  cnt+=diff
# print(A)
# for i in range(0,len(A)-1,2):
#   if A[i]+A[i+1]>x:
#     diff=A[i]+A[i+1]-x
#     if A[i]-diff>=0:
#       A[i]-=diff
#     else:
#       dif2=abs(diff-A[i])
#       A[i]=0
#       A[i+1]-=dif2
#     cnt+=diff
# print(A)
print(cnt)

Submission status: Wrong Answer

Solution code:
n,x=map(int,input().split())
A=list(map(int,input().split()))
cnt=0
for i in range(1,len(A),2):
  if A[i]+A[i-1]>x:
    diff=A[i]+A[i-1]-x
    if A[i]-diff>=0:
      A[i]-=diff
    else:
      dif2=abs(x-A[i])
      A[i]=0
      A[i-1]-=dif2
    cnt+=diff
# print(A)
# print(cnt)

if A[-1]+A[-2]>x:
  cnt+=A[-1]+A[-2]-x

# print(A)
print(cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

b = []
for i in range(N-1):
    b.append(a[i]+a[i+1])

count = 0
for left in range(N-2):
    right = left + 1
    diff = b[left] - x
    if diff > 0:
        count += diff
        b[left] -= diff
        b[right] -= diff
diff = b[N-2] - x
if diff > 0:
    count += diff
print(count)