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

Submission status: Wrong Answer

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

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):
    if a[i]>x:
        count+=a[i]-x
        a[i]=x
count1=count
a1=a.copy()
for i in range(0, N, 2):
    ai=a1[i]
    if i==0:
        a1[i]=min(ai, x-a1[i+1])
    elif i==N-1:
        a1[i]=min(ai, x-a1[i-1])
    else:
        a1[i]=min(x-a1[i-1], x-a1[i+1], ai)
    count1+=ai-a1[i]

count2=count
for i in range(1, N, 2):
    ai=a[i]
    if i==0:
        a[i]=min(ai, x-a[i+1])
    elif i==N-1:
        a[i]=min(x-a[i-1], ai)
    else:
        a[i]=min(x-a[i-1], x-a[i+1], ai)
    count2+=ai-a[i]
print(min(count1, count2))

Submission status: Wrong Answer

Solution code:
N, x = map(int, input().split())
a=list(map(int, input().split()))
count1=0
a1=a.copy()
for i in range(0, N, 2):
    ai=a1[i]
    if i==0:
        a1[i]=min(ai, x-a1[i+1])
    elif i==N-1:
        a1[i]=min(ai, x-a1[i-1])
    else:
        a1[i]=min(x-a1[i-1], x-a1[i+1], ai)
    count1+=ai-a1[i]
count2=0
for i in range(1, N, 2):
    ai=a[i]
    if i==0:
        a[i]=min(ai, x-a[i+1])
    elif i==N-1:
        a[i]=min(x-a[i-1], ai)
    else:
        a[i]=min(x-a[i-1], x-a[i+1], ai)
    count2+=ai-a[i]
print(min(count1, count2))

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):
  count+=max(0,a[i]+a[i+1]-x)
  a[i+1]-=max(0,a[i]+a[i+1]-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, a in enumerate(A[:-1]):
    if A[i] + A[i+1] >= x:
        ans += A[i+1] - (x - A[i])
        A[i+1] = x - A[i]

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
f=lambda:map(int,input().split())
n,x=f()
t,*l=f()
a=0
for i in l:
  t=min(i,x-t)
  a+=i-t
print(a)

Submission status: Wrong Answer

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

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

print(res)

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(A[i+1] - (A[i] + A[i+1] - x), 0)

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] -= max(A[i+1] - (A[i] + A[i+1] - x), A[i+1])

print(ans)

Submission status: Wrong Answer

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

y = 0
for i in range(n-1):
    if l[i] + l[i+1] > x:
        z = l[i+1] + l[i] - x
        y += z
        l[i+1] -= z

print(y)

Submission status: Wrong Answer

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

ans = 0

for i in range(n-1):
    # greedy
    if a[i] + a[i+1] > x:
        cost = ((a[i] + a[i+1]) - x)
        a[i+1] -= cost
        ans += cost

print(ans)

Submission status: Wrong Answer

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

if len(A) % 2 == 0:
    A = A + [0]
# print('A', A)

ans = 0
for i in range(1, N, 2):
    total = A[i - 1] + A[i]
    diff = total - x
    if diff <= 0:
        continue
    v1 = min(A[i], diff)
    v2 = diff - v1
    A[i] -= v1
    A[i - 1] -= v2
    ans += v1 + v2
    # print('A', A)
print(ans)

Submission status: Wrong Answer

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

if len(A) % 2 == 0:
    A = A + [0]

ans = 0
for i in range(1, N, 2):
    v = max(0, max(A[i - 1] + A[i] - x, A[i] + A[i + 1] - x))
    A[i] -= v
    ans += v
# print('A', A)
print(ans)

Submission status: Wrong Answer

Solution code:
from copy import deepcopy
import numpy as np
N, x = map(int, input().split())
A = np.array(list(map(int, input().split())))

ans = 0
if len(A) % 2 == 1:
    for i in range(1, N, 2):
        v = max(0, max(A[i - 1] + A[i] - x, A[i] + A[i + 1] - x))
        A[i] -= v
        ans += v
else:
    A2 = deepcopy(A)
    ans1, ans2 = 0, 0
    for i in range(1, N - 1, 2):
        v = max(0, max(A[i - 1] + A[i] - x, A[i] + A[i + 1] - x))
        A[i] -= v
        ans1 += v
    v = max(0, A[-2] + A[-1] - x)
    A[-1] -= v
    ans1 += v
    for i in range(0, N, 2):
        v = max(0, max(A2[i - 1] + A2[i] - x, A2[i] + A2[i + 1] - x))
        A2[i] -= v
        ans2 += v
    ans = min(ans1, ans2)

