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:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

N,x = map(int,input().split())
a = [int(i) for i in input().split()]
part_sum = [0 for i in range(N)]
tmp = 0
for i in range(N):
    tmp += a[i]
    part_sum[i] = tmp

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

ans = 0
cnt = 0
for i in range(N-1):
    if window[i]-cnt > x:
        ans += window[i]-x-cnt
        cnt = window[i]-x-cnt
    else:
        cnt = 0
window.reverse()

ans2 = 0
cnt = 0
for i in range(N-1):
    if window[i]-cnt > x:
        ans2 += window[i]-x-cnt
        cnt = window[i]-x-cnt
    else:
        cnt = 0
print(min(ans,ans2))

"""
1 6 1 2 0 4
1 0 1 0 0 1
0 1 0 1 0 1


"""

Submission status: Wrong Answer

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

N,x = map(int,input().split())
a = [int(i) for i in input().split()]
part_sum = [0 for i in range(N)]
tmp = 0
for i in range(N):
    tmp += a[i]
    part_sum[i] = tmp

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

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

"""
1 6 1 2 0 4
1 0 1 0 0 1
0 1 0 1 0 1


"""

Submission status: Wrong Answer

Solution code:
# encoding:utf-8
import copy
import random
import bisect #bisect_left　これで二部探索の大小検索が行える
import fractions #最小公倍数などはこっち
import math
import sys

mod = 10**9+7
sys.setrecursionlimit(mod) # 再帰回数上限はでdefault1000

N,x = map(int,input().split())
a = [int(i) for i in input().split()]
part_sum = [0 for i in range(N)]
tmp = 0
for i in range(N):
    tmp += a[i]
    part_sum[i] = tmp

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

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

"""
1 6 1 2 0 4
1 0 1 0 0 1
0 1 0 1 0 1


"""

Submission status: Wrong Answer

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

if a[0] > x:
    a[0] = x

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

Submission status: Wrong Answer

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

c = A[0]+A[1]-x
c = min(A[0],c)
c = max(c,0)
A[0] -= c
cost += c

c = A[-1]+A[-2]-x
c = min(A[-1],c)
c = max(c,0)
A[-1] -= c
cost += c

print(cost)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import sys, os

f = lambda:list(map(int,input().split()))
if 'local' in os.environ :
    sys.stdin = open('./input.txt', 'r')

def solve():
    n,x = f()
    a = f()

    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)

solve()

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]>x:
        ans+=A[i]-x
        A[i]=0
    if A[i+1]>x-A[i]:
        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()]
count=0
for i in range(1, n):
    if A[i-1]+A[i]>x:
        count = count + (A[i-1]+A[i]-x)
        A[i] = A[i]-(A[i-1]+A[i]-x)
print(count)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
from sys import stdin, stdout  
  
def main(): 
  [N, x] = stdin.readline().split()
  x = int(x)
  A = [int(x) for x in stdin.readline().split()] 
  ans = 0
  a = A[0]
  del A[0]
  for b in A:
    if a + b > x:
      r = a + b - x
      ans += r
      a = b - r
    else:
      a = b
  stdout.write(str(ans))
  
if __name__ == "__main__": 
    main()

Submission status: Wrong Answer

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

N, X = map(int, input().split())
L = [int(v) for v in input().split()]
M = [0] * (N - 1)

for i in range(N - 1):
    M[i] = L[i] + L[i + 1]
    
c = 0
for i in range(N - 1):
    if M[i] > X:
        t = abs(X - M[i])
        
        c += t
        M[i] -= t
        if i + 1 < N - 1:
            M[i + 1] -= t
        
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(n-1):
    p=a[i]+a[i+1]
    q=a[i+1]
    if a[i]>x:
        count+=abs(a[i]-x)
        a[i]=x
    if p>x:
        count+=a[i]+a[i+1]-x
        a[i+1]=q-(p-x)
print(count)

Submission status: Wrong Answer

Solution code:
N,x=map(int,input().split())
*A,=map(int,input().split())
A = [0] + A + [0]
ans = 0
for i,a in enumerate(A):
    if a>x:
        ans += a-x
        A[i] = x
ans1 = [0,0]
for i in range(1,N+1):
    if max(A[i-1],A[i+1])+A[i] <= x:
        ans += min(ans1)
        ans1 = [0,0]
    else:
        ans1[i%2] += A[i] - (x-max(A[i-1],A[i+1]))
