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:
def resolve():
    n, x = map(int, input().split())
    a = list(map(int, input().split()))
    c = 0
    if a[0] > x:
        c += a[0] - x
        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: Accepted

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] >= diff:
      A[i] -= diff
    else:
      A[i] = 1
      A[i+1] -= diff - (A[i] - 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: Accepted

Solution code:
from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import math
import bisect
import random
from itertools import permutations, accumulate, combinations
import sys
import string
from bisect import bisect_left


INF = float('inf')
def LI(): return list(map(int, sys.stdin.readline().split()))
def I(): return int(sys.stdin.readline())
def LIM(): return list(map(lambda x:int(x) - 1, sys.stdin.readline().split()))
def LS(): return sys.stdin.readline().split()
def S(): return sys.stdin.readline().strip()
def IR(n): return [I() for i in range(n)]
def LIR(n): return [LI() for i in range(n)]
def LIRM(n): return [LIM() for i in range(n)]
def SR(n): return [S() for i in range(n)]
def LSR(n): return [LS() for i in range(n)]
def SRL(n): return [list(S()) for i in range(n)]
mod = 1000000007


n, x = LI()
candy = LI()
S = sum(candy)
candy[0] = min(candy[0], x)

for i in range(1, n):
    candy[i] -= max(candy[i] + candy[i-1] - x, 0)



print(S - sum(candy))

Submission status: Accepted

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

Submission status: Accepted

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

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

Submission status: Accepted

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

count = 0
for i in range(n):
    diff = max(0, (a[i]+a[i+1]) - x)
    count += diff
    a[i+1] -= diff
print(count)

Submission status: Accepted

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)
  a[0] = x
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: Accepted

Solution code:
N, x = map(int, input().split())
a = list(map(int, input().split()))
c = 0
if a[0]>x:
  c = a[0] - x
  a[0] = x

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

print(c)

Submission status: Accepted

Solution code:
from sys import stdin
N,x = [int(x) for x in stdin.readline().rstrip().split()]
data = [int(x) for x in stdin.readline().rstrip().split()]
ans = 0
for i in range(N-1):
    v1 = data[i]
    v2 = data[i+1]
    if v1 + v2 >= x:
        ans += (v1+v2-x)
        data[i+1] = max(v2-(v1+v2-x),0)
print(ans)

Submission status: Accepted

Solution code:
N, x = map(int,input().split(' '))
all_jar = [int(x) for x in input().split()]
first_jar_candy = all_jar[0]
c = 0
min_opr = 0
for each_jar in all_jar[1:]:
  s = first_jar_candy + each_jar
  c = min(each_jar, max(0, s - x))
  c2 = max(0, s - c - x)
  first_jar_candy = each_jar - c
  min_opr += c + c2
else:
  c += max(0, each_jar - x)
print(min_opr)

Submission status: Accepted

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

