#P3817. Candy Boxes Minimization

    ID: 17067 Type: Default 1000ms 256MiB

Candy Boxes Minimization

Candy Boxes Minimization

Little A has n candy boxes. The i-th box contains a_i candies. Little A can remove (i.e. eat) one candy at a time from any box. He wants to achieve the condition that for every two consecutive boxes, the sum of candies is at most x. Formally, after some removals, let the number of candies in the boxes be \(b_1, b_2, \dots, b_n\) (with \(0 \le b_i \le a_i\)). They must satisfy

[ b_i + b_{i+1} \le x, \quad \text{for } i=1,2,\dots, n-1. ]

Determine the minimum number of candies that must be eaten so that the above condition is met.

Note: If there is only one box (i.e. \(n=1\)), no condition between adjacent boxes exists, so no candy needs to be eaten.

inputFormat

The first line contains two integers \(n\) and \(x\) (the number of boxes and the maximum allowed sum for any adjacent boxes).

The second line contains \(n\) integers \(a_1, a_2, \dots, a_n\), where \(a_i\) is the number of candies in the \(i\)-th box.

outputFormat

Output a single integer ― the minimum number of candies that must be eaten to satisfy \(b_i+b_{i+1} \le x\) for all \(1 \le i < n\).

sample

2 4
3 2
1