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())
l=list(map(int,input().split()))
c=0 
for i in range(n-1):
  a=l[i]
  b=l[i+1]
  if a+b<=x:
    continue
  c1=a+b-x
  c+=c1
  if b>=x:
    l[i+1]-=c1
  else:
    l[i]-=c1-b
    l[i+1]=0
print(c)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

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

print(cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if x % 2 == 0:
    b = x // 2
    a = b
else:
    b = x // 2
    a = b + 1

cnt = 0
for i in range(n):
    if i % 2 == 0 and A[i] > a:
        cnt += A[i] - a
    elif i % 2 == 1 and A[i] > b:
        cnt += A[i] - b
        
cnt2 = 0
for i in range(n):
    if i % 2 == 1 and A[i] > a:
        cnt2 += A[i] - a
    elif i % 2 == 0 and A[i] > b:
        cnt2 += A[i] - b
        
print(min(cnt, cnt2))

Submission status: Wrong Answer

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

for i in range(N-2):
    if d[i] <= x:
        continue
    de = d[i] - x
    ans += de
    d[i] = x
    d[i+1] -= de
    
if d[-1] > x:
    de = d[-1] - x
    ans += de
    d[-1] = x

print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

Solution code:
N,x=map(int,input().split())
A=list(map(int,input().split()))
A2=A[::-1]

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



ANS2=0
for i in range(1,N):
    if A2[i]+A2[i-1]>x:
        ANS2+=(A2[i]+A2[i-1]-x)
        A2[i]-=(A2[i]+A2[i-1]-x)

print(min(ANS,ANS2))

Submission status: Wrong Answer

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
n, x = map(int, input().split())
a = input().split()
a = [int(m) for m in a]
ans = 0
for i in range(1, n):
    t = a[i] + a[i - 1]
    if t <= x:
        pass
    else:
        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
if n==2:
    print(max(0, sum(a)-x))
    exit()
for i in range(n-1):
    s = a[i] + a[i+1]
    if s <= x:
        continue
    else:
        d = s-x
        if a[i+1] >= 1:
            if a[i+1] >= d:
                ans += d
                a[i+1] -= d
            else:
                ans += a[i+1]
                a[i+1] = 0
                ans += a[i]-(d-a[i+1])
                a[i] -= a[i]-(d-a[i+1])
        else:
            ans += d
            a[i] -= d
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

a += [0]

count = 0
for i in range(N):
    extra = a[i]+a[i+1] - X
    if extra <= 0:
        continue
    count += extra
    if a[i+1] <= extra:
        a[i+1] -= extra
    else:
        a[i] -= extra-a[i+1]
        a[i+1] = 0
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):
    extra = a[i]+a[i+1] - X
    if extra <= 0:
        continue
    count += extra
    if a[i+1] <= extra:
        a[i+1] -= extra
    else:
        a[i] -= extra-a[i+1]
        a[i+1] = 0
print(count)

Submission status: Wrong Answer

Solution code:
# encoding: utf-8
N, x = map(int, input().split())
a = list(map(int, input().split()))

sums = [0] * (N - 1)
for i, ai in enumerate(a):
    if i == 0: prev = ai
    else:
        sums[i - 1] = ai + prev
        prev = ai

ans = 0
for i in range(N - 1):
    # skip
    if sums[i] <= x: continue
    # eat
    ans += (sums[i] - x)
    if i < N - 2: sums[i + 1] -= (sums[i] - x)
    sums[i] = x

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

if a[-1] + a[-2] > x:
    tmp = x - a[-1]
    count += a[-2] - tmp
    a[-2] = tmp

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

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

print(count)

Submission status: Wrong Answer

Solution code:
N, x = [int(_) for _ in input().split()]
A = [int(_) for _ in input().split()]
eat = [0] * N
eat[0] = A[0] - x
for i in range(N - 1):
    eat[i + 1] = max(A[i + 1] + A[i] - eat[i] - x, 0)
print(sum(eat))

