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.

Solution in Rust (1.15.1) (judge verdict: Accepted)

use std::io::*;
use std::str::FromStr;
use utils::*;

pub fn main() {
    let i = stdin();
    let mut o = Vec::new();
    run(i.lock(), &mut o);
    stdout().write_all(&o).unwrap();
}

fn run<R: BufRead, W: Write>(i: R, o: &mut W) {
    let mut i = AtRead::from(i);
    let (n, x) = i.read2::<usize, usize>();
    let a = i.read_vec::<usize>(n);

    writeln!(o, "{}", solve(&a, x)).unwrap();
}
fn solve(a: &[usize], x: usize) -> usize {
    let mut b = Vec::new();
    b.push(0);
    b.push(a[0]);
    for i in 0..a.len() - 1 {
        b.push(a[i] + a[i + 1]);
    }
    b.push(a[a.len() - 1]);
    b.push(0);
    for bi in &mut b {
        if *bi > x {
            *bi = *bi - x;
        } else {
            *bi = 0;
        }
    }

    fn eat(b: &mut usize, value: usize) {
        if *b > value {
            *b -= value;
        } else {
            *b = 0;
        }
    }

    let mut result = 0;
    let mut changed = true;
    while changed {
        changed = false;
        for i in 1..b.len() - 1 {
            let bi = b[i];
            if bi == 0 {
                continue;
            }
            if b[i - 1] == 0 {
                eat(&mut b[i], bi);
                eat(&mut b[i + 1], bi);
                result += bi;
            } else if b[i + 1] == 0 {
                eat(&mut b[i - 1], bi);
                eat(&mut b[i], bi);
                result += bi;
            }
            changed = true;
        }
    }
    result
}

mod utils {
    use super::*;

    pub struct AtRead<R: BufRead> {
        r: R,
        s: String,
    }

    macro_rules! fn_read {
        {$f:ident($($v:ident: $t:ident),*)} => {
            pub fn $f<$($t: FromStr),*>(&mut self) -> ($($t),*) {
                let i = &mut self.read_line().split(' ');
                $(
                    let $v = next(i);
                )*
                ($($v),*)
            }
        };
    }

    impl<R: BufRead> AtRead<R> {
        pub fn from(r: R) -> Self {
            AtRead {
                r: r,
                s: String::new(),
            }
        }
        pub fn read_line(&mut self) -> &str {
            self.s.clear();
            self.r.read_line(&mut self.s).unwrap();
            self.s.trim()
        }

        fn_read! { read2(v1: T1, v2: T2) }

        pub fn read_vec<T: FromStr>(&mut self, n: usize) -> Vec<T> {
            self.read_line()
                .split(' ')
                .take(n)
                .map(|x| x.parse().ok().unwrap())
                .collect()
        }
    }

    fn next<'a, T: FromStr, I: std::iter::Iterator<Item = &'a str>>(i: &mut I) -> T {
        i.next().unwrap().parse().ok().unwrap()
    }

}