# print('A', A)
print(ans)

Submission status: Wrong Answer

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

last = lst[0]
ans = 0
for v in range(1, len(lst)):
    s = last + lst[v]
    if s <= x:
        continue
    
    e = last + lst[v] - x
    ans += e
    lst[v] -= e
    last = lst[v]

print(ans)

Submission status: Wrong Answer

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

last = lst[0]
ans = 0
for v in range(1, len(lst)):
    s = last + lst[v]
    if s < x:
        continue
    
    e = last + lst[v] - x
    ans += e
    lst[v] -= e
    last = lst[v]

print(ans)

Submission status: Wrong Answer

Solution code:
n, x = map(int,input().split())
A = list(map(int,input().split()))
sum = []
if A[0] > x:
    ans = A[0] - x
else:
    ans = 0
for i in range(n-1):
    sum.append(A[i]+A[i+1])
for j in range(n-1):
    if j != n-2:
        if sum[j] > x:
            diff = sum[j]-x
            sum[j] -= diff
            sum[j+1] = diff
            ans += diff
    else:
        if sum[j] > x:
            diff = sum[j]-x
            sum[j] -= diff
            ans += diff

print(ans)

Submission status: Wrong Answer

Solution code:
n, x = map(int,input().split())
A = list(map(int,input().split()))
sum = []
ans = 0
for i in range(n-1):
    sum.append(A[i]+A[i+1])
for j in range(n-1):
    if j != n-2:
        if sum[j] > x:
            diff = sum[j]-x
            sum[j] -= diff
            sum[j+1] = min(sum[j+1] - diff, 0)
            ans += min(diff, sum[j+1])
    else:
        if sum[j] > x:
            diff = sum[j]-x
            sum[j] -= diff
            ans += diff

print(ans)

Submission status: Wrong Answer

Solution code:
n, x = map(int,input().split())
A = list(map(int,input().split()))
sum = []
ans = 0
for i in range(n-1):
    sum.append(A[i]+A[i+1])
for j in range(n-1):
    if j != n-2:
        if sum[j] > x:
            diff = sum[j]-x
            sum[j] -= diff
            sum[j+1] = min(sum[j+1] - diff, 0)
            ans += diff
    else:
        if sum[j] > x:
            diff = sum[j]-x
            sum[j] -= diff
            ans += diff

print(ans)

Submission status: Wrong Answer

Solution code:
n, x = map(int,input().split())
A = list(map(int,input().split()))
sum = []
ans = 0
for i in range(n-1):
    sum.append(A[i]+A[i+1])
for j in range(n-1):
    if j != n-2:
        if sum[j] > x:
            diff = sum[j]-x
            sum[j] -= diff
            sum[j+1] -= diff
            ans += diff
    else:
        if sum[j] > x:
            diff = sum[j]-x
            sum[j] -= diff
            ans += diff

print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if __name__ == '__main__':
    resolve()

Submission status: Wrong Answer

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

n,m = map(int,readline().split())
lst1 = list(map(int,readline().split()))
ans = 0
for i in range(1,n):
    if lst1[i-1]+lst1[i] > m:
        res = max(0,lst1[i]-(m-lst1[i-1]))
        #print(res)
        ans += res
        lst1[i] = lst1[i]-res

if lst1[0]+lst1[1] > m:
    ans += lst1[0]-m

print(ans)

Submission status: Wrong Answer

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

n,m = map(int,readline().split())
lst1 = list(map(int,readline().split()))
ans = 0
for i in range(1,n):
    if lst1[i-1]+lst1[i] > m:
        res = max(0,lst1[i]-(m-lst1[i-1]))
        #print(res)
        ans += res
        lst1[i] = lst1[i]-res

if lst1[0]+lst1[1] > m:
    ans += lst1[0]-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[1]
    a[1] = 0
    ans += (a[0] - x)
    a[0] = x
else:
    ans += (a[0] + a[1] - x)
    a[1] = x - a[0]
for i in range(2,N):
    if (a[i - 1] + a[i]) > x:
        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( )))
a.append(0)
cnt = 0
for i in range(n):
    if a[i]>x:
        cnt += a[i]-x
        a[i]=x #
    else:
        cnt += max(a[i]+a[i+1]-x,0)
        a[i+1] -= max(a[i]+a[i+1]-x,0)
