Problem:


Score : 300 points

Problem Statement

There are N boxes arranged in a row.
Initially, the i-th box from the left contains a_i candies.

Snuke can perform the following operation any number of times:

Choose a box containing at least one candy, and eat one of the candies in the chosen box.

His objective is as follows:

Any two neighboring boxes contain at most x candies in total.

Find the minimum number of operations required to achieve the objective.

Constraints

2 ≤ N ≤ 10^5

0 ≤ a_i ≤ 10^9

0 ≤ x ≤ 10^9

Input

The input is given from Standard Input in the following format:

N x
a_1 a_2 ... a_N

Output

Print the minimum number of operations required to achieve the objective.

Sample Input 1

3 3
2 2 2

Sample Output 1

1

Eat one candy in the second box.
Then, the number of candies in each box becomes (2, 1, 2).

Sample Input 2

6 1
1 6 1 2 0 4

Sample Output 2

11

For example, eat six candies in the second box, two in the fourth box, and three in the sixth box.
Then, the number of candies in each box becomes (1, 0, 1, 0, 0, 1).

Sample Input 3

5 9
3 1 4 1 5

Sample Output 3

0

The objective is already achieved without performing operations.

Sample Input 4

2 0
5 5

Sample Output 4

10

All the candies need to be eaten.



Sample input:
3 3
2 2 2


Sample output:
1

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,A=map(int,input().split())
a=list(map(int,input().split()))
cnt=0
for i in range(1,N):
    tmp=max(0,(a[i-1]+a[i])-A)
    if i==1:
        if a[1]<tmp:
            a[0]-=tmp-a[1]
            a[1]=0
            continue
    
    cnt+=tmp
    a[i]-=tmp
    
print(cnt)

Submission status: Wrong Answer

Solution code:
N,A=map(int,input().split())
a=list(map(int,input().split()))
cnt=0
for i in range(1,N):
    tmp=max(0,(a[i-1]+a[i])-A)
    cnt+=tmp
    a[i]-=tmp
    
print(cnt)

Submission status: Wrong Answer

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

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

for i in range(1, len(a)):
    if a[i-1] + a[i] > x:
        cnt += abs(a[i]-x)

print(cnt)

Submission status: Wrong Answer

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

for i in range(N-1):
  r = B[i]+B[i+1]-X
  if i==0:
    if r<=B[1]:
      B[1]-=r
    else:
      B[0]-=r-B[1]
      B[1]=0
  else:
    if X<B[i]+B[i+1]:
      B[i+1]-=r

print(sum([a-b for a,b in zip(A,B)]))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
def resolve():
    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):
        s = a[i] + a[i + 1]
        if s > x:
            c += s - x
            a[i + 1] -= s - x
    print(c)


if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

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


if __name__ == "__main__":
    resolve()

Submission status: Wrong Answer

Solution code:
import copy

def sol1(N, X, A):
  cnt = 0
  for i in range(N-1):
    diff = A[i] + A[i+1] - X
    if diff <= 0: continue
    
    if A[i+1] >= diff:
      A[i+1] -= diff
    else:
      A[i] = A[i] - (diff - A[i+1])
      A[i+1] = 0
    cnt += diff
  return cnt

def sol2(N, X, A):
  cnt = 0
  for i in range(N-2, -1, -1):
    diff = A[i] + A[i+1] - X
    if diff <= 0: continue
    
    if A[i+1] >= diff:
      A[i+1] -= diff
    else:
      A[i+1] = 1
      A[i] -= diff - (A[i+1] - 1)

    cnt += diff
  return cnt

if __name__ == '__main__':
  N,X = map(int,input().split())
  A = list(map(int, input().split()))

  print(min(sol1(N,X,copy.deepcopy(A)), sol2(N,X,copy.deepcopy(A))))

Submission status: Wrong Answer

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

cnt = 0
for i in range(0,N-1):
  diff = A[i] + A[i+1] - X
  if diff <= 0: continue
    
  if A[i+1] >= diff:
    A[i+1] -= diff
  else:
    A[i+1] = 1
    A[i] -= diff - (A[i+1] - 1)

  cnt += diff
