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:
from copy import deepcopy
N, x = map(int, input().split())
a = list(map(int, input().split()))
b = deepcopy(a)
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] -= a[i] + a[i + 1] - x
d = 0
for i in range(N - 1, 0, - 1):
    if b[i] + b[i - 1] > x:
        d += b[i] + b[i - 1] - x
        b[i - 1] -= b[i] + b[i - 1] - x
print(min(c, d))

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

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] + a[i-1]
    if s > x:
        ans += s - x
        a[i] -= s - x
        if s > x:
            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()))
a2 = a.copy()
L = []
for j in range(2):
    if j == 1:
        a = a2[::-1]
    ans = 0
    for i in range(1, n):
        s = a[i] + a[i-1]
        if s > x:
            ans += s - x
            a[i] -= s - x
    L.append(ans)
print(min(L))

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

Submission status: Wrong Answer

Solution code:
candi_num, can_exist_num = list(map(int, input().split()))
A = list(map(int, input().split()))

ans = 0
for i in range(1, candi_num):
  two_sum = A[i - 1] + A[i]
  ofs = max(0, two_sum - can_exist_num)
  A[i] -= ofs
  ans += ofs

print(ans)

Submission status: Wrong Answer

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

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

print(result, a)

Submission status: Wrong Answer

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

count = 0

for i, c in enumerate(a):
	try:
		j = a[i + 1]
	except IndexError:
		break
	while (a[i + 1] + c) > x:
		if (a[i + 1] + c) == x:
			break
		count += abs(x - (a[i + 1] + c))
		a[i + 1] -= abs(x - (a[i + 1] + c))
	else:
		continue

print(count)

Submission status: Wrong Answer

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

ans = 0
i = 1
while i < N - 1:
    if a[i - 1] == 0:
        i += 1
        continue

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

ans += max(sum(a[-2:]) - x, 0)
print(ans)

Submission status: Wrong Answer

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

if N == 2:
    print(max(sum(a) - x, 0))
    exit()

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

print(ans)

Submission status: Wrong Answer

Solution code:
N,x = map(int,input().split())
a = list(map(int,input().split()))
A = [a[i]+a[i+1] for i in range(N-1)]
ans = 0
stop = 0
if x == 0:
  print(sum(a))
  exit()
for i in range(N-1):
    if i == 0 and A[i] > x:
        for j in range(i,N-1):
            if A[j] <= x:
                stop = j
                break
            if j == N-2:
                stop = j
                break
        for j in range(i,stop):
            if A[j] <= x:
                continue
            if A[j]-x > a[j+1]:
                d = x
                ans += d
                A[j] -= d
                A[j+1] -= a[j+1]
            else:
                d = A[j]-x
                ans += d
                A[j] -= d
                A[j+1] -= d
        if A[stop] > x:
          ans += A[stop]-x
          A[stop] = x
    elif A[i] > x and A[i-1] <= x:
        for j in range(i,N-1):
            if A[j] <= x:
                stop = j
                break
            if j == N-2:
                stop = j
                break
        for j in range(i,stop):
            if A[j] <= x:
                continue
            if A[j]-x > a[j+1]:
                d = x
                ans += d
                A[j] -= d
                A[j+1] -= a[j+1]
            else:
                d = A[j]-x
                ans += d
                A[j] -= d
                A[j+1] -= d
        if A[stop] > x:
          ans += A[stop]-x
          A[stop] = x
print(ans)

Submission status: Wrong Answer