Submission status: Wrong Answer

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

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

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

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

Submission status: Wrong Answer

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

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

if a[0] > x:
    a[0] = x
    count = x - a[0]

for i in range(len(a) - 1):

    while a[i] + a[i + 1] > x:
        a[i + 1] = a[i + 1] - 1
        count += 1
print(count)

Submission status: Wrong Answer

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

count = 0
for i in range(1, len(d)):
    if d[i - 1] != 0:
        count += d[i - 1]
        d[i] = max(0, d[i] - d[i - 1])
count += d[-1]
print(count)

Submission status: Wrong Answer

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

count = 0
print(d)
for i in range(1, len(d)):
    if d[i - 1] != 0:
        count += d[i - 1]
        d[i] = max(0, d[i] - d[i - 1])
count += d[-1]
print(count)

Submission status: Wrong Answer

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

count = 0
for i in range(1, len(d)):
    if d[i - 1] != 0:
        count += d[i - 1]
        d[i] = max(0, d[i] - d[i - 1])
count += d[-1]
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]+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()))
ans = 0
for i in range(N-1):
    if (a[i]+a[i+1]) > x and x - a[i+1] > 0 :
        
        ans += a[i+1]+a[i]-x
        a[i] = a[i]-(x-a[i+1])
        a[i+1] = 0
        
    elif (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+1]+a[i]-x
        a[i+1] = x-a[i]
print(ans)

Submission status: Wrong Answer

Solution code:
iN,ix = [int(_) for _ in input().split()]
aA =[int(_) for _ in input().split()]

#if iN*ix < sum(aA)
if ix == 0 :
    print(sum(aA))
else:
    iCount = 0
    for i in range(1,iN):
        i差 = aA[i-1] + aA[i] -ix
        if 0 < i差  :
            if aA[i] <= i差:
                aA[i] = 0
                iCount += i差
            else:
                iCount += i差
                aA[i] = i差
    print(iCount)

Submission status: Wrong Answer

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

AA = [0 for i in range(N-1)]
for i in range(N-1):
    AA[i] = A[i] + A[i+1]

cnt = 0

for i in range(N-1):
    aa = AA[i]
    if aa > x:
       cnt += aa - x
       A[i+1] -= aa - x
       if i < N - 2:
           AA[i]   = AA[i]   - (aa - x)
           AA[i+1] = AA[i+1] - (aa - x)
       else:
           AA[i]   = AA[i]   - (aa - x)

print(cnt)

Submission status: Wrong Answer

Solution code:
N,x=map(int,input().split())
A0=input().split('0')
A=[]
for a0 in A0:
    A.append(list(map(int,a0.split())))

for i in range(len(A)):
    A[i].insert(0,0)
    A[i].append(0)

ans = 0
for j in range(len(A)):
    for i in range(1,len(A[j])):
        total = A[j][i-1]+A[j][i]
        if total > x:
            A[j][i] -= total - x
            ans += total - x

print(ans)

Submission status: Wrong Answer

Solution code:
N,x=map(int,input().split())
A0=input().split('0')
A=[]
for a0 in A0:
    A.append(list(map(int,a0.split())))

for a in A:
    a.insert(0,0)
    a.append(0)

ans = 0
for j in range(len(A)):
    for i in range(1,len(A[j])):
        total = A[j][i-1]+A[j][i]
        if total > x:
            A[j][i] = A[j][i-1] - x
            ans += total - x

print(ans)

Submission status: Wrong Answer

Solution code:
N,x=map(int,input().split())
A0=input().split('0')
A=[]
for a0 in A0:
    A.append(list(map(int,a0.split())))

for a in A:
    if len(a)==1:
        a.append(0)

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

print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import sys


Nx = input().split()
an = input().split()

anx = list()

for item in an:
    anx.append(int(item))

