#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef pair<ll,ll> P;
typedef pair<P,ll> PLL;
typedef pair<P,P> PP;
typedef vector<ll> VECLL;
typedef vector<P> VECP;
typedef priority_queue<P,VECP,greater<P> > PQP;
typedef priority_queue<ll, VECLL, greater<ll> > PQll;

#define rep(i,a,n) for(ll i = a;i < n;i++)   
#define rrep(i,a,n) for(ll i = n; i >= a;i--)
#define index_check(i,n) if(i > n-1 || i < 0) continue; 
#define LINF (ll)1e18
#define INF (int)1e9
#define fs first
#define sc second

#include <bits/stdc++.h>
#define rep(i,a,n) for(ll i = a;i < n;i++)
typedef long long ll;
using namespace std;

int main(){
    ll n,k;
    cin >> n >> k;
    ll a[n] = {};
    rep(i,0,n) cin >> a[i];

    ll ans = 0;
    rep(i,0,n-1){
        if(a[i] + a[i+1] > k){
            if(a[i] >= k){
                ans += a[i+1] + a[i] - k;
                a[i] = k;
                a[i+1] = 0;
            }
            else{
                ans += a[i]+a[i+1]-k;
                a[i+1] -= a[i]+a[i+1]-k; 
            }
        }
    }
    cout << ans << endl;
}

#include <iostream>
#include <string>
#include <algorithm>
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
#include <iomanip>
#include <cmath>
#include <stdio.h>
#include <queue>
#include <deque>
#include <cstdio>
#include <set>
#include <map>
#include <bitset>
#include <stack>
#include <cctype>
using namespace std;
set<pair<long long, long long>> st;
long long sum = 0;
int main() {
	long long n, m;
	cin >> n >> m;
	long long ans = 0;
	st.insert(make_pair(0, 1));
	for (int i = 0; i < n; i++) {
		long long a;
		cin >> a;
		sum = (sum + a) % m;
		pair<long long, long long> pa;
		if (st.lower_bound(make_pair(sum, 0)) != st.end()) {
			pa = *st.lower_bound(make_pair(sum, 0));
			if (sum == pa.first) {
				ans += pa.second;
				st.erase(*st.lower_bound(make_pair(sum, 0)));
				st.insert(make_pair(sum, pa.second + 1));
			}
			else {
				st.insert(make_pair(sum, 1));
			}
		}
		else {
			st.insert(make_pair(sum, 1));
		}
	}
	cout << ans << endl;
	return 0;
}