stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include <iostream>

#include <cstdio>

#include <algorithm>

#include <cstring>

#include <cctype>

#include <sstream>

#include <cmath>

using namespace std;

const int inf =0x3f3f3f3f;

typedef long long LL;

const int MAXN=100010;

int a[MAXN];

int main()

{

    LL n, x;

    while(~scanf("%lld %lld", &n, &x))

    {

        LL sum=0;

        for(int i=1;i<=n;++i)

            scanf("%d", &a[i]);

        for(int i=1;i<=n-1;++i)

        {

            if(a[i]+a[i+1]>=x)

            {

                LL t=a[i]+a[i+1]-x;

                sum+=t;

                if(t>=a[i+1])

                    a[i+1]=0;

                else

                    a[i+1]-=t;

            }

        }

        printf("%lld\n", sum);

    }

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.180386825587379x):
#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<algorithm>

#include<cmath>

#include<ctime>

#include<vector>

#include<queue>

#include<stack>

#include<map>

using namespace std;

#define FOR(i,s,e) for (int i=s;i<e;i++)

#define FOE(i,s,e) for (int i=s;i<=e;i++)

#define FOD(i,s,e) for (int i=s;i>=e;i--)

#define SET(a,e) memset(a,e,sizeof(a))

#define LL long long

#define LD long double

#define pb push_back

#define x first

#define y second

#define PII pair<int,int>

#define PLI pair<LL,int>

#define PIL pair<int,LL>

#define PLL pair<LL,LL>

#define eps 1e-9

#define mp make_pair



int n,x,a[100005];

LL ans;



int main () {

	scanf("%d%d",&n,&x);

	a[0]=0;

	FOE(i,1,n) {

		scanf("%d",&a[i]);

		if (a[i]>x) {

			ans=(LL)ans+a[i]-x;	

			a[i]=x;

		}

		if (a[i]+a[i-1]>x) {

			ans=(LL)ans+a[i]+a[i-1]-x;

			a[i]-=(a[i]+a[i-1]-x);

		}

		//printf("%lld %lld\n",ans,a[i]);

	}

	printf("%lld\n",ans);

}
// Slower C++ solution:
#include<iostream>

#include<algorithm>

#include<cstdio>

#include<cstring>

#include<vector>

#include<queue>

#include<map>

#include<cmath>

#define N 100009

using namespace std;

const int INF = 0x3f3f3f3f;

int a[N];



int main()

{

    int n, x;

    scanf("%d%d", &n, &x);

    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);

    long long ans, now; ans = now = 0;

    for(int i = 1; i <= n; i++)

    {

        if(now + a[i] <= x) now = a[i];

        else ans += now + a[i] - x, now = a[i] - (now + a[i] - x);

    }

    printf("%lld\n", ans);

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.210493141358659x):
#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<algorithm>

#include<cmath>

#include<ctime>

#include<vector>

#include<queue>

#include<stack>

#include<map>

using namespace std;

#define FOR(i,s,e) for (int i=s;i<e;i++)

#define FOE(i,s,e) for (int i=s;i<=e;i++)

#define FOD(i,s,e) for (int i=s;i>=e;i--)

#define SET(a,e) memset(a,e,sizeof(a))

#define LL long long

#define LD long double

#define pb push_back

#define x first

#define y second

#define PII pair<int,int>

#define PLI pair<LL,int>

#define PIL pair<int,LL>

#define PLL pair<LL,LL>

#define eps 1e-9

#define mp make_pair



int n,x,a[100005];

LL ans;



int main () {

	scanf("%d%d",&n,&x);

	a[0]=0;

	FOE(i,1,n) {

		scanf("%d",&a[i]);

		if (a[i]>x) {

			ans=(LL)ans+a[i]-x;	

			a[i]=x;

		}

		if (a[i]+a[i-1]>x) {

			ans=(LL)ans+a[i]+a[i-1]-x;

			a[i]-=(a[i]+a[i-1]-x);

		}

		//printf("%lld %lld\n",ans,a[i]);

	}

	printf("%lld\n",ans);

}
// Slower C++ solution:
#include<stdio.h>

#include<string.h>

#include<algorithm>

using namespace std;

int a[1000001];

main()

