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:
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
import math
import queue
#import numpy as np
Mod = 1000000007




def main(): #startline--------------------------------------------
    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
        else:
            pass
    print(ans) 
if __name__ == "__main__":
    main() #endline===============================================





def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n < 2 is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))):
        if prime[i] == 1:
            for j in range(2*i,n):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n):
        if prime[i] == 1:
            res.append(i)
    return res

class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py

    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

D = [0]*N
answer = 0
for i in range(N-1):
    if A[i] + A[i+1] > x:
        d = A[i] + A[i+1] - x
        e = max(A[i+1], d)
        answer += e
        A[i+1] -= e
        d -= e
        if d > 0:
            A[i] -= d
            answer += d
print(answer) 
# x=3, A= 2 2 2
#      S= 4 4   8
#      D= 1 1
# answer=1

# x=1, A= 1 6 1 2 0 4
#      S= 7 7 3 2 4  = 23
#      ひとつたべると、2減りそうだが
#     D0= 6 6 3 1 3 0
#          (6)
#         0 0 3 1 3 0 = -6*2
#              (2)
#         0 0 1 0 3 0 = 


# answer=11
#         12+6=18

Submission status: Wrong Answer

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

D = [0]*N
for i in range(N-1):
    D[i] = max(A[i] + A[i+1] - x, 0)

answer = 0
for i in range(N-1):
    if D[i] > 0:
        if D[i+1] > 0:
            # A[i+1]
            e = max(D[i], A[i+1])
            D[i+1] -= e
            D[i] -= e
            A[i+1] -= e
            answer += e
        e = D[i]
        D[i] -= e
        answer += e
        A[i] -= e
print(answer) 
# x=3, A= 2 2 2
#      S= 4 4   8
#      D= 1 1
# answer=1

# x=1, A= 1 6 1 2 0 4
#      S= 7 7 3 2 4  = 23
#      ひとつたべると、2減りそうだが
#     D0= 6 6 3 1 3 0
#          (6)
#         0 0 3 1 3 0 = -6*2
#              (2)
#         0 0 1 0 3 0 = 


# answer=11
#         12+6=18

Submission status: Wrong Answer

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

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):
    while True:
        if a[i] + a[i-1] > x:
            if a[i] != 0:
                t = min((a[i] + a[i-1] - x), a[i])
                a[i] -= t
                cnt += t
            else:
                t = min((a[i] + a[i-1] - x), a[i-1])
                a[i] -= t
                cnt += t
        else:
            break
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, n):
    if a[i] + a[i-1] > x:
        t = (a[i] + a[i-1] - x)
        a[i] -= t
        cnt += t
    else:
        break
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, n):
    if a[i] + a[i-1] > x:
        a[i] -= (a[i] + a[i-1] - x)
        cnt += (a[i] + a[i-1] - x)
    else:
        break
print(cnt)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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


print(count+max(b[-1],0))

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):
    left=a[i]
    right=a[i+1]
    if left+right<=x:
        continue
    eat=(left+right)-x
    ans+=eat
    a[i+1]=min(0, right-eat)
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:

import collections
import itertools
import sys
import time

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

n,x=getints()
vs=getints()

res = 0
if vs[0] > x:
    eat = vs[0] - x
    res += eat
    vs[0] -= eat

for i in range(1,n):
    exceed = vs[i-1] + vs[i] - x
    eat = min(vs[i], exceed)
    vs[i] -= eat
    res += eat
    exceed = vs[i-1] + vs[i] - x
    eat = min(vs[i-1], exceed)
    res += eat

print (res)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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)
rev = sorted(a, reverse=True)
prev = rev[0]
count1 = 0
for i in range(n - 1):
    now = rev[i + 1]
    if now + prev > x:
        count1 += now + prev - x
        prev = max(0, x - prev)
print(min(count, count1))

Submission status: Wrong Answer

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)
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):
  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:
      print(i)
      continue
    else:
      ans += (A[i]+A[i-1]-x)
      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
