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:
import bisect, collections, copy, heapq, itertools, math, string, sys
input = lambda: sys.stdin.readline().rstrip() 
sys.setrecursionlimit(10**7)
INF = float('inf')
def I(): return int(input())
def F(): return float(input())
def SS(): return input()
def LI(): return [int(x) for x in input().split()]
def LI_(): return [int(x)-1 for x in input().split()]
def LF(): return [float(x) for x in input().split()]
def LSS(): return input().split()

def resolve():
    N, x = LI()
    a = LI()

    s = [max(a[i] + a[i+1] - x, 0) for i in range(N - 1)]
    
    ans = sum([s[i] for i in range(len(s)) if i % 2 == 0])
    print(ans)

if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

Solution code:
def candy():
    boxNum, x = [int(i) for i in input().split()]
    ain = input()
    alist = ain.split(' ')

    s = 0
    for i in range(0, boxNum-1):
        if int(alist[i]) > x:
            s = s + int(alist[i+1])
            alist[i+1] = 0

        if int(alist[i+1]) + int(alist[i]) > x:
            s = s + (int(alist[i+1]) + int(alist[i]) - x)
            alist[i+1] = x - int(alist[i])
        
    print(s)
    
candy()

Submission status: Wrong Answer

Solution code:
def candy():
    boxNum, x = [int(i) for i in input().split()]
    ain = input()
    alist = ain.split(' ')

    s = 0
    for i in range(0, boxNum-1):
        if int(alist[i]) > x:
            s = s + (int(alist[i+1]) -x)
            alist[i+1] = 0

        if int(alist[i+1]) + int(alist[i]) > x:
            s = s + (int(alist[i+1]) + int(alist[i]) - x)
            alist[i+1] = x - int(alist[i])
        
    print(s)
    
candy()

Submission status: Wrong Answer

Solution code:
def candy():
    boxNum, x = [int(i) for i in input().split()]
    ain = input()
    alist = ain.split(' ')

    s = 0
    for i in range(0, boxNum-1):
        if int(alist[i]) > x:
            s = s + (int(alist[i+1]) -x)
            alist[i+1] = 0

        elif int(alist[i+1]) + int(alist[i]) > x:
            s = s + (int(alist[i+1]) + int(alist[i]) - x)
            alist[i+1] = x - int(alist[i])
        
    print(s)
    
candy()

Submission status: Wrong Answer

Solution code:
def candy():
    boxNum, x = [int(i) for i in input().split()]
    ain = input()
    alist = ain.split(' ')

    s = 0
    for i in range(0, boxNum-1):
        if int(alist[i]) > x:
            s = s + (int(alist[i+1]) -x)
            alist[i+1] = x - int(alist[i+1])

        elif int(alist[i+1]) + int(alist[i]) > x:
            s = s + (int(alist[i+1]) + int(alist[i]) - x)
            alist[i+1] = x - int(alist[i])
        
    print(s)
    
candy()

Submission status: Wrong Answer

Solution code:
def candy():
    boxNum, x = [int(i) for i in input().split()]
    ain = input()
    alist = ain.split(' ')

    s = 0
    for i in range(0, boxNum-1):
        if int(alist[i]) > x:
            s = s + (int(alist[i]) -x)
            alist[i] = x - int(alist[i])

        elif int(alist[i+1]) + int(alist[i]) > x:
            s = s + (int(alist[i+1]) + int(alist[i]) - x)
            alist[i+1] = x - int(alist[i])
        
    print(s)
    
candy()

Submission status: Wrong Answer

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

result = 0

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

print(result)

Submission status: Wrong Answer

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

ans = 0
if n == 2:
    ans += max(a[0]+a[1] - x, 0)
else:
    minus = max(a[0] - x, 0)
    ans += minus
    a[0] -= minus
    a[1] -= minus
    minus = max(a[n-1] - x, 0)
    ans += minus
    a[n-2] -= minus
    a[n-1] -= minus
    twosum = [0] * (n-1)
    for i in range(n-1):
        twosum[i] = a[i] + a[i+1]
    for i in range(n-1):
        if twosum[i] > x:
            minus = twosum[i] - x
            twosum[i] -= minus
            if not i == n-2:
                twosum[i+1] -= minus
            ans += minus
print(ans)

Submission status: Wrong Answer

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

ans = 0
if n == 2:
    ans += max(a[0] - x, 0)
    ans += max(a[1] - x, 0)
else:
    minus = max(a[0] - x, 0)
    ans += minus
    a[0] -= minus
    a[1] -= minus
    minus = max(a[n-1] - x, 0)
    ans += minus
    a[n-2] -= minus
    a[n-1] -= minus
    twosum = [0] * (n-1)
    for i in range(n-1):
        twosum[i] = a[i] + a[i+1]
    for i in range(n-1):
        if twosum[i] > x:
            minus = twosum[i] - x
            twosum[i] -= minus
            if not i == n-2:
                twosum[i+1] -= minus
            ans += minus
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

r = 0
flow = 0

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

    if t > 0:
        if t > a[i]:
            r += t - a[i]
            t -= a[i]

        flow = max(0, t)

r += flow

