

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

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

B = A[::-1]



ansA = 0

ansB = 0



for i in range(N-1):

  if A[i] > x:

    ansA += A[i] - x

    A[i] -= A[i] - x

  

  s = A[i] + A[i+1]

  if s > x:

    ansA += s - x

    A[i+1] -= s - x



for i in range(N-1):

  if B[i] > x:

    ansB += B[i] - x

    B[i] -= B[i] - x

  

  s = B[i] + B[i+1]

  if s > x:

    ansB += s - x

    B[i+1] -= s - x

  

print((min(ansA, ansB)))


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

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

B = A[::-1]



ansA = 0

ansB = 0



for i in range(N-1):

  if A[i] > x:

    ansA += A[i] - x

    A[i] -= A[i] - x

  

  s = A[i] + A[i+1]

  if s > x:

    ansA += s - x

    A[i+1] -= s - x



print(ansA)
N, x = list(map(int, input().split()))
A = list(map(int, input().split()))
B = A[::-1]
ansA = 0
ansB = 0
for i in range(N - 1):
    if A[i] > x:
        ansA += A[i] - x
        A[i] -= A[i] - x
    s = A[i] + A[i + 1]
    if s > x:
        ansA += s - x
        A[i + 1] -= s - x
for i in range(N - 1):
    if B[i] > x:
        ansB += B[i] - x
        B[i] -= B[i] - x
    s = B[i] + B[i + 1]
    if s > x:
        ansB += s - x
        B[i + 1] -= s - x
print((min(ansA, ansB)))

N, x = list(map(int, input().split()))
A = list(map(int, input().split()))
B = A[::-1]
ansA = 0
ansB = 0
for i in range(N - 1):
    if A[i] > x:
        ansA += A[i] - x
        A[i] -= A[i] - x
    s = A[i] + A[i + 1]
    if s > x:
        ansA += s - x
        A[i + 1] -= s - x
print(ansA)

-for i in range(N - 1): -    if B[i] > x: -        ansB += B[i] - x -        B[i] -= B[i] - x -    s = B[i] + B[i + 1] -    if s > x: -        ansB += s - x -        B[i + 1] -= s - x -print((min(ansA, ansB))) +print(ansA)