d = 0
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: Wrong Answer

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:
            ret += (a[i] + a[i - 1]) - x
            tmp = min(ret, 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: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

N, X = map(int, input().split())
A = list(map(int, input().split()))
cnt1 = 0
for p in range(0, 2):
	for i in range(1, N-1):
		left = larger(0, smaller(A[i-1]+A[i]-X, A[i]))
		right = larger(0, smaller(A[i]+A[i+1]-X, A[i]))
		if left != 0 and right != 0:
			cnt1 += smaller(left, right)
			A[i] -= smaller(left, right)
		elif left == 0:
			cnt1 += right
			A[i] -= right
		else:
			cnt1 += left
			A[i] -= left
right = larger(0, smaller(A[0]+A[1]-X, A[0]))
cnt1 += right
A[0] -= right
left = larger(0, smaller(A[-1]+A[-2]-X, A[-1]))
cnt1 += left
A[0] -= left
print(cnt1)

Submission status: Wrong Answer

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,2):
    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: Wrong Answer

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

print(c)

Submission status: Wrong Answer

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,2):
    if a[i]+a[i-1]>x:
        c+=min(a[i],a[i]+a[i-1]-x)
        a[i]-=min(a[i],a[i]+a[i-1]-x)
        if a[i]==0:
            c+=a[i-1]-x
            a[i-1]-=(a[i-1]-x)

print(c)

Submission status: Wrong Answer

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

print(c)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

    for i in range(n):
        if a[i]>x:
            ans += a[i] - x
            a[i] = x
    sums = [a[i]+a[i+1] for i in range(n-1)]
    for i in range(n-1):
        if flag:
            if sums[i]>x:
                ans += sums[i]-x-1
            flag = False
        else:
            if sums[i]>x:
                ans += sums[i]-x
                flag = True
    print(ans)

if __name__=="__main__": main()

Submission status: Wrong Answer

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

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


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

Solution code:
# ARC064C - Boxes and Candies (ABC048C)
def main():
    n, x = list(map(int, input().rstrip().split()))
    A = list(map(int, input().rstrip().split()))
    ans = 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: Wrong Answer

Solution code:
# ABC048C - Boxes and Candies
def main():
    n, x = list(map(int, input().rstrip().split()))
    A = list(map(int, input().rstrip().split()))
    B = A
    cnt_a, cnt_b = 0, 0
    for i in range(n - 1):
        if i % 2 == 0:
            dif = min(0, x - (A[i] + A[i + 1]))
            A[i] -= dif
            cnt_a += abs(dif)
        else:
            dif = min(0, x - (B[i] + B[i - 1]))
            B[i] -= dif
            cnt_b += abs(dif)
    print(min(cnt_a, cnt_b))


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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 = (r+l) - x
    if diff >= r:
        ans += r

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

    else:
        r -= diff
        As[i + 1] = r
        ans += diff

print(max(ans,0))

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

Submission status: Wrong Answer

Solution code:
N,X = map(int,input().split())
a = list(map(int,input().split()))
minOdd = min(a[0::2])
minEve = min(a[1::2])
g = 0
if X - (minOdd+minEve) >= 0:
    l = X - (minOdd+minEve)
    g1 = 0
    for i,c in enumerate(a):
        if i%2==0:g1 += max(0,c-minOdd-l)
        else:g1 += max(0,c-minEve)
    g2 = 0
    for i,c in enumerate(a):
        if i%2==0:g2 += max(0,c-minOdd)
        else:g2 += max(0,c-minEve-l)
    g = min(g1,g2)
else:
    l = (minOdd+minEve) - X
    l2 = min(minOdd,l)
    r2 = (minOdd+minEve) - X - l2
    l3 = min(minEve,l)
    r3 = (minOdd+minEve) - X - l3
    g1 = 0
    for i,c in enumerate(a):
        if i%2==0:g1 += max(0,c-minOdd+l2)
        else:g1 += max(0,c-minEve+r2)
    g2 = 0
    for i,c in enumerate(a):
        if i%2==0:g2 += max(0,c-minOdd+r3)
        else:g2 += max(0,c-minEve+l3)
    g = min(g1,g2)
print(g)

Submission status: Wrong Answer

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
        t = min(S, A[i+1])
        A[i+1] -= t
print(ans)

Submission status: Wrong Answer

Solution code:
N, x = map(int, input().split())
*A, = map(int, input().split())
ans1 = 0
T = [a for a in A]
for i in range(N-1):
    S = T[i] + T[i+1]
    if S > x:
        ans1 += S - x
        t = min(S, T[i+1])
        T[i+1] -= t