print(r)

Submission status: Wrong Answer

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

r = 0
flow = 0

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

        if t > 0:
            r += flow
            flow = max(0, t - flow)

    elif t > 0:
        if t > a[i]:
            r += t - a[i]
            t -= a[i]

        flow = t

r += flow

print(r)

Submission status: Wrong Answer

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

r = 0
flow = 0

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

        else:
            r += flow
            flow = 0
    elif t > 0:
        flow = t

r += flow

print(r)

Submission status: Wrong Answer

Solution code:
#import
#import math
#import numpy as np
#= int(input())
#= input()
N, x = map(int, input().split())
a = list(map(int, input().split()))
#= [input(), input()]
#= [list(map(int, input().split())) for _ in range(N)]
#= {i:[] for i in range(N)}

r = 0
flow = 0

for i in range(1, N):
    t = (a[i] + a[i - 1]) - x
    if flow != 0:
        if t > 0:
            r += min(flow, t)
            flow = abs(t - min(flow, a[i-1]))
        else:
            r += flow
            flow = 0
    elif t > 0:
        flow = t

r += flow

print(r)

Submission status: Wrong Answer

Solution code:
n, x, *a = map(int, open(c:=0).read().split())
for i in range(n - 1):
    s = max(a[i] + a[i + 1] - x, 0)
    a[i + 1] -= s
    c += s
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()]

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

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

print(ans)

Submission status: Wrong Answer

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

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

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

print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

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

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

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

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

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

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

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

def main():
    N, x = mi()
    a = list(mi())
    b = a[::-1]
    cnt_a = 0
    cnt_b = 0
    for i in range(N-1):
        if a[i]+a[i+1] > x:
            cnt_a += a[i]+a[i+1]-x
            a[i+1] = x-a[i]
    for i in range(N-1):
        if b[i]+b[i+1] > x:
            cnt_b += b[i]+b[i+1]-x
            b[i+1] = x-b[i]
    print(min(cnt_a, cnt_b))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

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

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

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

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


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def candy():
    boxNum, x = [int(i) for i in input().split()]
    ain = input()
    alist = ain.split(' ')

    s = 0

    for i in range(0, boxNum-1):
        if int(alist[i+1]) + int(alist[i]) > x:
            if int(alist[i]) > x:
                s = s + x - int(alist[i])
                alist[i] = x
            s = s + (int(alist[i+1]) + int(alist[i]) - x)
            alist[i+1] = x - int(alist[i])
        
    print(s)
    
candy()

Submission status: Wrong Answer

Solution code:
def candy():
    boxNum, x = [int(i) for i in input().split()]
    ain = input()
    alist = ain.split(' ')

    s = 0

    if int(alist[0]) > x:
        s = x - int(alist[0])
        alist[0] = x

    for i in range(0, boxNum-1):
        if int(alist[i+1]) + int(alist[i]) > x:
            s = s + (int(alist[i+1]) + int(alist[i]) - x)
            alist[i+1] = x - int(alist[i])
        
    print(s)
    
candy()

Submission status: Wrong Answer

Solution code:
def candy():
    boxNum, x = [int(i) for i in input().split()]
    ain = input()
    alist = ain.split(' ')

    s = 0

    if int(alist[0]) > x:
        s = s + x - int(alist[0])
        alist[0] = x
    for i in range(0, boxNum-1):
        if int(alist[i+1]) + int(alist[i]) > x:
            s = s + (int(alist[i+1]) + int(alist[i]) - x)
            alist[i+1] = x - int(alist[i])
        
    print(s)
    
candy()

Submission status: Wrong Answer

Solution code:
def candy():
    boxNum, x = [int(i) for i in input().split()]
    ain = input()
    alist = ain.split(' ')

    s = 0
    for i in range(0, boxNum-1):
        if int(alist[i+1]) + int(alist[i]) > x:
            s = s + (int(alist[i+1]) + int(alist[i]) - x)
            alist[i+1] = x - int(alist[i+1])
        
    print(s)
    
candy()

Submission status: Wrong Answer

Solution code:
def candy():
    boxNum, x = [int(i) for i in input().split()]
    ain = input()
    alist = ain.split(' ')

    s = 0
    for i in range(0, boxNum-1):
        if int(alist[i+1]) + int(alist[i]) > x:
            s = s + (int(alist[i+1]) + int(alist[i]) - x)
            alist[i+1] = x - int(alist[i])
        
    print(s)
    
candy()

Submission status: Wrong Answer

Solution code:
def candy():
    boxNum, x = [int(i) for i in input().split()]
    ain = input()
    alist = ain.split(' ')

    int(alist[0])

    s = 0
    for i in range(0, boxNum-1):
        if int(alist[i+1]) + int(alist[i]) > x:
            s = s + (int(alist[i+1]) + int(alist[i]) - x)
            alist[i+1] = x - int(alist[i])
        
    print(s)
    
candy()

Submission status: Wrong Answer

Solution code:
n,m=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]
sum=0
for i in range(0,len(a)-1):
    d=a[i]+a[i+1]
    if(d<=m):
        pass
    else:
        f=d-m
        if(a[i]==a[i+1]):
            a[i+1]=a[i+1]-f
        elif(a[i]>a[i+1]):
            a[i]=a[i]-f
        else:
            a[i+1]=a[i+1]-f
        sum=sum+f
