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:
a = list(map(int,input().split(" ")))
br = list(map(int,input().split(" ")))
count = 0
for i in range(1,a[0]):
    while br[i-1] + br[i] > a[1]:
        if br[i] == 0:
            br[i-1] -= 1
            count += 1
        else:
            br[i] -= 1
            count += 1
print(count)

Submission status: Time Limit Exceeded

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

import copy

a_res = copy.deepcopy(a)
d = []

for i in range(N - 1) :
    d.append(a[i + 1]  + a[i])
    
cnt = 0

# do for diff
for i in range(N - 1) :
    bg = -1
    
    while d[i] > x :
        if a_res[i] <= a_res[i + 1] :
            a_res[i + 1] -= 1
            d[i] -= 1
            
            if i < N - 2 :
                d[i + 1] -= 1
        else :
            a_res[i] -= 1
            d[i] -= 1
            if i > 0 :
                d[i - 1] -= 1

        cnt += 1
        
print(cnt)

Submission status: Time Limit Exceeded

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

v=[i if i<x else x for i in a]
b=[v[i]+v[i+1]<=x for i in range(n-1)]     

while not all(b):
    f=True
    for i in range(n-2):
        if not b[i] and not b[i+1]:
            v[i+1]=x-v[i]
            f=False
            break
    if f:
        for i in range(n-1):
            if not b[i]:
                v[i]=min(x-v[i-1],x-v[i+1])
                break
    b=[v[i]+v[i+1]<=x for i in range(n-1)]     
    
print(sum(a)-sum(v))

Submission status: Time Limit Exceeded

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

v=[i if i<x else x for i in a]
b=[v[i]+v[i+1]<=x for i in range(n-1)]     

while not all(b):
    for i in range(n-2):
        if not b[i] and not b[i+1]:
            v[i+1]-=1
            break
        if not b[i]:
            v[i]-=1
            break
    b=[v[i]+v[i+1]<=x for i in range(n-1)]     
    
print(sum(a)-sum(v))

Submission status: Time Limit Exceeded

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  
else:
  pass
j = 1
while j <= n-1:
  if a[j-1]+a[j]<=x:
    pass
  else:
    ans += a[j]-x
    a[j]=x
print(ans)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
v=sum(a)
for i in range(n-1):
    if k<a[i]+a[i+1]:
        while k<a[i]+a[i+1]:
            a[i+1]-=1
print(v-sum(a))

Submission status: Time Limit Exceeded

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
v=sum(a)
for i in range(n-1):
    if k<a[i]+a[i+1]:
        while k<a[i]+a[i+1]:
            a[i+1]=a[i+1]-1
print(v-sum(a))

Submission status: Time Limit Exceeded

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

count = 0;

flg = 1

def taberu():
    m1 = 0
    m2 = 0
    max_val = x
    for i in range(n-1):
        if(max_val<a[i]+a[i+1]):
            m1 = i
            m2 = i+1
            max_val = a[i]+a[i+1]

    if(a[m1]<=a[m2]):
        a[m2] = a[m2]-1
        #print "del " , m2
    else:
        a[m1] = a[m1]-1
        #print "del " , m1

    #print "max_val ", max_val
    if(max_val==x):
        return 0
    else :
        return 1
    max_val = x

while(flg):
        #print "count: ",count
        flg = taberu()
        count+=1

print count-1

Submission status: Time Limit Exceeded

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

if a[0] > x:
    a[1] += a[0]
    a[0] = x
ans = 0
flag = True
while flag:
    for i in range(N-1):
        if a[i] + a[i+1] > x and a[i+1] > 0:
            a[i+1] -= 1
            ans += 1
            flag = False
    if flag:
        break
    flag = True
print(ans)

Submission status: Time Limit Exceeded

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

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

counter = 0

for i in range(N-2):
    while diff[i] > x:
        counter += 1
        diff[i] -= 1
        diff[i+1] -= 1