{

    int i,j;

    long long n,m,sum,p;

    long long a[100001];

    memset(a,0,sizeof(a));

    scanf("%lld %lld",&n,&m);

    for(i=0;i<n;i++)

        scanf("%lld",&a[i]);

    sum=0;

    for(i=0;i<n;i++)

    {

        if (a[i]+a[i+1]>m)

        {

            sum+=a[i]+a[i+1]-m;

             //printf("%lld\n",sum);

            p=a[i]+a[i+1]-m;

            a[i+1]=a[i+1]-p;

            //printf("%lld\n",a[i+1]);

            p=0;

        }

    }

    printf("%lld\n",sum);

}


// Faster C++ solution for the same problem (measured speedup: 5.223271065349043x):
#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<algorithm>

#include<cmath>

#include<ctime>

#include<vector>

#include<queue>

#include<stack>

#include<map>

using namespace std;

#define FOR(i,s,e) for (int i=s;i<e;i++)

#define FOE(i,s,e) for (int i=s;i<=e;i++)

#define FOD(i,s,e) for (int i=s;i>=e;i--)

#define SET(a,e) memset(a,e,sizeof(a))

#define LL long long

#define LD long double

#define pb push_back

#define x first

#define y second

#define PII pair<int,int>

#define PLI pair<LL,int>

#define PIL pair<int,LL>

#define PLL pair<LL,LL>

#define eps 1e-9

#define mp make_pair



int n,x,a[100005];

LL ans;



int main () {

	scanf("%d%d",&n,&x);

	a[0]=0;

	FOE(i,1,n) {

		scanf("%d",&a[i]);

		if (a[i]>x) {

			ans=(LL)ans+a[i]-x;	

			a[i]=x;

		}

		if (a[i]+a[i-1]>x) {

			ans=(LL)ans+a[i]+a[i-1]-x;

			a[i]-=(a[i]+a[i-1]-x);

		}

		//printf("%lld %lld\n",ans,a[i]);

	}

	printf("%lld\n",ans);

}
// Slower C++ solution:
#include <iostream>

#include <algorithm>

#include <cstdio>

#include <cmath>

#include <cstdlib>

#include <cstring>

#include <climits>

#include <cctype>

#include <map>

#include <list>

#include <stack>

#include <queue>

#include <vector>

#include <set>

#define mem(a,b) memset((a),(b),sizeof(a))

using namespace std;

const int INF=0x3f3f3f3f;

const double PI=acos(-1.0);

const double eps=1e-12;

const int MAX=100000+10;

const int EDGE=100000+10;//边的条数

typedef long long ll;

int main()

