Problem:


Score : 300 points

Problem Statement

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

Snuke can perform the following operation any number of times:

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

His objective is as follows:

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

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

Constraints

2 ≤ N ≤ 10^5

0 ≤ a_i ≤ 10^9

0 ≤ x ≤ 10^9

Input

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

N x
a_1 a_2 ... a_N

Output

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

Sample Input 1

3 3
2 2 2

Sample Output 1

1

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

Sample Input 2

6 1
1 6 1 2 0 4

Sample Output 2

11

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

Sample Input 3

5 9
3 1 4 1 5

Sample Output 3

0

The objective is already achieved without performing operations.

Sample Input 4

2 0
5 5

Sample Output 4

10

All the candies need to be eaten.



Sample input:
3 3
2 2 2


Sample output:
1

Submission status: Wrong Answer

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

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: Wrong Answer

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

print(cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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: Wrong Answer

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:
            cnt += t - X
            ans.append(k - (t-X))
    print(cnt)











if __name__=="__main__":
    main()

Submission status: Wrong Answer

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:
        A[i] -= s - x
print(S - sum(A))

Submission status: Wrong Answer

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:
        A[i+1] -= s - x
print(S - sum(A))

Submission status: Wrong Answer

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 = input_int_list()
    A_sort = sorted(A, reverse=True)
    idx = []
    for a in A_sort:
        idx.append(A.index(a) + 1)

    ans = 0
    A = [0] + A + [0]  # 1-indexedに変更
    # 方針
    # 値が大きいものから貪欲に減らしていく
    for i in idx:
        if A[i - 1] + A[i] > x and A[i] + A[i - 1] > x:
            dif = min(A[i], max(A[i] + A[i - 1] - x, A[i] + A[i + 1] - x))
            ans += dif
            A[i] -= dif

    print(ans)
    return


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

    # 方針
    # 1. i = 2 から i-1,i > x かつ i,i+1 > xであるiについてが キャンディを減らせないかやっていく。
    for i in range(2, n):
        if A[i - 1] + A[i] > x and A[i] + A[i - 1] > x:
            dif = min(A[i], max(A[i] + A[i - 1] - x, A[i] + A[i + 1] - x))
            ans += dif
            A[i] -= dif

    # 2. i = 1 から i-1,i > x かつ i,i+1 > xであるiについてが キャンディを減らせないかやっていく。
    for i in range(1, n):
        if A[i - 1] + A[i] > x and A[i] + A[i - 1] > x:
            dif = min(A[i], max(A[i] + A[i - 1] - x, A[i] + A[i + 1] - x))
            ans += dif
            A[i] -= dif
    print(ans)
    return


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

    # 方針
    # 1. i = 2 から i-1,i > x かつ i,i+1 > xであるiについてが キャンディを減らせないかやっていく。
    for i in range(2, n):
        if A[i - 1] + A[i] > x and A[i] + A[i - 1] > x:
            dif = min(A[i], max(A[i] + A[i - 1] - x, A[i] + A[i + 1] - x))
            ans += dif
            A[i] -= dif
    # 2. 両端について条件を満たすようにキャンディを減らす
    if A[1] + A[2] > x:
        dif = A[1] + A[2] - x
        A[1] -= dif
        ans += dif
    if A[n] + A[n - 1] > x:
        dif = A[n] + A[n - 1] - x
        A[n] -= dif
        ans += dif
    print(ans)
    return


if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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: Wrong Answer

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

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):
    ans = 0
    pre = 0
    N = len(arr)
    for i in range(1, N):
        val = max(0, arr[i - 1] + arr[i] - pre - x)
        ans += val
        pre = val
    return ans

def run():
    N, x, *A = map(int, read().split())
    ans1 = 0
    pre = 0
    print(min(count(A, x), count(A[::-1], x)))




if __name__ == "__main__":
    run()

Submission status: Wrong Answer

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
if a[N - 1] - x > 0:
    Sum += a[N - 1] - x

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

print(Sum)

Submission status: Wrong Answer

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):
    if (A[i] + A[i + 1]) > x:
        amari = (A[i] + A[i + 1]) % x
        syou = (A[i] + A[i + 1]) // x

        if A[i + 1] >= (syou - 1) * x:
            count += ((syou - 1) * x) + amari
            A[i + 1] = (A[i + 1] - ((syou - 1) * x)) - amari
        else:
            A[i + 1] = 0
            count += ((syou - 1) * x) + amari
    else:
        continue

print(count)

Submission status: Wrong Answer

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

        if A[i + 1] >= con:
            count += con
            A[i + 1] -= con
        else:
            A[i + 1] = 0
            count += con
    else:
        continue