if diff[N-2] > x:
    counter += diff[N-2] - x

print(counter)

Submission status: Time Limit Exceeded

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

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

ans = 0

for i in range(N-1):
    while seq[i] + seq[i+1] > x and seq[i+1] != 0:
        seq[i+1] -= 1
        ans += 1
    
    while seq[i] + seq[i+1] > x:
        seq[i] -= 1
        ans += 1

print(ans)

Submission status: Time Limit Exceeded

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+1] != 0:
        while seq[i] + seq[i+1] > x and seq[i+1] != 0:
            seq[i+1] -= 1
            ans += 1

    if seq[i] + seq[i+1] > x:
        while seq[i] + seq[i+1] > x:
            seq[i] -= 1
            ans += 1

print(ans)

Submission status: Time Limit Exceeded

Solution code:

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

def need_to_eat(a, i):
    return a[i] + a[i+1] > x


for i in range(N-1):
    while need_to_eat(a, i):
        a[i+1] -= 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()]

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

Submission status: Time Limit Exceeded

Solution code:
import math

i = input().split()
N = int(i[0])
n = int(i[1])
a = input().split()
a = list(map(int ,a))
ans=0
for i in range(0,N-1):
    while a[i]+a[i+1]>n:
        if a[i+1]!=0:
            a[i+1]-=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()]

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

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N, x = map(int, input().split())
num_list = [int(x) for x in input().split()]
count = 0
if x == 0:
  print(sum(num_list))
else:
  while True:
    mark = 0
    for i, num in enumerate(num_list[0:-1]):
      if (num_list[i] + num_list[i+1] > x):
        if num_list[i+1] != 0:
          num_list[i+1] = num_list[i+1] - 1
          count+=1
          mark = 1
    if mark == 0:
      break

  print(count)

Submission status: Time Limit Exceeded

Solution code:


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


counter = 0
for i in range(N-2):
    while a[i] + a[i+1] > x :
        if a[i+1] > 0:
            a[i+1] -=  1
        else :
            a[i] -=  1
        
        counter += 1
       
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: Time Limit Exceeded

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

Submission status: Accepted

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

Submission status: Accepted

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

res = 0
for i in range(n - 1):
    dif = max(b[i] - x, 0)
    minus = min(dif, a[i + 1])
    b[i + 1] -= minus
    b[i] -= dif
    res += dif

print(res)

Submission status: Accepted

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

# In[8]:


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


# In[9]:


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


# In[ ]:

Submission status: Accepted

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

if A[0] > X:
  ans += A[0]-X
  A[0] = X

for i in range(N-1):
  wa.append(A[i]+A[i+1])

for j in range(N-2):
  if wa[j] > X:
    ans += wa[j]-X
    wa[j+1] -= wa[j]-X

if wa[N-2] > X:
  ans += wa[N-2]-X

print(ans)

Submission status: Accepted

Solution code:
import sys

N, X = map(int, input().split())
A = list(map(int, sys.stdin.readline().rsplit()))
B = A[::]

for i in range(N - 1):
    a = A[i] + A[i + 1]
    if a > X:
        if A[i + 1] >= a - X:
            A[i + 1] -= a - X
        else:
            A[i] -= a - X - A[i + 1]
            A[i + 1] = 0

res = 0
for i in range(N):
    res += B[i] - A[i]

print(res)

Submission status: Accepted

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

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

print(cnt)
# print(al)

Submission status: Accepted

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

if __name__ == "__main__":
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# coding: utf-8
n, x = map(int,input().split())
A = list(map(int,input().split()))
S = sum(A)
for i in range(n-1):
    s = A[i+1] + A[i]
    if s > x:
        if A[i+1] >= s-x:
            A[i+1] -= (s-x)
        else:
            t = s-x-A[i+1]
            A[i+1] = 0
            A[i] -= t