Solution code:
N,x = map(int,input().split())
a = list(map(int,input().split()))
A = [a[i]+a[i+1] for i in range(N-1)]
ans = 0
stop = 0
for i in range(N-1):
    if i == 0 and A[i] > x:
        for j in range(i,N-1):
            if A[j] <= x:
                stop = j
                break
            if j == N-2:
                stop = j
                break
        for j in range(i,stop):
            if A[j] <= x:
                continue
            if A[j]-x > a[j+1]:
                d = x
                ans += d
                A[j] -= d
                A[j+1] -= a[j+1]
            else:
                d = A[j]-x
                ans += d
                A[j] -= d
                A[j+1] -= d
        if A[stop] > x:
          ans += A[stop]-x
          A[stop] = x
    elif A[i] > x and A[i-1] <= x:
        for j in range(i,N-1):
            if A[j] <= x:
                stop = j
                break
            if j == N-2:
                stop = j
                break
        for j in range(i,stop):
            if A[j] <= x:
                continue
            if A[j]-x > a[j+1]:
                d = x
                ans += d
                A[j] -= d
                A[j+1] -= a[j+1]
            else:
                d = A[j]-x
                ans += d
                A[j] -= d
                A[j+1] -= d
        if A[stop] > x:
          ans += A[stop]-x
          A[stop] = x
print(ans)

Submission status: Wrong Answer

Solution code:
N,x = map(int,input().split())
a = list(map(int,input().split()))
A = [a[i]+a[i+1] for i in range(N-1)]
ans = 0
stop = 0
for i in range(N-1):
    if i == 0 and A[i] > x:
        for j in range(i,N-1):
            if A[j] <= x:
                stop = j
                break
            if j == N-2:
                stop = j
                break
        for j in range(i,stop):
            if A[j] <= x:
                continue
            if A[j]-x > a[j+1]:
                d = A[j]-a[j+1]
            else:
                d = A[j]-x
            ans += d
            A[j] -= d
            A[j+1] -= d
        if A[stop] > x:
          ans += A[stop]-x
          A[stop] = x
    elif A[i] > x and A[i-1] <= x:
        for j in range(i,N-1):
            if A[j] <= x:
                stop = j
                break
            if j == N-2:
                stop = j
                break
        for j in range(i,stop):
            if A[j] <= x:
                continue
            if A[j]-x > a[j+1]:
                d = A[j]-a[j+1]
            else:
                d = A[j]-x
            ans += d
            A[j] -= d
            A[j+1] -= d
        if A[stop] > x:
          ans += A[stop]-x
          A[stop] = x
print(ans)

Submission status: Wrong Answer

Solution code:
import sys

# import re
import math
import collections
# import decimal
import bisect
import itertools
import fractions
# import functools
import copy
import heapq
import decimal
# import statistics
import queue
import numpy as np

sys.setrecursionlimit(10000001)
INF = 10 ** 16
MOD = 10 ** 9 + 7

ni = lambda: int(sys.stdin.readline())
ns = lambda: map(int, sys.stdin.readline().split())
na = lambda: list(map(int, sys.stdin.readline().split()))


# ===CODE===


def main():
    n, x = ns()
    a = na()

    ans = 0

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

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
from sys import stdin
import collections
import math


n,x   = [int(x) for x in stdin.readline().rstrip().split()]
arr = [int(x) for x in stdin.readline().rstrip().split()]

ans = 0
for i in range(0,n-1):
    s = arr[i] + arr[i+1]
    if s > x :
        d = s-x
        arr[i+1] -= s - x
        ans += d

print(ans)

Submission status: Wrong Answer

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

cnt = 0
L = [0]+L+[0]

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
print("he")

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

print(ans)

Submission status: Wrong Answer

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

ans = 0
now = a[0]
for i in a[1:]:
    tmp = max(0, now + i - x)
    now = i - tmp
    ans += tmp
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

print(result)

Submission status: Wrong Answer

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

cnt = 0
for i in range(n-1):
    total = a_list[i] + a_list[i+1]
    if total > x:
        diff = total - x
        a_list[i+1] -= diff
        cnt += diff
print(cnt)

Submission status: Wrong Answer

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

sum = [0] * (N-1)

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

count = 0

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

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

Submission status: Wrong Answer

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


ans = 0

for i in range(num - 1):
    tar = a[i] + a[i + 1]
    if (tar <= x):
        continue
    else:
        dif = tar - x
        if (a[i + 1] >= dif):
            ans += dif
            a[i + 1] -= dif

        else:
            ans += a[i + 1]

            a[i + 1] = 0
            dif -= a[i + 1]
            a[i] -= dif
            ans += dif


print(ans)

Submission status: Wrong Answer

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


ans = 0

