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:
def main():
    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
    print(ans)

    for i in range(n - 1):
        j = i + 1
        if j < n and A[i] + A[j] > x:
            diff = A[i] + A[j] - x
            ans += diff
            A[j] -= diff
    print(ans)


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

print(ans)

Submission status: Wrong Answer

Solution code:
N,x = map(int,input().split())
a = input().split()
a = ''.join(a)
b = a.split('0')

g = 0
c = []
for i in range(len(b)):
    c.append(list(map(int,b[i])))
for i in range(len(c)):
    z = []
    if len(c[i])%2:c[i].append(0)
    for j in range(len(c[i])-1):
        z.append(c[i][j]+c[i][j+1])
    if len(z)%2:z.append(0)
    for j in range(len(z)-1):
        if j%2:continue
        g += max(max(z[j],z[j+1])-x,0)
print(g)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

cnt = 0  
for i in range(1,N):
  if b[i] > x:
    c = b[i] - x
    if a[i+1] >= c:
      cnt += c
      if i+1 <= N-1:
        b[i+1] -= c
    else:
      cnt += c
print(cnt)

Submission status: Wrong Answer

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

b = a.copy()
cand1, cand2 = 0, 0
for num in range(n-1):
    if a[num] + a[num+1] > x:
        ate = (a[num]+a[num+1]) - x
        a[num+1] -= ate
        cand1 += ate

for num in range(n-1, 0, -1):
    if b[num] + b[num-1] > x:
        ate = (b[num]+b[num-1]) - x
        b[num-1] -= ate
        cand2 += ate
print(min(cand1, cand2))

Submission status: Wrong Answer

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

b = a.copy()
cand1, cand2 = 0, 0
for num in range(n-1):
    if a[num] + a[num+1] > x:
        ate = (a[num]+a[num+1]) - x
        a[num+1] -= ate
        cand1 += ate

for num in range(n-1):
    if b[num] + b[num-1] > x:
        ate = (b[num]+b[num-1]) - x
        b[num-1] -= ate
        cand2 += ate
print(min(cand1, cand2))

Submission status: Wrong Answer

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

cand1 = 0
ate = 0
for num in range(n-1):
    if a[num] + a[num+1] > x:
        ate = (a[num]+a[num+1]) - x
        a[num+1] -= ate
        cand1 += ate

print(cand1)

Submission status: Wrong Answer

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



if __name__ == '__main__':
    solve()

Submission status: Wrong Answer

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

cnt = 0  
for i in range(N-1,0,-1):
  if b[i] > x:
    c = b[i] - x
    cnt += c
    b[i] -= c
    b[i-1] -= c  
print(cnt)

Submission status: Wrong Answer

Solution code:
def LI(): return list(map(int,input().split()))
N,x = LI()
a = LI()
ans = 0
sum_a = []
for i in range(N-1):
    sum_a.append(a[i]+a[i+1])
sum_a.append(0)
for j in range(N-1):
    if sum_a[j]>x:
        ans += sum_a[j]-x
        sum_a[j+1] -= sum_a[j]-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(n - 1):
    if a[i] + a[i + 1] <= x:
        continue

    diff = (a[i] + a[i + 1]) - x
    ans += diff

    tmp = min(diff, a[i + 1])
    a[i + 1] -= tmp
    diff -= tmp
    a[i + 1] -= diff
      
print(ans)

Submission status: Wrong Answer

Solution code:
# Beginner 50 C
import copy

N, x = map(int, input().split())
A = list(map(int, input().split()))
B = copy.deepcopy(A)

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

cnt = sum(B) - sum(A)
print(cnt)

Submission status: Wrong Answer

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

    total = sum(a)

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

    print(total-sum(a))

Submission status: Wrong Answer

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

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

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

    print(min([ans_l, ans_r]))

Submission status: Wrong Answer

Solution code:
#Beginner 048 C
import copy

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

lst1 = copy.copy(A)
cnt1 = 0
for i in range(N-1):
    if lst1[i] + lst1[i+1] > x:
        cnt1 += lst1[i] + lst1[i+1] - x
        lst1[i] -= lst1[i] + lst1[i+1] - x

lst2 = copy.copy(A)
cnt2 = 0
for j in range(N-1):
    if lst2[j] + lst2[j+1] > x:
        cnt2 += lst2[j] + lst2[j+1] - x
        lst2[j+1] -= lst2[j] + lst2[j+1] - x