print(S - sum(A))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, x, *A = map(int, open(0).read().split())
ans = 0
for i in range(1, N):
    required = (A[i] + A[i - 1]) - x
    if required <= 0:
        continue
    elif required <= A[i]:
        A[i] -= required
    else:
        A[i] = 0
        rem = required - A[i]
        A[i - 1] -= rem
    ans += required

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, x = map(int, input().split())
A = tuple(map(int, input().split()))
pre = max(0, A[0] - x)
ans = pre

for i in range(n - 1):
    a = A[i]
    b = A[i + 1]
    a -= pre
    tmp = 0
    if b > x:
        tmp += b - x
        b = x

    if a + b > x:
        tmp += a + b - x
    ans += tmp
    pre = tmp
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):
    if A[i]+A[i-1]>x:
        d = A[i]+A[i-1]-x
        if A[i]>=d:
            A[i] -= d
            cnt += d
        else:
            A[i]=0
            cnt += A[i]
            d -= A[i]
            A[i-1] -= d
            cnt += d
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(n-1):
    y =  A[i]+A[i+1]
    if y > x:
        r = y-x
        if A[i+1] >= r:
            A[i+1] -= r
            ans += r
        else:
            A[i+1] = 0
            A[i] -= (r-A[i+1])
            ans += r
print(ans)

Submission status: Accepted

Solution code:
n, x = 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:
        y = a[i] + a[i+1] - x
        count += y
        a[i+1] = max(0, a[i+1]-y)
print(count)

Submission status: Accepted

Solution code:
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(1, N):
    if A[i]+A[i-1] > x:
        ans += A[i]+A[i-1]-x
        A[i] -= A[i]+A[i-1]-x
 
print(ans)

Submission status: Accepted

Solution code:
import math
from math import gcd,pi,sqrt
INF = float("inf")
MOD = 10**9 + 7

import sys
sys.setrecursionlimit(10**6)
import itertools
import bisect
from collections import Counter,deque
def i_input(): return int(input())
def i_map(): return map(int, input().split())
def i_list(): return list(i_map())
def i_row(N): return [i_input() for _ in range(N)]
def i_row_list(N): return [i_list() for _ in range(N)]
def s_input(): return input()
def s_map(): return input().split()
def s_list(): return list(s_map())
def s_row(N): return [s_input for _ in range(N)]
def s_row_str(N): return [s_list() for _ in range(N)]
def s_row_list(N): return [list(s_input()) for _ in range(N)]


def main():
    N, X = i_map()
    A = i_list()

    ans = [A[0]]
    cnt = 0

    for i,k in enumerate(A[1:]):
        t = k + ans[i]
        if t <= X:
            ans.append(k)
        else:
            if k - (t-X) >= 0:
                cnt += t - X
                ans.append(k - (t-X))
            else:
                cnt += t - X
                ans[-1] += k - (t-X)
                ans.append(0)
    print(cnt)











if __name__=="__main__":
    main()

Submission status: Accepted

Solution code:
# coding: utf-8
n, x = map(int,input().split())
A = list(map(int,input().split()))
S = sum(A)
for i in range(n-1):
    s = A[i+1] + A[i]
    if s > x:
        if s-x > A[i+1]:#後ろから全部引ける時
            t = s-x-A[i+1]
            A[i+1] = 0
            A[i] -= t
        else:#後ろから全部引くと負になるとき(前からも引く)
            A[i+1] -= s - x
print(S - sum(A))

Submission status: Accepted

Solution code:
# coding: utf-8
n, x = map(int,input().split())
A = list(map(int,input().split()))
S = sum(A)
for i in range(n-1):
    s = A[i+1] + A[i]
    if s > x:
        if s-x > A[i+1]:
            t = s-x-A[i+1]
            A[i+1] = 0
            A[i] -= t
        else:
            A[i+1] -= s - x
print(S - sum(A))

Submission status: Accepted

Solution code:
# coding: utf-8
n, x = map(int,input().split())
A = list(map(int,input().split()))
S = sum(A)
if A[0] > x:
    A[0] = x