print(ans+min(ans1))

Submission status: Wrong Answer

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

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

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

Submission status: Wrong Answer

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

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

Submission status: Wrong Answer

Solution code:


def read_input():
    n, x = map(int, input().split())
    alist = list(map(int, input().split()))

    return n, x, alist

def submit():
    n, x, alist = read_input()

    edit = 0

    if alist[0] > x:
        edit += x - alist[0]
        alist[0] = x

    for i in range(n - 1):
        if alist[i] + alist[i + 1] > x:
            diff = alist[i] + alist[i + 1] - x
            edit += diff
            alist[i + 1] = alist[i + 1] - diff

    print(edit)


if __name__ == '__main__':
    submit()

Submission status: Wrong Answer

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

print(min(count_a, count_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(len(a)-1):
  if a[i]+a[i+1] > x:
    count += a[i]+a[i+1] - x
    a[i+1] -= 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()))
b=[]
for i in range(1,n):
  b.append(max(0,a[i]+a[i-1]-x))
ans=sum(b)
print(ans//2+(ans%2>0))

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

if a[0] > x:
    a[0] = x
    cnt = a[0]-x

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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


for i in range(N-1):
    val = lst_a[i] + lst_a[i+1]
    if val > x:
        tmp = val - x
        lst_a[i+1] -= tmp
        if lst_a[i+1] < 0:
            lst_a[i] += lst_a[i+1]
            lst_a[i] = 0
        ans += tmp
print(ans)

Submission status: Wrong Answer

Solution code:
# 貪欲法

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


for i in range(N-1):
    val = lst_a[i] + lst_a[i+1]
    if val > x:
        tmp = val - x
        ans += tmp
        lst_a[i+1] -= tmp
print(ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
N,x = map(int,input().split())
a_list = [int(i) for i in input().split()]
a_list2=a_list[:]
result1 = 0
result2 = 0
for i in range(N-1):
	if a_list[i] + a_list[i+1] >= x:
		result1+=a_list[i+1] - (x-a_list[i])
		a_list[i+1] = x-a_list[i]
for i in range(N-1,0,-1):
	if a_list2[i] + a_list2[i-1] >= x:
		result2+=a_list2[i-1] - (x-a_list2[i])
		a_list2[i-1] = x-a_list2[i]
print(min(result1,result2))

Submission status: Wrong Answer

Solution code:


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

count_candy_eat = 0

if(candy_list[-1] == 0):
    for i in (reversed(range(N))):
        if (i == N):
            continue
        else:
            if (candy_list[i] + candy_list[i-1] > x):
                if(x - candy_list[i] > 0):
                    count_candy_eat += candy_list[i] + candy_list[i-1] - x
                    candy_list[i-1] = x - candy_list[i]
                else:
                    count_candy_eat += candy_list[i-1]
                    candy_list[i-1] = 0
else:
    for i in range(N):
        if(i == 0):
            continue
        else:
            if(candy_list[i-1] + candy_list[i] > x):
                if(x - candy_list[i-1] > 0):
                    count_candy_eat += candy_list[i-1] + candy_list[i] - x
                    candy_list[i] = x - candy_list[i-1]
                else:
                    count_candy_eat += candy_list[i]
                    candy_list[i] = 0


print(count_candy_eat)

Submission status: Wrong Answer

Solution code:


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

count_candy_eat = 0

if(candy_list[-1] == 0):
    for i in (reversed(range(N))):
        if (i == N):
            continue
        else:
            if (candy_list[i] + candy_list[i-1] > x):
                count_candy_eat += candy_list[i] + candy_list[i-1] - x
                candy_list[i-1] = x - candy_list[i]
else:
    for i in range(N):
        if(i == 0):
            continue
        else:
            if(candy_list[i-1] + candy_list[i] > x):
                count_candy_eat += candy_list[i-1] + candy_list[i] - x
                candy_list[i] = x - candy_list[i-1]

print(count_candy_eat)

Submission status: Wrong Answer

Solution code:


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

count_candy_eat = 0

if(candy_list[-1] == 0):
    for i in (reversed(range(N))):
        if (i == N):
            continue
        else:
            if (candy_list[i] + candy_list[i-1] > x):
                count_candy_eat += candy_list[i] + candy_list[i-1] - x
                candy_list[i-1] = x - candy_list[i]

print(candy_list)

for i in range(N):
    if(i == 0):
        continue
    else:
        if(candy_list[i-1] + candy_list[i] > x):
            count_candy_eat += candy_list[i-1] + candy_list[i] - x
            candy_list[i] = x - candy_list[i-1]

print(count_candy_eat)

Submission status: Wrong Answer

Solution code:


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

count_candy_eat = 0

if(candy_list[-1] == 0):
    candy_list[::-1]

for i in range(N):
    if(i == 0):
        continue
    else:
        if(candy_list[i-1] + candy_list[i] > x):
            count_candy_eat += candy_list[i-1] + candy_list[i] - x
            candy_list[i] = x - candy_list[i-1]

print(count_candy_eat)

Submission status: Wrong Answer

Solution code:


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

count_candy_eat = 0

if(candy_list[-1] == 0):
    candy_list[::-1]

for i in range(N):
    if(i == 0):
        continue
    else:
        if(candy_list[i-1] + candy_list[i] > x):
            count_candy_eat += candy_list[i-1] + candy_list[i] - x
            candy_list[i] = candy_list[i] - count_candy_eat

print(count_candy_eat)

Submission status: Wrong Answer

Solution code:


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

count_candy_eat = 0

if(candy_list[-1] == 0):
    for i in reversed(range(N)):
        if (i == 0):
            continue
        else:
            if (candy_list[i - 1] + candy_list[i] > x):
                count_candy_eat += candy_list[i - 1] + candy_list[i] - x
                candy_list[i] = candy_list[i] - count_candy_eat
else:
    for i in range(N):
        if(i == 0):
            continue
        else:
            if(candy_list[i-1] + candy_list[i] > x):
                count_candy_eat += candy_list[i-1] + candy_list[i] - x
                candy_list[i] = candy_list[i] - count_candy_eat

print(count_candy_eat)

Submission status: Wrong Answer

Solution code:


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

count_candy_eat = 0

if(candy_list[:-1] == 0):
    for i in reversed(range(N)):
        if (i == 0):
            continue
        else:
            if (candy_list[i - 1] + candy_list[i] > x):
                count_candy_eat += candy_list[i - 1] + candy_list[i] - x
                candy_list[i] = candy_list[i] - count_candy_eat
else:
    for i in range(N):
        if(i == 0):
            continue
        else:
            if(candy_list[i-1] + candy_list[i] > x):
                count_candy_eat += candy_list[i-1] + candy_list[i] - x
                candy_list[i] = candy_list[i] - count_candy_eat

print(count_candy_eat)

Submission status: Wrong Answer

Solution code:

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

count_candy_eat = 0

for i in range(N):
    if(i == 0):
        continue
    else:
        if(candy_list[i-1] + candy_list[i] > x):
            count_candy_eat += candy_list[i-1] + candy_list[i] - x
            candy_list[i] = candy_list[i] - count_candy_eat

print(count_candy_eat)

Submission status: Wrong Answer

Solution code:
import sys
import collections
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())

n, x = nm()
a = nl()
ans = 0
for i in range(n):
    if i == 0:
        if a[i] <= x:
            tmp = a[i]
        else:
            if a[i] - x > a[i + 1]:
                ans += a[i] - x - a[i + 1]
                tmp = x + a[i + 1]
            else:
                tmp = a[i]
    else:
        if tmp + a[i] <= x:
            tmp = a[i]
        else:
            ans += tmp + a[i] - x
            tmp = x - tmp
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:
        c +=  a[i] + a[i+1] - x
        a[i+1] -=  a[i] + a[i+1] - x

print(c)

Submission status: Wrong Answer

Solution code:
import sys
import collections
ns = lambda: sys.stdin.readline().rstrip()
ni = lambda: int(ns())
nm = lambda: map(int, sys.stdin.readline().split())
nl = lambda: list(nm())
nsl = lambda: map(str, sys.stdin.readline().split())

n, x = nm()
a = nl()
ans = 0
for i in range(n):
    if i == 0:
        if a[i] <= x:
            tmp = a[i]
        else:
            ans += a[i] - x
            tmp = x
    else:
        if tmp + a[i] <= x:
            tmp = a[i]
        else:
            ans += tmp + a[i] - x
            tmp = 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] -= A[i] + A[i+1] - x
