fun main(arr: Array<String>) {
    val (n,x) = readLine()!!.split(" ").map { it.toInt() }
    val a = readLine()!!.split(" ").map { it.toLong() }.toMutableList()
    var ans = 0L
    for(i in (1 until n)) {
        while (a[i-1]+a[i] > x) {
            var idx = i
            if(i == a.lastIndex && a[i-1] > 0) {
                idx = i-1
            } else if(i-1 == 0 && a[i] > 0) {
                idx = i
            } else if(a[i] > 0) {
                idx = i
            } else {
                idx = i-1
            }
            val eat = Math.min(Math.max((a[i-1]+a[i]) - x, 0), a[idx])
            a[idx] -= eat
            ans += eat
        }
    }
    println(ans)
}