if cnt1 >= cnt2:
    print(cnt2)
else:
    print(cnt1)

Submission status: Wrong Answer

Solution code:
def resolve():
	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:
			st = (a[i] + a[i + 1]) - x
			a[i+1] = 0
			c += st
	print(c)
resolve()

Submission status: Wrong Answer

Solution code:
def resolve():
	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:
			st = (a[i] + a[i + 1]) - x
			a[i+1] -= st
			c += st
	print(c)
resolve()

Submission status: Wrong Answer

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

print(ans)

Submission status: Wrong Answer

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

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

print(ans)

Submission status: Wrong Answer

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

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

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

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

print(ans)

Submission status: Wrong Answer

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

for i in range(n-1):
    wa.append(a[i] + a[i+1])
    
ans = 0
for i in range(n-2):
    if wa[i] >= x or wa[i+1] >= x:
        a = min(max(wa[i],wa[i+1]) - x , wa[i] , wa[i+1])
        wa[i] -= a
        wa[i+1] -= a
        ans += a
        
ans += max(0,wa[-1] - x)
        
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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):
    if a[i] + a[i - 1] > x:
        # どれだけ減らさないといけないか
        v = a[i] + a[i - 1] - x
        ans += v
        if i == n - 1:
            a[n - 1] -= v
        else:
            # できるだけ次の値から減算する
            if a[i] - v >= 0:
                a[i] -= v
            else:
                # a[i+1]の項で削りきれなかった分。
                a[i-1] -= v - a[i]
                a[i] = 0
print(ans)

Submission status: Wrong Answer

Solution code:
N, x = map(int,input().split())
a = list(map(int,input().split()))
b = [a[i] + a[i+1] for i in range(N-1)]
ans1 = 0
for i in range(N-1):
    if b[i] > x:
        temp = b[i] - x
        ans1 += temp
        b[i] = x
        if i != N-2:
            b[i+1] -= temp
b = [a[i] + a[i+1] for i in range(N-1)]
b.reverse()
ans2 = 0
for i in range(N-1):
    if b[i] > x:
        temp = b[i] - x
        ans2 += temp
        b[i] = x
        if i != N-2:
            b[i+1] -= temp