print(cnt)

Submission status: Wrong Answer

Solution code:
N,x=map(int,input().split())
a=input().split()
numbers=[int(b) for b in a]
count=0
n=len(numbers)
while numbers[0]>=x:
    numbers[0]-=1
    count+=1
for c in range(n-1):
    while numbers[c] + numbers[c+1] >x:
        numbers[c+1]-=1
        count+=1
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(-2, N - 2):
    # 必要な操作回数
    count = a[i+1] + a[i+2] - x
    if count <= 0:
        continue
    
    ans += count
    
    # 右の項を引くだけで完結する場合
    if count < a[i+2]:
        a[i+2] -= count
        
    # 両方の項から引かなければいけない場合
    else:
        a[i+1] = a[i+1] - count + a[i+2]  
        a[i+2] = 0
    
print(ans)

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 i == 0:
        diff = a[0] - x
        if diff > 0:
            count += diff
            a[0] = x

        diff = a[1] - x
        if diff > 0:
            count += diff
            a[1] = x
        continue

    diff = a[i - 1] + a[i + 1] - x
    if diff > 0:
        count += diff
        a[i + 1] -= diff

print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

_a = A[0]
if _a > x:
  cnt += _a - x
  _a = x

for a in A[1:]:
  if _a + a > x:
    dif = _a + a - x
    cnt += dif
    _a = a - dif
  
print(cnt)

Submission status: Wrong Answer

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

_a = A[0]
if _a > x:
  cnt += _a - x

for a in A:
  if _a + a > x:
    dif = _a + a - x
    cnt += dif
    _a = a - dif
  
print(cnt)

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

Submission status: Wrong Answer

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

c=0
#print(diff)
for i in range(n-2):
    if diff[i]>x:
        temp = diff[i+1] - x
        c += temp
        diff[i] -= temp
        diff[i+1] -= temp
if diff[-1] >x:
    c += diff[-1] - x
    diff[-1] = x

print(c)

Submission status: Wrong Answer

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

c=0
#print(diff)
for i in range(n-2):
    if diff[i]>x:
        temp = diff[i] - x
        c += temp
        diff[i] = x
        diff[i+1] -= temp
if diff[-1] >x:
    c += diff[-1] - x
    diff[-1] = x

print(c)

Submission status: Wrong Answer

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

c=0
#print(diff)
for i in range(n-2):
    if diff[i]>x:
        temp = diff[i] - x
        c += temp
        diff[i] = x
        diff[i+1] -= temp