{

	ll n,x,i,a[MAX],t,ans;

	while(~scanf("%lld%lld",&n,&x))

	{

		mem(a,0);

		t=0;

		for(i=0;i<n;i++)

		{

			scanf("%lld",&a[i]);

		}

		ans=0;

		if(a[0]>x)

		{

			t=a[0]-x;

			ans+=t;

			a[0]-=t;

		}

		for(i=1;i<n;i++)

		{

			if(a[i]+a[i-1]>x)

			{

				t=a[i]+a[i-1]-x;

				ans+=t;

				a[i]-=t;

			}

		}

	//	if(a[n-2]+a[n-1]>x) ans+=a[n-2]+a[n-1]-x;

		printf("%lld\n",ans);

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 9.73364573267664x):
#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<algorithm>

#include<cmath>

#include<ctime>

#include<vector>

#include<queue>

#include<stack>

#include<map>

using namespace std;

#define FOR(i,s,e) for (int i=s;i<e;i++)

#define FOE(i,s,e) for (int i=s;i<=e;i++)

#define FOD(i,s,e) for (int i=s;i>=e;i--)

#define SET(a,e) memset(a,e,sizeof(a))

#define LL long long

#define LD long double

#define pb push_back

#define x first

#define y second

#define PII pair<int,int>

#define PLI pair<LL,int>

#define PIL pair<int,LL>

#define PLL pair<LL,LL>

#define eps 1e-9

#define mp make_pair



int n,x,a[100005];

LL ans;



int main () {

	scanf("%d%d",&n,&x);

	a[0]=0;

	FOE(i,1,n) {

		scanf("%d",&a[i]);

		if (a[i]>x) {

			ans=(LL)ans+a[i]-x;	

			a[i]=x;

		}

		if (a[i]+a[i-1]>x) {

			ans=(LL)ans+a[i]+a[i-1]-x;

			a[i]-=(a[i]+a[i-1]-x);

		}

		//printf("%lld %lld\n",ans,a[i]);

	}

	printf("%lld\n",ans);

}
// Slower C++ solution:
#include<stdio.h>

#include<string.h>

#include<stdlib.h>

#include<algorithm>

#include<iostream>

#include<ctype.h>

#include<stack>

#include<queue>

#include <cmath>  

using namespace std;

#include <stdio.h>

#include <stdlib.h>

#include <math.h>

#include <string.h>

#include <algorithm>

using namespace std;

#define  inf 0x3fffffff 

int main()

{

	int i, n, x, a[110000];

	long long sum;

	while (cin>>n>>x)

	{

		sum = 0;

		for (i = 1; i <= n; i++)

			scanf("%d", &a[i]);

		if (a[1] > x)

		{

			sum += a[1] - x;

			a[1] = x;

		}

		for (i = 2; i <= n; i++)

		{

			int flag = a[i] + a[i - 1] - x;

			if (flag > 0)

			{

				sum += flag;

				a[i] -= flag;

			}

		}

		cout << sum << endl;

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.304646136372127x):
#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<algorithm>

#include<cmath>

#include<ctime>

#include<vector>

#include<queue>

#include<stack>

#include<map>

using namespace std;

#define FOR(i,s,e) for (int i=s;i<e;i++)

#define FOE(i,s,e) for (int i=s;i<=e;i++)

#define FOD(i,s,e) for (int i=s;i>=e;i--)

#define SET(a,e) memset(a,e,sizeof(a))

#define LL long long

#define LD long double

#define pb push_back

#define x first

#define y second

#define PII pair<int,int>

#define PLI pair<LL,int>

#define PIL pair<int,LL>

#define PLL pair<LL,LL>

#define eps 1e-9

#define mp make_pair



int n,x,a[100005];

LL ans;



int main () {

	scanf("%d%d",&n,&x);

	a[0]=0;

	FOE(i,1,n) {

		scanf("%d",&a[i]);

		if (a[i]>x) {

			ans=(LL)ans+a[i]-x;	

			a[i]=x;

		}

		if (a[i]+a[i-1]>x) {

			ans=(LL)ans+a[i]+a[i-1]-x;

			a[i]-=(a[i]+a[i-1]-x);

		}

		//printf("%lld %lld\n",ans,a[i]);

	}

	printf("%lld\n",ans);

}
// Slower C++ solution:
#include <map>

#include <math.h>

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <iostream>

#include <algorithm>

using namespace std;

#define INF 0x3f3f3f3f







int main()

{

	

	int i, a[200000];

	long long n, x, z, sum;

	std::ios::sync_with_stdio(false);

	while (cin >> n >> x)

	{

		z = 0;

		sum = 0;

		for (i = 0; i < n; i++)

			cin >> a[i];

		for (i = 0; i < n; i++)

		{

			if (a[i] + a[i + 1] > x)

			{

				sum += a[i + 1];

				a[i + 1] = x - a[i];

				sum -= a[i + 1];

			}

		}

		cout << sum << endl;

	}





	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.436628158540808x):
#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<algorithm>

#include<cmath>

#include<ctime>

#include<vector>

#include<queue>

#include<stack>

#include<map>

using namespace std;

#define FOR(i,s,e) for (int i=s;i<e;i++)

#define FOE(i,s,e) for (int i=s;i<=e;i++)

#define FOD(i,s,e) for (int i=s;i>=e;i--)

#define SET(a,e) memset(a,e,sizeof(a))

#define LL long long

#define LD long double

#define pb push_back

#define x first

#define y second

#define PII pair<int,int>

#define PLI pair<LL,int>

#define PIL pair<int,LL>

#define PLL pair<LL,LL>

#define eps 1e-9

#define mp make_pair



int n,x,a[100005];

LL ans;



int main () {

	scanf("%d%d",&n,&x);

	a[0]=0;

	FOE(i,1,n) {

		scanf("%d",&a[i]);

		if (a[i]>x) {

			ans=(LL)ans+a[i]-x;	

			a[i]=x;

		}

		if (a[i]+a[i-1]>x) {

			ans=(LL)ans+a[i]+a[i-1]-x;

			a[i]-=(a[i]+a[i-1]-x);

		}

		//printf("%lld %lld\n",ans,a[i]);

	}

	printf("%lld\n",ans);

}
// Slower C++ solution:
#include <bits/stdc++.h>

#define sz(x) ((int)(x).size())

#define rep(i,a,b) for (int i=(a); i<(b); i++)

using namespace std;

typedef long long ll;

const int MAX_N = 7 + 100000;

///----------------------------------------------

int dig[MAX_N];

int main() {



    //freopen ( "xx.in" ,  "r" , stdin );



    ///read

    int n,m; cin>>n>>m;

    rep(i,1,n+1) scanf("%d",&dig[i]);



    ///work

    ll ans=0;

    rep(i,1,n+1) if (dig[i]+dig[i-1]>m) {

        ans+=dig[i]+dig[i-1]-m;

        dig[i]=m-dig[i-1];

    }

    cout << ans;



}


// Faster C++ solution for the same problem (measured speedup: 5.225842114918277x):
#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<algorithm>

#include<cmath>

#include<ctime>

#include<vector>

#include<queue>

#include<stack>

#include<map>

using namespace std;

#define FOR(i,s,e) for (int i=s;i<e;i++)

#define FOE(i,s,e) for (int i=s;i<=e;i++)

#define FOD(i,s,e) for (int i=s;i>=e;i--)

#define SET(a,e) memset(a,e,sizeof(a))

#define LL long long

#define LD long double

#define pb push_back

#define x first

#define y second

#define PII pair<int,int>

#define PLI pair<LL,int>

#define PIL pair<int,LL>

#define PLL pair<LL,LL>

#define eps 1e-9

#define mp make_pair



int n,x,a[100005];

LL ans;



int main () {

	scanf("%d%d",&n,&x);

	a[0]=0;

	FOE(i,1,n) {

		scanf("%d",&a[i]);

		if (a[i]>x) {

			ans=(LL)ans+a[i]-x;	

			a[i]=x;

		}

		if (a[i]+a[i-1]>x) {

			ans=(LL)ans+a[i]+a[i-1]-x;

			a[i]-=(a[i]+a[i-1]-x);

		}

		//printf("%lld %lld\n",ans,a[i]);

	}

	printf("%lld\n",ans);

}
// Slower C++ solution:
#include <bits/stdc++.h>



using namespace std;



int main()

{

	long arr[100005];

	int n;

	long x;

	cin >> n >> x;

	for(int i = 0; i < n; i++) {

		cin >> arr[i];

   	}

   	long long ans = 0;

   	if(arr[0] > x) {

   	 	ans += arr[0] - x;

   	 	arr[0] = x;

   	}

   	for(int i = 1; i < n; i++) {

   		if((arr[i] + arr[i - 1] - x ) > 0) {

   		 	ans += (arr[i] - x + arr[i - 1]);

   		 	arr[i] -= (arr[i] - x + arr[i - 1]) ;

   		}

   	}

   	cout << ans << endl;



	return 0;

}


// Faster C++ solution for the same problem (measured speedup: 5.314835335175984x):
#include<cstdio>

#include<cstdlib>

#include<cstring>

#include<algorithm>

#include<cmath>

#include<ctime>

#include<vector>

#include<queue>

#include<stack>

#include<map>

using namespace std;

#define FOR(i,s,e) for (int i=s;i<e;i++)

#define FOE(i,s,e) for (int i=s;i<=e;i++)

#define FOD(i,s,e) for (int i=s;i>=e;i--)

#define SET(a,e) memset(a,e,sizeof(a))

#define LL long long

#define LD long double

#define pb push_back

#define x first

#define y second

#define PII pair<int,int>

#define PLI pair<LL,int>

#define PIL pair<int,LL>

#define PLL pair<LL,LL>

#define eps 1e-9

#define mp make_pair



int n,x,a[100005];

LL ans;



int main () {

	scanf("%d%d",&n,&x);

	a[0]=0;

	FOE(i,1,n) {

		scanf("%d",&a[i]);

		if (a[i]>x) {

			ans=(LL)ans+a[i]-x;	

			a[i]=x;

		}

		if (a[i]+a[i-1]>x) {

			ans=(LL)ans+a[i]+a[i-1]-x;

			a[i]-=(a[i]+a[i-1]-x);

		}

		//printf("%lld %lld\n",ans,a[i]);

	}

	printf("%lld\n",ans);

}
// Slower C++ solution:
#include <iostream>

#include <algorithm>

#include <cstdio>

#include <cmath>

#include <cstdlib>

#include <cstring>

#include <climits>

#include <cctype>

#include <map>

#include <list>

#include <stack>

#include <queue>

#include <vector>

#include <set>

#define mem(a,b) memset((a),(b),sizeof(a))

using namespace std;

const int INF=0x3f3f3f3f;

const double PI=acos(-1.0);

const double eps=1e-12;

const int MAX=100000+10;

const int EDGE=100000+10;//边的条数

typedef long long ll;

int main()

{

	ll n,x,i,a[MAX],t,ans;

	while(~scanf("%lld%lld",&n,&x))

	{

		mem(a,0);

		t=0;

		for(i=0;i<n;i++)

		{

			scanf("%lld",&a[i]);

		}

		ans=0;

		if(a[0]>x)

		{

			t=a[0]-x;

			ans+=t;

			a[0]-=t;

		}

		for(i=1;i<n;i++)

		{

			if(a[i]+a[i-1]>x)

			{

				t=a[i]+a[i-1]-x;

				ans+=t;

				a[i]-=t;

			}

		}

	//	if(a[n-2]+a[n-1]>x) ans+=a[n-2]+a[n-1]-x;

		printf("%lld\n",ans);

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.8643848673520238x):
#include <cstdio>

#include <algorithm>

#include <iostream>

#include <cstring>

using namespace std;



const int maxn = 1e5 + 5;

long long a[maxn],n,x;

long long ans;



int main()

{

    while(scanf("%lld%lld",&n,&x) != EOF)

    {

        ans = 0;

        for(int i = 1;i <= n;i++)

        {

            scanf("%lld",&a[i]);

        }

        for(int i = 2;i <= n;i++)

        {

            int k = a[i] + a[i - 1];

            if(k <= x)

            {

                continue;

            }

            if(a[i - 1] <= x)

            {

                ans = ans + a[i] - (x - a[i - 1]);

                a[i] = x - a[i - 1];

            }

            else

            {

                ans += k - x;

                a[i] = 0;

            }



        }

        printf("%lld\n",ans);

    }

    return 0;

}
// Slower C++ solution:
#include <iostream>

#include <algorithm>

#include <cstdio>

#include <cmath>

#include <cstdlib>

#include <cstring>

#include <climits>

#include <cctype>

#include <map>

#include <list>

#include <stack>

#include <queue>

#include <vector>

#include <set>

#define mem(a,b) memset((a),(b),sizeof(a))

using namespace std;

const int INF=0x3f3f3f3f;

const double PI=acos(-1.0);

const double eps=1e-12;

const int MAX=100000+10;

const int EDGE=100000+10;//边的条数

typedef long long ll;

int main()

{

	ll n,x,i,a[MAX],t,ans;

	while(~scanf("%lld%lld",&n,&x))

	{

		mem(a,0);

		t=0;

		for(i=0;i<n;i++)

		{

			scanf("%lld",&a[i]);

		}

		ans=0;

		if(a[0]>x)

		{

			t=a[0]-x;

			ans+=t;

			a[0]-=t;

		}

		for(i=1;i<n;i++)

		{

			if(a[i]+a[i-1]>x)

			{

				t=a[i]+a[i-1]-x;

				ans+=t;

				a[i]-=t;

			}

		}

	//	if(a[n-2]+a[n-1]>x) ans+=a[n-2]+a[n-1]-x;

		printf("%lld\n",ans);

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.8599250642371665x):
#include<bits/stdc++.h>

using namespace std;

int a[100005];



int main() {

    int n,x;

    cin>>n>>x;

    for(int i=0;i<n;++i) cin>>a[i];



    long long cnt = 0;

	if(a[0]>x) {

		cnt = a[0] - x;

		a[0] = x;

	}

    for(int i=1;i<n;++i) {

        if(a[i-1] + a[i] > x) {

            cnt += a[i-1]+a[i]-x;

            a[i] -= a[i-1]+a[i]-x;

        }

    }

    cout<<cnt<<endl;

    return 0;

}
// Slower C++ solution:
#include <iostream>

#include <algorithm>

#include <cstdio>

#include <cmath>

#include <cstdlib>

#include <cstring>

#include <climits>

#include <cctype>

#include <map>

#include <list>

#include <stack>

#include <queue>

#include <vector>

#include <set>

#define mem(a,b) memset((a),(b),sizeof(a))

using namespace std;

const int INF=0x3f3f3f3f;

const double PI=acos(-1.0);

const double eps=1e-12;

const int MAX=100000+10;

const int EDGE=100000+10;//边的条数

typedef long long ll;

int main()

{

	ll n,x,i,a[MAX],t,ans;

	while(~scanf("%lld%lld",&n,&x))

	{

		mem(a,0);

		t=0;

		for(i=0;i<n;i++)

		{

			scanf("%lld",&a[i]);

		}

		ans=0;

		if(a[0]>x)

		{

			t=a[0]-x;

			ans+=t;

			a[0]-=t;

		}

		for(i=1;i<n;i++)

		{

			if(a[i]+a[i-1]>x)

			{

				t=a[i]+a[i-1]-x;

				ans+=t;

				a[i]-=t;

			}

		}

	//	if(a[n-2]+a[n-1]>x) ans+=a[n-2]+a[n-1]-x;

		printf("%lld\n",ans);

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.8591588216872936x):
#include <iostream>

using namespace std;



typedef long long ll;

const int MAXN = 1e5 + 10;

ll a[MAXN], x, ans;

int n;



int main() {

	cin >> n >> x;

	for (int i = 0; i < n; i++)

		cin >> a[i];

	for (int i = 1; i < n; i++) {

		if (a[i] + a[i - 1] > x) {

			ans += (a[i] + a[i - 1]) - x;

			a[i] -= (a[i] + a[i - 1]) - x;

		}

		if (a[i] < 0)

			a[i - 1] += a[i], a[i] = 0;

	}

	cout << ans;

}
// Slower C++ solution:
#include <iostream>

#include <algorithm>

#include <cstdio>

#include <cmath>

#include <cstdlib>

#include <cstring>

#include <climits>

#include <cctype>

#include <map>

#include <list>

#include <stack>

#include <queue>

#include <vector>

#include <set>

#define mem(a,b) memset((a),(b),sizeof(a))

using namespace std;

const int INF=0x3f3f3f3f;

const double PI=acos(-1.0);

const double eps=1e-12;

const int MAX=100000+10;

const int EDGE=100000+10;//边的条数

typedef long long ll;

int main()

{

	ll n,x,i,a[MAX],t,ans;

	while(~scanf("%lld%lld",&n,&x))

	{

		mem(a,0);

		t=0;

		for(i=0;i<n;i++)

		{

			scanf("%lld",&a[i]);

		}

		ans=0;

		if(a[0]>x)

		{

			t=a[0]-x;

			ans+=t;

			a[0]-=t;

		}

		for(i=1;i<n;i++)

		{

			if(a[i]+a[i-1]>x)

			{

				t=a[i]+a[i-1]-x;

				ans+=t;

				a[i]-=t;

			}

		}

	//	if(a[n-2]+a[n-1]>x) ans+=a[n-2]+a[n-1]-x;

		printf("%lld\n",ans);

	}

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.8373283389730635x):
#include <bits/stdc++.h>

using namespace std;



#define int long long



const int N = 1e5 + 10;

int n, x, ans, ar[N];



int32_t main() {

	ios::sync_with_stdio(0);

	cin.tie(0);

	

	cin >> n >> x;

	for (int i = 0; i < n; i++)

		cin >> ar[i];

	

	for (int i = 1; i < n; i++) {

		int tmp = max(0LL, ar[i] + ar[i - 1] - x), rem = min(tmp, ar[i]);

		ans += tmp;

		ar[i] -= rem;

		ar[i - 1] -= tmp - rem;

		assert(ar[i - 1] >= 0 && ar[i] >= 0);

	}

	cout << ans << endl;

}
// Slower C++ solution:
#include<iostream>

#include<algorithm>

#include<cstdio>

#include<cstring>

#include<vector>

#include<queue>

#include<map>

#include<cmath>

#define N 100009

using namespace std;

const int INF = 0x3f3f3f3f;

int a[N];

int main()

{

    int n, x;

    scanf("%d%d", &n, &x);

    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);

    long long ans, now; ans = now = 0;

    for(int i = 1; i <= n; i++)

    {

        if(now + a[i] <= x) now = a[i];

        else ans += now + a[i] - x, now = a[i] - (now + a[i] - x);

    }

    printf("%lld\n", ans);

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.2317967961118805x):
#include "cstdio"

long long ss;int s[100005],i,x,n;

int A(){

    int a=0,k=1;char c=getchar();

    while(c<'0'||c>'9'){

        if(c=='-') k=-1;c=getchar();

    }

    while(c>='0'&&c<='9'){

        a=a*10+c-'0';c=getchar();

    }

    return a*k;

}

int main(){

	scanf("%d%d",&n,&x);

	for(i=1;i<=n;i++)

	s[i]=A();

	for(i=1;i<=n;i++){

		if(s[i]+s[i-1]>x)ss+=s[i]+s[i-1]-x,s[i]=x-s[i-1];

	}

	printf("%lld\n",ss);

}
// Slower C++ solution:
#include<iostream>

#include<algorithm>

#include<cstdio>

#include<cstring>

#include<vector>

#include<queue>

#include<map>

#include<cmath>

#define N 100009

using namespace std;

const int INF = 0x3f3f3f3f;

int a[N];

int main()

{

    int n, x;

    scanf("%d%d", &n, &x);

    for(int i = 1; i <= n; i++) scanf("%d", &a[i]);

    long long ans, now; ans = now = 0;

    for(int i = 1; i <= n; i++)

    {

        if(now + a[i] <= x) now = a[i];

        else ans += now + a[i] - x, now = a[i] - (now + a[i] - x);

    }

    printf("%lld\n", ans);

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.206189125031168x):
#include<stdio.h>

long long a[100005];

long long ans;

main(){

	int n,k;scanf("%d%d",&n,&k);

	for(register int i=1;i<=n;i++)

		scanf("%lld",&a[i]);

	for(register int i=1;i<n;i++){

		if(a[i]+a[i+1]>k){

			ans+=a[i]+a[i+1]-k;

			if(a[i]<=k)

				a[i+1]-=a[i]-k+a[i+1];

			else{

				a[i+1]=0;

				a[i]=k;

			}

		}

	}

	printf("%lld\n",ans);

	return 0;

}
// Slower C++ solution:
#include<iostream>



using namespace std;



int main()

{

    long long n,i,x,ans=0;

    long long a[100005];

    cin>>n>>x;

    for(i=0;i<n;i++)

    {

        cin>>a[i];

    }

    for(i=1;i<n;i++)

    {

        if(a[i-1]+a[i]>=x)

        {

            ans=ans+(a[i-1]+a[i]-x);

            if(a[i-1]<=x)

            {

                a[i]=x-a[i-1];

            }

            else

            {

                a[i]=0;

                a[i-1]=x;

            }

        }

    }

    cout<<ans<<endl;

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.3054729262324125x):
#include<stdio.h>

long long a[100005];

long long ans;

main(){

	int n,k;scanf("%d%d",&n,&k);

	for(register int i=1;i<=n;i++)

		scanf("%lld",&a[i]);

	for(register int i=1;i<n;i++){

		if(a[i]+a[i+1]>k){

			ans+=a[i]+a[i+1]-k;

			if(a[i]<=k)

				a[i+1]-=a[i]-k+a[i+1];

			else{

				a[i+1]=0;

				a[i]=k;

			}

		}

	}

	printf("%lld\n",ans);

	return 0;

}
// Slower C++ solution:
#include <iostream>



using namespace std;



int main()

{

    int n,x;

    int  a[100010]{};

    long long int k=0;



    cin>>n>>x;



    for(int i=0;i<n;i++)

    {

        cin>>a[i];

    }

    for(int i=0;i<n-1;i++)

    {

        if(a[i]+a[i+1]>=x)

        {

            int tem=a[i]+a[i+1]-x;

            k=k+tem;

            if(a[i+1]>=tem)

            {

                a[i+1]=a[i+1]-tem;

            }

            else

            {

                a[i+1]=0;

                a[i]=a[i]-(tem-a[i+1]);

            }

        }

    }

    cout<<k<<endl;

    return 0;

}


// Faster C++ solution for the same problem (measured speedup: 1.3957519542157517x):
#include <algorithm>

#include <iostream>

using namespace std;



typedef long long ll;



const ll MAX_N = 1e5 + 5;

ll n, x, ans;

ll Arr[MAX_N];



int main() {

	cin >> n >> x;

	for (int i = 0; i < n; i++)

		cin >> Arr[i];



	ans = max(0LL, Arr[0] - x);

	Arr[0] = min(Arr[0], x);

	for (int i = 1; i < n; i++) {

		ans += max(0LL, Arr[i] + Arr[i - 1] - x);

		Arr[i] = min(Arr[i], x - Arr[i - 1]);

	}



	cout << ans << "\n";

}