print(count)

Submission status: Wrong Answer

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

for i,v in enumerate(a):
    if i != n-1:
        sumNeighbor.append(a[i]+a[i+1])

count = 0

for i,v in enumerate(sumNeighbor):
    if i != n-2:
        if v > x:
            sumNeighbor[i] = x
            sumNeighbor[i+1] = sumNeighbor[i+1] - (v-x)
            count += v-x
    else:
        if v > x:
            sumNeighbor[i] = x
            count += v-x

print(count)

Submission status: Wrong Answer

Solution code:
N, x = map(int, input().split())
a = list(map(int, input().split()))
ac = a.copy()
ans1 = 0
ans2 = 0
for i in range(N-1):
    tmp = max(0, a[i]+a[i+1]-x)
    ans1 += tmp
    a[i+1] -= tmp
ac = ac[::-1]
for i in range(N-1):
    tmp = max(0, ac[i]+ac[i+1]-x)
    ans2 += tmp
    ac[i+1] -= tmp

print(min(ans1, ans2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

d = [a[i] + a[i+1] - x for i in range(n-1)]

ans = 0
if n > 2:
    if d[0] > a[1]:
        ans += d[0] - a[1]
        a[0] -= d[0] - a[1]
    for i in range(1, n-1):
        if d[i] <= 0: continue
        delta = min(a[i], d[i-1])
        d[i] -= max(0, delta)
        ans += max(0, delta)
        a[i] -= max(0, delta)
    ans += max(d[n-2], 0)
    a[n-1] -= max(d[n-2], 0) 
    
else: ans = max(d[0], 0)

print(ans)

Submission status: Wrong Answer

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

d = [a[i] + a[i+1] - x for i in range(n-1)]

ans = 0
if n > 2:
    if d[0] > a[1]:
        ans += d[0] - a[1]
        a[0] -= d[0] - a[1]
    for i in range(1, n-1):
        if d[i] <= 0: continue
        delta = min(a[i], d[i-1])
        d[i] -= delta
        ans += delta
        a[i] -= delta
    ans += max(d[n-2], 0)
    a[n-1] -= max(d[n-2], 0) 
    
else: ans = max(d[0], 0)
print(ans)

Submission status: Wrong Answer

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

d = [a[i] + a[i+1] - x for i in range(n-1)]

ans = 0
if n > 2:
    if d[0] > a[1]: ans += d[0] - a[1]
    for i in range(1, n-1):
        if d[i] <= 0: continue
        delta = min(a[i], d[i-1])
        d[i] -= delta
        ans += delta
    ans += max(d[n-2], 0)
else: ans = d[0]

print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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] -= ans
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: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
from copy import deepcopy
def input(): return sys.stdin.readline().strip()

def main():
    N, x = map(int, input().split())
    A = list(map(int, input().split()))
    B = deepcopy(A)
    s = sum(A)
    ans1 = 0
    for i in range(1, N):
        if A[i] + A[i - 1] > x:
            ans1 += (A[i - 1] + A[i]) - x
            A[i] -= (A[i - 1] + A[i]) - x
        if A[i] < 0:
            ans1 = 10000000000
            break
    ans2 = 0
    for i in range(N - 2, -1, -1):
        if B[i] + B[i + 1] > x:
            ans2 += (B[i] + B[i + 1]) - x
            B[i] -= (B[i] + B[i + 1]) - x
        if B[i] < 0:
            ans2 = 10000000000
    ans = min(ans1, ans2)
    if ans == 10000000000:
        print(s)
    else:
        print(min(ans1, ans2))



if __name__ == "__main__":
    main()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

print(min(ans2, ans))

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
N,x = map(int,input().split())
ls = list(map(int,input().split()))
ans = 0
for i in range(N-1):
    if ls[i]+ls[i+1] <= x:
        continue
    else:
        eat = - x + ls[i] + ls[i+1]
        ls[i+1] -= eat
        ans += eat
print(ans)

Submission status: Wrong Answer

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

if N==2:
    print(max(0,A[0]+A[1]-X))
    exit()

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

alpha=max(A[0]-X,0)
beta=max(A[-1]-X,0)
print((M+alpha+beta+1)//2)

Submission status: Wrong Answer

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

if N==2:
    print(max(0,A_sum-X))
    exit()

M=2*A_sum-(A[0]+A[-1])-(N-1)*X
alpha=max(A[0]-X,0)
beta=max(A[-1]-X,0)
print(max((M+alpha+beta+1)//2,0))

Submission status: Wrong Answer

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

K=0
for i in range(1,N-1):
    P=max(A[i-1],A[i+1])+A[i]-X
    if P<0:
        P=0
    elif A[i]<P:
        P=A[i]
    A[i]-=P
    K+=P

if A[0]+A[1]>X:
    A[0]-=(A[0]+A[1])-X

if A[-1]+A[-2]>X:
    A[-1]-=(A[-1]+A[-2])-X

New_Sum=sum(A)
print(Old_Sum-New_Sum)

Submission status: Wrong Answer

Solution code:
from sys import stdin, stdout
from time import perf_counter

import sys
sys.setrecursionlimit(10**9)
mod = 10**9+7

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

a = list(map(int, input().split()))
# print(a)

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

Submission status: Wrong Answer

Solution code:
from sys import stdin, stdout
from time import perf_counter

import sys
sys.setrecursionlimit(10**9)
mod = 10**9+7

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

a = list(map(int, input().split()))
# print(a)

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

Submission status: Wrong Answer

Solution code:
from sys import stdin, stdout
from time import perf_counter

import sys
sys.setrecursionlimit(10**9)
mod = 10**9+7

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

a = list(map(int, input().split()))
# print(a)

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

Submission status: Wrong Answer

Solution code:
from sys import stdin, stdout
from time import perf_counter

import sys
sys.setrecursionlimit(10**9)
mod = 10**9+7

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

a = list(map(int, input().split()))
# print(a)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
l=list(map(int,input().split()))
if k==0:
  print(sum(l))
else:
  c=0
  for i in range(1,n):
    if l[i]+l[i-1]>k:
      d=abs(k-l[i-1])
      if d<=0:
        c=c+l[i]
        l[i]=0
      else:
        c=c+abs(l[i]-d)
        l[i]=d
    print(c)

Submission status: Wrong Answer

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[0]-x
print(ans)

Submission status: Wrong Answer

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:
    dp[j+1]-=dp[j]-x
    ans+=dp[j]-x
if dp[n-2]>x:
  ans+=dp[n-2]-x
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

cnt = 0
for i in range(1, N):
    eat = max(a[i - 1] + a[i] - x, 0)
    cnt += eat
    a[i] -= max(eat,0)

print(cnt)

Submission status: Wrong Answer

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

cnt1 = 0
for i in range(1, N):
    eat = max(a[i - 1] + a[i] - x, 0)
    cnt1 += eat
    a[i] -= eat

cnt2 = 0
for i in range(N - 2, -1, -1):
    eat = max(a2[i - 1] + a2[i] - x, 0)
    cnt2 += eat
    a2[i] -= eat


print(min(cnt1, cnt2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

print(cnt)

Submission status: Wrong Answer

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

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

print(cnt)

Submission status: Wrong Answer

Solution code:
from copy import deepcopy

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

a1,a2=deepcopy(a),deepcopy(a)[::-1]

def solve(a):
  a_original = tuple(a)

  if a[0]+a[1]>x and a[0] > a[1]:
    d1 = a[0]-a[1]
    d2 = a[0]+a[1]-x
    if d1 >= d2: a[0] -= d2
    else:
      a[0] = a[1]
      d2 -= d1
      a[0] -= d2//2
      a[1] -= d2 - d2//2

  for i in range(N-1):
    if a[i]+a[i+1]>x:
      d1 = abs(a[i]-a[i+1])
      d2 =  a[i]+a[i+1]-x
      if d1 >= d2:
        if a[i] >= a[i+1]:
          a[i] -= d2
        else:
          a[i+1]-= d2
      else:
        a[i] = a[i+1] = min(a[i],a[i+1])
        d2 -= d1
        a[i] -= d2//2
        a[i+1] -= d2 - d2//2

  ans = 0
  for i in range(N):
    ans += abs(a_original[i]-a[i])

  return ans

print(min(solve(a1),solve(a2)))

Submission status: Wrong Answer

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

if a[0]+a[1]>x and a[0] > a[1]:
  d1 = a[0]-a[1]
  d2 = a[0]+a[1]-x
  if d1 >= d2: a[0] -= d2
  else:
    a[0] = a[1]
    d2 -= d1
    a[0] -= d2//2
    a[1] -= d2 - d2//2
    
for i in range(N-1):
  if a[i]+a[i+1]>x:
    d1 = abs(a[i]-a[i+1])
    d2 =  a[i]+a[i+1]-x
    if d1 >= d2:
      if a[i] >= a[i+1]:
        a[i] -= d2
      else:
        a[i+1]-= d2
    else:
      a[i] = a[i+1] = min(a[i],a[i+1])
      d2 -= d1
      a[i] -= d2//2
      a[i+1] -= d2 - d2//2
    
ans = 0
for i in range(N):
  ans += abs(a_original[i]-a[i])
  
print(ans)

Submission status: Wrong Answer

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:N-1]:
    ans += i
m = math.floor((N-1)/2)
if L[0] > x:
    ans += L[0] - x
if L[N-1] > x and (N-1)%2 == 0:
    ans += L[N-1] - x
ans -= m * x
if ans < 0:
    ans = 0
print(ans)

Submission status: Wrong Answer

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:N-1]:
    ans += i
m = math.floor((N-1)/2)
if L[0] > x:
    ans += L[0] - x
if L[N-1] > x:
    ans += L[N-1] - x
ans -= m * x
if ans < 0:
    ans = 0
print(ans)

Submission status: Wrong Answer

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:]:
    ans += i
m = math.floor((N-1)/2)
if L[0] > x:
    ans += L[0] - x
ans -= m * x
if ans < 0:
    ans = 0
print(ans)

Submission status: Wrong Answer

Solution code:
N,x = map(int,input().split())
A1 = list(map(int,input().split()))
cnt = 0
A = A1[:]
for i in range(N):
    if A[i]>x:
        cnt += A[i]-x
        A[i] = x
cnt0 = 0
for i in range(1,N,2):
    if A[i]+A[i-1]>x:
        cnt0 += A[i]+A[i-1]-x
        A[i] -= A[i]+A[i-1]-x
    if i+1<N and A[i]+A[i+1]>x:
        cnt0 += A[i]+A[i+1]-x
        A[i] -= A[i]+A[i+1]-x
cnt1 = 0
A = A1[:]
for i in range(0,N,2):
    if i-1>=0 and A[i]+A[i-1]>x:
        cnt1 += A[i]+A[i-1]-x
        A[i] -= A[i]+A[i-1]-x
    if i+1<N and A[i]+A[i+1]>x:
        cnt1 += A[i]+A[i+1]-x
        A[i] -= A[i]+A[i+1]-x
print(cnt + min(cnt0,cnt1))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,x,*A = map(int, open(0).read().split())

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

print(ans)

Submission status: Wrong Answer

Solution code:
N,x,*A = map(int, open(0).read().split())

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

print(ans)

Submission status: Wrong Answer

Solution code:
#from statistics import median
#import collections
#aa = collections.Counter(a) # list to list || .most_common(2)で最大の2個とりだせるお a[0][0]
from fractions import gcd
from itertools import combinations,permutations,accumulate, product # (string,3) 3回
#from collections import deque
from collections import deque,defaultdict,Counter
import decimal
import re
#import bisect
#
#    d = m - k[i] - k[j]
#    if kk[bisect.bisect_right(kk,d) - 1] == d:
#
#
#
# pythonで無理なときは、pypyでやると正解するかも！！
#
#
# my_round_int = lambda x:np.round((x*2 + 1)//2)
# 四捨五入
import sys
sys.setrecursionlimit(10000000)
mod = 10**9 + 7
#mod = 9982443453
def readInts():
  return list(map(int,input().split()))
def I():
  return int(input())
N,x = readInts()
A = readInts()
i = 0
a = 0
cnt = 0
while i < N:
    if i == 0:
        a += A[i]
        if a > x:
            cnt += a-x
            a -= a-x
    else:
        a += A[i]
        if a > x:
            cnt += a-x
            A[i] -= a-x
            a -= a-x
        a -= A[i-1]
    i += 1
print(cnt)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
L = list(map(int, input().split()))

count = 0

for i in range (1, N):
	if L[i]+L[i-1] > M:
		count+= (L[i]+L[i-1])-M
		L[i]-= (L[i]+L[i-1])-M
    
print(count)

Submission status: Wrong Answer

Solution code:
N, M = map(int, input().split())
L = list(map(int, input().split()))

for i in range (0, N-1):
	L[i]+=L[i+1]

D = L[0:N-1]

count = 0

for i in range (0, len(D)-1):
	if D[i] > M:
		count+= D[i]-M
		D[i+1]-=(D[i]-M)
        
if D[len(D)-1] > M:
	count+= D[len(D)-1]-M
    
print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

    # 隣り合う2つの和
    s = [a[i] + a[i+1] for i in range(N - 1)]
    # 隣り合う2つの和をx以下にしたもの
    s_after = [min(i, x) for i in s]

    # 隣り合う2つの和をx以下にしたものから元の数列を復元
    a_after = [0] * N
    a_after[0] = min(a[0], s_after[0])
    for i in range(N - 1):
        a_after[i+1] = s_after[i] - a_after[i]

    ans = sum(a) - sum(a_after)
    print(ans)

if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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