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

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

Submission status: Accepted

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

n,x = map(int, raw_input().split())
a = map(int, raw_input().split())

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
from collections import defaultdict


def main():
    N, x = map(int, input().split())
    c = list(map(int, input().split()))
    pre = sum(c)

    c = [min(i, x) for i in c]
    ans = pre - sum(c)

    for i in range(len(c) - 1):
        if c[i] + c[i + 1] > x:
            n = (c[i] + c[i + 1]) - x
            c[i + 1] -= n
            ans += n

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys

def debug(x, table):
    for name, val in table.items():
        if x is val:
            print('DEBUG:{} -> {}'.format(name, val), file=sys.stderr)
            return None

def solve():
    N, x = map(int, input().split())
    A = [int(i) for i in input().split()]
    ans = 0

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

    print(ans)


if __name__ == '__main__':
    solve()

Submission status: Accepted

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

res = 0
if a[0] > x:
    res += a[0] - x
    a[0] = x

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

print(res)

Submission status: Accepted

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

ans = 0

for i in range(l-1):
    a_0 = a[i]
    a_1 = a[i+1]
    dif = x - a_0
    if a_1 > dif:
        ans += a_1 - dif
        if dif > 0:
            a[i+1] = dif
        else:
            a[i+1] = 0

print(ans)

Submission status: Accepted

Solution code:
# -*- coding:utf-8 -*-
import math
N, x = map(int,input().split())
a = list(map(int, input().split()))
ans = 0
for tmp in range (0,N-1):
    if a[tmp] + a[tmp+1] <= x:
        continue
    else:
        tmp2 = (a[tmp]+a[tmp+1]) - x #差分を出す。
        if a[tmp+1] - tmp2 >= 0:
            a[tmp+1] -= tmp2
            ans += tmp2
        else: #足りない場合
            ans += tmp2
            a[tmp] += a[tmp+1] - tmp2 #足りない部分はa[tmp]から引く。
            tmp2 += a[tmp+1] - tmp2
            a[tmp+1] -= tmp2
print(ans)

Submission status: Accepted

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

print(Nc)

Submission status: Accepted

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

a += [0]
counter = 0

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

print(counter)

Submission status: Accepted

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

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

Submission status: Accepted

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

seq = [int(i) for i in input().split()]

ans = 0

for i in range(N-1):
    if seq[i]+seq[i+1] > x:
        dec = seq[i]+seq[i+1] - x
        if seq[i+1] > dec:
            seq[i+1] = seq[i+1] - dec
        else:
            seq[i+1] = 0
            seq[i] = seq[i] - dec + seq[i+1]
        ans += dec

print(ans)

Submission status: Accepted

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

result = 0
for i in range(N):
	result = result+max(0, A[i]-x)
	A[i] = min(x, A[i])

for i in range(N-1):
	if A[i]+A[i+1]<=x:
		continue
	result = result+A[i]+A[i+1]-x
	A[i+1] = A[i+1]-(A[i]+A[i+1]-x)

print(result)

Submission status: Accepted

Solution code:
N, x = map(int, raw_input().split())
A = map(int, raw_input().split())
ans = 0
for i in range(1, N):
    v = max(0, A[i-1] + A[i] - x)
    ans += v
    A[i] = max(0, A[i] - v)
print ans

Submission status: Accepted

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

b_list = [a_list[i]+a_list[i+1] for i in range(0,N-1)]

excess_list = [max(0,val -x) for val in b_list]
ans = 0
for i in range(0,len(excess_list)-1):
    min_val = min(a_list[i+1],excess_list[i],excess_list[i+1])
    a_list[i+1] -= min_val
    excess_list[i] -= min_val
    excess_list[i+1] -= min_val
    ans += min_val
ans += sum(excess_list)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
#!/usr/bin/env python3


def min_turns(n, x, ys):
    zs = ys[:]
    if zs[0] > x:
        zs[0] = x
    for i in range(n - 1):
        if zs[i] + zs[i + 1] > x:
            zs[i + 1] = x - zs[i]
    ans = sum(y - z for y, z in zip(ys, zs))
    return ans


def main():
    n, x = (int(z) for z in input().split())
    ys = [int(z) for z in input().split()]
    print(min_turns(n, x, ys))


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
#ABC048C

n,x=map(int,raw_input().split())
a=map(int,raw_input().split())
res0=sum(a)
if a[0]>x:
 a[0]=x