x = int(Nx[1])
N = int(Nx[0])
i = 0
count = 0
while i < N - 1:
    sum = anx[i] + anx[i + 1]
    
    if sum > x:
        count+= sum - x
        num = sum - x
        anxi = anx[i]
        anxi -= num
        if anxi < 0:
            anxi2 = anx[i + 1] + anxi
            anx[i+1] = anxi2
            anxi = 0
        anx[i] = anxi
    i += 1
print(count)

Submission status: Wrong Answer

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

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

m = x // 2
b = [m for i in a]
ans = 0
for i, j in zip(a, b):
    if i - j > 0:
        ans += i - j
if x % 2 == 1:
    cnt = [0, 0]
    for i, v in enumerate(a):
        if v > m:
            cnt[i % 2] += 1
    ans -= max(cnt)

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

Submission status: Wrong Answer

Solution code:
il = lambda: list(map(int, input().split()))
ils = lambda n, s="int(input())": [eval(s) for _ in range(n)]

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

Submission status: Wrong Answer

Solution code:
N,x=map(int, input().split())
A=list(map(int, input().split()))
B=[]
for i in range(0,N-1):
    B.append(A[i]+A[i+1])
if len(B)%2==1:
    B.append(0)
C=[]
for i in range(0,len(B)-1,2):
    C.append(max(B[i],B[i+1]))
ans=0
for i in range(len(C)):
    if C[i]>x:
        ans+=C[i]-x
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(1,n):
    if a[i-1]<0:
        a[i-1]=0
    b=a[i-1]+a[i]
    if b>x:
        a[i]-=b-x
        count+=b-x
print(count)
print(a)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

ans1 = tmp = 0
for a in src:
    if tmp + a > X:
        ans1 += tmp + a - X
        tmp = min(X, a-X)
    else:
        tmp = a

ans2 = tmp = 0
for a in src[::-1]:
    if tmp + a > X:
        ans2 += tmp + a - X
        tmp = min(X, a-X)
    else:
        tmp = a

print(ans2)

Submission status: Wrong Answer

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

ans1 = 0
for i in range(1,N,2):
    a,b,c = src[i-1], src[i], src[i+1] if i+1 < N else 0
    p = max(a,c)
    if b + p > X:
        ans1 += b + p - X

ans2 = 0
for i in range(0,N-1,2):
    a,b,c = src[i-1] if i-1 >= 0 else 0, src[i], src[i+1]
    p = max(a,c)
    if b + p > X:
        ans2 += b + p - X

print(min(ans1,ans2))

Submission status: Wrong Answer

Solution code:
import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(1000000)


# Debug output
def chkprint(*args):
    names = {
        id(v): k
        for k, v in inspect.currentframe().f_back.f_locals.items()
    }
    print(', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))


# Binary converter
def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in input().split()]


def gcd(n, m):
    if n % m == 0:
        return m
    else:
        return gcd(m, n % m)


def gcd_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = gcd(v, L[i])

    return v


def lcm(n, m):
    return (n * m) // gcd(n, m)


def lcm_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = lcm(v, L[i])

    return v


# Width First Search (+ Distance)
def wfs_d(D, N, K):
    """
    D: 隣接行列(距離付き)
    N: ノード数
    K: 始点ノード
    """

    dfk = [-1] * (N + 1)
    dfk[K] = 0

    cps = [(K, 0)]
    r = [False] * (N + 1)
    r[K] = True
    while len(cps) != 0:
        n_cps = []
        for cp, cd in cps:
            for i, dfcp in enumerate(D[cp]):
                if dfcp != -1 and not r[i]:
                    dfk[i] = cd + dfcp
                    n_cps.append((i, cd + dfcp))
                    r[i] = True

        cps = n_cps[:]

    return dfk


# Depth First Search (+Distance)
def dfs_d(v, pre, dist):
    """
    v:  現在のノード
    pre: １つ前のノード
    dist: 現在の距離

    以下は別途用意する
    D: 隣接リスト(行列ではない)
    D_dfs_d: dfs_d関数で用いる，始点ノードから見た距離リスト
    """

    global D
    global D_dfs_d

    D_dfs_d[v] = dist

    for next_v, d in D[v]:
        if next_v != pre:
            dfs_d(next_v, v, dist + d)

    return