print(cnt)

Submission status: Wrong Answer

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

original_number, min_number = 0, 0

for i in range(n):
  original_number += int(a[i])
  
a[0] = x
for i in range(n - 1):
  if int(a[i]) + int(a[i + 1]) > x:
    while True:
      a[i + 1] = int(a[i + 1]) - 1
      if int(a[i]) + int(a[i + 1]) == x:
        break

for i in range(n):
  min_number += int(a[i])
  
ans = original_number - min_number
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from collections import Counter

stdin = sys.stdin
ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline()

mod = 10 ** 9 + 7

n, x = na()
aaa = na()
ans = 0
s = aaa[0]
for i in range(1, n):
    nt = x - s
    if aaa[i] <= nt:
        s = aaa[i]
        continue
    else:
        ans += aaa[i] - nt
        s = nt
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:
        tmp =  (a[i]+a[i+1]) - x
        a[i+1] = a[i+1] - tmp
        ans += tmp
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

c = 0

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

Submission status: Wrong Answer

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

c = 0

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

Submission status: Wrong Answer

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

c = 0

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

Submission status: Wrong Answer

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

c = 0

if x == 0:
	print(sum(a))
else:
	if a[0] > x:
		c += a[0] - (x-1)
		a[0] = x - 1

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

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

Submission status: Wrong Answer

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

c = 0

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

print(c)

Submission status: Wrong Answer

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

c = 0

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

Submission status: Wrong Answer

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

c = 0

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

Submission status: Wrong Answer

Solution code:
#!/usr/bin/env python
# coding: utf-8

# In[1]:


"""
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."""


# In[71]:


rightDirective = 0
leftDirective = 1

def findSmallest(myList, index=1):
    newList = myList[:]
    newList.sort()
    smallestElement = newList[index-1]
    return myList.index(smallestElement)    

def anyElementGreater(myList, value):
    ret = False
    for i in myList:
        if i > value:
            ret = True
            break
        else:
            continue
    return ret


# In[130]:


class CandyBoxes:
    N = 0
    x = 0
    candyList = []
    sumList = []
    toRemoveList = []
    def __init__(self, N, x, candyList):
        self.N = N
        self.x = x
        self.candyList = candyList[:]
        self.sumList = []
        self.toRemoveList = []
        
        for i in range(len(self.candyList) - 1):
            mySum = candyList[i] + candyList[i+1]
            self.sumList.append( mySum )
            if mySum > x:
                self.toRemoveList.append( mySum - x )
            else:
                self.toRemoveList.append( 0 )            
        
    def reconfigureBoxes(self):
        for i in range(len(self.toRemoveList)):
            mySum = self.candyList[i] + self.candyList[i+1]
            if mySum > self.x:
                self.toRemoveList[i] = mySum - x
            else:
                self.toRemoveList[i] = 0
            self.sumList[i] = mySum
            
    def removeExtraCandies(self, direction):
        removed = 0
        i = 1
        
        while anyElementGreater(self.toRemoveList, 0):
            currentIndex = findSmallest(self.toRemoveList, i)
            removed += self.toRemoveList[currentIndex]
#             print(i, currentIndex, self.toRemoveList[currentIndex], end = "")
            i+=1
            
            if direction == leftDirective:
                if self.candyList[currentIndex] >= self.toRemoveList[currentIndex]:
#                     print('Removing ', self.toRemoveList[currentIndex], 'Candies')
                    self.candyList[currentIndex] -= self.toRemoveList[currentIndex]
                    self.reconfigureBoxes()
                    continue
                    
                elif self.candyList[currentIndex+1] >= self.toRemoveList[currentIndex]:
#                     print('Removing ', self.toRemoveList[currentIndex], 'Candies')
                    self.candyList[currentIndex + 1] -= self.toRemoveList[currentIndex]
                    self.reconfigureBoxes()
                    continue
                    
                else:
                    removeLeft = self.toRemoveList[currentIndex] // 2
                    
                    if self.candyList[currentIndex] > self.candyList[currentIndex+1]:
                        self.candyList[currentIndex+1] -= (self.toRemoveList[currentIndex] - self.candyList[currentIndex])
                        self.candyList[currentIndex] = 0
                        self.reconfigureBoxes()
                        
                    else :
                        self.candyList[currentIndex] -= (self.toRemoveList[currentIndex] - self.candyList[currentIndex+1])
                        self.candyList[currentIndex+1] = 0
                        self.reconfigureBoxes()
                        
            if direction == rightDirective:
                if self.candyList[currentIndex+1] >= self.toRemoveList[currentIndex]:
#                     print('Removing ', self.toRemoveList[currentIndex], 'Candies')
                    self.candyList[currentIndex+1] -= self.toRemoveList[currentIndex]
                    self.reconfigureBoxes()
                    continue
                    
                elif self.candyList[currentIndex] >= self.toRemoveList[currentIndex]:
#                     print('Removing ', self.toRemoveList[currentIndex], 'Candies')
                    self.candyList[currentIndex] -= self.toRemoveList[currentIndex]
                    self.reconfigureBoxes()
                    continue
                    
                else:
                    removeLeft = self.toRemoveList[currentIndex] // 2
                    
                    if self.candyList[currentIndex] > self.candyList[currentIndex+1]:
                        self.candyList[currentIndex+1] -= removeLeft
                        self.candyList[currentIndex] -= (self.toRemoveList[currentIndex] - removeLeft)
                        self.reconfigureBoxes()
                        
                    else :
                        self.candyList[currentIndex] -= removeLeft
                        self.candyList[currentIndex+1] -= (self.toRemoveList[currentIndex] - removeLeft)
                        self.reconfigureBoxes()
                    continue            
                
        return removed
        


# In[160]:


N, x = [int(i) for i in input().split()]
candyInBox = [int(i) for i in input().split()]


# In[161]:


myCandyLeft = CandyBoxes(N, x, candyInBox)
#myCandyRight = CandyBoxes(N, x, candyInBox)


# In[162]:


leftAns = myCandyLeft.removeExtraCandies(leftDirective)
#rightAns = myCandyRight.removeExtraCandies(rightDirective)

print(leftAns)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 31 14:24:15 2019

@author: priya
"""

inp = input().split()
n = int(inp[0])
x = int(inp[1])
candies = [int(x) for x in input().split()]

op = 0
for i in range(1,len(candies)):
    if candies[i-1] + candies[i] > x:
        eat = candies[i-1] + candies[i] - x
        if(eat > candies[i]):
            candies[i-1] -= eat - candies[i]
            candies[i] = 0
        else:    
            candies[i] -= eat
            op += eat
print(op)
#print(candies)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 31 14:24:15 2019

@author: priya
"""

inp = input().split()
n = int(inp[0])
x = int(inp[1])
candies = [int(x) for x in input().split()]

op = 0
for i in range(1,len(candies)):
    if candies[i-1] + candies[i] > x:
        eat = candies[i-1] + candies[i] + x
        candies[i] -= eat
        op += eat
print(op)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
"""
Created on Sat Aug 31 14:24:15 2019

@author: priya
"""

inp = input().split()
n = int(inp[0])
x = int(inp[1])
candies = [int(x) for x in input().split()]

op = 0
for i in range(1,len(candies)):
    if candies[i-1] + candies[i] > x:
        eat = candies[i-1] + candies[i] - x
        candies[i] -= eat
        op += eat
print(op)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, x = map(int, input().split())
nums = list(map(int, input().split()))
before = nums[0]
cnt = 0
for i in range(1, N):
  ope = before+nums[i]-x
  before = max(nums[i]-ope, 0)
  cnt += ope
print(cnt)

Submission status: Wrong Answer

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

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