for i in range(n-1):
 if a[i]+a[i+1]>x:
  a[i+1]=x-a[i]
  if a[i+1]<0:
   a[i+1]=0
res=res0-sum(a)
print res

Submission status: Accepted

Solution code:
import sys

line = sys.stdin.readline()

num = int(line.split()[0])
x = int(line.split()[1])

line = sys.stdin.readline()

nums = map(int, line.split())

cnt = 0
for i,v in enumerate(nums):
  if i == len(nums)-1:
    break
  if nums[i] + nums[i+1] > x:
    eat = nums[i] + nums[i+1] - x
    if nums[i+1] > eat:
      nums[i+1] -= eat
    else :
      nums[i] = nums[i] - (eat - nums[i+1])
      nums[i+1] = 0
    cnt += eat

print cnt

Submission status: Accepted

Solution code:
#003
N,x= [int(i) for i in input().split(" ")]
list = [int(i) for i in input().split(" ")]
count = 0 
for i in range(N):
    if(i==0):
        if list[i]>x:
            temp = list[i]-x
            count+=temp
            list[i]-=temp
    else:
        temp = list[i]+list[i-1]-x
        if(temp>0):
            count+=temp
            list[i]-=temp    
print(count)

Submission status: Accepted

Solution code:
#003
N,x=input().split(" ")
s = input().split(" ")
list = []
for i in range(int(N)):
    list.append(int(s[i]))
count = 0

if(list[0]>int(x)):
    count+=list[0]-int(x)
    list[0]-=list[0]-int(x)

for i in range(1,int(N)):
    temp = list[i]+list[i-1]
    if(temp>int(x)):
        count+=temp-int(x)
        list[i]-=temp-int(x)        
print(count)

Submission status: Accepted

Solution code:
import sys
import math

n, x = map(int, sys.stdin.readline().split())
s = map(int, sys.stdin.readline().split())
su = [0  for i in range(n+1)]
su[0] = s[0]
su[n] = s[n-1]
for i in range(1,n):
    su[i] = s[i-1]+s[i]

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

print ans

Submission status: Accepted

Solution code:
N, x = map(int, raw_input().split())
candies = map(int, raw_input().split())
counter = 0
for i  in range(0, len(candies)-1):
    delta = candies[i+1] + candies[i] - x
    if delta > 0:
        counter += delta
        candies[i+1] = x - candies[i]
        if candies[i+1] < 0:
            candies[i+1] = 0
print counter

Submission status: Accepted

Solution code:
n, x = map(int, input().split())
a = list(map(int, input().split()))
counter = 0
pointer = 1
if a[0] > x:
    counter += a[0] - x
    a[0] = x
while pointer <= n - 1:
    if a[pointer - 1] + a[pointer] > x:
        eat = a[pointer - 1] + a[pointer] - x
        counter += eat
        a[pointer] -= eat
    pointer += 1
print(counter)

Submission status: Accepted

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


def main():
    n, x = [int(i) for i in raw_input().split()]
    a_list = [int(i) for i in raw_input().split()]
    rel_sums = [a_list[i] if i == 0 else a_list[i-1]+a_list[i] for i in xrange(n)]
    count = 0
    for i in xrange(n-1):
        if rel_sums[i] > x:
            count += rel_sums[i] - x
            rel_sums[i+1] -= rel_sums[i] - x
    if rel_sums[n-1] > x:
        count += rel_sums[n-1] - x
        rel_sums[n-1] -= rel_sums[n-1] - x
    print count

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import sys
from functools import reduce

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

cnt, prv = 0, 0

for i in range(N):
    diff = max(arr[i] + prv - x, 0)
    cnt += diff
    arr[i] -= diff
    prv = arr[i]

print(cnt)

Submission status: Accepted

Solution code:
import sys
from functools import reduce

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

if x == 0:
    print(reduce(lambda acc, x: acc + x, arr))
    sys.exit()

cnt = 0
prv = 0

for i in range(N):
    diff = max(arr[i] + prv - x,0)
    cnt += diff
    arr[i] -= diff
    prv = arr[i]

print(cnt)

Submission status: Accepted

Solution code:
# def makelist(n, m):
# 	return [[0 for i in range(m)] for j in range(n)]

# a = int(input())
# b, c = map(int, input().split())
# s = input()



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