def sigma(N):
    ans = 0
    for i in range(1, N + 1):
        ans += i
    return ans


class Combination:
    def __init__(self, n, mod):

        g1 = [1, 1]
        g2 = [1, 1]
        inverse = [0, 1]
        for i in range(2, n + 1):
            g1.append((g1[-1] * i) % mod)
            inverse.append((-inverse[mod % i] * (mod // i)) % mod)
            g2.append((g2[-1] * inverse[-1]) % mod)
        self.MOD = mod
        self.N = n
        self.g1 = g1
        self.g2 = g2
        self.inverse = inverse

    def __call__(self, n, r):
        if (r < 0 or r > n):
            return 0
        r = min(r, n - r)
        return self.g1[n] * self.g2[r] * self.g2[n - r] % self.MOD


# --------------------------------------------

dp = None


def main():
    N, X = li_input()
    A = li_input()

    ans = 0

    for i in range(len(A)):
        if A[i] > X:
            ans += A[i] - X
            A[i] = X

    for i in range(1, len(A) - 1, 2):
        s1 = A[i] + A[i - 1]
        s2 = A[i] + A[i + 1]

        if s1 > X or s2 > X:
            s = max(s1, s2)
            d = s - X
            ans += d
            A[i] -= d

    print(ans)


main()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
f,g = list(map(int,input().split()))

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

c = 0
p = 0
if a[0] > g:
    c += a[0] - g
    a[0] = g
while p <= f-1:
    if a[p-1] + a[p] > g:
        eat = a[p-1] + a[p] - g
        c += eat
        a[p] -= eat
    p += 1
print(c)

Submission status: Wrong Answer

Solution code:
N,x = (int(i) for i in input().split())
a = [int(i) for i in input().split()]
count = 0
if a[1] > x:
    difference = x-a[1]
    count += difference
    a[1] -= difference
     
for i in range(N-1):
    difference = x - (a[i] + a[i+1])
    if difference < 0:
        count += abs(difference)
        a[i+1] += difference


print(count)

Submission status: Wrong Answer

Solution code:
N,x = (int(i) for i in input().split())
a = [int(i) for i in input().split()]
count = 0
if a[1] > x:
    difference = x-a[1]
    count += difference
    a[1] -= difference
     
for i in range(N-1):
    difference = x - (a[i] + a[i+1])
    if difference < 0:
        count += abs(difference)
        a[i+1] += difference

print(a)
print(count)

Submission status: Wrong Answer

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

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

print(res)

Submission status: Wrong Answer

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

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

print(res)

Submission status: Wrong Answer

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

def main():
    n, x = map(int, input().split())
    a_list = list(map(int, input().split()))
    a_list_cp = a_list[:]
    ans = 0

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

    for a, b in zip(a_list, a_list_cp):
        ans += b - a
    print(ans)

if __name__ == '__main__':
    main()

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

Submission status: Wrong Answer

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

import copy

a_res = copy.deepcopy(a)
d = []

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

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

Submission status: Wrong Answer

Solution code:
def inpl(): return input().split()
n, x = map(int, inpl())
a = list(map(int, inpl()))
ans = 0
flg = False
for i in range(n):
    if flg:
        if (a[i-1] + a[i]) > x:
            ans += (a[i-1] + a[i]) - x
            a[i] = x - a[i-1]
        flg = False
    else:
        if a[i] > x:
            ans += a[i] - x#a[i] == xにする
            a[i] = x        
        flg = True
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):
    s = A[i-1] + A[i]
    if s > x:
        eat = s - 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()))
c=0
for i in range(n):
    if a[i]>x:
        a[i]=x
        c+=a[i]-x
for i in range(n-1):
    if a[i]+a[i+1]>x:
        c+=a[i+1]-(x-a[i])
        a[i+1]=x-a[i]
print(c)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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