for i in range(num - 1):
    tar = a[i] + a[i + 1]
    if (tar <= x):
        continue
    else:
        dif = tar - x
        if (a[i + 1] >= dif):
            ans += dif
            a[i+1] -= dif
        else:
            a[i + 1] = 0
            dif -= a[i + 1]
            a[i] -= dif


print(ans)

Submission status: Wrong Answer

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

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

ans = 0
for i in range(N-1):
    ans += s[i]
    try:
        s[i+1] = max(0,s[i+1]-s[i])
    except:
        pass

print(ans)

Submission status: Wrong Answer

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

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

if N == 2:
    print(max(0,s[0]-x))
    exit()

ans = 0
for i in range(N-1):
    if i == 0:
        ans += s[i]
        s[i+1] = max(0,s[i+1]-s[i])
        s[i] = 0
    elif i == N-2:
        ans += s[i]
    else:
        ans += s[i]
        s[i-1] = max(0,s[i-1]-s[i])
        s[i+1] = max(0,s[i+1]-s[i])

print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
#6問目
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 += a[i] + a[i+1] - x
        a[i+1] = x - a[i]
print(count)

Submission status: Wrong Answer

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

print(cnt)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
import sys

N, x = map(int, sys.stdin.readline().strip().split())
A = list(map(int, sys.stdin.readline().strip().split()))

ans = 0
tmp_sum = A[0]
for i in range(N-1):
    tmp_sum += A[i+1]
    # print("tmp_sum", tmp_sum)
    need = tmp_sum - x
    if need > 0:
        ans += need
        if A[i+1] >= need:
            A[i+1] -= need
            tmp_sum -= need
        else:
            need -= A[i+1]
            tmp_sum -= A[i+1]
            A[i+1] = 0
            if need > A[i]:
                # 明示的には書かれていないが、
                # 制約になっていてこのパターンはないはず
                print("something wrong")
                sys.exit()
            else:
                A[i] -= need

        # print("ans", ans)
    
    # print("A[i]", A[i])
    tmp_sum -= A[i]

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

N, x = map(int, sys.stdin.readline().strip().split())
A = list(map(int, sys.stdin.readline().strip().split()))

ans = 0
tmp_sum = A[0]
for i in range(N-1):
    tmp_sum += A[i+1]
    # print("tmp_sum", tmp_sum)
    need = tmp_sum - x
    if need > 0:
        ans += need
        if A[i+1] >= need:
            A[i+1] -= need
            tmp_sum -= need
        else:
            need -= A[i+1]
            A[i+1] = 0
            tmp_sum -= A[i+1]
            if need > A[i]:
                # 明示的には書かれていないが、
                # 制約になっていてこのパターンはないはず
                print("something wrong")
                sys.exit()
            else:
                A[i] -= need

        # print("ans", ans)
    
    # print("A[i]", A[i])
    tmp_sum -= A[i]

print(ans)

Submission status: Wrong Answer

Solution code:
import sys

N, x = map(int, sys.stdin.readline().strip().split())
A = list(map(int, sys.stdin.readline().strip().split()))

ans = 0
tmp_sum = A[0]
for i in range(N-1):
    tmp_sum += A[i+1]
    # print("tmp_sum", tmp_sum)
    need = tmp_sum - x
    if need > 0:
        ans += need
        if A[i+1] >= need:
            A[i+1] -= need
            tmp_sum -= need
        else:
            need -= A[i+1]
            A[i+1] = 0
            if need > A[i]:
                # 明示的には書かれていないが、
                # 制約になっていてこのパターンはないはず
                print("something wrong")
                sys.exit()
            else:
                A[i] -= need

        # print("ans", ans)
    
    # print("A[i]", A[i])
    tmp_sum -= A[i]

print(ans)

Submission status: Wrong Answer

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

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

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

print(ans)