ans2 = 0
T = [a for a in A]
for i in reversed(range(N-1)):
    S = T[i] + T[i+1]
    if S > x:
        ans2 += S - x
        t = min(S, T[i])
        T[i] -= t
print(min(ans1, ans2))

Submission status: Wrong Answer

Solution code:
N, x = map(int, input().split())
*A, = map(int, input().split())
ans1 = 0
for i in range(N-1):
    S = A[i] + A[i+1]
    if S > x:
        ans1 += S - x
        t = min(S, A[i+1])
        A[i+1] -= t
ans2 = 0
for i in reversed(range(N-1)):
    S = A[i] + A[i+1]
    if S > x:
        ans2 += S - x
        t = min(S, A[i])
        A[i] -= t
print(min(ans1, ans2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

print ((total+1)//2)

Submission status: Wrong Answer

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]))
    ans2 = 0
    ans2 += max(0, boxesrev[0] - x)
    boxesrev[0] = min(boxesrev[0], x)
    ans2 += max(0, boxesrev[n - 1] - x)
    boxesrev[n - 1] = min(boxesrev[n - 1], x)
    for i in range(1, n - 2):
        ans2 += max(0, (boxesrev[i] + boxesrev[i - 1]) - x)
        boxesrev[i] = min(boxesrev[i], x - boxesrev[i - 1])
    if n > 2:
        ans2 += max(0, (boxesrev[n - 2] + max(boxesrev[n - 3], boxesrev[n - 1])) - x)
        boxesrev[n - 2] = min(boxesrev[n - 2], x - max(boxesrev[n - 3], boxesrev[n - 1]))
    print(min(ans1, ans2))


main()

Submission status: Wrong Answer

Solution code:
import sys
import copy
import numpy as np


def main():
    n, x = [int(x) for x in input().split()]
    boxes = [int(x) for x in input().split()]
    ans = 0
    ans += max(0, boxes[0] - x)
    boxes[0] = min(boxes[0], x)
    ans += max(0, boxes[n - 1] - x)
    boxes[n - 1] = min(boxes[n - 1], x)
    for i in range(1, n - 2):
        ans += max(0, (boxes[i] + boxes[i - 1]) - x)
        boxes[i] = min(boxes[i], x - boxes[i - 1])
    if n > 2:
        ans += 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]))
    print(ans)


main()

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

