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: Time Limit Exceeded

Solution code:
import copy

N, X = map(int, input().split())
cans = list(map(int, input().split()))
oririn_cans = copy.deepcopy(cans)
count = 0
# 奇数インデックスの要素を引いていく
for n in range(1, N, 2):
  if n + 1 == N:
    while cans[n-1] + cans[n] > X:
      cans[n] -= 1
      count += 1  
    continue

  while cans[n-1] + cans[n] > X or cans[n+1] + cans[n] > X:
    cans[n] -= 1
    count += 1

for n in range(0, N, 2):
  if n == 0:
    while cans[n+1] + cans[n] > X:

      cans[n] -= 1
      count += 1  
    continue

  if n + 1 == N:
    while cans[n-1] + cans[n] > X:
      cans[n] -= 1
      count += 1  
    continue

  while cans[n-1] + cans[n] > X or cans[n+1] + cans[n] > X:
    cans[n] -= 1
    count += 1


odd_count = count

count = 0
cans = oririn_cans

# 偶数インデックスの要素を引いていく
for n in range(0, N, 2):
  if n == 0:
    while cans[n+1] + cans[n] > X:

      cans[n] -= 1
      count += 1  
    continue

  if n + 1 == N:
    while cans[n-1] + cans[n] > X:
      cans[n] -= 1
      count += 1  
    continue

  while cans[n-1] + cans[n] > X or cans[n+1] + cans[n] > X:
    cans[n] -= 1
    count += 1

for n in range(1, N, 2):
  if n + 1 == N:
    while cans[n-1] + cans[n] > X:
      cans[n] -= 1
      count += 1  
    continue

  while cans[n-1] + cans[n] > X or cans[n+1] + cans[n] > X:
    cans[n] -= 1
    count += 1

even_count = count
print(min(odd_count, even_count))

Submission status: Time Limit Exceeded

Solution code:
n, x = map(int,input().split())
A = list(map(int,input().split()))
sum = []
ans = 0
for i in range(n-1):
    sum.append(A[i]+A[i+1])
for j in range(n-1):
    if j != n-2:
        while sum[j] > x:
            sum[j] -= 1
            sum[j+1] -= 1
            ans += 1
    else:
        while sum[j] > x:
            sum[j] -= 1
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,x=map(int,input().split())
A=list(map(int,input().split()))
B=[[0 for k in range(N)]for k in range(x+1)]
for k in range(x+1):
    for n in range(N):
        if n%2==0:
            B[k][n]=x-k
        else:
            B[k][n]=k
#print(B)

ans=[0 for k in range(x+1)]
for k in range(x+1):
    for n in range(N):
        if A[n]-B[k][n]>0:
            ans[k] +=A[n]-B[k][n]

print(min(ans))

Submission status: Time Limit Exceeded

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

ans = 0

for i in range(n-1):
    if i == 0:
        q = 0
    else:
        q = a[i-1]
    if i == n-2:
        r = 0
    else:
        r = a[i+2]

    while a[i]+a[i+1] > x:
        if q+a[i] < a[i+1]+r:
            a[i+1] -= 1
            ans += 1
        else:
            a[i] -= 1
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

while a[0] > x:
    a[0] -= 1
    count += 1

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

Submission status: Time Limit Exceeded

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

for i in range(N-1):
    while a[i] + a[i+1] > x:
        a[i+1] -= 1
        count += 1

print(count)

Submission status: Time Limit Exceeded

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

for al in range(1,N):
    while (alist[al-1]+alist[al]) > x:
        alist[al] -= 1
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

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

print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

count=0
if(a[0]>x):
    count=a[0]-x
    a[0]=x
    
for ind,i in enumerate(a):
    if(ind==len(a)-1):
      break
    s=i+a[ind+1]
    while(s>x):
        count+=1
        a[ind+1]-=1
        s=i+a[ind+1]
print(count)

Submission status: Time Limit Exceeded

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

eat=0

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

Submission status: Time Limit Exceeded

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

ans = 0
for i in range(1, len(a)):
    while True:
        if a[i-1] + a[i] > x:
            if a[i] > 0:
                a[i] -= 1
                ans += 1
            else:
                a[i-1] -= 1
                ans += 1
        else:
            break

print(ans)

Submission status: Time Limit Exceeded

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

count1 = count
for i in range(n-1):
    while a[i] + a[i+1] > x:
        if a[i] > 0:
            a[i] -= 1
        else:
            a[i+1] -= 1
        count1 += 1

count2 = count
for i in range(n-1):
    while b[i] + b[i+1] > x:
        
        if b[i+1] > 0:
            b[i+1] -= 1
        else:
            b[i] -= 1
        count2 += 1