if a[0] + a[1] > x:
	tmp = a[0] + a[1] - x
	ans += tmp
	if a[1] >= tmp:
		a[1] -= tmp
	else:
		a[0] -= tmp - a[1]
		a[1] = 0

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, x = map(int,input().split())
a = list(map(int,input().split()))
a_min = [0] * n

if a[0] > x:
    a_min[0] = x
else:
    a_min[0] = a[0]

ans = a[0] - a_min[0]

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

print(ans)

Submission status: Accepted

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

_, x = raw_input().split(' ')
x = long(x)
aa = raw_input().split(' ')

ret = long(0)
last = 0
for i, a in enumerate(aa):
    a = long(a)
    if i == 0:
       last = min(a, x)
       ret += a - last
    else:   
       last = min(a, x - last)
       ret += a - last

print ret

Submission status: Accepted

Solution code:
# problem C
N, x = map(int, input().split())
num_list = [int(x) for x in input().split()]
if x < num_list[0]:
    count = num_list[0] - x
    num_list[0] = x
else:
    count = 0

for i in range(N-1):
    n = num_list[i] + num_list[i+1]
    if x < n:
        num_list[i+1] = x - num_list[i]
        count += n - x
    else:
        pass
print(count)

Submission status: Accepted

Solution code:
n,x,*A=map(int,open(0).read().split())
A+=0,
s=0
for i in range(n):d=max(0,A[i]+A[i-1]-x);s+=d;A[i]-=d
print(s)

Submission status: Accepted

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

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

print(count)

Submission status: Accepted

Solution code:
n, x = map(int, input().split())
a = list(map(int, input().split()))
eat = 0
i = 0
total = 0
while i < (n - 1):
  a[i] -= eat
  if (a[i] < 0):
    a[i] = 0
  if (a[i] + a[i+1]) > x:
    eat = a[i] + a[i+1] - x
    total += eat
  else:
    eat = 0
  i += 1
print(str(total))

Submission status: Accepted

Solution code:
#coding:utf-8

n,x = map(int, raw_input().split())
a = map(int, raw_input().split())

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