for i in range(n-1):
    s = A[i+1] + A[i]
    if s > x:
        A[i+1] -= s - x
print(S - sum(A))

Submission status: Accepted

Solution code:
import sys
# sys.setrecursionlimit(100000)


def input():
    return sys.stdin.readline().strip()


def input_int():
    return int(input())


def input_int_list():
    return [int(i) for i in input().split()]


def main():
    # 入力
    n, x = input_int_list()
    A = [0] + input_int_list() + [0]
    ans = 0
    # 方針
    # 貪欲にi+1を減らしていく
    for i in range(n + 1):
        if A[i] + A[i + 1] > x:
            dif = min(A[i + 1], A[i] + A[i + 1] - x)
            ans += dif
            A[i + 1] -= dif

    print(ans)
    return


if __name__ == "__main__":
    main()

Submission status: Accepted

Solution code:
import copy
n, x = map(int, input().split())
A = list(map(int, input().split()))
B = copy.deepcopy(A)

if A[0] > x:
  A[0] = x
for i in range(1, len(A)):
  if A[i-1] + A[i] > x:
    A[i] = x - A[i-1]
ans = 0
for a, b in zip(A, B):
  ans += b - a
print(ans)

Submission status: Accepted

Solution code:
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(1, N):
    if A[i]+A[i-1] > x:
        ans += A[i]+A[i-1]-x
        A[i] -= A[i]+A[i-1]-x

print(ans)

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

Submission status: Accepted

Solution code:
import sys

input = sys.stdin.readline

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] -= a_list[0] - x
for i in range(len(a_list)):
    if i+1 == len(a_list):
        break
    if a_list[i] + a_list[i+1] > x:
        count += a_list[i] + a_list[i+1] - x
        a_list[i+1] -= a_list[i] + a_list[i+1] - x
        

print(count)

Submission status: Accepted

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

y = min(a[0], x)
ans = a[0]-y
for i in range(1, N):
    z = a[i]
    w = max(y+z-x, 0)
    y = z-w
    ans += w

print(ans)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
n, x = map(int, input().split())
A = list(map(int, input().split()))
cnt = [0] * (n - 1)
ans1 = 0
for i in range(n - 1):
    cnt[i] = A[i] + A[i + 1]
    cnt[i] -= x
    cnt[i] = max(cnt[i], 0)
temp_cnt = cnt[::]
# print(cnt)
for i in range(n - 1):
    if temp_cnt[i] == 0:
        continue
    if i < n - 2:
        ans1 += temp_cnt[i]
        temp_cnt[i + 1] -= min(A[i + 1], temp_cnt[i])
        temp_cnt[i] = 0
        temp_cnt[i + 1] = max(temp_cnt[i + 1], 0)
    # print(ans1)
ans1 += temp_cnt[-1]


print(ans1)

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
        ans+=diff
        a[i+1]-=min(diff,a[i+1])
print(ans)

Submission status: Accepted

Solution code:
import sys
from sys import exit
from collections import deque
from copy import deepcopy
from bisect import bisect_left, bisect_right, insort_left, insort_right
from heapq import heapify, heappop, heappush
from itertools import product, permutations, combinations, combinations_with_replacement
from functools import reduce
from math import gcd, sin, cos, tan, asin, acos, atan, degrees, radians

sys.setrecursionlimit(10**6)
INF = 10**20
eps = 1.0e-20
MOD = 10**9+7

def lcm(x,y):
    return x*y//gcd(x,y)
def lgcd(l):
    return reduce(gcd,l)
def llcm(l):
    return reduce(lcm,l)
def powmod(n,i,mod=MOD):
    return pow(n,mod-1+i,mod) if i<0 else pow(n,i,mod)
def div2(x):
    return x.bit_length()
def div10(x):
    return len(str(x))-(x==0)
def intput():
    return int(input())
def mint():
    return map(int,input().split())
def lint():
    return list(map(int,input().split()))