count = min(count1, count2)
print(count)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N,x = map(int,input().split())
a = list(map(int,input().split()))
i = 0
answer = 0
if a[0] > x:
  answer = a[0] - x
  a[0] = x
while i < N-1:
    if a[i] + a[i+1] > x:
        answer = answer + a[i] + a[i+1] - x
        a[i+1] = x - a[i]
        i = i+1
print(answer)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8
N, x = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
for i in range(N - 1): # 貪欲法で解く
    while a[i] + a[i + 1] > x and a[i + 1] >= 1: # 左の組から順に、組の右側から食べていく
        ans += 1
        a[i + 1] -= 1
    while a[i] + a[i + 1] > x: # それでも足りないなら組の左側も食べる
        ans += 1
        a[i] -= 1
print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,x =map(int,input().split())
a=list(map(int,input().split()))
cnt=0
for i in range(N-1):
    if a[i]+a[i+1]>x:
        while a[i+1]>0 and a[i]+a[i+1]>x:
            a[i+1]-=1
            cnt+=1
#端はこの操作では実現できない可能性があるので別に
if a[0]+a[1]>x:
    while a[0]+a[1]>x:
        a[0]-=1
        cnt+=1
if a[-1]+a[-2]>x:
    while a[-1]+a[-2]>x:
        a[-1]-=1
        cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N, x = map(int, input().split())
a = list(map(int, input().split()))
b = []
cnt = 0
for i in a:
    if i>x:
        tmp = abs(x-i)
        i-=tmp
        cnt+=tmp
    b.append(i)
for j in range(N-1):
    while b[j] + b[j+1] > x:
        b[j+1]-=1
        cnt+=1
print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N,x= map(int,input().split())
array = list(map(int,input().split()))
result=0
for i in range(0,N-1):
    #i と i+1 が閾値以下じゃなかったら 
    while array[i] + array[i+1] > x:
        if array[i+1]!=0:
            array[i+1]-=1
        else:
            array[i]-=1
        result+=1
print(result)

Submission status: Time Limit Exceeded

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]:


def findSmallest(myList, index=1):
    newList = myList[:]
    for i in range(index-1):
        minm = min(newList)
        newList.remove(minm)
#        print("minminum", newList, minm)
    return myList.index(min(newList))

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 = [0]*(N-1)
        self.toRemoveList = [0]*(N-1)
        
        self.reconfigureBoxes()
        
    def sumListF(self, i):
        return self.candyList[i] + self.candyList[i+1]
    
    def toRemoveListF(self, i):
        return self.candyList[i] + self.candyList[i+1] - x
    
    def reconfigureBoxes(self):
        self.sumList = [self.sumListF(x) for x in range(self.N-1)]
        self.toRemoveList = [self.toRemoveListF(x) for x in range(self.N-1)]
            
    def removeExtraCandies(self):
        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 self.candyList[currentIndex] > self.candyList[currentIndex+1]:
                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:                    
                    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()
                        
            else:
                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:                    
                    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()
                
        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]:


myAns = myCandyLeft.removeExtraCandies()

print(myAns)

Submission status: Time Limit Exceeded

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]:


def findSmallest(myList, index=1):
    newList = myList[:]
    for i in range(index-1):
        minm = min(newList)
        newList.remove(minm)
#        print("minminum", newList, minm)
    return myList.index(min(newList))

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 = [0]*(N-1)
        self.toRemoveList = [0]*(N-1)
        
        self.reconfigureBoxes()
        
    def sumListF(self, i):
        return self.candyList[i] + self.candyList[i+1]
    
    def toRemoveListF(self, i):
        return self.candyList[i] + self.candyList[i+1] - x
    
    def reconfigureBoxes(self):
        self.sumList = [self.sumListF(x) for x in range(self.N-1)]
        self.toRemoveList = [self.toRemoveListF(x) for x in range(self.N-1)]
            
    def removeExtraCandies(self):
        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 self.candyList[currentIndex] > self.candyList[currentIndex+1]:
                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()
                        
            else:
                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]:


myAns = myCandyLeft.removeExtraCandies()

print(myAns)

Submission status: Time Limit Exceeded

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[:]
    for i in range(index-1):
        minm = min(newList)
        newList.remove(minm)
#        print("minminum", newList, minm)
    return myList.index(min(newList))

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 = [0]*(N-1)
        self.toRemoveList = [0]*(N-1)
        
        self.reconfigureBoxes()
        
    def sumListF(self, i):
        return self.candyList[i] + self.candyList[i+1]
    
    def toRemoveListF(self, i):
        return self.candyList[i] + self.candyList[i+1] - x
    
    def reconfigureBoxes(self):
        self.sumList = [self.sumListF(x) for x in range(self.N-1)]
        self.toRemoveList = [self.toRemoveListF(x) for x in range(self.N-1)]
            
    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(min(leftAns, rightAns))