ans=0
if N>2:
  for i in range(1,N-1):
    sum0=a[i-1]+a[i]
    sum1=a[i]+a[i+1]
    if i==1 and sum0>x:
      ans+=sum0-x
      a[1]-=sum0-x
      if a[1]<0:
        ans-=a[1]
        a[1]=0
      sum0=a[i-1]+a[i]
      sum1=a[i]+a[i+1]
    if (sum0<=x and sum1>x):
      ans+=sum1-x
      a[i+1]-=sum1-x
      if a[i+1]<0:
        ans-=a[i+1]
        a[i+1]=0

else:
  if a[0]+a[1]>x:
    ans=a[0]+a[1]-x
print(ans)

Submission status: Wrong Answer

Solution code:
def minimumNumOfCandies(candies,atmostValue):
  print(atmostValue)
  candiesToEat = 0
  for i in range( 0 , len ( candies ) - 1 ) :
    if( candies[i] > atmostValue ) :
      candiesToEat = candiesToEat + ( candies[i] - atmostValue)
      candies[i] = candies[i] - ( candies[i] - atmostValue )
    if( candies[i] + candies[i + 1] > atmostValue ):
      candiesToEat = candiesToEat + ( (candies[i + 1] + candies[i]) - atmostValue )
      candies[i + 1] = candies[i + 1] - ( (candies[i + 1] + candies[i])- atmostValue)
  return candiesToEat
n = list(int(i) for i in input().split())
candies = list( int (i) for i in input().split())
print(minimumNumOfCandies(candies,n[1]))

Submission status: Wrong Answer

Solution code:
def minimumNumOfCandies(candies,atmostValue):
  candiesToEat = 0
  for i in range( 0 , len ( candies ) - 1 ) :
    if( candies[i] > atmostValue ) :
      candiesToEat = candiesToEat + ( candies[i] - atmostValue)
      candies[i] = candies[i] - ( candies[i] - atmostValue )
    if( candies[i] + candies[i + 1] > atmostValue ):
      candiesToEat = candiesToEat + ( candies[i + 1] - atmostValue )
      candies[i + 1] = candies[i + 1] - ( candies[i + 1] - atmostValue)
  return candiesToEat
n = list(int(i) for i in input().split())
candies = list( int (i) for i in input().split())
print(minimumNumOfCandies(candies,n[1]))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def minCandies(candies, n, max_candy):
    count = 0
    if candies[0] > max_candy:
        remove = max_candy - candies[0]
        count += remove
        candies[0] = max_candy
    for i in range(n - 1):
        if candies[i] + candies[i + 1] > max_candy:
            remove = (candies[i] + candies[i + 1]) - max_candy
            candies[i + 1] -= remove
            count += remove 
    return count


n, max_candy = input().split()
candies = list(map(int, input().split()))
print(minCandies(candies, int(n), int(max_candy)))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

a_list_2 = a_list.copy()

count_1 = 0
for i in range(1, N):
  if a_list[i] + a_list[i-1] > x:
    tmp = x - a_list[i-1]
    count_1 += a_list[i] - tmp
    a_list[i] = tmp
    
count_2 = 0
for i in range(N-2, -1, -1):
  if a_list_2[i] + a_list_2[i+1] > x:
    tmp = x - a_list_2[i+1]
    count_2 += a_list_2[i] - tmp
    a_list_2[i] = tmp

print(min(count_1, count_2))

Submission status: Wrong Answer

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

count = 0
for i in range(1, N):
  if a_list[i] + a_list[i-1] > x:
    tmp = x - a_list[i-1]
    count += a_list[i] - tmp
    a_list[i] = tmp
    
print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def min_candies(li, x):
    result = 0
    for i in range(len(li) - 1):
        num = li[i] + li[i + 1]
        if num > x:
            result += num - x
            li[i + 1] = abs(li[i + 1] - (num - x))

    return result

boxes, x = map(int, input().split())
li = list(map(int, input().split()))
print(min_candies(li, x))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import copy
"""
def ct(x,y):
    if (x>y):print("+")
    elif (x<y): print("-")
    else: print("?")
"""