for i in range(len(a)-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: Accepted

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

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

Submission status: Accepted

Solution code:
st = input().split()
N = int(st[0])
x = int(st[1])
A = input().split()
a = []
for i in range(N):
     a.append(int(A[i]))   
a.append(0)
a.append(0)
ret = 0
for i in range(N):
    if a[i] + a[i + 1] > x :
        d1 = max((a[i] + a[i + 1]) - x,0)
        ret +=  min(a[i+1],d1)
        a[i+1] -=  min(a[i+1],d1)
        d1 = max((a[i] + a[i + 1]) - x,0)
        ret +=  min(a[i],d1)
        a[i] -=  min(a[i],d1)
print(ret)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, x = map(int, raw_input().split())
a = map(int, raw_input().split())
 
ans = 0
 
if a[0] > x:
    ans += (a[0]-x)
    a[0] = x
for i in xrange(len(a)-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: Accepted

Solution code:
# coding: utf-8
"""Boxes and Candies

@author: Liz
@modified: 12-04-2016

"""


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

N, x = readline()
candies = readline()


def solve():

    count = 0

    for i in range(N - 1):

        stc = candies[i] + candies[i + 1]
        if stc > x:
            count += stc - x
            candies[i] += min(0, candies[i + 1] - (stc - x))
            candies[i + 1] = max(0, candies[i + 1] - (stc - x))

    return count

print(solve())

Submission status: Accepted

Solution code:
a,x=map(int,raw_input().split())
l=map(int,raw_input().split())
k=0
for i in range(1,a):
    b=l[i-1]
    c=l[i]
    d=b+c
    if(d>x):
        e=d-x
        k=k+e
        if(c>=e):
            f=c-e
            l[i]=f
        else:
            f=0
            g=b-(e-c)
            l[i]=f
            l[i-1]=g
print k

Submission status: Accepted

Solution code:


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


counter = 0


dif1 = a[N-2] + a[N-1] - x
if dif1 > 0:
        a1 = a[N-2] - dif1
        if a1 > 0:
            a[N-2] -= dif1
            counter  += dif1
        else:
            a[N-1]-= -a1
            a[N-2] = 0
            counter += dif1

for i in range(N-2):
    dif1 = a[i] + a[i+1] - x
    if dif1 > 0:
        a1 = a[i+1] - dif1
        if a1 > 0:
            a[i+1] -= dif1
            counter  += dif1
        else:
            a[i]-= -a1
            a[i+1] = 0
            counter += dif1

       
while a[N-2] + a[N-1] > x:
    if a[N-2] > 0:
        a[N-2] -= 1
    else :
        a[N-1] -= 1
    
    counter += 1

print(counter)

Submission status: Accepted

Solution code:
s=input().split()
n=int(s[0])
x=int(s[1])
s=input().split()
ans=max(0,int(s[0])+int(s[1])-x)
prev=max(0,int(s[1])-ans)
for i in range(2,len(s)):
 now=max(0,int(s[i])+prev-x)
 ans+=now
 prev=int(s[i])-now
print(ans)

Submission status: Accepted

Solution code:
ini=map(int, raw_input().split())
box=map(int, raw_input().split())

time=0

for i in range(ini[0]-1):
	pre_time=0
	if box[i]+box[i+1]>ini[1]:
		pre_time+=box[i]+box[i+1]-ini[1]
		box[i+1]-=pre_time
		if box[i+1]<0:
			box[i]+=box[i+1]
			box[i+1]=0
	time+=pre_time

print time

Submission status: Accepted

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

cnt = 0

for i in range(0, len(a_lst) - 1):
    if a_lst[i] > x:
        d = a_lst[i] - x
        cnt += d
        a_lst[i] -= d

    if a_lst[i] + a_lst[i + 1] > x:
        d = a_lst[i] + a_lst[i + 1] - x
        cnt += d
        a_lst[i + 1] -= d

print(cnt)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import math;
N,x = map(int, raw_input().split());
a = map(int, raw_input().split());
cnt = 0;
for i in range(N):
    if(a[i] > x):
        cnt += a[i] - x;
        a[i] = x;
    if(i > 0 and a[i] + a[i-1] > x):
        cnt += a[i] + a[i-1] - x;
        a[i] = x - a[i-1];
print(cnt);

Submission status: Accepted

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

for i in range(N):
	if i == 0:
		if a[i] >= x:
			a[i] = x
	else:
		if a[i] + a[i-1] >= x:
			d = a[i] + a[i-1] - x
			a[i] = max(0, -a[i-1]+x)
s2 = sum(a)

print(s1-s2)

Submission status: Accepted

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

l = list(map(int, input().split()))
a = [0]
a.extend(list(map(int, input().split())))

num = 0
f = True
while(f):
    f = False
    for i in range(l[0]):
        if(((a[i] + a[i+1]) > l[1])):
            f = True
            sub = (a[i] + a[i+1]) - l[1]
            a[i+1] -= sub
            num += sub
print(num)

Submission status: Accepted

Solution code:
N,x=map(int,raw_input().split())
a=map(int,raw_input().split())
ans=0
for looking in range(1,len(a)):
    calc=a[looking]+a[looking-1]
    if calc<=x:
        continue
    sa=calc-x
    taberu=min(a[looking],sa)
    a[looking]-=taberu
    ans+=taberu
    sa-=taberu
    a[looking-1]-=sa
    ans+=sa
print ans

#print a

Submission status: Accepted

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

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

print(count)

Submission status: Accepted

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

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

Submission status: Accepted

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

n, x = [int(w) for w in raw_input().split()]
a = [int(w) for w in raw_input().split()]

b = [a[i] + a[i+1] for i in xrange(n - 1)]

ans = 0
for i in xrange(n - 1):
    if b[i] > x:
        d = b[i] - x
        ans += d
        b[i] = x
        if i < n - 2:
            b[i + 1] = b[i + 1] - min(d, a[i + 1])

print ans

Submission status: Accepted

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

ans = 0

if a[0] > x:
    ans += (a[0]-x)
    a[0] = x
for i in xrange(len(a)-1):
    if sum(a[i:i+2]) > x:
        ans += (sum(a[i:i+2]) - x)
        a[i+1] -= (sum(a[i:i+2]) - x)

print ans

Submission status: Accepted

Solution code:
#coding:utf-8
import math
import string
import time
import fractions

n, x = map(int,raw_input().split())
a = map(int,raw_input().split())


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

Submission status: Accepted

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

import sys

def rl(proc=None):
    if proc is not None:
        return proc(sys.stdin.readline())
    else:
        return sys.stdin.readline().rstrip()

def srl(proc=None):
    if proc is not None:
        return map(proc, rl().split())
    else:
        return rl().split()

def solve(A, x):
    p = min(A[0], x)
    r = A[0] - p
    for i in xrange(1, len(A)):
        c = A[i]
        eat = max(0, c + p - x)
        p = c - eat
        r += eat
    return r

def main():
    N, x = srl(int)
    A = srl(int)
    print min(solve(A, x), solve(list(reversed(A)), x))

if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
a,b=map(int, input().split())
*c,=map(int, input().split())
ans=0
for i in range(a - 1):
    d=c[i]+c[i + 1]
    if d<= b: continue
    r = max(0, A[i + 1] - (d - b))
    l = b - r
    ans += c[i] - l + c[i + 1] - r
    c[i + 1] = r
print(ans)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

Solution code:
N, A = map(int, input().split())
n = 0
def pick(list, num):
    4yu
for i in range(N):
    x = int(input().split())
    if x != A:
        L.append(int(input().split())-A)
    else:
        n += 1
L.sort()

Submission status: Runtime Error

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

    s = 0

    for t in range(0, 10):
        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])
                if alist[i+1] < 0:
                    alist[i] = alist[i] + alist[i+1]
                    alist[i+1] = 0
        
    print(s)
    