print(ans)

Submission status: Wrong Answer

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

count = 0
for i in range(N-1):
    tmp = a[i] +a[i+1]
    if tmp  > x:
        count += tmp - 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()))
import copy
tmpa = copy.deepcopy(a)

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

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
import math

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

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

S=[A[i]+A[i+1] for i in range(n-1)]

a=0
b=0

if S[0]-x>A[1]:
    a=S[0]-x-A[1]
    S[0]-=a

if S[-1]-x>A[-2]:
    b=S[-1]-x-A[-2]
    S[-1]-=b

    
ans=math.ceil((sum(S)-x*(n-1))/2)+a+b

if ans<=0:
    ans=0
    
print(ans)

Submission status: Wrong Answer

Solution code:
import math

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

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

S=[A[i]+A[i+1] for i in range(n-1)]

a=0
b=0

if S[0]-x>A[1]:
    a=S[0]-x-A[1]
    S[0]-=a

if S[-1]-x>A[-2]:
    b=S[-1]-x-A[-2]
    S[-1]-=b

    
ans=math.ceil((sum(S)-x*(n-1)-a-b)/2)+a+b

if ans<=0:
    ans=0
    
print(ans)

Submission status: Wrong Answer

Solution code:
import math

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

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

S=[A[i]+A[i+1] for i in range(n-1)]

