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.42.0) (judge verdict: Accepted)

// -*- coding:utf-8-unix -*-
use std::cmp;
use std::collections::BTreeMap;
use std::collections::BTreeSet;
use std::collections::BinaryHeap;
use std::collections::HashMap;
use std::collections::HashSet;
use std::collections::LinkedList;
use std::collections::VecDeque;
use std::fs::File;
use std::io;
use std::io::prelude::*;
 


const INF: i64 = 1223372036854775807;

macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        let mut next = || { iter.next().unwrap() };
        input_inner!{next, $($r)*}
    };
    ($($r:tt)*) => {
        let stdin = std::io::stdin();
        let mut bytes = std::io::Read::bytes(std::io::BufReader::new(stdin.lock()));
        let mut next = move || -> String{
            bytes
                .by_ref()
                .map(|r|r.unwrap() as char)
                .skip_while(|c|c.is_whitespace())
                .take_while(|c|!c.is_whitespace())
                .collect()
        };
        input_inner!{next, $($r)*}
    };
}

macro_rules! input_inner {
    ($next:expr) => {};
    ($next:expr, ) => {};

    ($next:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($next, $t);
        input_inner!{$next $($r)*}
    };
}

macro_rules! read_value {
    ($next:expr, ( $($t:tt),* )) => {
        ( $(read_value!($next, $t)),* )
    };

    ($next:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($next, $t)).collect::<Vec<_>>()
    };

    ($next:expr, chars) => {
        read_value!($next, String).chars().collect::<Vec<char>>()
    };

    ($next:expr, usize1) => {
        read_value!($next, usize) - 1
    };

    ($next:expr, $t:ty) => {
        $next().parse::<$t>().expect("Parse error")
    };
}

use std::cmp::*;
use std::collections::*;
use std::io::stdin;
use std::io::stdout;
use std::io::Write;
type vi = Vec<i64>;
type vf = Vec<f64>;
type vu = Vec<usize>;
type vvi = Vec<Vec<i64>>;
type vvf = Vec<Vec<f64>>;
type vvu = Vec<Vec<usize>>;

fn readi() -> i64 {
    let mut str = String::new();
    let _ = stdin().read_line(&mut str).unwrap();
    str.trim().parse::<i64>().unwrap()
}

fn readii() -> (i64, i64) {
    let mut str = String::new();
    let _ = stdin().read_line(&mut str).unwrap();
    let mut iter = str.split_whitespace();
    (
        iter.next().unwrap().parse::<i64>().unwrap(),
        iter.next().unwrap().parse::<i64>().unwrap(),
    )
}
fn readiii() -> (i64, i64, i64) {
    let mut str = String::new();
    let _ = stdin().read_line(&mut str).unwrap();
    let mut iter = str.split_whitespace();
    (
        iter.next().unwrap().parse::<i64>().unwrap(),
        iter.next().unwrap().parse::<i64>().unwrap(),
        iter.next().unwrap().parse::<i64>().unwrap(),
    )
}
fn readvi() -> vi {
    let mut str = String::new();
    let _ = stdin().read_line(&mut str).unwrap();
    let iter = str.split_whitespace();
    iter.fold(Vec::new(), |mut acc, x| {
        acc.push(x.parse::<i64>().unwrap());
        acc
    })
}

fn readvvi(n: usize) -> vvi {
    let mut res: vvi = Vec::new();
    for _ in 0..n {
        let mut str = String::new();
        let _ = stdin().read_line(&mut str).unwrap();
        let iter = str.split_whitespace();
        res.push(iter.fold(Vec::new(), |mut acc, x| {
            acc.push(x.parse::<i64>().unwrap());
            acc
        }));
    }
    res
}

fn prtni(n: i64) {
    println!("{}", n);
}
fn prti(n: i64) {
    print!("{} ", n);
}