candy()

Submission status: Runtime Error

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])
            if alist[i+1] < 0:
                alist[i] = alist[i] + alist[i+1]
                alist[i+1] = 0
        
    print(s)
    
candy()

Submission status: Runtime Error

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([ss[i] for i in range(len(s)) if i % 2 == 0])
    print(ans)

if __name__ == '__main__':
    resolve()

Submission status: Runtime Error

Solution code:
def candy():
    n = input().split()
    N = int(n[0])
    x = int(n[1])
    alist = [int(i) for i in input().split()]

    s = 0
    for i in range(0, N - 1):
        c = alist[i] , d = alist[i + 1]
        if int( alist[i] + alist[i + 1] ) > x:
            if int( alist[i] ) >= x:
                alist[i] = x
                alist[i + 1] = 0
            else :
                alist[i + 1] = x - a[i] 
            s += c - alist[i] + d - alist[i + 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: Runtime Error

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):
        c = alist[i] , d = alist[i + 1]
        if int( alist[i] + alist[i + 1] ) > x:
            if int( alist[i] ) >= x:
                alist[i] = x
                alist[i + 1] = 0
            else :
                alist[i + 1] = x - a[i] 
            s += c - alist[i] + d - alist[i + 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: Runtime Error

Solution code:
def candy():
    N = int(input())
    x = int(input())
    a = int(input().split)

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

Submission status: Runtime Error

Solution code:
import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007


def main():
    a, b, x = map(int, readline().split())

    if a == 0:
        ans = b // x + 1
    else:
        ans = b // x - (a - 1) // x

    print(ans)
    return


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
N = int(input())
A = list(map(int, input().split()))
A_ind = []
for i in range(N):
    A_ind.append((A[i], i+1))
A_ind.sort(reverse=True)
for _, i in A_ind:
    print(i)

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

Submission status: Runtime Error

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

sum = [0] * (N-1)

for i in range(N-1):
  sum[i] = a[i] + a[i+1]

count = 0

if len(sum) == 1:
  print(sum[0] - x)
  exit()

while max(sum) > x:
  max_index = sum.index(max(sum))
  sum[max_index] += -1
  if max_index == 0:
    sum[max_index+1] += -1
  elif max_index == (N-1):
    sum[max_index-1] += -1
  else:
    if sum[max_index-1] > sum[max_index+1]:
      sum[max_index-1] += -1
    else:
      sum[max_index+1] += -1
  count += 1
print(count)

Submission status: Runtime Error

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

sum = [0] * (N-1)

for i in range(N-1):
  sum[i] = a[i] + a[i+1]

count = 0

if len(sum) == 1:
  print(sum[0] - x)
  exit()

while max(sum) > x:
  max_index = sum.index(max(sum))
  sum[max_index] += -1
  if max_index == 0:
    sum[max_index+1] += -1
  elif max_index == (N-1):
    sum[max_index-1] += -1
  else:
    if sum[max_index-1] > sum[max_index+1]:
      sum[max_index-1] += -1
    else:
      sum[max_index+1] += -1
  count += 1
  print(max(sum))
print(count)

Submission status: Runtime Error

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

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

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

print(ans)