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:
n, x = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 0
for i in range(n-1):
    a1 = a[i]
    a2 = a[i+1]
    if a1+a2> x:
        a[i+1] = x-a1
        if a[i+1] < 0:
            a[i] += a[i+1]
            a[i+1] = 0
    ans = a1-a[i]+a2-a[i+1]
ans2 = 0
for i in range(n-1):
    a1 = a[i]
    a2 = a[i+1]
    if a1+a2> x:
        a[ix] = x-a1
        if a[i] < 0:
            a[i+1] += a[i]
            a[i] = 0
    ans = a1-a[i]+a2-a[i+1]
print(min(ans,ans2))

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

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

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

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

Submission status: Wrong Answer

Solution code:
n, x = map(int,raw_input().split())

arr = map(int,raw_input().split())

sums = []
for i in range(1,n):
	sums.append(arr[i-1]+arr[i])

ans = 0

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



print ans if (n-1)%2 == 0 else ans + sums[n-2] - x if sums[n-2] > x else 0

Submission status: Wrong Answer

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

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

print(result)

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):
    d = max(0,A[i]+A[i-1]-X)
    A[i] -= d
    Ans += d

print(Ans)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

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

print(v)

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
  a[0]=x  
else:
  pass
j = 1
while j <= n-1:
  if a[j-1]+a[j]<=x:
    j += 1
  else:
    ans += a[j]-x
    a[j]=x
    j += 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
  a[0]=x  
else:
  pass
j = 1
while j <= n-1:
  if a[j-1]+a[j]<=x:
    pass
  else:
    ans += a[j]-x
    a[j]=x
    j += 1
print(ans)

Submission status: Wrong Answer

Solution code:
def m():
    n, x = map(int, input().split())
    a = list(map(int, input().split()))
    ans = 0
    for i in range(1, n-1, 2):
        if a[i+1] > a[i-1]:
            if a[i+1] + a[i] < x:
                continue
            ans += (a[i+1] + a[i]) -x
            if a[i+1] > x:
                a[i+1] = x
        else:
            if a[i-1] + a[i] < x:
                continue
            ans += (a[i-1] + a[i]) -x
    if len(a) % 2 == 0:
        if a[-1] + a[-2] < x:
            return ans
        ans += (a[-1] + a[-2]) -x
        return ans
    else:
        return ans

print(m())

Submission status: Wrong Answer

Solution code:
def m():
    n, x = map(int, input().split())
    a = list(map(int, input().split()))
    ans = 0
    for i in range(1, n-1, 2):
        if a[i+1] > a[i-1]:
            if a[i+1] + a[i] < x:
                continue
            if a[i+1] > x:
                ans += x - a[i+1]
                ans += a[i]
                continue
            ans += (a[i+1] + a[i]) -x
        else:
            if a[i-1] + a[i] < x:
                continue
            if a[i-1] > x:
                ans += x - a[i-1]
                ans += a[i]
                continue
            ans += (a[i-1] + a[i]) -x
    if len(a) % 2 == 0:
        if a[-1] + a[-2] < x:
            return ans
        if a[-2] > x:
            ans += x - a[-2]
            ans += a[-1]
            return ans
        ans += (a[-1] + a[-2]) -x
        return ans
    else:
        return ans

print(m())

Submission status: Wrong Answer

Solution code:
def m():
    n, x = map(int, input().split())
    a = list(map(int, input().split()))
    ans = 0
    for i in range(1, n-1, 2):
        if a[i+1] > a[i-1]:
            if a[i+1] + a[i] < x:
                continue
            ans += (a[i+1] + a[i]) -x
        else:
            if a[i-1] + a[i] < x:
                continue
            ans += (a[i-1] + a[i]) -x
    if len(a) % 2 == 0:
        if a[-1] + a[-2] < x:
            return ans
        ans += (a[-1] + a[-2]) -x
        return ans
    else:
        return ans

print(m())

Submission status: Wrong Answer

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


print(m())

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

Submission status: Wrong Answer

Solution code:
from sys import stdin

N, x = [int(x) for x in stdin.readline().rstrip().split()]
candys = [int(x) for x in stdin.readline().rstrip().split()]

count = 0

# xより多く入っている箱のキャンディは必ず食べる必要がある。
for i in range(N):
    if candys[i] > x:
        num = candys[i] - x
        candys[i] = x
        count += num