if diff[-1] >x:
    c += diff[-1] - x
    diff[-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(n-1):
    total = a[i] + a[i+1]
    if total > x:
        diff = total - x
        ans += diff
        a[i+1] -= diff
print(ans)

Submission status: Wrong Answer

Solution code:
import collections

N, X = [int(n) for n in input().split()]
A = [int(n) for n in input().split()]

def solve(a):
    ans = 0
    while a:
        l = a.popleft()
        if not a:
            break
        r = a[0]
        n = l + r - X
        m = max(0, n)
        ans += max(0, n - m)
        ans += m
        a[0] -= m
    return ans

def main():
    a1 = collections.deque(A)
    A.reverse()
    a2 = collections.deque(A)
    ans = min(solve(a1), solve(a2))
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import collections

N, X = [int(n) for n in input().split()]
A = [int(n) for n in input().split()]

def main():
    ans = 0
    a = collections.deque(A)
    while a:
        l = a.popleft()
        if not a:
            break
        r = a[0]
        n = l + r - X
        m = max(0, n)
        ans += max(0, n - m)
        ans += m
        a[0] -= m
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
import sys, re, os
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians, floor
from itertools import permutations, combinations, product, accumulate
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits
from fractions import gcd
 
def input(): return sys.stdin.readline().strip()
def INT(): return int(input())
def MAP(): return map(int, input().split())
def S_MAP(): return map(str, input().split())
def LIST(): return list(map(int, input().split()))
def S_LIST(): return list(map(str, input().split()))
 
sys.setrecursionlimit(10 ** 9)
INF = float('inf')
mod = 10 ** 9 + 7

N, x = MAP()
A = LIST()

# Who are you?


#B = [0] * (N - 1)
B = []
for i in range(N-1):
    B.append(A[i+1] + A[i])

B.append(0)
#print(B)
pre = sum(A)
for i in range(N - 1):
    if B[i] > x:
        if A[i+1] >= B[i] - x:
            A[i+1] -= B[i] - x
            B[i+1] -= B[i] - x
            B[i] = x
        else:
            A[i] -= B[i] - x - A[i+1]
            B[i+1] -= B[i] - x
            B[i] = x
            A[i+1] = 0
        #print("A:", A)
        #print("B:", B)
            #B[i+1] -= B[i] - x
after = sum(A)
print(pre-after)
#print(A)
#print(B)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N, x = map(int, input().split())
A = list(map(int, input().split()))
A.append(0)
CA = [a for a in A]
for i in range(N):
    total = CA[i] + CA[i+1]
    if x < total:
        if CA[i + 1] <= (total - x):
            rest = total - x - CA[i + 1]
            CA[i + 1] = 0
            CA[i] = CA[i] - rest if CA[i] - rest > 0 else 0
        else:
            CA[i + 1] = total - x
cnt = 0
for a, ca in zip(A, CA):
    cnt += (a - ca)
print(cnt)

Submission status: Wrong Answer

Solution code:
N, x = map(int, input().split())
A = list(map(int, input().split()))
A.append(0)
CA = [a for a in A]
for i in range(N):
    total = CA[i] + CA[i+1]
    if x < total:
        if CA[i + 1] <= (total - x):
            rest = total - x - CA[i + 1]
            CA[i + 1] = 0
            CA[i] -= rest
        else:
            CA[i + 1] = total - x
cnt = 0
for a, ca in zip(A, CA):
    cnt += (a - ca)
print(cnt)

Submission status: Wrong Answer

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

if N==2:
    y=a[0]+a[1]
    if y>x:
        ans=y-x
        print(ans)
        exit()
    else:
        print(0)
        exit()

check=-1#0,1,2,3
ans=0
flag=False
for i in range(N-2):
    A=a[i]+a[i+1]
    B=a[i+1]+a[i+2]
    if A>x and B>x:
        check=1
    elif A>x and B<=x:
        check=2
    elif A<=x and B>x:
        check=3
    elif A<=x and B<=x:
        check=0
    if check==0:
        continue
    if check==1:
        y=A-x
        ans+=y
        if a[i+1]>0:
            a[i+1]=a[i+1]-y
        else:
            a[i]=a[i]-y
    if check==2:
        y=A-x
        ans+=y
        if a[i+1]>0:
            a[i+1]=a[i+1]-y
        else:
            a[i]=a[i]-y
    if check==3:
        continue
if check==3:
    if (a[-1]+a[-2])>x:
        ans+=((a[-1]+a[-2])-x)
if check==1:
    if (a[-1]+a[-2])>x:
        ans+=((a[-1]+a[-2])-x)

print(ans)

Submission status: Wrong Answer

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

if N==2:
    y=a[0]+a[1]
    if y>x:
        ans=y-x
        print(ans)
        exit()
    else:
        print(0)
        exit()

check=0#1,2,3
ans=0
flag=False
for i in range(N-2):
    A=a[i]+a[i+1]
    B=a[i+1]+a[i+2]
    if A>x and B>x:
        check=1
    elif A>x and B<=x:
        check=2
    elif A<=x and B>x:
        check=3
    elif A<=x and B<=x:
        check=0
    if check==0:
        continue
    if check==1:
        y=A-x
        ans+=y
        a[i+1]=a[i+1]-y
    if check==2:
        y=A-x
        ans+=y
        a[i+1]=a[i+1]-y
    if check==3:
        continue
if check==3:
    if (a[-1]+a[-2])>x:
        ans+=((a[-1]+a[-2])-x)
if check==1:
    if (a[-1]+a[-2])>x:
        ans+=((a[-1]+a[-2])-x)

print(ans)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
from itertools import product

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

n, x = na()
a = na()
apre = a[0]
ans = 0
for ai in a[1:]:
    eat = max(0, apre + ai - x)
    ans += eat
    apre = ai - eat
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
from itertools import product

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

n, x = na()
a = na()
b = list()

apre = a[0]
ans = 0
for ai in a:
    eat = max(0, apre + ai - x)
    ans += eat
    apre = ai - eat
print(ans)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
from itertools import product

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

n, x = na()
a = na()
b = list()

if n == 2:
    print(max(sum(a) - x, 0))
    quit()
else:
    for ai, aj in zip(a[:n-1], a[1:]):
        b.append(max(0, ai + aj - x))
    bpre = b[0]
    ans = 0
    ans += bpre
    for bi in b[1:]:
        bpre = max(0, bi - bpre)
        ans += bpre
    print(ans)

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):
    ans += max(0,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:
#!/usr/bin/env python3
n,x = map(int,input().split())	
a = list(map(int,input().split()))
ans = 0
for i in range(n-1):
    ans += max(0,-1*(x-a[i]-a[i+1]))
    a[i+1] -= max(0,-1*(x-a[i]-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
for i in range (N):
	if i==(N-1):
		if A[i-1]+A[i]>X:
			ans+=(A[i-1]+A[i])-X
			A[i]=(A[i-1]+A[i])-X
	elif i%2==0:
		if A[i]>X:
			ans+=(A[i]-X)
			A[i]=X
		else:
			ans=ans
	
	elif i%2==1:
		if A[i-1]+A[i]>X:
			T=(X-A[i-1])
			ans+=(A[i]-T)
			A[i]=T
print(ans)

Submission status: Wrong Answer

Solution code:
n,x=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:
        temp=a[i]+a[i+1]-x
        #print(a[i+1],temp)
        a[i+1]-=temp
        cnt+=temp

print(cnt)

Submission status: Wrong Answer

Solution code:
import sys
stdin = sys.stdin
from itertools import product

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

n, x = na()
a = na()
b = list()

if n == 2:
    print(max(sum(a) - 2*x, 0))
    quit()
else:
    for ai, aj in zip(a[:n-1], a[1:]):
        b.append(max(0, ai + aj - x))
    ans = 0
    bpre = b[0]
    for bi in b[1:]:
        ans += bpre
        bpre = max(0, bi - bpre)
    ans += bpre
    print(ans)

Submission status: Wrong Answer

Solution code:
a,b = map(int,input().split())
l = list(map(int,input().split()))
t = l[0]
ans = max(0,t-b)
t = min(t,b)
for i in l[1:]:
    ans += max(0,(t+i)-b)
    t = max(0,i-b)
print(ans)

Submission status: Wrong Answer

Solution code:
import collections

N, X = [int(n) for n in input().split()]
A = [int(n) for n in input().split()]

def main():
    ans = 0
    a = collections.deque(A)
    while a:
        l = a.popleft()
        if not a:
            break
        r = a[0]
        n = l + r - X
        m = max(0, n)
        ans += m
        a[0] -= m
    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

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

r = 0
for i in range(1, n):
  r += abs(a[i]+a[i-1] - x)
  a[i] = a[i] - r  
  if a[i] < 0:
    a[i-1] += a[i]
    a[i] = 0
  
  
print(r)

Submission status: Wrong Answer

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

kekka = ((x*n*-1)//2)*-1
r = max(0, sum(a) - kekka)

print(r)

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,x=map(int,input().split())
a=list(map(int,input().split()))
b=[i for i in a]
ans=0
for i in range(1,n):
    if a[i-1]+a[i]>x:
        ans+=a[i]+a[i-1]-x
        if a[i]>x:
            a[i]=0
        else:
            a[i]=x-a[i-1]
ans2=0
for i in range(n-2,-1,-1):
    if b[i+1]+b[i]>x:
        ans2+=b[i]+b[i+1]-x
        if b[i]>x:
            b[i]=0
        else:
            b[i]=x-b[i+1]
print(min(ans,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(1,n):
    if a[i-1]+a[i]>x:
        ans+=a[i]+a[i-1]-x
        if a[i]>x:
            a[i]=0
        else:
            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()))
cnt=0
flag=False
for i in range(N):
    if i%2==1:
        if a[i-1]+a[i]>x:
            if a[i-1]<=x:
                tmp=a[i]
                a[i]=x-a[i-1]
                cnt+=tmp-a[i]
            elif a[i-1]>x:
                tmp=a[i]
                a[i]=0
                cnt+=tmp
for i in range(N):
    if i%2==0:
        try:
            if a[i]+a[i+1]>x:
                tmp=a[i]
                a[i]=x-a[i+1]
                cnt+=tmp-a[i]
        except:
            if a[i-1]+a[i]>x:
                tmp=a[i]
                a[i]=x-a[i-1]
                cnt+=tmp-a[i]

print(cnt)