def ilint():
    return int(input()), list(map(int,input().split()))
def judge(x, l=['Yes', 'No']):
    print(l[0] if x else l[1])
def lprint(l, sep='\n'):
    for x in l:
        print(x, end=sep)
def ston(c, c0='a'):
    return ord(c)-ord(c0)
def ntos(x, c0='a'):
    return chr(x+ord(c0))
class counter(dict):
    def __init__(self, *args):
        super().__init__(args)
    def add(self,x,d=1):
        self.setdefault(x,0)
        self[x] += d
    def list(self):
        l = []
        for k in self:
            l.extend([k]*self[k])
        return l
class comb():
    def __init__(self, n, mod=None):
        self.l = [1]
        self.n = n
        self.mod = mod
    def get(self,k):
        l,n,mod = self.l, self.n, self.mod
        k = n-k if k>n//2 else k
        while len(l)<=k:
            i = len(l)
            l.append(l[i-1]*(n+1-i)//i if mod==None else (l[i-1]*(n+1-i)*powmod(i,-1,mod))%mod)
        return l[k]
def pf(x,mode='counter'):
    C = counter()
    p = 2
    while x>1:
        k = 0
        while x%p==0:
            x //= p
            k += 1
        if k>0:
            C.add(p,k)
        p = p+2-(p==2) if p*p<x else x
    if mode=='counter':
        return C
    S = set([1])
    for k in C:
        T = deepcopy(S)
        for x in T:
            for i in range(1,C[k]+1):
                S.add(x*(k**i))
    if mode=='set':
        return S
    if mode=='list':
        return sorted(list(S))

######################################################

N,x=mint()
A=lint()
ans=max(0,A[0]+A[1]-x)
A[1]-=min(A[1],ans)
for i in range(2,N):
    tmp=max(0,A[i-1]+A[i]-x)
    A[i]-=min(A[i],tmp)
    ans+=tmp
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(N-1):
    temp = 0
    if A[i]+A[i+1] <= X:
        continue
    else:
        temp = A[i]+A[i+1]-X
        if A[i+1] < temp:
            A[i+1] = 0
            cnt += A[i+1]
            temp -= A[i+1]
        else:
            A[i+1] -= temp
            cnt += temp
            temp -= A[i+1]

        if A[i]+A[i+1] > X:
            A[i] -= temp
            cnt += temp

print(cnt)

Submission status: Accepted

Solution code:

def resolve():
    N, X = map(int, input().split())
    A = list(map(int, input().split()))

    ans = 0
    for i in range(1, N):
        diff = max((A[i - 1] + A[i]) - X, 0)
        ans += diff
        A[i] = max(A[i] - diff, 0)

    print(ans)


if __name__ == "__main__":
    resolve()

Submission status: Accepted

Solution code:
n,x=map(int,input().split())
a=list(map(int,input().split()))
s=[0]*(n-1)
ans=0
if a[0]>x:
    ans+=a[0]-x
    a[0]=x
for i in range(n-1):
    s[i]=a[i]+a[i+1]
for i in range(n-2):
    if s[i]>x:
        t=s[i]-x
        s[i+1]-=t
        s[i]=x
        ans+=t
if s[n-2]>x:
    ans+=s[n-2]-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:
        y = a[i] + a[i+1] - x
        ans += y
        a[i+1] = max(0,a[i+1]-y)
print(ans)

Submission status: Accepted

Solution code:
# coding: utf-8
import sys
#from operator import itemgetter
sysread = sys.stdin.buffer.readline
read = sys.stdin.buffer.read
#from heapq import heappop, heappush
#from collections import defaultdict
sys.setrecursionlimit(10**7)
#import math
#from itertools import product, accumulate, combinations, product
#import bisect
#import numpy as np
#from copy import deepcopy
#from collections import deque
#from decimal import Decimal
#from numba import jit

INF = 1 << 50
EPS = 1e-8
mod = 10 ** 9 + 7

def count(arr, x):
    ans1 = 0
    pre = 0
    N = len(arr)
    for i in range(1, N):
        val = max(0, arr[i - 1] + arr[i] - pre - x)
        if val > arr[i]:
            pre = arr[i]
        else:
            pre = val
        ans1 += val
    return ans1

def run():
    N, x, *A = map(int, read().split())

    print(min(count(A, x), count(A[::-1], x)))

if __name__ == "__main__":
    run()

Submission status: Accepted

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

Submission status: Accepted

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

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

print(Sum)

Submission status: Accepted

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

Submission status: Accepted

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

if x == 0:
    print(sum(A))
    exit()

count = 0
for i in range(N - 1):
    d = A[i] + A[i + 1] - x
    if d <= 0:
        continue
    if A[i + 1] >= d:
        A[i + 1] -= d
        count += d
    else:
        count += d
        A[i] = d - A[i + 1]
        A[i + 1] = 0


print(count)

Submission status: Accepted

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

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

elif a[1] + a[0] > x and x < a[0] :
    k = 0
    cnt += a[1] + a[0] - x

else :
    k = a[1]

for i in range(1, n - 1) :
    if k + a[i + 1] <= x :
        k = a[i + 1]     

    elif x >= k :
        cnt += k + a[i + 1] - x
        k = x - k

    else :
        cnt += k + a[i + 1] - x
        k = 0
    

print(cnt)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, x = map(int, input().split())
a1 = list(map(int, input().split()))
a2 = a1[::-1]
ans1 = 0
ans2 = 0

for i in range(N-1):
    tmp = a1[i] + a1[i+1] - x
    if tmp <= 0:
        continue
    red = min(a1[i+1], tmp)
    a1[i+1] -= red
    ans1 += red
    if red < tmp:
        a1[i] -= tmp-red
        ans1 += tmp-red
    
for i in range(N-1):
    tmp = a2[i] + a2[i+1] - x
    if tmp <= 0:
        continue
    red = min(a2[i+1], tmp)
    a2[i+1] -= red
    ans2 += red
    if red < tmp:
        a2[i] -= tmp-red
        ans2 += tmp-red
print(min(ans1, ans2))

Submission status: Accepted

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

Submission status: Accepted

Solution code:
import copy

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

a=[int(x) for x in input().split()]
aa=copy.deepcopy(a)

ans=0
for i in range(n):
  if x<a[i]:
    a[i]=x

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

ans=sum(aa[i]-a[i] for i in range(n))
print(ans)

Submission status: Accepted

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

left = 0
total = 0
for i in range(N):
    cnt = max(a[i]-(x-left),0)
    total += cnt
    left = a[i]-cnt
print(total)

Submission status: Accepted

Solution code:
f=lambda:map(int,input().split())
N,X=f()
*A,=f()
A=[0]+A

c=0
for i in range(N):
 c+=max(0,A[i]+A[i+1]-X)
 A[i+1]=min(A[i+1],X-A[i])
print(c)

Submission status: Accepted

Solution code:
f=lambda:map(int,input().split())
N,X=f()
*A,=f()

B=[a if a<=X else X for a in A]
c=sum(A)-sum(B)
for i in range(N-1):
 t=B[i]+B[i+1]
 if X < t:
    d=t-X
    B[i+1]-=d
    c+=d
print(c)

Submission status: Accepted

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

B=[a if a<=X else X for a in A]
c=sum(A)-sum(B)
for i in range(N-1):
 t=B[i]+B[i+1]
 if X < t:
    d=t-X
    B[i+1]-=d
    c+=d
print(c)

Submission status: Accepted

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

B=[0]*N
for i in range(N):
    B[i] = A[i] if A[i]<=X else X

cnt=sum(A)-sum(B)    
for i in range(N-1):
    if X < B[i]+B[i+1]:
        d=B[i]+B[i+1]-X
        B[i+1]-=d
        cnt+=d
print(cnt)