# 隣り合った箱を見て、xを越えていれば食べる
for i in range(N-1):
    if candys[i] + candys[i+1] > x:
        num = candys[i] + candys[i+1] - x
        count += num
        candys[i+1] = candys[i+1] - 1

# 結果
print(count)

Submission status: Wrong Answer

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

    bef = sum(a)
    c = [x//2, (x+1)//2]
    for i in range(0, n-1):
        if a[i] + a[i+1] > x:
            a[i] = max(c[1], x - a[i + 1])
            a[i+1] = x - a[i]

    print(bef-sum(a))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

    c = [x//2, (x+1)//2]
    cnt = [0, 0]
    bef = sum(a)
    for i in range(2):
        b = a[:]
        for j in range(n-1):
            if b[j] + b[j+1] > x:
                b[j] -= b[j] - c[(i+j) % 2]
                b[j+1] -= b[j+1] - c[(i+j+1) % 2]

        cnt[i] = bef - sum(b)

    print(min(cnt))


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

    bef = sum(a)
    aft = -(-x*n//2)

    if bef - aft > 0:
        print(bef-aft)
    else:
        print(0)


if __name__ == '__main__':
    main()

Submission status: Wrong Answer

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

b = [0]
for i in range(n):
    b.append(a[i])
b.append(0)    
counter = 0

if b[1] > x:
    counter = b[1] - x
    b[1] = x
for i in range(n+1):
    if b[i] + b[i+1] > x:
        s = b[i] + b[i+1] - x
        if b[i] - s > -1:
            b[i] = b[i] -s
            counter = counter + s
        else:
            k = b[i]
            counter = counter + s
            b[i] = 0
            b[i+1] = x
        
print(counter)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

s = a[0] + a[1]
a.append(0)
p = 0
if a[0] > x:
    a[0] = x
    p = a[0] - x
    
for i in range(n-1):
    pp = a[i] + a[i + 1]
    if pp > x:
        q = a[i+1]
        r = pp - x
        a[i+1] = q - r
        p = p + r
        
print(p)

Submission status: Wrong Answer

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

s = a[0] + a[1]
a.append(0)
p = 0
if a[0] > x:
    a[0] = x
    p = a[0] - x
    
for i in range(n-1):
    pp = a[i] + a[i + 1]
    if pp > x:
        a[i + 1] = a[ i + 1] - pp + x
        p = p + pp -x
        
print(p)

Submission status: Wrong Answer

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

s = a[0] + a[1]
a.append(0)
p = 0
if a[0] > x:
    a[0] = x
    p = a[0] - x
    
for i in range(n):
    pp = a[i] + a[i + 1]
    if pp > x:
        a[i + 1] = a[ i + 1] - pp + x
        p = p + pp -x
        
print(p)

Submission status: Wrong Answer

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

s = a[0] + a[1]
a.append(0)
p = 0
if a[0] > x:
    a[0] = x
    p = a[0] - x
    
for i in range(n):
    pp = a[i] + a[i + 1]
    if pp > x and a[i] > x:
        p = p + a[i] - x
        p = p + a[i + 1]
        a[i] = x
        a[i + 1] = 0
    elif a[i] > x and a[i + 1] > x:
        p = p + a[i] -x + a[i + 1]
        a[i] = x
        a[i + 1] = 0
    elif pp > x:
        if a[i+1] - (pp -x) > -1:
            a[i +1] = a[i+1] - (pp -x) 
            p = p + pp-x
        else:
            a[i] = a[i] - ( pp - x - a[i+1])
            p = p + pp -x
    else:
        None
        
print(p)

Submission status: Wrong Answer

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

s = a[0] + a[1]
a.append(0)
p = 0
if a[0] > x:
    a[0] = x
    p = a[0] - x
    
for i in range(0,n):
    pp = a[i] + a[i + 1]
    if pp > x and a[i] > x:
        p = p + a[i] - x
        p = p + a[i + 1]
        a[i] = x
        a[i + 1] = 0
    elif pp > x:
        p = p + pp - x
        a[i + 1] = a[i + 1] - pp + x
        
print(p)

Submission status: Wrong Answer

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

s = a[0] + a[1]
a.append(0)
p = 0
if a[0] > x:
    a[0] = x
    p = a[0] - x
    
for i in range(0,n):
    pp = a[i] + a[i + 1]
    if pp > x:
        a[i+1] = a[i+1] - pp + x
        p = p + pp -x
        
print(p)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
inline = list(map(int,input().split()))
N = inline[0]
x = inline[1]
inline = list(map(int,input().split()))
two = []
cost = 0;
for i in range(N-1):
    two.append(inline[i]+inline[i+1])

for i in range(N-2):
    if two[i] > x:
        cost += (two[i]-x)
        two[i+1] -= max((two[i]-x),inline[i+1])

if two[-1]>x:
    cost += (two[-1]-x)

print(cost)

Submission status: Wrong Answer

Solution code:
inline = list(map(int,input().split()))
N = inline[0]
x = inline[1]
inline = list(map(int,input().split()))
two = []
cost = 0;
for i in range(N-1):
    two.append(inline[i]+inline[i+1])

for i in range(N-2):
    if two[i] > x:
        cost += (two[i]-x)
        two[i+1] -= (two[i]-x)

if two[-1]>x:
    cost += (two[-1]-x)

print(cost)

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

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

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
a.append(0)
total=sum(a)
for i in range(1,n+1,2):
    a[i]+=a[i-1]
    a[i-1]=0
    if k<a[i]:
        a[i]-=a[i]-k
print(total-sum(a))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
total=sum(a)
if n%2==1:
    a.append(0)
for i in range(1,n+1,2):
    if k<a[i]+a[i-1]:
        if k<a[i]+a[i-1]-k:
            a[i]=0
            a[i-1]-=a[i]+a[i-1]-k-a[i]
        else:
            a[i]-=a[i]+a[i-1]-k
print(total-sum(a))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
total=sum(a)
if n%2==1:
    a.append(0)
for i in range(1,n,2):
    if k<a[i]+a[i-1]:
        if k<a[i]+a[i-1]-k:
            a[i]=0
            a[i-1]-=a[i]+a[i-1]-k-a[i]
        else:
            a[i]-=a[i]+a[i-1]-k
print(total-sum(a))

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
total=sum(a)
count=0
for i in range(1,n,2):
    if k<a[i]+a[i-1]:
        if a[i]<k:
            a[i]=0
            a[i-1]-=a[i-1]-k
        else:
            a[i]-=a[i]+a[i-1]-k
print(total-sum(a))

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

Submission status: Wrong Answer

Solution code:
n,k=map(int,input().split())
a=list(map(int,input().split()))
v=sum(a)
for i in range(n-1):
    if k<a[i]+a[i+1]:
        a[i+1]=k-a[i]
print(v-sum(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):
    if A[i] + A[i+1] > X:
        ans -= X - (A[i] + A[i + 1])
        A[i+1] += X - (A[i] + A[i+1])

print(ans)

Submission status: Wrong Answer

Solution code:
def main():
    """
    端の最適解は決定している
        idx= 0~ 1:sumがx以下になるようにがA[ 1]を減らす、それでもx以下にならないならA[ 0]を減らす
        idx=-2~-1:sumがx以下になるようにがA[-2]を減らす、それでもx以下にならないならA[-1]を減らす

        -> 左端は決定しているので右端まで減らす
        -> 右端から行っても減らす数は変わらない
        -> 端から2番目を優先に減らせばどちらでもよい？
    """
    N, x = map(int, input().split())
    A = list(map(int, input().split()))

    ans = f(N, x, A)
    print(ans)


def f(N, x, A):
    ans = 0
    for i in range(1, N):
        s = sum(A[i - 1:i + 1])
        if s > x:
            # 引き過ぎない
            n = min(s - x, A[i])
            A[i] -= n
            ans += n

            s = sum(A[i - 1:i + 1])
            if s > x:
                n = min(s - x, A[i - 1])
                A[i] -= n
                ans += n

    return ans


def WA(N, x, A):
    ans = 0
    for i in range(N // 2):
        s = sum(A[i:i + 2])
        if x <= s:
            n = s - x
            A[i + 1] -= n
            ans += n

            s = sum(A[i:i + 2])
            if x <= s:
                n = s - x
                A[i] -= n
                ans += n

        if i == 0:
            # slice(-2, 0) -> []
            s = sum(A[-2:])
        else:
            s = sum(A[-(i + 2):-i])
        if x <= s:
            n = s - x
            A[-(i + 2)] -= n
            ans += n

            if i == 0:
                s = sum(A[-2:])
            else:
                s = sum(A[-(i + 2):-i])
            if x <= s:
                n = s - x
                A[-(i + 1)] -= n
                ans += n

    return ans

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    """
    奇数個の時
        偶数番目の数を減らすと両隣との和を減らせる
    端の最適解は決定している
        idx= 0~ 1:sumがx以下になるようにがA[ 1]を減らす、それでもx以下にならないならA[ 0]を減らす
        idx=-2~-1:sumがx以下になるようにがA[-2]を減らす、それでもx以下にならないならA[-1]を減らす
    """
    N, x = map(int, input().split())
    A = list(map(int, input().split()))

    ans1 = WA(N, x, A)
    ans2 = WA(N, x, A[::-1])
    ans = min(ans1, ans2)

    print(ans)


def WA(N, x, A):
    ans = 0
    for i in range(N // 2):
        s = sum(A[i:i + 2])
        if x <= s:
            n = s - x
            A[i + 1] -= n
            ans += n

            s = sum(A[i:i + 2])
            if x <= s:
                n = s - x
                A[i] -= n
                ans += n

        if i == 0:
            # slice(-2, 0) -> []
            s = sum(A[-2:])
        else:
            s = sum(A[-(i + 2):-i])
        if x <= s:
            n = s - x
            A[-(i + 2)] -= n
            ans += n

            if i == 0:
                s = sum(A[-2:])
            else:
                s = sum(A[-(i + 2):-i])
            if x <= s:
                n = s - x
                A[-(i + 1)] -= n
                ans += n

    return ans

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
def main():
    """
    奇数個の時
        偶数番目の数を減らすと両隣との和を減らせる
    端の最適解は決定している
        idx= 0~ 1:sumがx以下になるようにがA[ 1]を減らす、それでもx以下にならないならA[ 0]を減らす
        idx=-2~-1:sumがx以下になるようにがA[-2]を減らす、それでもx以下にならないならA[-1]を減らす
    """
    N, x = map(int, input().split())
    A = list(map(int, input().split()))

    solve(N, x, A)


def solve(N, x, A):
    ans = 0
    for i in range(N // 2):
        s = sum(A[i:i + 2])
        if x <= s:
            n = s - x
            A[i + 1] -= n
            ans += n

            s = sum(A[i:i + 2])
            if x <= s:
                n = s - x
                A[i] -= n
                ans += n

        if i == 0:
            # slice(-2, 0) -> []
            s = sum(A[-2:])
        else:
            s = sum(A[-(i + 2):-i])
        if x <= s:
            n = s - x
            A[-(i + 2)] -= n
            ans += n

            if i == 0:
                s = sum(A[-2:])
            else:
                s = sum(A[-(i + 2):-i])
            if x <= s:
                n = s - x
                A[-(i + 1)] -= n
                ans += n

    print(ans)

if __name__ == '__main__':
    main()

Submission status: Wrong Answer

Solution code:
num, lim = map(int, input().split())
val = [int(x) for x in input().split()]

res = 0
for i in range(num-1):
    if (val[i]+val[i+1]) > lim:
        res += (val[i]+val[i+1]) - lim
        if val[i] > lim:
            val[i+1] -= (val[i]+val[i+1]) - lim
        else:
            val[i+1] = 0
print(res)

Submission status: Wrong Answer

Solution code:
num, lim = map(int, input().split())
val = [int(x) for x in input().split()]

res = 0
for i in range(num-1):
    if (val[i]+val[i+1]) > lim:
        res += (val[i]+val[i+1]) - lim
        if val[i+1] >= lim:
            val[i+1] -= (val[i]+val[i+1]) - lim
        else:
            val[i+1] = 0
print(res)

Submission status: Wrong Answer

Solution code:
num, lim = map(int, input().split())
val = [int(x) for x in input().split()]

res = 0
for i in range(num-1):
    if (val[i]+val[i+1]-lim) > 0:
        res += (val[i]+val[i+1]) - lim
        val[i+1] -= (val[i]+val[i+1]) - lim
print(res)

Submission status: Wrong Answer

Solution code:
num, lim = map(int, input().split())
val = [int(x) for x in input().split()]

array = []
for i in range(1, num, 2):
    if i == num - 1:
        res = val[i] + val[i - 1]
    else:
        res = val[i] + val[i + 1] if val[i] + val[i + 1] > val[i] + val[i - 1] else val[i] + val[i - 1]
    array.append(res)

res = 0
for j in array:
    res += j-lim if j > lim else 0
print(res)

Submission status: Wrong Answer

Solution code:
N,x=map(int,raw_input().split())
a=map(int,raw_input().split())
b=[ a[i]+a[i+1] for i in range(len(a)-1)]
#print b

cnt=0
for i in range(len(b)):
	if b[i]>x:
		t_val=b[i]-x
		cnt+=t_val
		if i<len(b)-1:
			b[i]-=t_val
			b[i+1]-=t_val
		else:
			b[i]-=t_val
print cnt

Submission status: Wrong Answer

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

count = 0;

flg = 1

sum_arr = []

for i in range(n-1):
    sum_arr.append(a[i]+a[i+1])


def taberu():
    m1 = 0
    m2 = 0
    max_val = x
    count = 0
    for i in range(n-2):
        if(x<sum_arr[i]+sum_arr[i+1]):
            if(a[i]<=a[i+1]):
                sum_arr[i] -= 1
                sum_arr[i+1] -= 1
                #print "del " , m2
            else:
                sum_arr[i-1] -= 1
                sum_arr[i] -= 1
                #print "del " , m1
            count += 1
    print count

taberu()

Submission status: Wrong Answer

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

count = 0;

flg = 1

def taberu():
    m1 = 0
    m2 = 0
    max_val = x
    for i in range(n-1):
        if(max_val<a[i]+a[i+1]):
            m1 = i
            m2 = i+1
            max_val = a[i]+a[i+1]

    if(a[m1]<=a[m2]):
        a[m2] = a[m2]-1
        print "del " , m2
    else:
        a[m1] = a[m1]-1
        print "del " , m1

    print "max_val ", max_val
    if(max_val==x):
        return 0
    else :
        return 1
    max_val = x

while(flg):
        print "count: ",count
        flg = taberu()
        count+=1

print count-1

Submission status: Wrong Answer

Solution code:
def solve():
    N, x = list(map(int, input().split(" ")))
    candies = list(map(int, input().split(" ")))
    cnt = 0
 
    if N % 2:
        for i in range(1, N, 2):
            if (candies[i - 1] + candies[i] > x) and (candies[i] + candies[i + 1] > x):
                cnt += max(candies[i - 1] + candies[i], candies[i] + candies[i + 1]) - x
                candies[i] -= max(candies[i - 1] + candies[i], candies[i] + candies[i + 1]) - x
            elif (candies[i - 1] + candies[i] > x) and (candies[i] + candies[i + 1] <= x):
                cnt += candies[i - 1] + candies[i] - x
                candies[i] -= candies[i - 1] + candies[i] - x
            elif (candies[i - 1] + candies[i] <= x) and (candies[i] + candies[i + 1] > x):
                cnt += candies[i] + candies[i + 1] - x
                candies[i + 1] -= candies[i] + candies[i + 1] - x
            else:
                pass
    else:
        print("III")
        if N == 2:
            if sum(candies) > x:
                cnt += sum(candies) - x
        else:
            for i in range(1, N + 1, 2):
                if i == N - 1:
                    if candies[i - 1] + candies[i] > x:
                        cnt += candies[i - 1] + candies[i] - x
                    else:
                        pass
                else:
                    if (candies[i - 1] + candies[i] > x) and (candies[i] + candies[i + 1] > x):
                        cnt += max(candies[i - 1] + candies[i], candies[i] + candies[i + 1]) - x
                        candies[i] -= max(candies[i - 1] + candies[i], candies[i] + candies[i + 1]) - x
                    elif (candies[i - 1] + candies[i] > x) and (candies[i] + candies[i + 1] <= x):
                        cnt += candies[i - 1] + candies[i] - x
                        candies[i + 1] -= candies[i - 1] + candies[i] - x
                    elif (candies[i - 1] + candies[i] <= x) and (candies[i] + candies[i + 1] > x):
                        cnt += candies[i] + candies[i + 1] - x
                        candies[i + 1] -= candies[i] + candies[i + 1] - x
                    else:
                        pass
 
    print(cnt)
 
 
solve()

Submission status: Wrong Answer

Solution code:
def solve():
    N, x = list(map(int, input().split(" ")))
    candies = list(map(int, input().split(" ")))
    cnt = 0
 
    if N % 2:
        print("aiueoooo")
        for i in range(1, N, 2):
            if (candies[i - 1] + candies[i] > x) and (candies[i] + candies[i + 1] > x):
                cnt += max(candies[i - 1] + candies[i], candies[i] + candies[i + 1]) - x
                candies[i] -= max(candies[i - 1] + candies[i], candies[i] + candies[i + 1]) - x
            elif (candies[i - 1] + candies[i] > x) and (candies[i] + candies[i + 1] <= x):
                cnt += candies[i - 1] + candies[i] - x
                candies[i] -= candies[i - 1] + candies[i] - x
            elif (candies[i - 1] + candies[i] <= x) and (candies[i] + candies[i + 1] > x):
                cnt += candies[i] + candies[i + 1] - x
                candies[i + 1] -= candies[i] + candies[i + 1] - x
            else:
                pass
    else:
        if N == 2:
            if sum(candies) > x:
                cnt += sum(candies) - x
        else:
            for i in range(1, N + 1, 2):
                if i == N - 1:
                    if candies[i - 1] + candies[i] > x:
                        cnt += candies[i - 1] + candies[i] - x
                    else:
                        pass
                else:
                    if (candies[i - 1] + candies[i] > x) and (candies[i] + candies[i + 1] > x):
                        cnt += max(candies[i - 1] + candies[i], candies[i] + candies[i + 1]) - x
                        candies[i] -= max(candies[i - 1] + candies[i], candies[i] + candies[i + 1]) - x
                    elif (candies[i - 1] + candies[i] > x) and (candies[i] + candies[i + 1] <= x):
                        cnt += candies[i - 1] + candies[i] - x
                        candies[i + 1] -= candies[i - 1] + candies[i] - x
                    elif (candies[i - 1] + candies[i] <= x) and (candies[i] + candies[i + 1] > x):
                        cnt += candies[i] + candies[i + 1] - x
                        candies[i + 1] -= candies[i] + candies[i + 1] - x
                    else:
                        pass
 
    print(cnt)
 
 
solve()

Submission status: Wrong Answer

Solution code:
def solve():
    N, x = list(map(int, input().split(" ")))
    candies = list(map(int, input().split(" ")))
    cnt = 0

    if N % 2:
        for i in range(1, N, 2):
            if (candies[i - 1] + candies[i] > x) and (candies[i] + candies[i + 1] > x):
                cnt += max(candies[i - 1] + candies[i], candies[i] + candies[i + 1]) - x
                candies[i] -= max(candies[i - 1] + candies[i], candies[i] + candies[i + 1]) - x
            elif (candies[i - 1] + candies[i] > x) and (candies[i] + candies[i + 1] <= x):
                cnt += candies[i - 1] + candies[i] - x
                candies[i] -= candies[i - 1] + candies[i] - x
            elif (candies[i - 1] + candies[i] <= x) and (candies[i] + candies[i + 1] > x):
                cnt += candies[i] + candies[i + 1] - x
                candies[i + 1] -= candies[i] + candies[i + 1] - x
            else:
                pass
    else:
        if N == 2:
            if sum(candies) > x:
                cnt += sum(candies) - x
        else:
            for i in range(1, N + 1, 2):
                if i == N - 1:
                    if candies[i - 1] + candies[i] > x:
                        cnt += candies[i - 1] + candies[i] - x
                    else:
                        pass
                else:
                    if (candies[i - 1] + candies[i] > x) and (candies[i] + candies[i + 1] > x):
                        cnt += max(candies[i - 1] + candies[i], candies[i] + candies[i + 1]) - x
                        candies[i] -= max(candies[i - 1] + candies[i], candies[i] + candies[i + 1]) - x
                    elif (candies[i - 1] + candies[i] > x) and (candies[i] + candies[i + 1] <= x):
                        cnt += candies[i - 1] + candies[i] - x
                        candies[i + 1] -= candies[i - 1] + candies[i] - x
                    elif (candies[i - 1] + candies[i] <= x) and (candies[i] + candies[i + 1] > x):
                        cnt += candies[i] + candies[i + 1] - x
                        candies[i + 1] -= candies[i] + candies[i + 1] - x
                    else:
                        pass

    print(cnt)


solve()