fn readu() -> usize {
    let mut str = String::new();
    let _ = stdin().read_line(&mut str).unwrap();
    str.trim().parse::<usize>().unwrap()
}

fn readuu() -> (usize, usize) {
    let mut str = String::new();
    let _ = stdin().read_line(&mut str).unwrap();
    let mut iter = str.split_whitespace();
    (
        iter.next().unwrap().parse::<usize>().unwrap(),
        iter.next().unwrap().parse::<usize>().unwrap(),
    )
}
fn readuuu() -> (usize, usize, usize) {
    let mut str = String::new();
    let _ = stdin().read_line(&mut str).unwrap();
    let mut iter = str.split_whitespace();
    (
        iter.next().unwrap().parse::<usize>().unwrap(),
        iter.next().unwrap().parse::<usize>().unwrap(),
        iter.next().unwrap().parse::<usize>().unwrap(),
    )
}
fn readvu() -> vu {
    let mut str = String::new();
    let _ = stdin().read_line(&mut str).unwrap();
    let iter = str.split_whitespace();
    iter.fold(Vec::new(), |mut acc, x| {
        acc.push(x.parse::<usize>().unwrap());
        acc
    })
}

fn readvvu(n: usize) -> vvu {
    let mut res: vvu = Vec::new();
    for _ in 0..n {
        let mut str = String::new();
        let _ = stdin().read_line(&mut str).unwrap();
        let iter = str.split_whitespace();
        res.push(iter.fold(Vec::new(), |mut acc, x| {
            acc.push(x.parse::<usize>().unwrap());
            acc
        }));
    }
    res
}

fn prtnu(n: usize) {
    println!("{}", n);
}
fn prtu(n: usize) {
    print!("{} ", n);
}

fn readf() -> f64 {
    let mut str = String::new();
    let _ = stdin().read_line(&mut str).unwrap();
    str.trim().parse::<f64>().unwrap()
}

fn readff() -> (f64, f64) {
    let mut str = String::new();
    let _ = stdin().read_line(&mut str).unwrap();
    let mut iter = str.split_whitespace();
    (
        iter.next().unwrap().parse::<f64>().unwrap(),
        iter.next().unwrap().parse::<f64>().unwrap(),
    )
}
fn readfff() -> (f64, f64, f64) {
    let mut str = String::new();
    let _ = stdin().read_line(&mut str).unwrap();
    let mut iter = str.split_whitespace();
    (
        iter.next().unwrap().parse::<f64>().unwrap(),
        iter.next().unwrap().parse::<f64>().unwrap(),
        iter.next().unwrap().parse::<f64>().unwrap(),
    )
}
fn readvf() -> vf {
    let mut str = String::new();
    let _ = stdin().read_line(&mut str).unwrap();
    let iter = str.split_whitespace();
    iter.fold(Vec::new(), |mut acc, x| {
        acc.push(x.parse::<f64>().unwrap());
        acc
    })
}

fn readvvf(n: usize) -> vvf {
    let mut res: vvf = Vec::new();
    for _ in 0..n {
        let mut str = String::new();
        let _ = stdin().read_line(&mut str).unwrap();
        let iter = str.split_whitespace();
        res.push(iter.fold(Vec::new(), |mut acc, x| {
            acc.push(x.parse::<f64>().unwrap());
            acc
        }));
    }
    res
}

fn prtnf(n: f64) {
    println!("{}", n);
}
fn prtf(n: f64) {
    print!("{} ", n);
}



fn solve(){
    let(n,x) = readii();
    let mut vec =readvi();
    let mut res:i64 = 0;
    for i in 0..(n-1) {
        let index:usize = i as usize;

        if vec[index] + vec[index+1] > x {
            let diff = vec[index] + vec[index+1] - x;
            res += diff;
            vec[index+1] -= diff;
            if vec[index+1] < 0 {
                vec[index+1] = 0;
                
            }
        }
    }
    println!("{:?}", res);



}
fn main() {
    solve()
    
}