Submission status: Wrong Answer

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

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

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

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def main():
    n, x, *a = map(int, open(0).read().split())

    ans = 0
    tmp = 0
    for i, j in zip(a, a[1:]):
        i -= tmp
        if i + j > x:
            c = (i + j) - x
            ans += 1
            tmp = c
        else:
            tmp = 0

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    n, x, *a = map(int, open(0).read().split())

    ans = 0
    tmp = 0
    for i, j in zip(a, a[1:]):
        i -= tmp
        if i + j > x:
            c = (i + j) - x
            ans += c
            tmp = c
        else:
            tmp = 0

    print(ans)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def getN():
    return int(input())
def getNM():
    return map(int, input().split())
def getList():
    return list(map(int, input().split()))
def getArray(intn):
    return [int(input()) for i in range(intn)]

from collections import defaultdict, deque
from sys import exit
import heapq
import math
import copy
from bisect import bisect_left, bisect_right

import sys
sys.setrecursionlimit(1000000000)

N, X = getNM()
A = getList()
between = [A[i] + A[i + 1] - X for i in range(N - 1)]
ans = 0
flag = True
while flag:
    flag = False
    for i in range(N - 2):
        if between[i] > 0 and between[i + 1] > 0:
            minuspoint = min(between[i], between[i + 1], A[i + 1])
            A[i + 1] -= minuspoint
            ans += minuspoint
            flag = True
    between = [A[i] + A[i + 1] - X for i in range(N - 1)]
for i in between:
    if i > 0:
        ans += i
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,x = map(int,input().split())
a=list(map(int,input().split()))
cnt=0
b=[0]*N

#0,2
b[0]=a[0] if a[0]<=x else x
cnt+= a[0]-b[0]

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

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-1]+A[i] <= x:
        continue
    eat = A[i-1]+A[i] - x
    ans += eat
    A[i] = A[i]-eat

print(ans)

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:
n,x = map(int,input().split())
a = [int(i) for i in input().split()]
ans = 0
for i in range(n-1):
    if a[i] + a[i+1] <= x: continue
    a[i+1] = max(0,a[i+1]-x)
    ans += 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()))
B = []
ans1 = 0
ans2 = 0
for i in range(1, N):
    B.append(A[i] + A[i-1])
B_ = B.copy()
for i in range(N-1):
    s = B[i] - x
    if s > 0:
        B[i] -= s
        ans1 += s
        if i + 1 < N - 1:
            B[i+1] -= s
B = B_[::-1]
for i in range(N-1):
    s = B[i] - x
    if s > 0:
        B[i] -= s
        ans2 += s
        if i + 1 < N - 1:
            B[i+1] -= s

print(min(ans1, ans2))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, x = map(int, input().split())
A = list(map(int, input().split()))
ans = 0
i = 0
while i < N-1:
  if A[i]+A[i+1] > x:
    d = A[i]+A[i+1]-x
    ans += d
    A[i+1] -= d
  i += 1
print(ans)

Submission status: Wrong Answer

Solution code:
N, x = map(int, input().split())
lst = [int(x) for x in input().split()]
ret = max(lst[0] + lst[1] - x, 0)
for i in range(2, N):
    a = max(lst[i] - x, 0)
    if a == 0 and lst[i] == 1:
        ret += 1
    else:
        ret += a
print(ret)

Submission status: Wrong Answer

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

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:
        tmp = (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 = list(map(int, input().split()))

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
def solve(n, x, a_list):
    
    ans = 0
    for i in range(len(a_list)-1):
        tmp = a_list[i] + a_list[i+1]
        if tmp > x:
            d = tmp - x
            ans += d
            a_list[i+1] = a_list[i+1] -d

    return ans

if __name__ == "__main__":
    n, x = [int(i) for i in input().split()]
    a_list = [int(i) for i in input().split()]
    print(solve(n, x, a_list))

Submission status: Wrong Answer

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


if '__main__' == __name__:
    resolve()

Submission status: Wrong Answer

Solution code:
n, x = [int(i) for i in input().split()]
a = [int(i) for i in input().split()]
s = [a[i] + a[i+1] for i in range(n-1)]
cnt = 0
for i in range(n-2):
    t = max(0, s[i] - x)
    s[i] -= t
    s[i+1] -= t
    cnt += t
cnt += max(0, s[-1] - x)
print(cnt)

Submission status: Wrong Answer

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