print(cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, X = map(int, input().split())
a = list(map(int, input().split()))
sum = 0
if (N % 2) == 1:
    if a[0] > a[-1]:
        for i in range(0, N, 2):
            if i == (N - 1):
                if a[i] > X:
                    sum += (a[i] - X)
            elif (a[i] + a[i + 1]) > X:
                sum += (a[i] + a[i + 1] - X)
    else:
        for i in range(N-1, 0, -2):
            if i == 0:
                if a[i] > X:
                    sum += (a[i] - X)
            elif (a[i] + a[i - 1]) > X:
                sum += (a[i] + a[i - 1] - X)
else:
    for i in range(0, N, 2):
        if (a[i]+a[i+1]) > X:
            sum += (a[i]+a[i+1]-X)
print(sum)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

for i in range(n):
    a_sum += int(a[i])

tolerance = -(-(n * x) // 2)
result = a_sum - tolerance

if int(a[0]) > x:
    result += 1
if int(a[n-1]) > x:
    result += 1
if int(a[0]) > x and int(a[n-1]) > x:
    result -= 2

if tolerance == a_sum:
    result += 1
if result < 0:
    print("0")
else:
    print(result)

# 左右の和がx以下になるように
# 何個キャンディを食べればよいか
# n*1/2 と a の総和の差が答え？
# 9/2= 4.5 '=.5 - 6 = -1
# 6*1/2 = 3 - 14 = -11
# 5*9/2 = 22.5 '=.23 - 14 = 9

Submission status: Wrong Answer

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

for i in range(n):
    a_sum += int(a[i])

tolerance = -(-(n * x) // 2)
result = a_sum - tolerance

if tolerance == a_sum:
    result +=1
if result < 0:
    print("0")
else:
    print(result)

Submission status: Wrong Answer

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

for i in range(n):
    a_sum += int(a[i])

tolerance = -(-(n * x) // 2)
result = a_sum - tolerance
if int(a[0]) > x or int(a[n-1]):
    result += 1
if int(a[0]) > x and int(a[n-1]) > x:
    result -= 1
if result < 0:
    print("0")
else:
    print(result)

Submission status: Wrong Answer

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

for i in range(n):
    a_sum += int(a[i])

tolerance = -(-(n * x) // 2)
result = a_sum - tolerance
if int(a[0]) > x:
    result += 1
if int(a[0]) > x and int(a[n-1]) > x:
    result -= 1
if result < 0:
    print("0")
else:
    print(result)

Submission status: Wrong Answer

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

for i in range(n):
    a_sum += int(a[i])

tolerance = -(-(n * x) // 2)

result = a_sum - tolerance
if int(a[0]) > x:
    result += 1
if int(a[n-1]) > x:
    result += 1
if int(a[0]) > x and int(a[n-1]) > x:
    result -= 2
if result < 0:
    print("0")
else:
    print(result)

Submission status: Wrong Answer

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

for i in range(n):
    a_sum += int(a[i])

tolerance = -(-(n * x) // 2)

result = a_sum - tolerance
if int(a[0]) > x:
    result += 1
if result < 0:
    print("0")
else:
    print(result)

Submission status: Wrong Answer

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

tolerance = -(-(n * x) // 2)

result = sum(a) -tolerance
if result < 0:
    print("0")
else:
    print(result)

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*-
"""考え方
   i:    0 1   2 3   4
------------------  
A[i]:  100 2 100 2 100

iが奇数のときのA[i]をまず減らす戦略が有効。

その後、まだ条件を満たさないなら、iが偶数のときのA[i]を減らす。

"""
def solve():
    N, x = list(map(int, input().split()))
    A = list(map(int, input().split()))

    ans = 0
    for i in range(1, N, 2):

        # iが奇数のときの数字を減らす。
        if A[i-1]+A[i] > x:
            diff = (A[i-1]+A[i]) - x
            diff = min(diff, A[i])
            A[i] -= diff
            ans += diff
    
    for i in range(0, N, 2):
        if i == N-1:
            if A[i]+A[i-1] > x:
                diff = (A[i]+A[i-1]) - x
                A[i] -= diff
                ans += diff
            break

        # iが偶数のときの数字を減らす
        if A[i]+A[i+1] > x:
            diff = (A[i]+A[i+1]) - x
            A[i] -= diff
            ans += diff
    
    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

Solution code:
# -*- coding:utf-8 -*-
"""考え方
   i:    0 1   2 3   4
------------------  
A[i]:  100 2 100 2 100

iが奇数のときのA[i]をまず減らす戦略が有効。

その後、まだ条件を満たさないなら、iが偶数のときのA[i]を減らす。

"""
def solve():
    N, x = list(map(int, input().split()))
    A = list(map(int, input().split()))

    ans = 0
    for i in range(1, N):
        if i%2 == 0: continue

        # iが奇数のときの数字を減らす。
        if A[i-1]+A[i] > x:
            diff = (A[i-1]+A[i]) - x
            if A[i] <= diff:
                A[i] -= diff
                ans += diff
    
    for i in range(0, N):
        if i%2 != 0: continue
        
        if i+1 == N: break

        # iが偶数のときの数字を減らす
        if A[i]+A[i+1] > x:
            diff = (A[i]+A[i+1]) - x
            A[i] -= diff
            ans += diff
    
    print(ans)


if __name__ == "__main__":
    solve()

Submission status: Wrong Answer

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

        ans += diff
        l[i] -= diff

ans = max(0,ans)
print(ans)

Submission status: Wrong Answer

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

        ans += diff
        l[j] -= diff

ans = max(0,ans)
print(ans)

Submission status: Wrong Answer

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

ans = 0

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

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

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
# C問題
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] <= a[i+1] and a[i] >= x:
            ans += a[i] - x
            a[i] = x
            ans += a[i+1] - (x - a[i])
            a[i+1] = x - a[i]
            continue
        elif a[i] <= a[i+1] and a[i] < x:
            ans += a[i+1] - (x - a[i])
            a[i + 1] = x - a[i]
            continue
        elif a[i] > a[i+1] and a[i+1] >= x:
            ans += a[i] - (x - a[i+1])
            a[i] = x - a[i+1]
            continue
        else:
            ans += a[i+1] - (x - a[i])
            a[i+1] = x - a[i]
            continue
print(ans)

Submission status: Wrong Answer

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

    tmp = a[i]


print(ans)

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


"""