print(max(ans1, ans2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def resolve():
	n, x = map(int, input().split())
	a = list(map(int, input().split()))
	dp = [0]*n
	for i in range(n):
		if i != 0:
			if (a[i] + a[i-1]) > x:
				dp[i] = a[i] + a[i-1] - x
				a[i] = a[i-1] - dp[i]
		if a[0] > x:
			dp[i] = a[0] - x
			a[0] = x
	print(sum(dp))
resolve()

Submission status: Wrong Answer

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

def SUM(a):
    L=len(a)
    ans=0
    for i in range(L):
        ans+=a[i]
    return ans

def check(a):
    if all(A[i-1]+A[i]<=x for i in range(1,N)):
        return 1
    else:
        return 0

if check(A)==1:
    print(0)
    exit()

SA=SUM(A)
count=0

for i in range(N):
    if A[i]>x:
        A[i]=x

if check(A)==1:
    print(count)
    exit()

for j in range(1,N):
    if A[j-1]+A[j]>x:
        s=A[j]+A[j-1]
        r=s-x
        A[j]=A[j]+x-s
    else:
        pass

count=SA-SUM(A)

print(count)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n, x, *a = map(int, read().split())
memo = 0
cnt = 0
for i in range(n - 1):
    v = a[i] + a[i + 1] - memo
    if v > x:
        memo = v - x
    else:
        memo = 0
    cnt += memo
print(cnt)

Submission status: Wrong Answer

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

n, x, *a = map(int, read().split())
memo = [0] * (n - 1)
for i in range(n - 1):
    memo[i] = a[i] + a[i + 1]
cnt = 0
for i in range(n - 2):
    if memo[i] > x:
        cnt += memo[i] - x
        memo[i + 1] -= memo[i] - x
if memo[-1] > x:
    cnt += memo[-1] - x
print(cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import copy
N,x = map(int,input().split())
a = list(map(int,input().split()))
b = copy.deepcopy(a)
ans = 0
ans1 = 0
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

a = b[::-1]
for i in range(N-1):
    if a[i]+a[i+1]>x:
        ans1 += (a[i]+a[i+1])-x
        a[i+1] -= (a[i]+a[i+1])-x
ans = min(ans,ans1)
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(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: 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:
		if x > a[i]:
			c += a[i+1]-x
			a[i+1] -= a[i+1]-x
		else:
			c += a[i+1]
			a[i+1] = 0
			c += a[i]-x
			a[i] -= a[i]-x
print (c)

Submission status: Wrong Answer

Solution code:
n,x = map(int,input().split())
A = list(map(int,input().split()))
B = A.copy()
ans = [0,0]
for i in range(1,n):
    if A[i] + A[i-1] > x:
        ans[0] += A[i] + A[i-1] - x
        A[i] = x - A[i-1]
    if B[n-i] + B[n-i-1] > x:
        ans[1] += B[n-i] + B[n-i-1] - x
        B[n-i-1] = x - B[n-i]
print(min(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(1,N):
    if A[i] + A[i-1] > x:
        ans += A[i] + A[i-1] - x
        A[i] = x - A[i-1]
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

sys.setrecursionlimit(10 ** 6)
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep="\n")
def II(): return int(sys.stdin.readline())
def MI(): return map(int, sys.stdin.readline().split())
def LI(): return list(map(int, sys.stdin.readline().split()))
def LLI(rows_number): return [LI() for _ in range(rows_number)]
def SI(): return sys.stdin.readline()[:-1]

def main():
    n,x=MI()
    aa=LI()
    ans=0
    for i in range(n-1):
        cur=max(aa[i]+aa[i+1]-x,0)
        ans+=cur
        aa[i+1]-=cur
    print(ans)

main()

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

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

print(ans)

Submission status: Wrong Answer

Solution code:
import collections
import sys

N, x = map(int, input().split())
A_lis = list(map(int, input().split()))
A_q = collections.deque(A_lis)

sys.setrecursionlimit(10 ** 9)

con = 0
tmp1 = A_q.popleft()
for i in range(N-1):
    tmp2 = A_q.popleft()
    if tmp1 + tmp2 > x:
        con += (tmp1 + tmp2) - x
        tmp1 = max(0, tmp1 - x)
    else:
        tmp1 = tmp2

print(con)

Submission status: Wrong Answer

Solution code:
import collections

N, x = map(int, input().split())
A_lis = list(map(int, input().split()))
A_q = collections.deque(A_lis)

con = 0
tmp1 = A_q.popleft()
for i in range(N-1):
    tmp2 = A_q.popleft()
    if tmp1 + tmp2 > x:
        con += (tmp1 + tmp2) - x
        tmp1 = max(0, tmp1 - x)
    else:
        tmp1 = tmp2

print(con)

Submission status: Wrong Answer

Solution code:
n,k = map(int,input().split())
A = list(map(int,input().split()))
ans = 0
if n==2:
    print(A[0]+A[1]-k)
    exit()
for i in range(1, n):
    if A[i]+A[i-1]<=k:
        continue
    ans += (A[i]+A[i-1])-k
    A[i] = k-A[i-1]
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from fractions import gcd
from math import factorial, sqrt
from functools import lru_cache, reduce
INF = 1 << 60
mod = 1000000007
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

# UnionFind
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# ダイクストラ
def dijkstra_heap(s, edge, n):
    #始点sから各頂点への最短距離
    d = [10**20] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d

# 素因数分解
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

# 2数の最小公倍数
def lcm(x, y):
    return (x * y) // gcd(x, y)

# リストの要素の最小公倍数
def lcm_list(numbers):
    return reduce(lcm, numbers, 1)

# リストの要素の最大公約数
def gcd_list(numbers):
    return reduce(gcd, numbers)

# ここから書き始める
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:
        continue
    ans += a[i] + a[i - 1] - x
    # if a[i] > x:
    #     a[i] -= a[i] + a[i - 1] - x
    #     a[i] = max(a[i], 0)
    # elif a[i] + a[i - 1] > x:
    #     a[i] = 0
    if a[i] <= x:
        a[i] = 0
    else:
        a[i] = a[i - 1] + a[i] - x
print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from fractions import gcd
from math import factorial, sqrt
from functools import lru_cache, reduce
INF = 1 << 60
mod = 1000000007
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

# UnionFind
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# ダイクストラ
def dijkstra_heap(s, edge, n):
    #始点sから各頂点への最短距離
    d = [10**20] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d

# 素因数分解
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

# 2数の最小公倍数
def lcm(x, y):
    return (x * y) // gcd(x, y)

# リストの要素の最小公倍数
def lcm_list(numbers):
    return reduce(lcm, numbers, 1)

# リストの要素の最大公約数
def gcd_list(numbers):
    return reduce(gcd, numbers)

# ここから書き始める
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:
        continue
    ans += a[i] + a[i - 1] - x
    # if a[i] > x:
    #     a[i] -= a[i] + a[i - 1] - x
    #     a[i] = max(a[i], 0)
    # elif a[i] + a[i - 1] > x:
    #     a[i] = 0
    if a[i] <= x:
        a[i] = 0
    else:
        a[i] = max(0, x - a[i - 1])
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys
import heapq
import re
from itertools import permutations
from bisect import bisect_left, bisect_right
from collections import Counter, deque
from fractions import gcd
from math import factorial, sqrt
from functools import lru_cache, reduce
INF = 1 << 60
mod = 1000000007
sys.setrecursionlimit(10 ** 7)
input = sys.stdin.readline

# UnionFind
class UnionFind():
    def __init__(self, n):
        self.n = n
        self.parents = [-1] * n

    def find(self, x):
        if self.parents[x] < 0:
            return x
        else:
            self.parents[x] = self.find(self.parents[x])
            return self.parents[x]

    def union(self, x, y):
        x = self.find(x)
        y = self.find(y)

        if x == y:
            return

        if self.parents[x] > self.parents[y]:
            x, y = y, x

        self.parents[x] += self.parents[y]
        self.parents[y] = x

    def size(self, x):
        return -self.parents[self.find(x)]

    def same(self, x, y):
        return self.find(x) == self.find(y)

    def members(self, x):
        root = self.find(x)
        return [i for i in range(self.n) if self.find(i) == root]

    def roots(self):
        return [i for i, x in enumerate(self.parents) if x < 0]

    def group_count(self):
        return len(self.roots())

    def all_group_members(self):
        return {r: self.members(r) for r in self.roots()}

    def __str__(self):
        return '\n'.join('{}: {}'.format(r, self.members(r)) for r in self.roots())

# ダイクストラ
def dijkstra_heap(s, edge, n):
    #始点sから各頂点への最短距離
    d = [10**20] * n
    used = [True] * n #True:未確定
    d[s] = 0
    used[s] = False
    edgelist = []
    for a,b in edge[s]:
        heapq.heappush(edgelist,a*(10**6)+b)
    while len(edgelist):
        minedge = heapq.heappop(edgelist)
        #まだ使われてない頂点の中から最小の距離のものを探す
        if not used[minedge%(10**6)]:
            continue
        v = minedge%(10**6)
        d[v] = minedge//(10**6)
        used[v] = False
        for e in edge[v]:
            if used[e[1]]:
                heapq.heappush(edgelist,(e[0]+d[v])*(10**6)+e[1])
    return d

# 素因数分解
def factorization(n):
    arr = []
    temp = n
    for i in range(2, int(-(-n**0.5//1))+1):
        if temp%i==0:
            cnt=0
            while temp%i==0:
                cnt+=1
                temp //= i
            arr.append([i, cnt])

    if temp!=1:
        arr.append([temp, 1])

    if arr==[]:
        arr.append([n, 1])

    return arr

# 2数の最小公倍数
def lcm(x, y):
    return (x * y) // gcd(x, y)

# リストの要素の最小公倍数
def lcm_list(numbers):
    return reduce(lcm, numbers, 1)

# リストの要素の最大公約数
def gcd_list(numbers):
    return reduce(gcd, numbers)

# ここから書き始める
n, x = map(int, input().split())
a = list(map(int, input().split()))
ans = 0
for i in range(1, n):
    if a[i] > x:
        ans += a[i] + a[i - 1] - x
        a[i] -= a[i] + a[i - 1] - x
        a[i] = max(a[i], 0)
    elif a[i] + a[i - 1] > x:
        ans += (a[i] + a[i - 1]) - x
        a[i] = 0
print(ans)