a=0
b=0

if S[0]-x>A[1]:
    a=S[0]-x-A[1]

if S[-1]-x>A[-2]:
    b=S[-1]-x-A[-2]

    
ans=math.ceil((sum(S[1:-2])-x*(n-1)-a-b)/2)+a+b

if ans<=0:
    ans=0
    
print(ans)

Submission status: Wrong Answer

Solution code:
import math

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

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

S=[A[i]+A[i+1] for i in range(n-1)]

a=0
b=0

if S[0]-x>A[1]:
    a=S[0]-x-A[1]

if S[-1]-x>A[-2]:
    b=S[-1]-x-A[-2]

    
ans=math.ceil((sum(S)-x*(n-1)-a-b)/2)+a+b

if ans<=0:
    ans=0
    
print(ans)

Submission status: Wrong Answer

Solution code:
#C問題
N,x=map(int,input().split())
cand=list(map(int,input().split()))
count=0
for i in range(N-1):
    if cand[i]+cand[i+1]<=x:
        pass
    else:
        sa=x-cand[i]
        count+=(cand[i+1]-sa)
        cand[i+1]=sa
print(count)

Submission status: Wrong Answer

Solution code:
def boxes_and_candies(N: int, x: int, A: list) -> int:
    count = 0
    for i in range(1, N):
        a1, a2 = A[i - 1], A[i]
        print(a1, a2)

        if a1 + a2 <= x:
            # OK
            # print('--> OK')
            continue

        diff = (a1 + a2) - x
        count += diff
        # print('--> {}'.format(diff))

        if a1 > a2:
            i1, i2 = i - 1, i
        else:
            i1, i2 = i, i - 1

        if A[i1] > diff:
            A[i1] -= diff
        else:
            A[i2] -= diff - A[i1]
            A[i1] = 0

    return count


if __name__ == "__main__":
    N, x = map(int, input().split())
    A = [int(s) for s in input().split()]
    ans = boxes_and_candies(N, x, A)
    print(ans)

Submission status: Wrong Answer

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

a2 = []
a3 = []
for i in range(n):
    if i % 2 == 0:
        a2.append(mina)
        a3.append(b)
    else:
        a2.append(b)
        a3.append(mina)

cnt2 = 0
cnt3 = 0
for i in range(n):
    if a[i] >= a2[i]:
        cnt2 += a[i] - a2[i]
    else:
        cnt2 = None
        break
    
for i in range(n):
    if a[i] >= a3[i]:
        cnt3 += a[i] - a3[i]
    else:
        cnt3 = None
        break

if cnt2 and cnt3:
    print(min(cnt2,cnt3))
elif cnt2:
    print(cnt2)
elif cnt3:
    print(cnt3)
else:
    print(0)

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

Submission status: Wrong Answer

Solution code:
def func(a, b):
    global count

    n = min(A[a]+A[b]-x, A[a])
    count += n
    A[i] -= n