c = 0
for i in range(n-1):
	t = max(0, 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: Accepted

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

Submission status: Accepted

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

a = A[0]
c = 0
ret = 0
for a_post in A[1:]:
  s = a + a_post
  c = min(a_post, max(0, (a + a_post) - x))
  c2 = max(0, a + a_post - c - x)
  a = a_post - c
  ret += c + c2
else:
  c += max(0, a_post - x)
print(ret)

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc048/tasks/arc064_a
# C - Boxes and Candies

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

c = 0
for i in range(N-1):
    s = a_list[i] + a_list[i+1]
    if s < x:
        # すでにクリアしているので何もしなくてよい
        continue
    elif s > x:
        # どちらを減らすか？ iとi+1あわせてs-x個分食べれば良い
        # 0の時点を考えると、先にある1を食べれるだけ食べた方が効率が良い
        if s - x < a_list[i+1]:
            a_list[i+1] -= s - x
        else:
            nokori = x - a_list[i+1]
            a_list[i+1] = 0
            a_list[i] -= nokori
        c += s - x

print(c)

Submission status: Accepted

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
  else:
    before = nums[i]
print(cnt)

Submission status: Accepted

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

Submission status: Accepted

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

boxes = [int(x) for x in input().split()]

index = 0
count = 0

while index < len(boxes):
    if boxes[index] == x:
        if index + 1 < len(boxes):
            count += boxes[index + 1]
            boxes[index + 1] = 0
        index += 2
    elif boxes[index] > x:
        count += boxes[index] - x
        boxes[index] = x
        if index + 1 < len(boxes):
            count += boxes[index + 1]
            boxes[index + 1] = 0
        index += 2
    else:
        if index + 1 < len(boxes):
            if boxes[index] + boxes[index + 1] > x:
                diff = boxes[index] + boxes[index + 1] - x
                boxes[index + 1] = boxes[index + 1] - diff
                count += diff
        index += 1
print(count)

Submission status: Accepted

Solution code:

def computeMinimumNoOfOperations(candies, x):
    noOfOperations = 0
    for i in range(len(candies) - 1):
        if (candies[i] > x):
            candiesToEat = candies[i] - x
            candies[i] -= candiesToEat
            noOfOperations += candiesToEat
        if (candies[i] + candies[i + 1] > x):
            candiesToEat = candies[i] + candies[i + 1] - x
            candies[i + 1] -= candiesToEat
            noOfOperations += candiesToEat 
    return noOfOperations


def start():
    n, x = list(map(int, input().split()))
    candies = list(map(int, input().split()))
    return computeMinimumNoOfOperations(candies, x)

print(start())

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

    if(a[i+1] > 0 and sa <= a[i+1]):
      a[i+1]=a[i+1]-sa
    else:
      a[i+1]=0

print(count)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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:
        a[0]+=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:
        a[i]+=a[i+1]
        a[i+1]=0

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

Submission status: Accepted

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

Solution code:
def minCandies(candies, n, max_candy):
    count = 0
    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 if candies[i + 1] >= remove else candies[i + 1]
            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: Accepted

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

count = 0

if a_list[0] > x:
  count += a_list[0] - x
  a_list[0] = x

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

Solution code:
def minCandies(boxes,limit):
    out = 0
    for i in range(len(boxes) - 1):
        total = boxes[i] + boxes[i + 1]
        if total > limit:
            out += total - limit
            boxes[i + 1] = boxes[i + 1] - (total - limit)
            if boxes[i + 1] < 0:
                boxes[i + 1] = 0
    return out

num_of_boxes, limit = map(int,input().split())
boxes = list(map(int,input().split()))

if limit == 0:
    print(sum(boxes))
elif max([a + b for (a,b) in zip(boxes[:-1],boxes[1:])]) <= limit:
    print(0)
else:
    print(minCandies(boxes,limit))

Submission status: Accepted

Solution code:
n, x=map(int, input().split())
a=list(map(int,input().split()))
ans=0
for i in range(n):
  if a[i]>x:
    ans+=a[i]-x
    a[i]=x
for i in range(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: Accepted

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] = li[i + 1] - (num - x)
            if li[i + 1] < 0:
                li[i + 1] = 0

    return result

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

if __name__ == '__main__':
    main()

Submission status: Accepted

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

    count = 0
    for i in range(1, len(A)):
        if A[i-1]+ A[i] > x:
            if A[i-1] <= x:
                count += A[i]+A[i-1]-x
                A[i] -=  A[i]+A[i-1]-x
            else:
                count += A[i]
                A[i] = 0
                count += A[i-1] - x
                A[i-1] = x
    # print(A)
    return count

if __name__ == '__main__':

    print(main())

Submission status: Accepted

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

pre = sum(a)

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

print(pre - sum(a))

Submission status: Accepted

Solution code:
#import pysnooper
#import os,re,sys,operator,math,heapq,string
from collections import Counter,deque
#from operator import itemgetter
#from itertools import accumulate,combinations,groupby,combinations_with_replacement
from sys import stdin,setrecursionlimit
#from copy import deepcopy
setrecursionlimit(10**6)
input=stdin.readline

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

Submission status: Accepted

Solution code:
# https://atcoder.jp/contests/abc048/tasks/arc064_a
import sys
sys.setrecursionlimit(12345678)
import itertools
from collections import Counter
from collections import defaultdict
from collections import deque
import bisect
from heapq import heappush, heappop


def main():
    n, x = map(int, input().split())
    al = list(map(int, input().split()))
    ans = 0
    for i in range(n-1):
        if al[i] + al[i+1] > x:
            dec = (al[i] + al[i+1]) - x
            ans += dec
            al[i+1] = max(0, al[i+1] - dec)

    # print(al)
    print(ans)


if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def _I(): return int(sys.stdin.readline())
def _F(): return float(sys.stdin.readline())
def _pf(s): return print(s, flush=True)

N, x = LI()
A = LI()
original_sum = sum(A)
for aidx in range(N-1):
    if A[aidx] + A[aidx+1] <= x:
        continue
    else:
        diff = A[aidx] + A[aidx+1] - x
        sub = min(diff, A[aidx+1])
        A[aidx+1] -= sub
        diff -= sub
        if diff > 0:
            A[aidx] -= min(A[aidx], diff)

#  print(A)
ans = original_sum - sum(A)
print(ans)

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

Submission status: Accepted

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

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

Submission status: Accepted

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

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

print(res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, x = [int(x) for x in input().split()]
a = list(map(int,input().split())) 
answer=0
if a[0]>x:
  answer+=(a[0]-x)
  a[0]=x
  #print(a[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: Accepted

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

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

Submission status: Accepted

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
#print(p)
for i in range(1, N):
  a = As[i]
  if p+a > x:
    r += p+a-x
    p = a-(p+a-x)
  else:
    p = a
  #print(p)
print(r)

Submission status: Accepted

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

ct = 0
for i in range(len(alist)-1):
    extra = alist[i]+alist[i+1]-x
    if extra <= 0:
        continue
    ct += extra
    if extra <= alist[i+1]-1:
        alist[i+1] -= extra
    else:
        alist[i] -= (extra-alist[i+1])
        alist[i+1] = 0

print(ct)

Submission status: Accepted

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

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

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

Submission status: Accepted

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

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

ans = tmp
print(tmp)

Submission status: Accepted

Solution code:
# 2019/08/16

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


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

ans=solve()
print(ans)

Submission status: Accepted

Solution code:
from collections import deque,defaultdict,Counter
from itertools import accumulate
import bisect
from heapq import heappop,heappush
from fractions import gcd
from copy import deepcopy
import math
import queue
#import numpy as np
Mod = 1000000007




def main(): #startline--------------------------------------------
    n,x = map(int,input().split())
    a = list(map(int,input().split()))
    ans = 0
    if a[0] > x:
        ans += a[0]-x
        a[0] = x
    for i in range(n-1):
        if a[i+1]+a[i] > x:
            ans += a[i+1]+a[i]-x
            a[i+1] -= a[i+1]+a[i]-x
        else:
            pass
    print(ans) 
if __name__ == "__main__":
    main() #endline===============================================





def sieve_of_eratosthenes(n):
    if not isinstance(n,int):
        raise TypeError("n is not int")
    if n<2:
        raise ValueError("n < 2 is not effective")
    prime = [1]*(n+1)
    for i in range(2,int(math.sqrt(n))):
        if prime[i] == 1:
            for j in range(2*i,n):
                if j%i == 0:
                    prime[j] = 0
    res = []
    for i in range(2,n):
        if prime[i] == 1:
            res.append(i)
    return res

class UnionFind:
    def __init__(self,n):
        self.parent = [i for i in range(n+1)]
        self.rank = [0 for i in range(n+1)]
    
    def findroot(self,x):
        if x == self.parent[x]:
            return x
        else:
            y = self.parent[x]
            y = self.findroot(self.parent[x])
            return y
    
    def union(self,x,y):
        px = self.findroot(x)
        py = self.findroot(y)
        if px < py:
            self.parent[py] = px
        else:
            self.parent[px] = py

    def same_group_or_no(self,x,y):
        return self.findroot(x) == self.findroot(y)

Submission status: Accepted

Solution code:
# 最初の回答が半分くらいWA になってしまったので、解説見てシンプルに書き直した
N, x = map(int, input().split())
A = [int(x) for x in input().split()]

D = [0]*N
answer = 0
for i in range(N-1):
    if A[i] + A[i+1] > x:
        d = A[i] + A[i+1] - x
        e = min(A[i+1], d)
        answer += e
        A[i+1] -= e
        d -= e
        if d > 0:
            A[i] -= d
            answer += d
print(answer)

Submission status: Accepted

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

D = [0]*N
answer = 0
for i in range(N-1):
    if A[i] + A[i+1] > x:
        d = A[i] + A[i+1] - x
        e = min(A[i+1], d)
        answer += e
        A[i+1] -= e
        d -= e
        if d > 0:
            A[i] -= d
            answer += d
print(answer)

Submission status: Accepted

Solution code:
n,x=map(int,input().split())
*l,=map(int,input().split())
ans=0
for i in range(n-1):
  m=max([0,l[i]+l[i+1]-x])
  ans+=m
  l[i+1]-=min([l[i+1],m])
print(ans)

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

Submission status: Accepted

Solution code:
from sys import exit, stdin
N, x = [int(_) for _ in stdin.readline().rstrip().split()]
A = [int(_) for _ in stdin.readline().rstrip().split()]

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

print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

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

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):
    left=a[i]
    right=a[i+1]
    if left+right<=x:
        continue
    eat=(left+right)-x
    ans+=eat
    a[i+1]=max(0, right-eat)
print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:

import collections
import itertools
import sys
import time

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

n,x=getints()
vs=getints()

res = 0
if vs[0] > x:
    eat = vs[0] - x
    res += eat
    vs[0] -= eat

for i in range(1,n):
    exceed = max(0, vs[i-1] + vs[i] - x)
    eat = min(vs[i], exceed)
    vs[i] -= eat
    res += eat
    exceed = max(0, vs[i-1] + vs[i] - x)
    eat = min(vs[i-1], exceed)
    res += eat

print (res)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N,x=map(int,input().split())
A=list(map(int,input().split()))
def solve(arr,sup):
    ans=0
    a=arr[:]
    for i in range(len(a)-1):
        tmp = a[i]+a[i+1]-sup
        if tmp>0:
            a[i+1]-=min(tmp,a[i+1])
            ans+=tmp
    return ans
print(min(solve(A,x), solve(A[::-1],x)))

Submission status: Accepted

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

cnt = 0

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

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

print(cnt)

Submission status: Accepted

Solution code:
N, total = map(int, input().split())
li = list(map(int, input().split()))

ans = 0
for i in range(N-1):
    diff = li[i] + li[i+1] - total
    if diff > 0:
        if li[i+1] > diff:
            li[i+1] -= diff
        else:
            li[i] -= (diff - li[i+1])
            li[i+1] = 0
        ans += diff
print(ans)

Submission status: Accepted

Solution code:
N, total = map(int, input().split())
li = list(map(int, input().split()))

ans = 0
for i in range(N-1):
    wa = li[i] + li[i+1]
    if wa > total:
        if li[i+1] > wa - total:
            li[i+1] -= (wa - total)
        else:
            li[i] -= ((wa - total) - li[i+1])
            li[i+1] = 0
        ans += wa - total
print(ans)

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) :
    if A[i] + A[i + 1] > X :
        if A[i] > X :
            ans += A[i] - X
            A[i] = X
        if A[i + 1] > X - A[i] :
            dis = A[i + 1] - (X - A[i])
            ans += dis
            A[i + 1] -= dis

print(ans)