def ip():
    return int(input())
def iprow():
    return [int(i) for i in input().split()]
def ips():
    return (int(i) for i in input().split())
def printrow(a):
    for i in a:
        print(i)

n,m = ips()
a = iprow()
s = 0
for i in range(1,n-1):
    if m<(a[i]+a[i+1]):
        if a[i]+a[i+1]>=m:
            difference = a[i] + a[i + 1] - m
            if a[i]>=difference:
                s+=difference
                a[i]-=difference
            else:
                s+=a[i]
                a[i]=0

        else:
            s+=a[i]
            a[i]=0

if a[0]+a[1] > m:
    if a[0]+a[1]>=m:
        difference = a[0] + a[1] - m
        if a[0]>=difference:
            s+=difference
            a[0]-=difference
        else:
            s+=a[0]
            a[0]=0

    else:
        s+=a[0]
        a[0]=0

if a[n-2]+a[n-1] > m:
    if a[n-2]+a[n-1]>=m:
        difference = a[n-2] + a[n-1] - m
        if a[n-1]>=difference:
            s+=difference
            a[n-1]-=difference
        else:
            s+=a[n-1]
            a[n-1]=0

    else:
        s+=a[n-1]
        a[n-1]=0
print(s)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

answer = 0
an[0] = x
for i in range(1, n):
    tmp = max(0, an[i] + an[i-1] - x)
    answer += tmp
    an[i] -= tmp
print(answer)

Submission status: Wrong Answer

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

answer = 0
for i in range(1, n):
    tmp = max(0, an[i] + an[i-1] - x)
    answer += tmp
    an[i] -= tmp
print(answer)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
N,x = [int(i) for i in input().split()]
alist = [int(i) for i in input().split()]
blist = []
for i in range(len(alist)-1):
    blist.append(alist[i]+alist[i+1])

ct = 0
#print(blist)
for i in range(len(blist)):
    if blist[i] > x:
        extra = blist[i] - x
        ct += extra
        blist[i] = x
        if i+1 < len(blist):
            blist[i+1] -= extra
    #print(blist,extra,ct)

print(ct)

Submission status: Wrong Answer

Solution code:
N,x = [int(i) for i in input().split()]
alist = [int(i) for i in input().split()]
blist = []
for i in range(len(alist)-1):
    blist.append(alist[i]+alist[i+1])

ct = 0

for i in range(len(blist)):
    if blist[i] > x:
        ct += blist[i] - x
        blist[i] = x
        if i+1 < len(blist):
            blist[i+1] -= ct

print(ct)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,x = [int(i) for i in input().split()]
alist = [int(i) for i in input().split()]
blist = []
for i in range(len(alist)-1):
    blist.append(alist[i]+alist[i+1])

ct = 0

for i in range(len(blist)):
    if blist[i] > x:
        ct += blist[i] - x
        blist[i] = x
        if i+1 < len(blist):
            blist[i+1] = x

print(ct)

Submission status: Wrong Answer

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

left = a[0]
for right in a[1:]:
    s = left + right
    if s > x:
        if left > x:
            right = 0
            left = x
        else:
            right -= s - x
        ans += s - x
        left = right

print(ans)

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

Submission status: Wrong Answer

Solution code:
# 2019/08/16

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

A=a[::-1]

def solve(a):
    cnt=0
    for i in range(n-1):
        tmp=a[i]+a[i+1]-x
        if tmp>a[i+1]:
            buf=tmp-a[i+1]
            cnt+=buf
            a[i]-=buf
            tmp-=a[i+1]
        if tmp>0:
            a[i+1]-=tmp
            cnt+=tmp
    return cnt

ans=min(solve(a),solve(A))
print(ans)

Submission status: Wrong Answer

Solution code:
# 2019/08/16

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

        tmp-=a[i+1]

    if tmp>0:
        a[i+1]-=tmp
        cnt+=tmp
print(cnt)