N, x = list(map(int, input().split()))
A = [0] + list(map(int, input().split())) + [0]
count = 0

for n in [2, 1]:
    for i in range(n, N+1, 2):
        if A[i-1] + A[i] > x:
            func(i-1, i)

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

print(ans)

Submission status: Wrong Answer

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

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

print(cnt)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
a,b = map(int, input().split())
X = list(map(int, input().split()))

num = a*b

if(num/2 > int(num/2)):
    num = int(num/2) + 1
else:
    num = int(num/2)

if(num < sum(X)):
    print(sum(X) - num)
else:
    print(0)

Submission status: Wrong Answer

Solution code:
# -*- coding: utf-8 -*-
import sys

def main():
	N, x = map(int,input().split(" "))
	tmp = input().split(" ")
	candy = [int(s) for s in tmp]
	
	ans = 0
	if candy[1] > x:
		ans = ans + candy[1] - x
		candy[1] = x
	if candy[len(candy)-2] > x:
		ans = ans + candy[len(candy)-2] - x
		candy[len(candy)-2] = x
	
	for i, val in enumerate(candy):
		if i+2 == len(candy): break
		
		tmp = candy[i] + candy[i+2]
		if x < tmp and candy[i+2] >= tmp - x:
			candy[i+2] = candy[i+2] - (tmp - x)
			ans = ans + (tmp - x)
		elif x < tmp:
			candy[i] = candy[i] - ((tmp - x) - candy[i+2])
			candy[i+2] = 0
			ans = ans + (tmp - x)
	
	print(ans)

if __name__ == "__main__":
	main()

Submission status: Wrong Answer

Solution code:
n,x=map(int,input().split())
a = list(map(int,input().split()))
if (x*n)%2==0:
    sum_goal =  x*n//2
else:
    sum_goal = (x*n)//2+1
if sum(a)<sum_goal:
    print(0)
else:
    print(sum(a)-sum_goal)

Submission status: Wrong Answer

Solution code:
n,x=map(int,input().split())
a = list(map(int,input().split()))
if ((x*n)%2)==0:
    sum_goal =  x*n//2
else:
    sum_goal = (x*n+2)//2
if sum(a)<sum_goal:
    print(0)
else:
    print(sum(a)-sum_goal)

Submission status: Wrong Answer

Solution code:
n,x=map(int,input().split())
a = list(map(int,input().split()))
if (x*n%2)==0:
    sum_goal =  x*n//2
else:
    sum_goal = (x*n+2)//2
if sum(a)<=sum_goal:
    print(0)
else:
    print(sum(a)-sum_goal)

Submission status: Wrong Answer

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

ans = 0

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

Submission status: Wrong Answer

Solution code:
n,x=map(int,input().split())
a=list(map(int,input().split()))
S=sum(a)*2-a[0]-a[-1]
X=x*(len(a)-1)
value=S-X
S_in=sum(a)-a[0]-a[-1]
if value>=S_in*2:
    print(S_in+(value-S_in*2))
else:
    print(-(-value//2))

Submission status: Wrong Answer

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

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):
  tmp = max(0, A[i]+A[i+1] - x)
  cnt += tmp
  A[i+1] -= tmp
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(N-1):
  if(A[i]+A[i+1] > x):
    cnt += A[i]+A[i+1] - x
    A[i+1] = min(x-1,A[i]+A[i+1] - 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(n-1):
  if A[i]+A[i+1]>x:
    p=x-A[i]-A[i+1] 
    if A[i+1]<p:
      A[i]-=p-A[i+1]
      A[i+1]=0
    else:
      A[i+1]-=p
    ans+=p
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:
    p=x-A[i]-A[i+1] 
    A[i+1]-=p
    ans+=p
print(ans)

Submission status: Wrong Answer

Solution code:
n,x=map(int,input().split())
A=list(map(int,input().split()))
ans=0
diff=[abs(x-(A[i]+A[i-1])) if x-(A[i]+A[i-1])<0 else 0 for i in range(1,len(A))]
if sum(diff)== 0:
  print(0)
  exit()
for i in range(0,len(diff),2):
  try:
    ans+=max(diff[i],diff[i+1])
  except IndexError:
    ans+=diff[i]
print(ans)

Submission status: Wrong Answer

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