print(sum)

Submission status: Wrong Answer

Solution code:
n,m=[int(i) for i in input().split()]
a=[int(i) for i in input().split()]
sum=0
for i in range(1,len(a),2):
    d=a[i-1]+a[i]
    if(i!=len(a)-1):
        e=a[i+1]+a[i]
    else:
        e=0
    f=max(d,e)
    g=f-m
    if(g>=0):
        sum=sum+g
print(sum)

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

ans = 0
flg = 0
sum1 = 0
sum2 = 0
for i in range(n - 1):
  work = a[i] + a[i + 1]
  if i % 2 == 0:
    if work > x:
      sum1 = work - x
      flg = 0
  else:
    if work > x:
      sum2 = work - x
      flg = 1
  if flg == 1:
    if sum1 > sum2:
      ans += sum1
    else:
      ans += sum2

if flg == 0:
  ans += sum1
  
print(ans)

Submission status: Wrong Answer

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

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

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

        diff = sSum - x

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

    print(result)


calculate(N, X, ARR)

Submission status: Wrong Answer

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

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

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


solve()

Submission status: Wrong Answer

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



solve()

Submission status: Wrong Answer

Solution code:
def solve():
    n, x = map(int, input().split())
    s = list(map(int, input().split()))
    ss = s[1:] * 1
    ss.append(s[0])
    
    def candies(s):
        tb = []
        for i in range(1, n):
            tb.append(s[i] + s[i - 1])
        cnt, tmp, flg = 0, 0, False
        for p in tb:
            if flg:
                p -= tmp
                flg = False
                tmp = 0
            if p > x:
                tmp = p - x
                flg = True
                cnt += tmp
        return cnt

    print(min(candies(s), candies(ss)))


solve()

Submission status: Wrong Answer

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

solve()

Submission status: Wrong Answer

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

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

ans = 0

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

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys

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

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

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

Submission status: Wrong Answer

Solution code:
import sys

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

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

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

Submission status: Wrong Answer

Solution code:
import sys

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

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

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

Submission status: Wrong Answer

Solution code:
import sys

def eat(i,j,candies,counter):
    s=candies[i]+candies[j]
    if s>x:
        if candies[i]>=s-x:
            counter+=s-x
            candies[i]-=s+x
        else:
            counter+=s-x
            candies[j]-=s-x-candies[i]
            candies[i]=0
    return counter

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

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

Submission status: Wrong Answer

Solution code:
import sys

def eat(i,j,candies,counter):
    s=candies[i]+candies[j]
    if s>x:
        if candies[i]>=s-x:
            counter+=s-x
            candies[i]-=s+x
        else:
            counter+=s-x
            candies[j]-=s-x-candies[i]
            candies[i]=0
    return counter

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

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

Submission status: Wrong Answer

Solution code:
import sys

def eat(i,j,candies,counter):
    s=candies[i]+candies[j]
    if s>x:
        if candies[i]>=s-x:
            counter+=s-x
            candies[i]-=s+x
        else:
            counter+=s-x
            candies[j]-=s-x-candies[i]
            candies[i]=0
    return counter

def main(n,x,candies):
    counter=0
    for i in range(1,n-1,2):
        counter=eat(i,i+1,candies,eat(i,i-1,candies,counter))
    counter=eat(-2,-1,candies,eat(0,1,candies,counter))
    return counter

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

Submission status: Wrong Answer

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

ans = 0
p = 0

for ai in a:
  if p + ai > x:
    d = p + ai - x
    ans += d
    p = ai - d
    
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):
    ref = a[i] + a[i + 1]
    ans += max(ref - x, 0)
    a[i + 1] = max(a[i + 1] - abs(ref - x), 0)

print(ans)

Submission status: Wrong Answer

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

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

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

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

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

gap = total - goal

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

if gap <= 0:
    print(ans)
else:
    for i in range(1, N - 1):
        if A[i] == 0:
            continue

        if gap >= 2 * A[i]:
            gap -= 2 * A[i]
            ans += A[i]
        else:
            ans += (gap + 1) // 2
            gap = 0
            break
    if gap > 0:
        ans += gap
    print(ans)

Submission status: Wrong Answer

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

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

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

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

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

gap = total - goal

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

if gap <= 0:
    print(ans)
else:
    for i in range(1, N - 1):
        if A[i] == 0:
            continue

        if gap >= 2 * A[i]:
            gap -= 2 * A[i]
            ans += A[i]
        else:
            ans += (gap + 1) // 2
            break
    print(ans)

Submission status: Wrong Answer

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

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

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

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

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

gap = total - goal

ans = 0
if A[0] > x:
    diff = A[0] - x
    ans += diff
    gap -= diff
if A[N - 1] > x:
    diff = A[N - 1] - x
    ans += diff
    gap -= diff
if gap <= 0:
    print(ans)
else:
    print(ans + (gap + 1) // 2)

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

    print(ans)

if __name__ == '__main__':
    main()